summaryrefslogtreecommitdiff
path: root/src/filebuf.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2015-07-24 19:22:41 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2015-07-24 23:02:11 +0200
commit19d9beb7ffbde3e171c17fc3544d508304a30fbf (patch)
tree1999c60c5fd7cb91ec7830de516896540deabde7 /src/filebuf.c
parent668053befecfd0979ff790bc9a2a3c308c38a9be (diff)
downloadlibgit2-cmn/filebuf-rename-error.tar.gz
filebuf: remove lockfile upon rename errorscmn/filebuf-rename-error
When we have an error renaming the lockfile, we need to make sure that we remove it upon cleanup. For this, we need to keep track of whether we opened the file and whether the rename succeeded. If we did create the lockfile but the rename did not succeed, we remove the lockfile. This won't protect against all errors, but the most common ones (target file is open) does get handled.
Diffstat (limited to 'src/filebuf.c')
-rw-r--r--src/filebuf.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/filebuf.c b/src/filebuf.c
index 848ac343b..838f4b4d2 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -101,7 +101,7 @@ void git_filebuf_cleanup(git_filebuf *file)
if (file->fd_is_open && file->fd >= 0)
p_close(file->fd);
- if (file->fd_is_open && file->path_lock && git_path_exists(file->path_lock))
+ if (file->created_lock && !file->did_rename && file->path_lock && git_path_exists(file->path_lock))
p_unlink(file->path_lock);
if (file->compute_digest) {
@@ -258,6 +258,7 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode
goto cleanup;
}
file->fd_is_open = true;
+ file->created_lock = true;
/* No original path */
file->path_original = NULL;
@@ -281,6 +282,8 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode
/* open the file for locking */
if ((error = lock_file(file, flags, mode)) < 0)
goto cleanup;
+
+ file->created_lock = true;
}
return 0;
@@ -340,6 +343,8 @@ int git_filebuf_commit(git_filebuf *file)
goto on_error;
}
+ file->did_rename = true;
+
git_filebuf_cleanup(file);
return 0;