config: pass 'repo' directly to 'config_with_options()'
[alt-git.git] / copy.c
blob882c79cffb0d38434dde66a307b32b460440f875
1 #include "git-compat-util.h"
2 #include "copy.h"
3 #include "path.h"
4 #include "wrapper.h"
6 int copy_fd(int ifd, int ofd)
8 while (1) {
9 char buffer[8192];
10 ssize_t len = xread(ifd, buffer, sizeof(buffer));
11 if (!len)
12 break;
13 if (len < 0)
14 return COPY_READ_ERROR;
15 if (write_in_full(ofd, buffer, len) < 0)
16 return COPY_WRITE_ERROR;
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 switch (status) {
47 case COPY_READ_ERROR:
48 error_errno("copy-fd: read returned");
49 break;
50 case COPY_WRITE_ERROR:
51 error_errno("copy-fd: write returned");
52 break;
54 close(fdi);
55 if (close(fdo) != 0)
56 return error_errno("%s: close error", dst);
58 if (!status && adjust_shared_perm(dst))
59 return -1;
61 return status;
64 int copy_file_with_time(const char *dst, const char *src, int mode)
66 int status = copy_file(dst, src, mode);
67 if (!status)
68 return copy_times(dst, src);
69 return status;