Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62a273c

Browse files
committed
filebuf: remove lockfile upon rename errors
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.
1 parent adb8f8d commit 62a273c

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/filebuf.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void git_filebuf_cleanup(git_filebuf *file)
101101
if (file->fd_is_open && file->fd >= 0)
102102
p_close(file->fd);
103103

104-
if (file->fd_is_open && file->path_lock && git_path_exists(file->path_lock))
104+
if (file->created_lock && !file->did_rename && file->path_lock && git_path_exists(file->path_lock))
105105
p_unlink(file->path_lock);
106106

107107
if (file->compute_digest) {
@@ -258,6 +258,7 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode
258258
goto cleanup;
259259
}
260260
file->fd_is_open = true;
261+
file->created_lock = true;
261262

262263
/* No original path */
263264
file->path_original = NULL;
@@ -281,6 +282,8 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode
281282
/* open the file for locking */
282283
if ((error = lock_file(file, flags, mode)) < 0)
283284
goto cleanup;
285+
286+
file->created_lock = true;
284287
}
285288

286289
return 0;
@@ -340,6 +343,8 @@ int git_filebuf_commit(git_filebuf *file)
340343
goto on_error;
341344
}
342345

346+
file->did_rename = true;
347+
343348
git_filebuf_cleanup(file);
344349
return 0;
345350

src/filebuf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ struct git_filebuf {
4444
size_t buf_size, buf_pos;
4545
git_file fd;
4646
bool fd_is_open;
47+
bool created_lock;
48+
bool did_rename;
4749
bool do_not_buffer;
4850
int last_error;
4951
};

tests/core/filebuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,6 @@ void test_core_filebuf__rename_error(void)
148148
p_close(fd);
149149

150150
git_filebuf_cleanup(&file);
151-
cl_assert_equal_i(false, git_path_exists(test_lock));
152151

152+
cl_assert_equal_i(false, git_path_exists(test_lock));
153153
}

0 commit comments

Comments
 (0)