Skip to content

Commit fc71db3

Browse files
raalkmlgitster
authored andcommitted
Introduce an unlink(2) wrapper which gives warning if unlink failed
This seem to be a very common pattern in the current code. The function prints a generic removal failure message, the file name which failed and readable errno presentation. The function preserves errno and always returns the value unlink(2) returned, but prints no message for ENOENT, as it was the most often filtered out in the code calling unlink. Besides, removing a file is anyway the purpose of calling unlink. Signed-off-by: Alex Riesen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d1c8c0c commit fc71db3

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

git-compat-util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,10 @@ void git_qsort(void *base, size_t nmemb, size_t size,
415415
#define fstat_is_reliable() 1
416416
#endif
417417

418+
/*
419+
* Preserves errno, prints a message, but gives no warning for ENOENT.
420+
* Always returns the return value of unlink(2).
421+
*/
422+
int unlink_or_warn(const char *path);
423+
418424
#endif

wrapper.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,19 @@ int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
289289
safe_create_leading_directories(name);
290290
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
291291
}
292+
293+
int unlink_or_warn(const char *file)
294+
{
295+
int rc = unlink(file);
296+
297+
if (rc < 0) {
298+
int err = errno;
299+
if (ENOENT != err) {
300+
warning("unable to unlink %s: %s",
301+
file, strerror(errno));
302+
errno = err;
303+
}
304+
}
305+
return rc;
306+
}
307+

0 commit comments

Comments
 (0)