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