]> git.ipfire.org Git - thirdparty/git.git/blob - copy.c
The fourth batch
[thirdparty/git.git] / copy.c
1 #include "git-compat-util.h"
2 #include "copy.h"
3 #include "path.h"
4 #include "gettext.h"
5 #include "strbuf.h"
6 #include "abspath.h"
7
8 int copy_fd(int ifd, int ofd)
9 {
10 while (1) {
11 char buffer[8192];
12 ssize_t len = xread(ifd, buffer, sizeof(buffer));
13 if (!len)
14 break;
15 if (len < 0)
16 return COPY_READ_ERROR;
17 if (write_in_full(ofd, buffer, len) < 0)
18 return COPY_WRITE_ERROR;
19 }
20 return 0;
21 }
22
23 static int copy_times(const char *dst, const char *src)
24 {
25 struct stat st;
26 struct utimbuf times;
27 if (stat(src, &st) < 0)
28 return -1;
29 times.actime = st.st_atime;
30 times.modtime = st.st_mtime;
31 if (utime(dst, &times) < 0)
32 return -1;
33 return 0;
34 }
35
36 int copy_file(const char *dst, const char *src, int mode)
37 {
38 int fdi, fdo, status;
39
40 mode = (mode & 0111) ? 0777 : 0666;
41 if ((fdi = open(src, O_RDONLY)) < 0)
42 return fdi;
43 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
44 close(fdi);
45 return fdo;
46 }
47 status = copy_fd(fdi, fdo);
48 switch (status) {
49 case COPY_READ_ERROR:
50 error_errno("copy-fd: read returned");
51 break;
52 case COPY_WRITE_ERROR:
53 error_errno("copy-fd: write returned");
54 break;
55 }
56 close(fdi);
57 if (close(fdo) != 0)
58 return error_errno("%s: close error", dst);
59
60 if (!status && adjust_shared_perm(dst))
61 return -1;
62
63 return status;
64 }
65
66 int copy_file_with_time(const char *dst, const char *src, int mode)
67 {
68 int status = copy_file(dst, src, mode);
69 if (!status)
70 return copy_times(dst, src);
71 return status;
72 }
73
74 static int do_symlinks_match(const char *path1, const char *path2)
75 {
76 struct strbuf buf1 = STRBUF_INIT, buf2 = STRBUF_INIT;
77 int ret = 0;
78
79 if (!strbuf_readlink(&buf1, path1, 0) &&
80 !strbuf_readlink(&buf2, path2, 0))
81 ret = !strcmp(buf1.buf, buf2.buf);
82
83 strbuf_release(&buf1);
84 strbuf_release(&buf2);
85 return ret;
86 }
87
88 int do_files_match(const char *path1, const char *path2)
89 {
90 struct stat st1, st2;
91 int fd1 = -1, fd2 = -1, ret = 1;
92 char buf1[8192], buf2[8192];
93
94 if ((fd1 = open_nofollow(path1, O_RDONLY)) < 0 ||
95 fstat(fd1, &st1) || !S_ISREG(st1.st_mode)) {
96 if (fd1 < 0 && errno == ELOOP)
97 /* maybe this is a symbolic link? */
98 return do_symlinks_match(path1, path2);
99 ret = 0;
100 } else if ((fd2 = open_nofollow(path2, O_RDONLY)) < 0 ||
101 fstat(fd2, &st2) || !S_ISREG(st2.st_mode)) {
102 ret = 0;
103 }
104
105 if (ret)
106 /* to match, neither must be executable, or both */
107 ret = !(st1.st_mode & 0111) == !(st2.st_mode & 0111);
108
109 if (ret)
110 ret = st1.st_size == st2.st_size;
111
112 while (ret) {
113 ssize_t len1 = read_in_full(fd1, buf1, sizeof(buf1));
114 ssize_t len2 = read_in_full(fd2, buf2, sizeof(buf2));
115
116 if (len1 < 0 || len2 < 0 || len1 != len2)
117 ret = 0; /* read error or different file size */
118 else if (!len1) /* len2 is also 0; hit EOF on both */
119 break; /* ret is still true */
120 else
121 ret = !memcmp(buf1, buf2, len1);
122 }
123
124 if (fd1 >= 0)
125 close(fd1);
126 if (fd2 >= 0)
127 close(fd2);
128
129 return ret;
130 }