Skip to content

Commit 4ae540d

Browse files
pks-tgitster
authored andcommitted
lockfile: report when rollback fails
We do not report to the caller when rolling back a lockfile fails, which will be needed by the reftable compaction logic in a subsequent commit. It also cannot really report on all errors because the function calls `delete_tempfile()`, which doesn't return an error either. Refactor the code so that both `delete_tempfile()` and `rollback_lock_file()` return an error code. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b387623 commit 4ae540d

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

lockfile.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ static inline int commit_lock_file_to(struct lock_file *lk, const char *path)
321321
* Roll back `lk`: close the file descriptor and/or file pointer and
322322
* remove the lockfile. It is a NOOP to call `rollback_lock_file()`
323323
* for a `lock_file` object that has already been committed or rolled
324-
* back.
324+
* back. No error will be returned in this case.
325325
*/
326-
static inline void rollback_lock_file(struct lock_file *lk)
326+
static inline int rollback_lock_file(struct lock_file *lk)
327327
{
328-
delete_tempfile(&lk->tempfile);
328+
return delete_tempfile(&lk->tempfile);
329329
}
330330

331331
#endif /* LOCKFILE_H */

tempfile.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@
5050

5151
static VOLATILE_LIST_HEAD(tempfile_list);
5252

53-
static void remove_template_directory(struct tempfile *tempfile,
53+
static int remove_template_directory(struct tempfile *tempfile,
5454
int in_signal_handler)
5555
{
5656
if (tempfile->directory) {
5757
if (in_signal_handler)
58-
rmdir(tempfile->directory);
58+
return rmdir(tempfile->directory);
5959
else
60-
rmdir_or_warn(tempfile->directory);
60+
return rmdir_or_warn(tempfile->directory);
6161
}
62+
63+
return 0;
6264
}
6365

6466
static void remove_tempfiles(int in_signal_handler)
@@ -353,16 +355,19 @@ int rename_tempfile(struct tempfile **tempfile_p, const char *path)
353355
return 0;
354356
}
355357

356-
void delete_tempfile(struct tempfile **tempfile_p)
358+
int delete_tempfile(struct tempfile **tempfile_p)
357359
{
358360
struct tempfile *tempfile = *tempfile_p;
361+
int err = 0;
359362

360363
if (!is_tempfile_active(tempfile))
361-
return;
364+
return 0;
362365

363-
close_tempfile_gently(tempfile);
364-
unlink_or_warn(tempfile->filename.buf);
365-
remove_template_directory(tempfile, 0);
366+
err |= close_tempfile_gently(tempfile);
367+
err |= unlink_or_warn(tempfile->filename.buf);
368+
err |= remove_template_directory(tempfile, 0);
366369
deactivate_tempfile(tempfile);
367370
*tempfile_p = NULL;
371+
372+
return err ? -1 : 0;
368373
}

tempfile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ int reopen_tempfile(struct tempfile *tempfile);
269269
* `delete_tempfile()` for a `tempfile` object that has already been
270270
* deleted or renamed.
271271
*/
272-
void delete_tempfile(struct tempfile **tempfile_p);
272+
int delete_tempfile(struct tempfile **tempfile_p);
273273

274274
/*
275275
* Close the file descriptor and/or file pointer if they are still

0 commit comments

Comments
 (0)