hold_lock_file_for_append(): restore errno before returning

Callers who don't pass LOCK_DIE_ON_ERROR might want to examine errno to see what went wrong, so restore errno before returning. In fact this function only has one caller, add_to_alternates_file(), and it *does* use LOCK_DIE_ON_ERROR, but, you know, think of future generations. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Oct 1, 2014 at 12:28 UTC 4d423a3e62c7ab0b04c4bd84995c32daff3b24c3
1 file changed +8 -1
lockfile.c
+8 -1
@@ -243,15 +243,22 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
243 orig_fd = open(path, O_RDONLY);
244 if (orig_fd < 0) {
245 if (errno != ENOENT) {
246 + int save_errno = errno;
247 +
248 if (flags & LOCK_DIE_ON_ERROR)
249 die("cannot open '%s' for copying", path);
250 rollback_lock_file(lk);
249 - return error("cannot open '%s' for copying", path);
251 + error("cannot open '%s' for copying", path);
252 + errno = save_errno;
253 + return -1;
254 }
255 } else if (copy_fd(orig_fd, fd)) {
256 + int save_errno = errno;
257 +
258 if (flags & LOCK_DIE_ON_ERROR)
259 exit(128);
260 rollback_lock_file(lk);
261 + errno = save_errno;
262 return -1;
263 }
264 return fd;