wrapper.c: remove/unlink_or_warn: simplify, treat ENOENT as success
authorRonnie Sahlberg <sahlberg@google.com>
Wed, 16 Jul 2014 18:01:18 +0000 (11:01 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Oct 2014 17:47:20 +0000 (10:47 -0700)
Simplify the function warn_if_unremovable slightly. Additionally, change
behaviour slightly. If we failed to remove the object because the object
does not exist, we can still return success back to the caller since none of
the callers depend on "fail if the file did not exist".

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-compat-util.h
refs.c
wrapper.c
index fb41118c0705c628bed093c4a915a58aa01a5a1a..d67592fd969bb8e5cb1401e426f51f9773799461 100644 (file)
@@ -777,11 +777,14 @@ void git_qsort(void *base, size_t nmemb, size_t size,
 
 /*
  * Preserves errno, prints a message, but gives no warning for ENOENT.
- * Always returns the return value of unlink(2).
+ * Returns 0 on success, which includes trying to unlink an object that does
+ * not exist.
  */
 int unlink_or_warn(const char *path);
 /*
- * Likewise for rmdir(2).
+ * Preserves errno, prints a message, but gives no warning for ENOENT.
+ * Returns 0 on success, which includes trying to remove a directory that does
+ * not exist.
  */
 int rmdir_or_warn(const char *path);
 /*
diff --git a/refs.c b/refs.c
index a77458f2f6eb108a90a1cf3d2cc4a771549c05f9..2dcf6c6e03c08c91f6734b49308c93f044fae120 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2607,7 +2607,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
                char *loose_filename = get_locked_file_path(lock->lk);
                int err = unlink_or_warn(loose_filename);
                free(loose_filename);
-               if (err && errno != ENOENT)
+               if (err)
                        return 1;
        }
        return 0;
index 5b77d2a1aecd494ea4bb5eb1e6f31afc9c96f82e..8d4be66e6678816f5a92c639c40d090c18252c8e 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -466,14 +466,12 @@ int xmkstemp_mode(char *template, int mode)
 
 static int warn_if_unremovable(const char *op, const char *file, int rc)
 {
-       if (rc < 0) {
-               int err = errno;
-               if (ENOENT != err) {
-                       warning("unable to %s %s: %s",
-                               op, file, strerror(errno));
-                       errno = err;
-               }
-       }
+       int err;
+       if (!rc || errno == ENOENT)
+               return 0;
+       err = errno;
+       warning("unable to %s %s: %s", op, file, strerror(errno));
+       errno = err;
        return rc;
 }