diff options
author | Alex Riesen <raa.lkml@gmail.com> | 2009-04-29 23:21:46 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2009-04-29 18:37:41 -0700 |
commit | fc71db39e0d75e8a403f36f3fecf4450886cd165 (patch) | |
tree | ea69404a0a4afc9417e39e4dcf3e09dca08b4008 | |
parent | d1c8c0c8c42912eb768a05115c798de50e595f8b (diff) | |
download | git-fc71db39e0d75e8a403f36f3fecf4450886cd165.tar.gz git-fc71db39e0d75e8a403f36f3fecf4450886cd165.tar.xz |
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>
-rw-r--r-- | git-compat-util.h | 6 | ||||
-rw-r--r-- | wrapper.c | 16 |
2 files changed, 22 insertions, 0 deletions
diff --git a/git-compat-util.h b/git-compat-util.h index 1ac16bde5..c7cf2d5d9 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -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; +} + |