1
0
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:
Alex Riesen 2009-04-29 23:21:46 +02:00 committed by Junio C Hamano
parent d1c8c0c8c4
commit fc71db39e0
2 changed files with 22 additions and 0 deletions

@ -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

@ -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;
}