Skip to content

Commit

Permalink
Fix diff between symlinks and fifos
Browse files Browse the repository at this point in the history
diff should allow comparison between regular files and fifos
Added a few tests to help catch regressions here, and added
some cleanup for files the test creates.
  • Loading branch information
drosen-google authored and landley committed Sep 5, 2024
1 parent c556618 commit aea9568
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 21 additions & 0 deletions tests/diff.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ testcmd "simple" "-u -L lll -L rrr left right" '--- lll
10
+11
' "" ""
rm left right

mkdir -p tree1 tree2
echo foo > tree1/file
Expand All @@ -31,6 +32,8 @@ testcmd "-r" "-u -r -L tree1/file -L tree2/file tree1 tree2 | grep -v ^diff" \
-foo
+food
' "" ""
rm tree1/file tree2/file
rmdir tree1 tree2

echo -e "hello\r\nworld\r\n"> a
echo -e "hello\nworld\n"> b
Expand All @@ -49,6 +52,7 @@ testcmd "no follow symlink" "-q --no-dereference -L aa -L cc aa cc" "File aa is
ln -s ./aa dd
testcmd "symlink differs" "-q -L cc -L dd cc dd" "" "" ""
testcmd "symlink differs no follow" "-q --no-dereference -L cc -L dd cc dd" "Symbolic links cc and dd differ\n" "" ""
rm aa bb cc dd

mkfifo fifo1
mkfifo fifo2
Expand All @@ -62,6 +66,22 @@ testcmd "fifos" "-u -L fifo1 -L fifo2 fifo1 fifo2" '--- fifo1
+3
' "" ""

echo -e "1\n2" > fifo1&
echo -e "1\n3" > file1
ln -s file1 link1

testcmd "fifo symlinked file" "-u -L fifo1 -L link1 fifo1 link1" '--- fifo1
+++ link1
@@ -1,2 +1,2 @@
1
-2
+3
' "" ""

testcmd "fifo symlinked file no follow" "-u -L fifo1 -L link1 fifo1 link1 --no-dereference" "File fifo1 is a fifo while file link1 is a symbolic link\n" "" ""
testcmd "symlinked file stdin no follow" "-u -L link1 -L - link1 - --no-dereference" "File link1 is a symbolic link while file - is a fifo\n" "" "test"
rm fifo1 fifo2 link1 file1

echo -e 'int bar() {
}
Expand Down Expand Up @@ -109,6 +129,7 @@ testcmd 'show function' "--show-function-line=' {$' -U1 -L lll -L rrr a b" \
}
' \
'' ''
rm a b

seq 1 100000 > one
seq 1 4 100000 > two
Expand Down
8 changes: 5 additions & 3 deletions toys/pending/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,10 +914,12 @@ void diff_main(void)
if ((FLAG(no_dereference) ? lstat : stat)(files[!d], &TT.st[!d]))
perror_exit("%s", files[!d]);
}
if ((i = S_ISREG(TT.st[0].st_mode)) != S_ISREG(TT.st[1].st_mode)) {
char *fidir[] = {"regular file", "symbolic link"};
if ((S_ISLNK(TT.st[0].st_mode)) != S_ISLNK(TT.st[1].st_mode)) {
i = !strcmp(files[0], "-") ? 0 : S_ISREG(TT.st[0].st_mode) + 2 * S_ISLNK(TT.st[0].st_mode);
int k = !strcmp(files[0], "-") ? 0 : S_ISREG(TT.st[1].st_mode) + 2 * S_ISLNK(TT.st[1].st_mode);
char *fidir[] = {"fifo", "regular file", "symbolic link"};
printf("File %s is a %s while file %s is a %s\n",
files[0], fidir[!i], files[1], fidir[i]);
files[0], fidir[i], files[1], fidir[k]);
TT.differ = 1;
} else {
if (S_ISLNK(TT.st[0].st_mode)) do_symlink_diff(files);
Expand Down

0 comments on commit aea9568

Please sign in to comment.