hold_lock_file_for_append(): restore errno before returning
authorMichael Haggerty <mhagger@alum.mit.edu>
Wed, 1 Oct 2014 10:28:40 +0000 (12:28 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Oct 2014 20:53:54 +0000 (13:53 -0700)
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>
lockfile.c
index c51c6ec69d0352378c2d31bea45bb7029c8f5657..b2f5d36f7e944798afdd9ae5b591b3dd03eadf7b 100644 (file)
@@ -243,15 +243,22 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
        orig_fd = open(path, O_RDONLY);
        if (orig_fd < 0) {
                if (errno != ENOENT) {
+                       int save_errno = errno;
+
                        if (flags & LOCK_DIE_ON_ERROR)
                                die("cannot open '%s' for copying", path);
                        rollback_lock_file(lk);
-                       return error("cannot open '%s' for copying", path);
+                       error("cannot open '%s' for copying", path);
+                       errno = save_errno;
+                       return -1;
                }
        } else if (copy_fd(orig_fd, fd)) {
+               int save_errno = errno;
+
                if (flags & LOCK_DIE_ON_ERROR)
                        exit(128);
                rollback_lock_file(lk);
+               errno = save_errno;
                return -1;
        }
        return fd;