git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Riesen <raa.lkml@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH 1/2] Introduce an unlink(2) wrapper which gives warning if unlink failed
Date: Wed, 29 Apr 2009 23:21:46 +0200	[thread overview]
Message-ID: <20090429212146.GA12099@blimp.localdomain> (raw)

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

             reply	other threads:[~2009-04-29 21:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <9eb8d64313e3907449150b3a1310a2a6078c0c79.1241039463.git.raa.lkml@gmail.com>
2009-04-29 21:21 ` Alex Riesen [this message]
2009-04-29 21:22   ` [PATCH 2/2] replace direct calls to unlink(2) with unlink_or_warn Alex Riesen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090429212146.GA12099@blimp.localdomain \
    --to=raa.lkml@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).