try_remove_empty_parents(): don't trash argument contents
[gitweb.git] / refs / files-backend.c
index f9023939d5884e0f975d5997336313695e407ca1..88f8c7aa4b2be898712f90be4394e6233dee9f80 100644 (file)
@@ -1985,6 +1985,13 @@ static int remove_empty_directories(struct strbuf *path)
        return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
 }
 
+static int create_reflock(const char *path, void *cb)
+{
+       struct lock_file *lk = cb;
+
+       return hold_lock_file_for_update(lk, path, LOCK_NO_DEREF) < 0 ? -1 : 0;
+}
+
 /*
  * Locks a ref returning the lock on success and NULL on failure.
  * On failure errno is set to something meaningful.
@@ -2000,10 +2007,8 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
        struct strbuf ref_file = STRBUF_INIT;
        struct ref_lock *lock;
        int last_errno = 0;
-       int lflags = LOCK_NO_DEREF;
        int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
        int resolve_flags = RESOLVE_REF_NO_RECURSE;
-       int attempts_remaining = 3;
        int resolved;
 
        assert_main_repository(&refs->base, "lock_ref_sha1_basic");
@@ -2068,35 +2073,12 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
 
        lock->ref_name = xstrdup(refname);
 
- retry:
-       switch (safe_create_leading_directories_const(ref_file.buf)) {
-       case SCLD_OK:
-               break; /* success */
-       case SCLD_VANISHED:
-               if (--attempts_remaining > 0)
-                       goto retry;
-               /* fall through */
-       default:
+       if (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {
                last_errno = errno;
-               strbuf_addf(err, "unable to create directory for '%s'",
-                           ref_file.buf);
+               unable_to_lock_message(ref_file.buf, errno, err);
                goto error_return;
        }
 
-       if (hold_lock_file_for_update(lock->lk, ref_file.buf, lflags) < 0) {
-               last_errno = errno;
-               if (errno == ENOENT && --attempts_remaining > 0)
-                       /*
-                        * Maybe somebody just deleted one of the
-                        * directories leading to ref_file.  Try
-                        * again:
-                        */
-                       goto retry;
-               else {
-                       unable_to_lock_message(ref_file.buf, errno, err);
-                       goto error_return;
-               }
-       }
        if (verify_lock(lock, old_sha1, mustexist, err)) {
                last_errno = errno;
                goto error_return;
@@ -2300,13 +2282,15 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
 
 /*
  * Remove empty parents, but spare refs/ and immediate subdirs.
- * Note: munges *name.
  */
-static void try_remove_empty_parents(char *name)
+static void try_remove_empty_parents(const char *refname)
 {
+       struct strbuf buf = STRBUF_INIT;
        char *p, *q;
        int i;
-       p = name;
+
+       strbuf_addstr(&buf, refname);
+       p = buf.buf;
        for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
                while (*p && *p != '/')
                        p++;
@@ -2314,8 +2298,7 @@ static void try_remove_empty_parents(char *name)
                while (*p == '/')
                        p++;
        }
-       for (q = p; *q; q++)
-               ;
+       q = buf.buf + buf.len;
        while (1) {
                while (q > p && *q != '/')
                        q--;
@@ -2323,10 +2306,11 @@ static void try_remove_empty_parents(char *name)
                        q--;
                if (q == p)
                        break;
-               *q = '\0';
-               if (rmdir(git_path("%s", name)))
+               strbuf_setlen(&buf, q - buf.buf);
+               if (rmdir(git_path("%s", buf.buf)))
                        break;
        }
+       strbuf_release(&buf);
 }
 
 /* make sure nobody touched the ref, and unlink */
@@ -2439,24 +2423,6 @@ static int repack_without_refs(struct files_ref_store *refs,
        return ret;
 }
 
-static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
-{
-       assert(err);
-
-       if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
-               /*
-                * loose.  The loose file name is the same as the
-                * lockfile name, minus ".lock":
-                */
-               char *loose_filename = get_locked_file_path(lock->lk);
-               int res = unlink_or_msg(loose_filename, err);
-               free(loose_filename);
-               if (res)
-                       return 1;
-       }
-       return 0;
-}
-
 static int files_delete_refs(struct ref_store *ref_store,
                             struct string_list *refnames, unsigned int flags)
 {
@@ -2507,55 +2473,43 @@ static int files_delete_refs(struct ref_store *ref_store,
  */
 #define TMP_RENAMED_LOG  "logs/refs/.tmp-renamed-log"
 
-static int rename_tmp_log(const char *newrefname)
+static int rename_tmp_log_callback(const char *path, void *cb)
 {
-       int attempts_remaining = 4;
-       struct strbuf path = STRBUF_INIT;
-       int ret = -1;
+       int *true_errno = cb;
 
- retry:
-       strbuf_reset(&path);
-       strbuf_git_path(&path, "logs/%s", newrefname);
-       switch (safe_create_leading_directories_const(path.buf)) {
-       case SCLD_OK:
-               break; /* success */
-       case SCLD_VANISHED:
-               if (--attempts_remaining > 0)
-                       goto retry;
-               /* fall through */
-       default:
-               error("unable to create directory for %s", newrefname);
-               goto out;
+       if (rename(git_path(TMP_RENAMED_LOG), path)) {
+               /*
+                * rename(a, b) when b is an existing directory ought
+                * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
+                * Sheesh. Record the true errno for error reporting,
+                * but report EISDIR to raceproof_create_file() so
+                * that it knows to retry.
+                */
+               *true_errno = errno;
+               if (errno == ENOTDIR)
+                       errno = EISDIR;
+               return -1;
+       } else {
+               return 0;
        }
+}
 
-       if (rename(git_path(TMP_RENAMED_LOG), path.buf)) {
-               if ((errno==EISDIR || errno==ENOTDIR) && --attempts_remaining > 0) {
-                       /*
-                        * rename(a, b) when b is an existing
-                        * directory ought to result in ISDIR, but
-                        * Solaris 5.8 gives ENOTDIR.  Sheesh.
-                        */
-                       if (remove_empty_directories(&path)) {
-                               error("Directory not empty: logs/%s", newrefname);
-                               goto out;
-                       }
-                       goto retry;
-               } else if (errno == ENOENT && --attempts_remaining > 0) {
-                       /*
-                        * Maybe another process just deleted one of
-                        * the directories in the path to newrefname.
-                        * Try again from the beginning.
-                        */
-                       goto retry;
-               } else {
-                       error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s",
-                               newrefname, strerror(errno));
-                       goto out;
-               }
+static int rename_tmp_log(const char *newrefname)
+{
+       char *path = git_pathdup("logs/%s", newrefname);
+       int ret, true_errno;
+
+       ret = raceproof_create_file(path, rename_tmp_log_callback, &true_errno);
+       if (ret) {
+               if (errno == EISDIR)
+                       error("directory not empty: %s", path);
+               else
+                       error("unable to move logfile %s to %s: %s",
+                             git_path(TMP_RENAMED_LOG), path,
+                             strerror(true_errno));
        }
-       ret = 0;
-out:
-       strbuf_release(&path);
+
+       free(path);
        return ret;
 }
 
@@ -2631,7 +2585,7 @@ static int files_rename_ref(struct ref_store *ref_store,
        if (!read_ref_full(newrefname, RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
                           sha1, NULL) &&
            delete_ref(newrefname, NULL, REF_NODEREF)) {
-               if (errno==EISDIR) {
+               if (errno == EISDIR) {
                        struct strbuf path = STRBUF_INIT;
                        int result;
 
@@ -2740,66 +2694,89 @@ static int commit_ref(struct ref_lock *lock)
        return 0;
 }
 
+static int open_or_create_logfile(const char *path, void *cb)
+{
+       int *fd = cb;
+
+       *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
+       return (*fd < 0) ? -1 : 0;
+}
+
 /*
- * Create a reflog for a ref.  If force_create = 0, the reflog will
- * only be created for certain refs (those for which
- * should_autocreate_reflog returns non-zero.  Otherwise, create it
- * regardless of the ref name.  Fill in *err and return -1 on failure.
+ * Create a reflog for a ref. If force_create = 0, only create the
+ * reflog for certain refs (those for which should_autocreate_reflog
+ * returns non-zero). Otherwise, create it regardless of the reference
+ * name. If the logfile already existed or was created, return 0 and
+ * set *logfd to the file descriptor opened for appending to the file.
+ * If no logfile exists and we decided not to create one, return 0 and
+ * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
+ * return -1.
  */
-static int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err, int force_create)
+static int log_ref_setup(const char *refname, int force_create,
+                        int *logfd, struct strbuf *err)
 {
-       int logfd, oflags = O_APPEND | O_WRONLY;
+       char *logfile = git_pathdup("logs/%s", refname);
 
-       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;
+               if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
+                       if (errno == ENOENT)
+                               strbuf_addf(err, "unable to create directory for '%s': "
+                                           "%s", logfile, strerror(errno));
+                       else if (errno == EISDIR)
+                               strbuf_addf(err, "there are still logs under '%s'",
+                                           logfile);
+                       else
+                               strbuf_addf(err, "unable to append to '%s': %s",
+                                           logfile, strerror(errno));
 
-               if (errno == EISDIR) {
-                       if (remove_empty_directories(logfile)) {
-                               strbuf_addf(err, "there are still logs under "
-                                           "'%s'", logfile->buf);
-                               return -1;
-                       }
-                       logfd = open(logfile->buf, oflags, 0666);
+                       goto error;
                }
-
-               if (logfd < 0) {
-                       strbuf_addf(err, "unable to append to '%s': %s",
-                                   logfile->buf, strerror(errno));
-                       return -1;
+       } else {
+               *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
+               if (*logfd < 0) {
+                       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, strerror(errno));
+                               goto error;
+                       }
                }
        }
 
-       adjust_shared_perm(logfile->buf);
-       close(logfd);
+       if (*logfd >= 0)
+               adjust_shared_perm(logfile);
+
+       free(logfile);
        return 0;
-}
 
+error:
+       free(logfile);
+       return -1;
+}
 
 static int files_create_reflog(struct ref_store *ref_store,
                               const char *refname, int force_create,
                               struct strbuf *err)
 {
-       int ret;
-       struct strbuf sb = STRBUF_INIT;
+       int fd;
 
        /* Check validity (but we don't need the result): */
        files_downcast(ref_store, 0, "create_reflog");
 
-       ret = log_ref_setup(refname, &sb, err, force_create);
-       strbuf_release(&sb);
-       return ret;
+       if (log_ref_setup(refname, force_create, &fd, err))
+               return -1;
+
+       if (fd >= 0)
+               close(fd);
+
+       return 0;
 }
 
 static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
@@ -2828,59 +2805,43 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
        return 0;
 }
 
-static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
-                          const unsigned char *new_sha1, const char *msg,
-                          struct strbuf *logfile, int flags,
-                          struct strbuf *err)
+int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
+                       const unsigned char *new_sha1, const char *msg,
+                       int flags, struct strbuf *err)
 {
-       int logfd, result, oflags = O_APPEND | O_WRONLY;
+       int logfd, result;
 
        if (log_all_ref_updates < 0)
                log_all_ref_updates = !is_bare_repository();
 
-       result = log_ref_setup(refname, logfile, err, flags & REF_FORCE_CREATE_REFLOG);
+       result = log_ref_setup(refname, flags & REF_FORCE_CREATE_REFLOG,
+                              &logfd, err);
 
        if (result)
                return result;
 
-       logfd = open(logfile->buf, oflags);
        if (logfd < 0)
                return 0;
        result = log_ref_write_fd(logfd, old_sha1, new_sha1,
                                  git_committer_info(0), msg);
        if (result) {
-               strbuf_addf(err, "unable to append to '%s': %s", logfile->buf,
-                           strerror(errno));
+               int save_errno = errno;
+
+               strbuf_addf(err, "unable to append to '%s': %s",
+                           git_path("logs/%s", refname), strerror(save_errno));
                close(logfd);
                return -1;
        }
        if (close(logfd)) {
-               strbuf_addf(err, "unable to append to '%s': %s", logfile->buf,
-                           strerror(errno));
+               int save_errno = errno;
+
+               strbuf_addf(err, "unable to append to '%s': %s",
+                           git_path("logs/%s", refname), strerror(save_errno));
                return -1;
        }
        return 0;
 }
 
