Merge branch 'jk/config-no-ungetc-eof'
[git/mingw/j6t.git] / copy.c
blobf2970ec46282a07593366c598f688e5d70c05c83
1 #include "cache.h"
3 int copy_fd(int ifd, int ofd)
5 while (1) {
6 char buffer[8192];
7 ssize_t len = xread(ifd, buffer, sizeof(buffer));
8 if (!len)
9 break;
10 if (len < 0) {
11 return error("copy-fd: read returned %s",
12 strerror(errno));
14 if (write_in_full(ofd, buffer, len) < 0)
15 return error("copy-fd: write returned %s",
16 strerror(errno));
18 return 0;
21 static int copy_times(const char *dst, const char *src)
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, &times) < 0)
30 return -1;
31 return 0;
34 int copy_file(const char *dst, const char *src, int mode)
36 int fdi, fdo, status;
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;
45 status = copy_fd(fdi, fdo);
46 close(fdi);
47 if (close(fdo) != 0)
48 return error("%s: close error: %s", dst, strerror(errno));
50 if (!status && adjust_shared_perm(dst))
51 return -1;
53 return status;
56 int copy_file_with_time(const char *dst, const char *src, int mode)
58 int status = copy_file(dst, src, mode);
59 if (!status)
60 return copy_times(dst, src);
61 return status;