lockfile.c: make lock_file return a meaningful errno on failurei
authorRonnie Sahlberg <sahlberg@google.com>
Fri, 20 Jun 2014 14:42:48 +0000 (07:42 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 14 Jul 2014 18:54:41 +0000 (11:54 -0700)
Making errno when returning from lock_file() meaningful, which should
fix

* an existing almost-bug in lock_ref_sha1_basic where it assumes
errno==ENOENT is meaningful and could waste some work on retries

* an existing bug in repack_without_refs where it prints
strerror(errno) and picks advice based on errno, despite errno
potentially being zero and potentially having been clobbered by
that point

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Acked-by: Michael Haggerty <mhagger@alum.mit.edu>
lockfile.c
refs.c
refs.h
index 464031bf1ca6cd389e47ce35f87edb26d528ca00..a921d77afcc5b54fdeba7bca6afcd85548332f0d 100644 (file)
@@ -121,7 +121,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 +130,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 +150,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 +194,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);
diff --git a/refs.c b/refs.c
index db0560259069b87961f9799a01b37035d73ff091..e9d53e4a573b641e6333ba9184a3884c2002e9f6 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2212,6 +2212,7 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
        return 0;
 }
 
+/* This should return a meaningful errno on failure */
 int lock_packed_refs(int flags)
 {
        struct packed_ref_cache *packed_ref_cache;
diff --git a/refs.h b/refs.h
index 09d3564116974c862f974bc9c86e7a409b1cb48d..64f25d99a35914e18851beef19fe29bbead3182a 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -82,6 +82,7 @@ extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct st
 /*
  * Lock the packed-refs file for writing.  Flags is passed to
  * hold_lock_file_for_update().  Return 0 on success.
+ * Errno is set to something meaningful on error.
  */
 extern int lock_packed_refs(int flags);