Line | Count | Source (jump to first uncovered line) |
1 | | #include "git-compat-util.h" |
2 | | #include "copy.h" |
3 | | #include "path.h" |
4 | | #include "gettext.h" |
5 | | #include "strbuf.h" |
6 | | #include "abspath.h" |
7 | | |
8 | | int copy_fd(int ifd, int ofd) |
9 | 0 | { |
10 | 0 | while (1) { |
11 | 0 | char buffer[8192]; |
12 | 0 | ssize_t len = xread(ifd, buffer, sizeof(buffer)); |
13 | 0 | if (!len) |
14 | 0 | break; |
15 | 0 | if (len < 0) |
16 | 0 | return COPY_READ_ERROR; |
17 | 0 | if (write_in_full(ofd, buffer, len) < 0) |
18 | 0 | return COPY_WRITE_ERROR; |
19 | 0 | } |
20 | 0 | return 0; |
21 | 0 | } |
22 | | |
23 | | static int copy_times(const char *dst, const char *src) |
24 | 0 | { |
25 | 0 | struct stat st; |
26 | 0 | struct utimbuf times; |
27 | 0 | if (stat(src, &st) < 0) |
28 | 0 | return -1; |
29 | 0 | times.actime = st.st_atime; |
30 | 0 | times.modtime = st.st_mtime; |
31 | 0 | if (utime(dst, ×) < 0) |
32 | 0 | return -1; |
33 | 0 | return 0; |
34 | 0 | } |
35 | | |
36 | | int copy_file(const char *dst, const char *src, int mode) |
37 | 0 | { |
38 | 0 | int fdi, fdo, status; |
39 | |
|
40 | 0 | mode = (mode & 0111) ? 0777 : 0666; |
41 | 0 | if ((fdi = open(src, O_RDONLY)) < 0) |
42 | 0 | return fdi; |
43 | 0 | if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) { |
44 | 0 | close(fdi); |
45 | 0 | return fdo; |
46 | 0 | } |
47 | 0 | status = copy_fd(fdi, fdo); |
48 | 0 | switch (status) { |
49 | 0 | case COPY_READ_ERROR: |
50 | 0 | error_errno("copy-fd: read returned"); |
51 | 0 | break; |
52 | 0 | case COPY_WRITE_ERROR: |
53 | 0 | error_errno("copy-fd: write returned"); |
54 | 0 | break; |
55 | 0 | } |
56 | 0 | close(fdi); |
57 | 0 | if (close(fdo) != 0) |
58 | 0 | return error_errno("%s: close error", dst); |
59 | | |
60 | 0 | if (!status && adjust_shared_perm(dst)) |
61 | 0 | return -1; |
62 | | |
63 | 0 | return status; |
64 | 0 | } |
65 | | |
66 | | int copy_file_with_time(const char *dst, const char *src, int mode) |
67 | 0 | { |
68 | 0 | int status = copy_file(dst, src, mode); |
69 | 0 | if (!status) |
70 | 0 | return copy_times(dst, src); |
71 | 0 | return status; |
72 | 0 | } |