-static int log_ref_write(const char *refname, const unsigned char *old_sha1,
-                        const unsigned char *new_sha1, const char *msg,
-                        int flags, struct strbuf *err)
-{
-       return files_log_ref_write(refname, old_sha1, new_sha1, msg, flags,
-                                  err);
-}
-
-int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
-                       const unsigned char *new_sha1, const char *msg,
-                       int flags, struct strbuf *err)
-{
-       struct strbuf sb = STRBUF_INIT;
-       int ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb, flags,
-                                 err);
-       strbuf_release(&sb);
-       return ret;
-}
-
 /*
  * Write sha1 into the open lockfile, then close the lockfile. On
  * errors, rollback the lockfile, fill in *err and
@@ -2933,7 +2894,8 @@ static int commit_ref_update(struct files_ref_store *refs,
        assert_main_repository(&refs->base, "commit_ref_update");
 
        clear_loose_ref_cache(refs);
-       if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {
+       if (files_log_ref_write(lock->ref_name, lock->old_oid.hash, sha1,
+                               logmsg, 0, err)) {
                char *old_msg = strbuf_detach(err, NULL);
                strbuf_addf(err, "cannot update the ref '%s': %s",
                            lock->ref_name, old_msg);
@@ -2964,7 +2926,7 @@ static int commit_ref_update(struct files_ref_store *refs,
                if (head_ref && (head_flag & REF_ISSYMREF) &&
                    !strcmp(head_ref, lock->ref_name)) {
                        struct strbuf log_err = STRBUF_INIT;
-                       if (log_ref_write("HEAD", lock->old_oid.hash, sha1,
+                       if (files_log_ref_write("HEAD", lock->old_oid.hash, sha1,
                                          logmsg, 0, &log_err)) {
                                error("%s", log_err.buf);
                                strbuf_release(&log_err);
@@ -3003,7 +2965,8 @@ static void update_symref_reflog(struct ref_lock *lock, const char *refname,
        struct strbuf err = STRBUF_INIT;
        unsigned char new_sha1[20];
        if (logmsg && !read_ref(target, new_sha1) &&
-           log_ref_write(refname, lock->old_oid.hash, new_sha1, logmsg, 0, &err)) {
+           files_log_ref_write(refname, lock->old_oid.hash, new_sha1,
+                               logmsg, 0, &err)) {
                error("%s", err.buf);
                strbuf_release(&err);
        }
@@ -3778,9 +3741,11 @@ static int files_transaction_commit(struct ref_store *ref_store,
 
                if (update->flags & REF_NEEDS_COMMIT ||
                    update->flags & REF_LOG_ONLY) {
-                       if (log_ref_write(lock->ref_name, lock->old_oid.hash,
-                                         update->new_sha1,
-                                         update->msg, update->flags, err)) {
+                       if (files_log_ref_write(lock->ref_name,
+                                               lock->old_oid.hash,
+                                               update->new_sha1,
+                                               update->msg, update->flags,
+                                               err)) {
                                char *old_msg = strbuf_detach(err, NULL);
 
                                strbuf_addf(err, "cannot update the ref '%s': %s",
@@ -3810,9 +3775,13 @@ static int files_transaction_commit(struct ref_store *ref_store,
 
                if (update->flags & REF_DELETING &&
                    !(update->flags & REF_LOG_ONLY)) {
-                       if (delete_ref_loose(lock, update->type, err)) {
-                               ret = TRANSACTION_GENERIC_ERROR;
-                               goto cleanup;
+                       if (!(update->type & REF_ISPACKED) ||
+                           update->type & REF_ISSYMREF) {
+                               /* It is a loose reference. */
+                               if (unlink_or_msg(git_path("%s", lock->ref_name), err)) {
+                                       ret = TRANSACTION_GENERIC_ERROR;
+                                       goto cleanup;
+                               }
                        }
 
                        if (!(update->flags & REF_ISPRUNING))