copy_fd(): do not close the input file descriptor
authorSteffen Prohaska <prohaska@zib.de>
Tue, 26 Aug 2014 15:23:24 +0000 (17:23 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Aug 2014 17:25:14 +0000 (10:25 -0700)
The caller, not this function, opened the file descriptor; it is
selfish for the callee to close it when it is done reading from it.
The caller may want an option to rewind and re-read the contents
after it returns.

Simplify the loop to copy the input in full to the output; its
body essentially is what a call to write_in_full() helper does.

Signed-off-by: Steffen Prohaska <prohaska@zib.de>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
copy.c
lockfile.c
diff --git a/copy.c b/copy.c
index a7f58fd905b31b2634f74580090ec664a640e279..f2970ec46282a07593366c598f688e5d70c05c83 100644 (file)
--- a/copy.c
+++ b/copy.c
@@ -4,34 +4,17 @@ int copy_fd(int ifd, int ofd)
 {
        while (1) {
                char buffer[8192];
-               char *buf = buffer;
                ssize_t len = xread(ifd, buffer, sizeof(buffer));
                if (!len)
                        break;
                if (len < 0) {
-                       int read_error = errno;
-                       close(ifd);
                        return error("copy-fd: read returned %s",
-                                    strerror(read_error));
-               }
-               while (len) {
-                       int written = xwrite(ofd, buf, len);
-                       if (written > 0) {
-                               buf += written;
-                               len -= written;
-                       }
-                       else if (!written) {
-                               close(ifd);
-                               return error("copy-fd: write returned 0");
-                       } else {
-                               int write_error = errno;
-                               close(ifd);
-                               return error("copy-fd: write returned %s",
-                                            strerror(write_error));
-                       }
+                                    strerror(errno));
                }
+               if (write_in_full(ofd, buffer, len) < 0)
+                       return error("copy-fd: write returned %s",
+                                    strerror(errno));
        }
-       close(ifd);
        return 0;
 }
 
@@ -60,6 +43,7 @@ int copy_file(const char *dst, const char *src, int mode)
                return fdo;
        }
        status = copy_fd(fdi, fdo);
+       close(fdi);
        if (close(fdo) != 0)
                return error("%s: close error: %s", dst, strerror(errno));
 
index 2564a7f5447b904585f629f0d1233c8a59483a40..2448d30cd06b5f356b1cf3ca0d37200a408d9da1 100644 (file)
@@ -224,8 +224,11 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
        } else if (copy_fd(orig_fd, fd)) {
                if (flags & LOCK_DIE_ON_ERROR)
                        exit(128);
+               close(orig_fd);
                close(fd);
                return -1;
+       } else {
+               close(orig_fd);
        }
        return fd;
 }