wrapper.c: add a new function unlink_or_msg

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>

Ronnie Sahlberg committed Jul 16, 2014 at 11:20 UTC 9ccc0c089667e6dccc888590376f138521784e5e
2 files changed +23
git-compat-util.h
+9
@@ -326,6 +326,8 @@ static inline char *git_find_last_dir_sep(const char *path)
326
327 #include "wildmatch.h"
328
329 +struct strbuf;
330 +
331 /* General helper functions */
332 extern void vreportf(const char *prefix, const char *err, va_list params);
333 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,
783 * not exist.
784 */
785 int unlink_or_warn(const char *path);
786 + /*
787 + * Tries to unlink file. Returns 0 if unlink succeeded
788 + * or the file already didn't exist. Returns -1 and
789 + * appends a message to err suitable for
790 + * 'error("%s", err->buf)' on error.
791 + */
792 +int unlink_or_msg(const char *file, struct strbuf *err);
793 /*
794 * Preserves errno, prints a message, but gives no warning for ENOENT.
795 * Returns 0 on success, which includes trying to remove a directory that does
wrapper.c
+14
@@ -475,6 +475,20 @@ static int warn_if_unremovable(const char *op, const char *file, int rc)
475 return rc;
476 }
477
478 +int unlink_or_msg(const char *file, struct strbuf *err)
479 +{
480 + int rc = unlink(file);
481 +
482 + assert(err);
483 +
484 + if (!rc || errno == ENOENT)
485 + return 0;
486 +
487 + strbuf_addf(err, "unable to unlink %s: %s",
488 + file, strerror(errno));
489 + return -1;
490 +}
491 +
492 int unlink_or_warn(const char *file)
493 {
494 return warn_if_unremovable("unlink", file, unlink(file));