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:
committed by
Junio C Hamano
parent
d1c8c0c8c4
commit
fc71db39e0
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user