format-patch: ensure that --rfc and -k are mutually exclusive
[alt-git.git] / copy.c
blob23d84c6c1db554bcc7a33804d58821dfb3fb7634
1 #include "git-compat-util.h"
2 #include "copy.h"
3 #include "path.h"
5 int copy_fd(int ifd, int ofd)
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;
17 return 0;
20 static int copy_times(const char *dst, const char *src)
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, &times) < 0)
29 return -1;
30 return 0;
33 int copy_file(const char *dst, const char *src, int mode)
35 int fdi, fdo, status;
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;
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;
53 close(fdi);
54 if (close(fdo) != 0)
55 return error_errno("%s: close error", dst);
57 if (!status && adjust_shared_perm(dst))
58 return -1;
60 return status;
63 int copy_file_with_time(const char *dst, const char *src, int mode)
65 int status = copy_file(dst, src, mode);
66 if (!status)
67 return copy_times(dst, src);
68 return status;