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.
---
 tests/diff.test     | 21 +++++++++++++++++++++
 toys/pending/diff.c |  8 +++++---
 2 files changed, 26 insertions(+), 3 deletions(-)
From 277a717ab728083749d2837dae2c2dba42570c2a Mon Sep 17 00:00:00 2001
From: Daniel Rosenberg <dro...@google.com>
Date: Wed, 4 Sep 2024 15:32:13 -0700
Subject: [PATCH] Fix diff between symlinks and fifos

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.
---
 tests/diff.test     | 21 +++++++++++++++++++++
 toys/pending/diff.c |  8 +++++---
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/tests/diff.test b/tests/diff.test
index 8c5834d3..4c2fd8a7 100644
--- a/tests/diff.test
+++ b/tests/diff.test
@@ -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
@@ -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
@@ -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
@@ -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() {
 }
 
@@ -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
diff --git a/toys/pending/diff.c b/toys/pending/diff.c
index 95ae7171..362365df 100644
--- a/toys/pending/diff.c
+++ b/toys/pending/diff.c
@@ -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);

base-commit: c55661843fd906073850eb2c9224009ff92acbeb
-- 
2.46.0.598.g6f2099f65c-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to