diff options
author | Ronnie Sahlberg <sahlberg@google.com> | 2014-07-16 15:23:02 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-09-03 10:06:25 -0700 |
commit | 65fef2b68097aa6852155ac7c701cc1350fd0bae (patch) | |
tree | 57b3ea4c76c598602f6833694f5ece5ba65552d2 | |
parent | b76ff379e4d9a9db143fb3d8daaeeff6322e9243 (diff) | |
download | git-65fef2b68097aa6852155ac7c701cc1350fd0bae.tar.gz |
wrapper.c: add a new function unlink_or_msg
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r-- | git-compat-util.h | 6 | ||||
-rw-r--r-- | wrapper.c | 18 |
2 files changed, 24 insertions, 0 deletions
diff --git a/git-compat-util.h b/git-compat-util.h index b6f03b36dc..426bc989b9 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -704,12 +704,18 @@ void git_qsort(void *base, size_t nmemb, size_t size, #endif #endif +#include "strbuf.h" + /* * 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); /* + * Like unlink_or_warn but populates a strbuf + */ +int unlink_or_msg(const char *file, struct strbuf *err); +/* * Likewise for rmdir(2). */ int rmdir_or_warn(const char *path); @@ -438,6 +438,24 @@ 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) +{ + if (err) { + int rc = unlink(file); + int save_errno = errno; + + if (rc < 0 && errno != ENOENT) { + strbuf_addf(err, "unable to unlink %s: %s", + file, strerror(errno)); + errno = save_errno; + return -1; + } + return 0; + } + + return unlink_or_warn(file); +} + int unlink_or_warn(const char *file) { return warn_if_unremovable("unlink", file, unlink(file)); |