git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] Introduce an unlink(2) wrapper which gives warning if unlink failed

From
Alex Riesen <raa.lkml@gmail.com>
Date
Apr 29, 2009, 21:21 UTC
Message-ID
<20090429212146.GA12099@blimp.localdomain>
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>
---
 git-compat-util.h |    6 ++++++
 wrapper.c         |   16 ++++++++++++++++
 2 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index 1ac16bd..d1d4eaf 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
diff --git a/wrapper.c b/wrapper.c
index d8efb13..7eb3218 100644
--- a/wrapper.c
+++ b/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;
+}
+
-- 
1.6.3.rc3.39.g49fd5
Next: Alex Riesen
Message 1 of 2 in “Introduce an unlink(2) wrapper which gives warning if unlink failed”
  1. 1/2 Introduce an unlink(2) wrapper which gives warning if unlink failedAlex Riesen, Apr 29, 2009
  2. 2/2 replace direct calls to unlink(2) with unlink_or_warnAlex Riesen, Apr 29, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.