Git 2.39.4
[git.git] / copy.c
blob8492f6fc8319af3d7e1fe69b6d541fe086b93902
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 COPY_READ_ERROR;
12 if (write_in_full(ofd, buffer, len) < 0)
13 return COPY_WRITE_ERROR;
15 return 0;
18 static int copy_times(const char *dst, const char *src)
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;
31 int copy_file(const char *dst, const char *src, int mode)
33 int fdi, fdo, status;
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;
42 status = copy_fd(fdi, fdo);
43 switch (status) {
44 case COPY_READ_ERROR:
45 error_errno("copy-fd: read returned");
46 break;
47 case COPY_WRITE_ERROR:
48 error_errno("copy-fd: write returned");
49 break;
51 close(fdi);
52 if (close(fdo) != 0)
53 return error_errno("%s: close error", dst);
55 if (!status && adjust_shared_perm(dst))
56 return -1;
58 return status;
61 int copy_file_with_time(const char *dst, const char *src, int mode)
63 int status = copy_file(dst, src, mode);
64 if (!status)
65 return copy_times(dst, src);
66 return status;
69 static int do_symlinks_match(const char *path1, const char *path2)
71 struct strbuf buf1 = STRBUF_INIT, buf2 = STRBUF_INIT;
72 int ret = 0;
74 if (!strbuf_readlink(&buf1, path1, 0) &&
75 !strbuf_readlink(&buf2, path2, 0))
76 ret = !strcmp(buf1.buf, buf2.buf);
78 strbuf_release(&buf1);
79 strbuf_release(&buf2);
80 return ret;
83 int do_files_match(const char *path1, const char *path2)
85 struct stat st1, st2;
86 int fd1 = -1, fd2 = -1, ret = 1;
87 char buf1[8192], buf2[8192];
89 if ((fd1 = open_nofollow(path1, O_RDONLY)) < 0 ||
90 fstat(fd1, &st1) || !S_ISREG(st1.st_mode)) {
91 if (fd1 < 0 && errno == ELOOP)
92 /* maybe this is a symbolic link? */
93 return do_symlinks_match(path1, path2);
94 ret = 0;
95 } else if ((fd2 = open_nofollow(path2, O_RDONLY)) < 0 ||
96 fstat(fd2, &st2) || !S_ISREG(st2.st_mode)) {
97 ret = 0;
100 if (ret)
101 /* to match, neither must be executable, or both */
102 ret = !(st1.st_mode & 0111) == !(st2.st_mode & 0111);
104 if (ret)
105 ret = st1.st_size == st2.st_size;
107 while (ret) {
108 ssize_t len1 = read_in_full(fd1, buf1, sizeof(buf1));
109 ssize_t len2 = read_in_full(fd2, buf2, sizeof(buf2));
111 if (len1 < 0 || len2 < 0 || len1 != len2)
112 ret = 0; /* read error or different file size */
113 else if (!len1) /* len2 is also 0; hit EOF on both */
114 break; /* ret is still true */
115 else
116 ret = !memcmp(buf1, buf2, len1);
119 if (fd1 >= 0)
120 close(fd1);
121 if (fd2 >= 0)
122 close(fd2);
124 return ret;