]>
Commit | Line | Data |
---|---|---|
1 | #include "git-compat-util.h" | |
2 | #include "copy.h" | |
3 | #include "path.h" | |
4 | ||
5 | int copy_fd(int ifd, int ofd) | |
6 | { | |
7 | while (1) { | |
8 | char buffer[8192]; | |
9 | ssize_t len = xread(ifd, buffer, sizeof(buffer)); | |
10 | if (!len) | |
11 | break; | |
12 | if (len < 0) | |
13 | return COPY_READ_ERROR; | |
14 | if (write_in_full(ofd, buffer, len) < 0) | |
15 | return COPY_WRITE_ERROR; | |
16 | } | |
17 | return 0; | |
18 | } | |
19 | ||
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 | ||
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); | |
45 | switch (status) { | |
46 | case COPY_READ_ERROR: | |
47 | error_errno("copy-fd: read returned"); | |
48 | break; | |
49 | case COPY_WRITE_ERROR: | |
50 | error_errno("copy-fd: write returned"); | |
51 | break; | |
52 | } | |
53 | close(fdi); | |
54 | if (close(fdo) != 0) | |
55 | return error_errno("%s: close error", dst); | |
56 | ||
57 | if (!status && adjust_shared_perm(dst)) | |
58 | return -1; | |
59 | ||
60 | return status; | |
61 | } | |
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 | } |