Skip to content

Commit 33248b9

Browse files
committed
refdb: remove a check-delete race when removing a loose ref
It does not help us to check whether the file exists before trying to unlink it since it might be gone by the time unlink is called. Instead try to remove it and handle the resulting error if it did not exist.
1 parent 40ffa07 commit 33248b9

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/refdb_fs.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,15 +1281,14 @@ static int refdb_fs_backend__delete_tail(
12811281
if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0)
12821282
return -1;
12831283

1284-
if (git_path_isfile(loose_path.ptr)) {
1285-
error = p_unlink(loose_path.ptr);
1286-
loose_deleted = 1;
1287-
}
1288-
1289-
git_buf_free(&loose_path);
12901284

1291-
if (error != 0)
1285+
error = p_unlink(loose_path.ptr);
1286+
if (error < 0 && errno == ENOENT)
1287+
error = 0;
1288+
else if (error < 0)
12921289
goto cleanup;
1290+
else if (error == 0)
1291+
loose_deleted = 1;
12931292

12941293
if ((error = packed_reload(backend)) < 0)
12951294
goto cleanup;
@@ -1312,6 +1311,7 @@ static int refdb_fs_backend__delete_tail(
13121311
error = packed_write(backend);
13131312

13141313
cleanup:
1314+
git_buf_free(&loose_path);
13151315
git_filebuf_cleanup(file);
13161316

13171317
return error;

0 commit comments

Comments
 (0)