refs.c: make sure log_ref_setup returns a meaningful errno

Making errno when returning from log_ref_setup() meaningful, Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Acked-by: Michael Haggerty <mhagger@alum.mit.edu>

Ronnie Sahlberg committed Jun 20, 2014 at 07:42 UTC bd3b02daec241651231f63b3fd8ee2a8ea1dac68
2 files changed +22 -9
refs.c
+19 -8
@@ -2751,6 +2751,7 @@ static int copy_msg(char *buf, const char *msg)
2751 return cp - buf;
2752 }
2753
2754 +/* This function must set a meaningful errno on failure */
2755 int log_ref_setup(const char *refname, char *logfile, int bufsize)
2756 {
2757 int logfd, oflags = O_APPEND | O_WRONLY;
@@ -2761,9 +2762,12 @@ int log_ref_setup(const char *refname, char *logfile, int bufsize)
2762 starts_with(refname, "refs/remotes/") ||
2763 starts_with(refname, "refs/notes/") ||
2764 !strcmp(refname, "HEAD"))) {
2764 - if (safe_create_leading_directories(logfile) < 0)
2765 - return error("unable to create directory for %s",
2766 - logfile);
2765 + if (safe_create_leading_directories(logfile) < 0) {
2766 + int save_errno = errno;
2767 + error("unable to create directory for %s", logfile);
2768 + errno = save_errno;
2769 + return -1;
2770 + }
2771 oflags |= O_CREAT;
2772 }
2773
@@ -2774,15 +2778,22 @@ int log_ref_setup(const char *refname, char *logfile, int bufsize)
2778
2779 if ((oflags & O_CREAT) && errno == EISDIR) {
2780 if (remove_empty_directories(logfile)) {
2777 - return error("There are still logs under '%s'",
2778 - logfile);
2781 + int save_errno = errno;
2782 + error("There are still logs under '%s'",
2783 + logfile);
2784 + errno = save_errno;
2785 + return -1;
2786 }
2787 logfd = open(logfile, oflags, 0666);
2788 }
2789
2783 - if (logfd < 0)
2784 - return error("Unable to append to %s: %s",
2785 - logfile, strerror(errno));
2790 + if (logfd < 0) {
2791 + int save_errno = errno;
2792 + error("Unable to append to %s: %s", logfile,
2793 + strerror(errno));
2794 + errno = save_errno;
2795 + return -1;
2796 + }
2797 }
2798
2799 adjust_shared_perm(logfile);
refs.h
+3 -1
@@ -158,7 +158,9 @@ extern void unlock_ref(struct ref_lock *lock);
158 /** Writes sha1 into the ref specified by the lock. **/
159 extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
160
161 -/** Setup reflog before using. **/
161 +/*
162 + * Setup reflog before using. Set errno to something meaningful on failure.
163 + */
164 int log_ref_setup(const char *refname, char *logfile, int bufsize);
165
166 /** Reads log for the value of ref during at_time. **/