3 int copy_fd(int ifd
, int ofd
)
7 ssize_t len
= xread(ifd
, buffer
, sizeof(buffer
));
11 return COPY_READ_ERROR
;
12 if (write_in_full(ofd
, buffer
, len
) < 0)
13 return COPY_WRITE_ERROR
;
18 static int copy_times(const char *dst
, const char *src
)
22 if (stat(src
, &st
) < 0)
24 times
.actime
= st
.st_atime
;
25 times
.modtime
= st
.st_mtime
;
26 if (utime(dst
, ×
) < 0)
31 int copy_file(const char *dst
, const char *src
, int mode
)
35 mode
= (mode
& 0111) ? 0777 : 0666;
36 if ((fdi
= open(src
, O_RDONLY
)) < 0)
38 if ((fdo
= open(dst
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
)) < 0) {
42 status
= copy_fd(fdi
, fdo
);
45 error_errno("copy-fd: read returned");
47 case COPY_WRITE_ERROR
:
48 error_errno("copy-fd: write returned");
53 return error_errno("%s: close error", dst
);
55 if (!status
&& adjust_shared_perm(dst
))
61 int copy_file_with_time(const char *dst
, const char *src
, int mode
)
63 int status
= copy_file(dst
, src
, mode
);
65 return copy_times(dst
, src
);
69 static int do_symlinks_match(const char *path1
, const char *path2
)
71 struct strbuf buf1
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
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
);
83 int do_files_match(const char *path1
, const char *path2
)
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
);
95 } else if ((fd2
= open_nofollow(path2
, O_RDONLY
)) < 0 ||
96 fstat(fd2
, &st2
) || !S_ISREG(st2
.st_mode
)) {
101 /* to match, neither must be executable, or both */
102 ret
= !(st1
.st_mode
& 0111) == !(st2
.st_mode
& 0111);
105 ret
= st1
.st_size
== st2
.st_size
;
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 */
116 ret
= !memcmp(buf1
, buf2
, len1
);