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