Merge branch 'bc/asciidoc-pretty-formats-fix'
[gitweb.git] / copy.c
diff --git a/copy.c b/copy.c
index e54d15aced7595ccb11423b0de121db9051ad1f3..f2970ec46282a07593366c598f688e5d70c05c83 100644 (file)
--- a/copy.c
+++ b/copy.c
@@ -4,34 +4,30 @@ 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;
+}
+
+static int copy_times(const char *dst, const char *src)
+{
+       struct stat st;
+       struct utimbuf times;
+       if (stat(src, &st) < 0)
+               return -1;
+       times.actime = st.st_atime;
+       times.modtime = st.st_mtime;
+       if (utime(dst, &times) < 0)
+               return -1;
        return 0;
 }
 
@@ -47,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));
 
@@ -55,3 +52,11 @@ int copy_file(const char *dst, const char *src, int mode)
 
        return status;
 }
+
+int copy_file_with_time(const char *dst, const char *src, int mode)
+{
+       int status = copy_file(dst, src, mode);
+       if (!status)
+               return copy_times(dst, src);
+       return status;
+}