From: Michael Haggerty Date: Fri, 6 Jan 2017 16:22:32 +0000 (+0100) Subject: log_ref_setup(): separate code for create vs non-create X-Git-Tag: v2.13.0-rc0~177^2~11 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/854bda6b4f7d6bc6087966bfa74a59c232eceac6?ds=sidebyside log_ref_setup(): separate code for create vs non-create The behavior of this function (especially how it handles errors) is quite different depending on whether we are willing to create the reflog vs. whether we are only trying to open an existing reflog. So separate the code paths. This also simplifies the next steps. Signed-off-by: Michael Haggerty Reviewed-by: Jeff King Signed-off-by: Junio C Hamano --- diff --git a/refs/files-backend.c b/refs/files-backend.c index fd8a751e10..c8f6d82e6d 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -2718,45 +2718,64 @@ static int commit_ref(struct ref_lock *lock) */ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err, int force_create) { - int logfd, oflags = O_APPEND | O_WRONLY; + int logfd; strbuf_git_path(logfile, "logs/%s", refname); + if (force_create || should_autocreate_reflog(refname)) { if (safe_create_leading_directories(logfile->buf) < 0) { strbuf_addf(err, "unable to create directory for '%s': " "%s", logfile->buf, strerror(errno)); return -1; } - oflags |= O_CREAT; - } - - logfd = open(logfile->buf, oflags, 0666); - if (logfd < 0) { - if (!(oflags & O_CREAT) && (errno == ENOENT || errno == EISDIR)) - return 0; + logfd = open(logfile->buf, O_APPEND | O_WRONLY | O_CREAT, 0666); + if (logfd < 0) { + if (errno == EISDIR) { + /* + * The directory that is in the way might be + * empty. Try to remove it. + */ + if (remove_empty_directories(logfile)) { + strbuf_addf(err, "there are still logs under " + "'%s'", logfile->buf); + return -1; + } + logfd = open(logfile->buf, O_APPEND | O_WRONLY | O_CREAT, 0666); + } - if (errno == EISDIR) { - if (remove_empty_directories(logfile)) { - strbuf_addf(err, "there are still logs under " - "'%s'", logfile->buf); + if (logfd < 0) { + strbuf_addf(err, "unable to append to '%s': %s", + logfile->buf, strerror(errno)); return -1; } - logfd = open(logfile->buf, oflags, 0666); } - + } else { + logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666); if (logfd < 0) { - strbuf_addf(err, "unable to append to '%s': %s", - logfile->buf, strerror(errno)); - return -1; + if (errno == ENOENT || errno == EISDIR) { + /* + * The logfile doesn't already exist, + * but that is not an error; it only + * means that we won't write log + * entries to it. + */ + ; + } else { + strbuf_addf(err, "unable to append to '%s': %s", + logfile->buf, strerror(errno)); + return -1; + } } } - adjust_shared_perm(logfile->buf); - close(logfd); + if (logfd >= 0) { + adjust_shared_perm(logfile->buf); + close(logfd); + } + return 0; } - static int files_create_reflog(struct ref_store *ref_store, const char *refname, int force_create, struct strbuf *err)