lockfile: unlock file if lockfile permissions cannot be adjusted
[gitweb.git] / lockfile.c
index 2564a7f5447b904585f629f0d1233c8a59483a40..d74de8d329a4adc3e9c2b158365292c36a37a10d 100644 (file)
@@ -153,6 +153,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
                        int save_errno = errno;
                        error("cannot fix permission bits on %s",
                              lk->filename);
+                       rollback_lock_file(lk);
                        errno = save_errno;
                        return -1;
                }
@@ -185,7 +186,7 @@ int unable_to_lock_error(const char *path, int err)
        return -1;
 }
 
-NORETURN void unable_to_lock_index_die(const char *path, int err)
+NORETURN void unable_to_lock_die(const char *path, int err)
 {
        struct strbuf buf = STRBUF_INIT;
 
@@ -198,7 +199,7 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
 {
        int fd = lock_file(lk, path, flags);
        if (fd < 0 && (flags & LOCK_DIE_ON_ERROR))
-               unable_to_lock_index_die(path, errno);
+               unable_to_lock_die(path, errno);
        return fd;
 }
 
@@ -209,7 +210,7 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
        fd = lock_file(lk, path, flags);
        if (fd < 0) {
                if (flags & LOCK_DIE_ON_ERROR)
-                       unable_to_lock_index_die(path, errno);
+                       unable_to_lock_die(path, errno);
                return fd;
        }
 
@@ -233,15 +234,29 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
 int close_lock_file(struct lock_file *lk)
 {
        int fd = lk->fd;
+
+       if (fd < 0)
+               return 0;
+
        lk->fd = -1;
        return close(fd);
 }
 
+int reopen_lock_file(struct lock_file *lk)
+{
+       if (0 <= lk->fd)
+               die(_("BUG: reopen a lockfile that is still open"));
+       if (!lk->filename[0])
+               die(_("BUG: reopen a lockfile that has been committed"));
+       lk->fd = open(lk->filename, O_WRONLY);
+       return lk->fd;
+}
+
 int commit_lock_file(struct lock_file *lk)
 {
        char result_file[PATH_MAX];
        size_t i;
-       if (lk->fd >= 0 && close_lock_file(lk))
+       if (close_lock_file(lk))
                return -1;
        strcpy(result_file, lk->filename);
        i = strlen(result_file) - 5; /* .lock */
@@ -262,10 +277,10 @@ int hold_locked_index(struct lock_file *lk, int die_on_error)
 
 void rollback_lock_file(struct lock_file *lk)
 {
-       if (lk->filename[0]) {
-               if (lk->fd >= 0)
-                       close(lk->fd);
-               unlink_or_warn(lk->filename);
-       }
+       if (!lk->filename[0])
+               return;
+
+       close_lock_file(lk);
+       unlink_or_warn(lk->filename);
        lk->filename[0] = 0;
 }