t1007: add hash-object --literally tests
[gitweb.git] / lockfile.c
index 464031bf1ca6cd389e47ce35f87edb26d528ca00..2a800cef33ed7fb1ba7d3499459eccd824ab33e4 100644 (file)
@@ -5,7 +5,6 @@
 #include "sigchain.h"
 
 static struct lock_file *lock_file_list;
-static const char *alternate_index_output;
 
 static void remove_lock_file(void)
 {
@@ -121,7 +120,7 @@ static char *resolve_symlink(char *p, size_t s)
        return p;
 }
 
-
+/* Make sure errno contains a meaningful value on error */
 static int lock_file(struct lock_file *lk, const char *path, int flags)
 {
        /*
@@ -130,8 +129,10 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
         */
        static const size_t max_path_len = sizeof(lk->filename) - 5;
 
-       if (strlen(path) >= max_path_len)
+       if (strlen(path) >= max_path_len) {
+               errno = ENAMETOOLONG;
                return -1;
+       }
        strcpy(lk->filename, path);
        if (!(flags & LOCK_NODEREF))
                resolve_symlink(lk->filename, max_path_len);
@@ -148,9 +149,13 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
                        lock_file_list = lk;
                        lk->on_list = 1;
                }
-               if (adjust_shared_perm(lk->filename))
-                       return error("cannot fix permission bits on %s",
-                                    lk->filename);
+               if (adjust_shared_perm(lk->filename)) {
+                       int save_errno = errno;
+                       error("cannot fix permission bits on %s",
+                             lk->filename);
+                       errno = save_errno;
+                       return -1;
+               }
        }
        else
                lk->filename[0] = 0;
@@ -188,6 +193,7 @@ NORETURN void unable_to_lock_index_die(const char *path, int err)
        die("%s", buf.buf);
 }
 
+/* This should return a meaningful errno on failure */
 int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
 {
        int fd = lock_file(lk, path, flags);
@@ -231,6 +237,16 @@ int close_lock_file(struct lock_file *lk)
        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];
@@ -254,25 +270,6 @@ int hold_locked_index(struct lock_file *lk, int die_on_error)
                                         : 0);
 }
 
-void set_alternate_index_output(const char *name)
-{
-       alternate_index_output = name;
-}
-
-int commit_locked_index(struct lock_file *lk)
-{
-       if (alternate_index_output) {
-               if (lk->fd >= 0 && close_lock_file(lk))
-                       return -1;
-               if (rename(lk->filename, alternate_index_output))
-                       return -1;
-               lk->filename[0] = 0;
-               return 0;
-       }
-       else
-               return commit_lock_file(lk);
-}
-
 void rollback_lock_file(struct lock_file *lk)
 {
        if (lk->filename[0]) {