lockfile.c: make lock_file return a meaningful errno on failurei

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>

Ronnie Sahlberg committed Jun 20, 2014 at 07:42 UTC 447ff1bf0acf9a1d7d2dc3aed032c209f105fb8a
3 files changed +14 -5
lockfile.c
+12 -5
@@ -121,7 +121,7 @@ static char *resolve_symlink(char *p, size_t s)
121 return p;
122 }
123
124 -
124 +/* Make sure errno contains a meaningful value on error */
125 static int lock_file(struct lock_file *lk, const char *path, int flags)
126 {
127 /*
@@ -130,8 +130,10 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
130 */
131 static const size_t max_path_len = sizeof(lk->filename) - 5;
132
133 - if (strlen(path) >= max_path_len)
133 + if (strlen(path) >= max_path_len) {
134 + errno = ENAMETOOLONG;
135 return -1;
136 + }
137 strcpy(lk->filename, path);
138 if (!(flags & LOCK_NODEREF))
139 resolve_symlink(lk->filename, max_path_len);
@@ -148,9 +150,13 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
150 lock_file_list = lk;
151 lk->on_list = 1;
152 }
151 - if (adjust_shared_perm(lk->filename))
152 - return error("cannot fix permission bits on %s",
153 - lk->filename);
153 + if (adjust_shared_perm(lk->filename)) {
154 + int save_errno = errno;
155 + error("cannot fix permission bits on %s",
156 + lk->filename);
157 + errno = save_errno;
158 + return -1;
159 + }
160 }
161 else
162 lk->filename[0] = 0;
@@ -188,6 +194,7 @@ NORETURN void unable_to_lock_index_die(const char *path, int err)
194 die("%s", buf.buf);
195 }
196
197 +/* This should return a meaningful errno on failure */
198 int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
199 {
200 int fd = lock_file(lk, path, flags);
refs.c
+1
@@ -2212,6 +2212,7 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
2212 return 0;
2213 }
2214
2215 +/* This should return a meaningful errno on failure */
2216 int lock_packed_refs(int flags)
2217 {
2218 struct packed_ref_cache *packed_ref_cache;
refs.h
+1
@@ -82,6 +82,7 @@ extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct st
82 /*
83 * Lock the packed-refs file for writing. Flags is passed to
84 * hold_lock_file_for_update(). Return 0 on success.
85 + * Errno is set to something meaningful on error.
86 */
87 extern int lock_packed_refs(int flags);
88