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