log_ref_setup(): pass the open file descriptor back to the caller
This function will most often be called by log_ref_write_1(), which wants to append to the reflog file. In that case, it is silly to close the file only for the caller to reopen it immediately. So, in the case that the file was opened, pass the open file descriptor back to the caller. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Jan 6, 2017 at 17:22 UTC
e404f459fdc0d42e9ea83084cb1acdd241c14de3
1 file changed
+22
-17
refs/files-backend.c
+22
-17
@@ -2719,19 +2719,23 @@ static int open_or_create_logfile(const char *path, void *cb)
2719
}
2720
2721
/*
2722
- * Create a reflog for a ref. If force_create = 0, the reflog will
2723
- * only be created for certain refs (those for which
2724
- * should_autocreate_reflog returns non-zero. Otherwise, create it
2725
- * regardless of the ref name. Fill in *err and return -1 on failure.
2722
+ * Create a reflog for a ref. Store its path to *logfile. If
2723
+ * force_create = 0, only create the reflog for certain refs (those
2724
+ * for which should_autocreate_reflog returns non-zero). Otherwise,
2725
+ * create it regardless of the reference name. If the logfile already
2726
+ * existed or was created, return 0 and set *logfd to the file
2727
+ * descriptor opened for appending to the file. If no logfile exists
2728
+ * and we decided not to create one, return 0 and set *logfd to -1. On
2729
+ * failure, fill in *err, set *logfd to -1, and return -1.
2730
*/
2727
-static int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err, int force_create)
2731
+static int log_ref_setup(const char *refname,
2732
+ struct strbuf *logfile, int *logfd,
2733
+ struct strbuf *err, int force_create)
2734
{
2729
- int logfd;
2730
-
2735
strbuf_git_path(logfile, "logs/%s", refname);
2736
2737
if (force_create || should_autocreate_reflog(refname)) {
2734
- if (raceproof_create_file(logfile->buf, open_or_create_logfile, &logfd)) {
2738
+ if (raceproof_create_file(logfile->buf, open_or_create_logfile, logfd)) {
2739
if (errno == ENOENT)
2740
strbuf_addf(err, "unable to create directory for '%s': "
2741
"%s", logfile->buf, strerror(errno));
@@ -2745,8 +2749,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
2749
return -1;
2750
}
2751
} else {
2748
- logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);
2749
- if (logfd < 0) {
2752
+ *logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);
2753
+ if (*logfd < 0) {
2754
if (errno == ENOENT || errno == EISDIR) {
2755
/*
2756
* The logfile doesn't already exist,
@@ -2763,10 +2767,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
2767
}
2768
}
2769
2766
- if (logfd >= 0) {
2770
+ if (*logfd >= 0)
2771
adjust_shared_perm(logfile->buf);
2768
- close(logfd);
2769
- }
2772
2773
return 0;
2774
}
@@ -2777,11 +2779,14 @@ static int files_create_reflog(struct ref_store *ref_store,
2779
{
2780
int ret;
2781
struct strbuf sb = STRBUF_INIT;
2782
+ int fd;
2783
2784
/* Check validity (but we don't need the result): */
2785
files_downcast(ref_store, 0, "create_reflog");
2786
2784
- ret = log_ref_setup(refname, &sb, err, force_create);
2787
+ ret = log_ref_setup(refname, &sb, &fd, err, force_create);
2788
+ if (fd >= 0)
2789
+ close(fd);
2790
strbuf_release(&sb);
2791
return ret;
2792
}
@@ -2817,17 +2822,17 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
2822
struct strbuf *logfile, int flags,
2823
struct strbuf *err)
2824
{
2820
- int logfd, result, oflags = O_APPEND | O_WRONLY;
2825
+ int logfd, result;
2826
2827
if (log_all_ref_updates < 0)
2828
log_all_ref_updates = !is_bare_repository();
2829
2825
- result = log_ref_setup(refname, logfile, err, flags & REF_FORCE_CREATE_REFLOG);
2830
+ result = log_ref_setup(refname, logfile, &logfd, err,
2831
+ flags & REF_FORCE_CREATE_REFLOG);
2832
2833
if (result)
2834
return result;
2835
2830
- logfd = open(logfile->buf, oflags);
2836
if (logfd < 0)
2837
return 0;
2838
result = log_ref_write_fd(logfd, old_sha1, new_sha1,