]> git.ipfire.org Git - thirdparty/git.git/commitdiff
wrapper.c: add a new function unlink_or_msg
authorRonnie Sahlberg <sahlberg@google.com>
Wed, 16 Jul 2014 18:20:36 +0000 (11:20 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Oct 2014 17:47:21 +0000 (10:47 -0700)
This behaves like unlink_or_warn except that on failure it writes the message
to its 'err' argument, which the caller can display in an appropriate way or
ignore.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-compat-util.h
wrapper.c

index d67592fd969bb8e5cb1401e426f51f9773799461..59ecf218cbca417dc7d6e0a571f6c82111041225 100644 (file)
@@ -326,6 +326,8 @@ static inline char *git_find_last_dir_sep(const char *path)
 
 #include "wildmatch.h"
 
+struct strbuf;
+
 /* General helper functions */
 extern void vreportf(const char *prefix, const char *err, va_list params);
 extern void vwritef(int fd, const char *prefix, const char *err, va_list params);
@@ -781,6 +783,13 @@ void git_qsort(void *base, size_t nmemb, size_t size,
  * not exist.
  */
 int unlink_or_warn(const char *path);
+ /*
+  * Tries to unlink file.  Returns 0 if unlink succeeded
+  * or the file already didn't exist.  Returns -1 and
+  * appends a message to err suitable for
+  * 'error("%s", err->buf)' on error.
+  */
+int unlink_or_msg(const char *file, struct strbuf *err);
 /*
  * Preserves errno, prints a message, but gives no warning for ENOENT.
  * Returns 0 on success, which includes trying to remove a directory that does
index 8d4be66e6678816f5a92c639c40d090c18252c8e..007ec0d8eac529579cc00b14f9baaddb7ac68487 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -475,6 +475,20 @@ static int warn_if_unremovable(const char *op, const char *file, int rc)
        return rc;
 }
 
+int unlink_or_msg(const char *file, struct strbuf *err)
+{
+       int rc = unlink(file);
+
+       assert(err);
+
+       if (!rc || errno == ENOENT)
+               return 0;
+
+       strbuf_addf(err, "unable to unlink %s: %s",
+                   file, strerror(errno));
+       return -1;
+}
+
 int unlink_or_warn(const char *file)
 {
        return warn_if_unremovable("unlink", file, unlink(file));