Skip to content

Commit 584de0b

Browse files
committed
Add a helper function to compare file contents
In the next commit, Git will learn to disallow hooks during `git clone` operations _except_ when those hooks come from the templates (which are inherently supposed to be trusted). To that end, we add a function to compare the contents of two files. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent df93e40 commit 584de0b

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

cache.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,20 @@ int copy_fd(int ifd, int ofd);
17851785
int copy_file(const char *dst, const char *src, int mode);
17861786
int copy_file_with_time(const char *dst, const char *src, int mode);
17871787

1788+
/*
1789+
* Compare the file mode and contents of two given files.
1790+
*
1791+
* If both files are actually symbolic links, the function returns 1 if the link
1792+
* targets are identical or 0 if they are not.
1793+
*
1794+
* If any of the two files cannot be accessed or in case of read failures, this
1795+
* function returns 0.
1796+
*
1797+
* If the file modes and contents are identical, the function returns 1,
1798+
* otherwise it returns 0.
1799+
*/
1800+
int do_files_match(const char *path1, const char *path2);
1801+
17881802
void write_or_die(int fd, const void *buf, size_t count);
17891803
void fsync_or_die(int fd, const char *);
17901804
int fsync_component(enum fsync_component component, int fd);

copy.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,61 @@ int copy_file_with_time(const char *dst, const char *src, int mode)
6565
return copy_times(dst, src);
6666
return status;
6767
}
68+
69+
static int do_symlinks_match(const char *path1, const char *path2)
70+
{
71+
struct strbuf buf1 = STRBUF_INIT, buf2 = STRBUF_INIT;
72+
int ret = 0;
73+
74+
if (!strbuf_readlink(&buf1, path1, 0) &&
75+
!strbuf_readlink(&buf2, path2, 0))
76+
ret = !strcmp(buf1.buf, buf2.buf);
77+
78+
strbuf_release(&buf1);
79+
strbuf_release(&buf2);
80+
return ret;
81+
}
82+
83+
int do_files_match(const char *path1, const char *path2)
84+
{
85+
struct stat st1, st2;
86+
int fd1 = -1, fd2 = -1, ret = 1;
87+
char buf1[8192], buf2[8192];
88+
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;
98+
}
99+
100+
if (ret)
101+
/* to match, neither must be executable, or both */
102+
ret = !(st1.st_mode & 0111) == !(st2.st_mode & 0111);
103+
104+
if (ret)
105+
ret = st1.st_size == st2.st_size;
106+
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));
110+
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);
117+
}
118+
119+
if (fd1 >= 0)
120+
close(fd1);
121+
if (fd2 >= 0)
122+
close(fd2);
123+
124+
return ret;
125+
}

t/helper/test-path-utils.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,16 @@ int cmd__path_utils(int argc, const char **argv)
495495
return !!res;
496496
}
497497

498+
if (argc == 4 && !strcmp(argv[1], "do_files_match")) {
499+
int ret = do_files_match(argv[2], argv[3]);
500+
501+
if (ret)
502+
printf("equal\n");
503+
else
504+
printf("different\n");
505+
return !ret;
506+
}
507+
498508
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
499509
argv[1] ? argv[1] : "(there was none)");
500510
return 1;

t/t0060-path-utils.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,4 +560,45 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works'
560560
test_cmp expect actual
561561
'
562562

563+
test_expect_success 'do_files_match()' '
564+
test_seq 0 10 >0-10.txt &&
565+
test_seq -1 10 >-1-10.txt &&
566+
test_seq 1 10 >1-10.txt &&
567+
test_seq 1 9 >1-9.txt &&
568+
test_seq 0 8 >0-8.txt &&
569+
570+
test-tool path-utils do_files_match 0-10.txt 0-10.txt >out &&
571+
572+
assert_fails() {
573+
test_must_fail \
574+
test-tool path-utils do_files_match "$1" "$2" >out &&
575+
grep different out
576+
} &&
577+
578+
assert_fails 0-8.txt 1-9.txt &&
579+
assert_fails -1-10.txt 0-10.txt &&
580+
assert_fails 1-10.txt 1-9.txt &&
581+
assert_fails 1-10.txt .git &&
582+
assert_fails does-not-exist 1-10.txt &&
583+
584+
if test_have_prereq FILEMODE
585+
then
586+
cp 0-10.txt 0-10.x &&
587+
chmod a+x 0-10.x &&
588+
assert_fails 0-10.txt 0-10.x
589+
fi &&
590+
591+
if test_have_prereq SYMLINKS
592+
then
593+
ln -sf 0-10.txt symlink &&
594+
ln -s 0-10.txt another-symlink &&
595+
ln -s over-the-ocean yet-another-symlink &&
596+
ln -s "$PWD/0-10.txt" absolute-symlink &&
597+
assert_fails 0-10.txt symlink &&
598+
test-tool path-utils do_files_match symlink another-symlink &&
599+
assert_fails symlink yet-another-symlink &&
600+
assert_fails symlink absolute-symlink
601+
fi
602+
'
603+
563604
test_done

0 commit comments

Comments
 (0)