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>
Alex Riesen committed
Apr 29, 2009 at 23:21 UTC
fc71db39e0d75e8a403f36f3fecf4450886cd165
2 files changed
+22
git-compat-util.h
+6
@@ -415,4 +415,10 @@ void git_qsort(void *base, size_t nmemb, size_t size,
415
#define fstat_is_reliable() 1
416
#endif
417
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
+
424
#endif
wrapper.c
+16
@@ -289,3 +289,19 @@ int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
289
safe_create_leading_directories(name);
290
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
291
}
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
+