mirror of
https://github.com/git/git.git
synced 2024-11-18 21:14:17 +01:00
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 <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
d1c8c0c8c4
commit
fc71db39e0
@ -415,4 +415,10 @@ void git_qsort(void *base, size_t nmemb, size_t size,
|
||||
#define fstat_is_reliable() 1
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Preserves errno, prints a message, but gives no warning for ENOENT.
|
||||
* Always returns the return value of unlink(2).
|
||||
*/
|
||||
int unlink_or_warn(const char *path);
|
||||
|
||||
#endif
|
||||
|
16
wrapper.c
16
wrapper.c
@ -289,3 +289,19 @@ int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
|
||||
safe_create_leading_directories(name);
|
||||
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
|
||||
}
|
||||
|
||||
int unlink_or_warn(const char *file)
|
||||
{
|
||||
int rc = unlink(file);
|
||||
|
||||
if (rc < 0) {
|
||||
int err = errno;
|
||||
if (ENOENT != err) {
|
||||
warning("unable to unlink %s: %s",
|
||||
file, strerror(errno));
|
||||
errno = err;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user