Junio C Hamano | f3123c4 | 2005-10-22 01:28:13 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | |
| 3 | int copy_fd(int ifd, int ofd) |
| 4 | { |
| 5 | while (1) { |
Junio C Hamano | f3123c4 | 2005-10-22 01:28:13 -0700 | [diff] [blame] | 6 | char buffer[8192]; |
Johan Herland | 8a912bc | 2007-05-15 14:49:22 +0200 | [diff] [blame] | 7 | ssize_t len = xread(ifd, buffer, sizeof(buffer)); |
Junio C Hamano | f3123c4 | 2005-10-22 01:28:13 -0700 | [diff] [blame] | 8 | if (!len) |
| 9 | break; |
Junio C Hamano | 00b7cbf | 2015-05-19 10:55:16 -0700 | [diff] [blame] | 10 | if (len < 0) |
| 11 | return COPY_READ_ERROR; |
Steffen Prohaska | b29763a | 2014-08-26 17:23:24 +0200 | [diff] [blame] | 12 | if (write_in_full(ofd, buffer, len) < 0) |
Junio C Hamano | 00b7cbf | 2015-05-19 10:55:16 -0700 | [diff] [blame] | 13 | return COPY_WRITE_ERROR; |
Junio C Hamano | f3123c4 | 2005-10-22 01:28:13 -0700 | [diff] [blame] | 14 | } |
Junio C Hamano | f3123c4 | 2005-10-22 01:28:13 -0700 | [diff] [blame] | 15 | return 0; |
| 16 | } |
Daniel Barkalow | 1468bd4 | 2008-02-25 14:24:48 -0500 | [diff] [blame] | 17 | |
Clemens Buchacher | f7835a2 | 2009-09-12 11:03:48 +0200 | [diff] [blame] | 18 | static int copy_times(const char *dst, const char *src) |
| 19 | { |
| 20 | struct stat st; |
| 21 | struct utimbuf times; |
| 22 | if (stat(src, &st) < 0) |
| 23 | return -1; |
| 24 | times.actime = st.st_atime; |
| 25 | times.modtime = st.st_mtime; |
| 26 | if (utime(dst, ×) < 0) |
| 27 | return -1; |
| 28 | return 0; |
| 29 | } |
| 30 | |
Daniel Barkalow | 1468bd4 | 2008-02-25 14:24:48 -0500 | [diff] [blame] | 31 | int copy_file(const char *dst, const char *src, int mode) |
| 32 | { |
| 33 | int fdi, fdo, status; |
| 34 | |
| 35 | mode = (mode & 0111) ? 0777 : 0666; |
| 36 | if ((fdi = open(src, O_RDONLY)) < 0) |
| 37 | return fdi; |
| 38 | if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) { |
| 39 | close(fdi); |
| 40 | return fdo; |
| 41 | } |
| 42 | status = copy_fd(fdi, fdo); |
Junio C Hamano | 00b7cbf | 2015-05-19 10:55:16 -0700 | [diff] [blame] | 43 | switch (status) { |
| 44 | case COPY_READ_ERROR: |
Nguyễn Thái Ngọc Duy | 37653a1 | 2016-05-08 16:47:40 +0700 | [diff] [blame] | 45 | error_errno("copy-fd: read returned"); |
Junio C Hamano | 00b7cbf | 2015-05-19 10:55:16 -0700 | [diff] [blame] | 46 | break; |
| 47 | case COPY_WRITE_ERROR: |
Nguyễn Thái Ngọc Duy | 37653a1 | 2016-05-08 16:47:40 +0700 | [diff] [blame] | 48 | error_errno("copy-fd: write returned"); |
Junio C Hamano | 00b7cbf | 2015-05-19 10:55:16 -0700 | [diff] [blame] | 49 | break; |
| 50 | } |
Steffen Prohaska | b29763a | 2014-08-26 17:23:24 +0200 | [diff] [blame] | 51 | close(fdi); |
Daniel Barkalow | 1468bd4 | 2008-02-25 14:24:48 -0500 | [diff] [blame] | 52 | if (close(fdo) != 0) |
Nguyễn Thái Ngọc Duy | 37653a1 | 2016-05-08 16:47:40 +0700 | [diff] [blame] | 53 | return error_errno("%s: close error", dst); |
Daniel Barkalow | 1468bd4 | 2008-02-25 14:24:48 -0500 | [diff] [blame] | 54 | |
| 55 | if (!status && adjust_shared_perm(dst)) |
| 56 | return -1; |
| 57 | |
| 58 | return status; |
| 59 | } |
Clemens Buchacher | f7835a2 | 2009-09-12 11:03:48 +0200 | [diff] [blame] | 60 | |
| 61 | int copy_file_with_time(const char *dst, const char *src, int mode) |
| 62 | { |
| 63 | int status = copy_file(dst, src, mode); |
| 64 | if (!status) |
| 65 | return copy_times(dst, src); |
| 66 | return status; |
| 67 | } |