wrapper.c: remove/unlink_or_warn: simplify, treat ENOENT as success
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>
Ronnie Sahlberg committed
Jul 16, 2014 at 11:01 UTC
1054af7d04aef64378d69a0496b45cdbf6a0bef2
3 files changed
+12
-11
git-compat-util.h
+5
-2
@@ -777,11 +777,14 @@ void git_qsort(void *base, size_t nmemb, size_t size,
777
778
/*
779
* Preserves errno, prints a message, but gives no warning for ENOENT.
780
- * Always returns the return value of unlink(2).
780
+ * Returns 0 on success, which includes trying to unlink an object that does
781
+ * not exist.
782
*/
783
int unlink_or_warn(const char *path);
784
/*
784
- * Likewise for rmdir(2).
785
+ * Preserves errno, prints a message, but gives no warning for ENOENT.
786
+ * Returns 0 on success, which includes trying to remove a directory that does
787
+ * not exist.
788
*/
789
int rmdir_or_warn(const char *path);
790
/*
refs.c
+1
-1
@@ -2607,7 +2607,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
2607
char *loose_filename = get_locked_file_path(lock->lk);
2608
int err = unlink_or_warn(loose_filename);
2609
free(loose_filename);
2610
- if (err && errno != ENOENT)
2610
+ if (err)
2611
return 1;
2612
}
2613
return 0;
wrapper.c
+6
-8
@@ -466,14 +466,12 @@ int xmkstemp_mode(char *template, int mode)
466
467
static int warn_if_unremovable(const char *op, const char *file, int rc)
468
{
469
- if (rc < 0) {
470
- int err = errno;
471
- if (ENOENT != err) {
472
- warning("unable to %s %s: %s",
473
- op, file, strerror(errno));
474
- errno = err;
475
- }
476
- }
469
+ int err;
470
+ if (!rc || errno == ENOENT)
471
+ return 0;
472
+ err = errno;
473
+ warning("unable to %s %s: %s", op, file, strerror(errno));
474
+ errno = err;
475
return rc;
476
}
477