Commit c26870dab3462c6176936384b090df6b9ba46dee broke ls' error
reporting for files that don't exist. `ls $F` is used by some as an
equivalent of `test -e $F`.

This patch also adds a regression test.
---
 tests/ls.test   |  3 +++
 toys/posix/ls.c | 10 +++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
From 73b1f012a0b29bd6665d66d45cca50715066264d Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Thu, 29 Aug 2019 12:47:52 -0700
Subject: [PATCH] ls: fix recent regression.

Commit c26870dab3462c6176936384b090df6b9ba46dee broke ls' error
reporting for files that don't exist. `ls $F` is used by some as an
equivalent of `test -e $F`.

This patch also adds a regression test.
---
 tests/ls.test   |  3 +++
 toys/posix/ls.c | 10 +++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/tests/ls.test b/tests/ls.test
index 56f74034..6c8e0efe 100755
--- a/tests/ls.test
+++ b/tests/ls.test
@@ -50,5 +50,8 @@ rm -rf lstest/* && touch lstest/file1.txt && INODE=`stat -c %i lstest/file1.txt`
 testing "with -i" "$IN && ls -i 2>/dev/null; $OUT" "$INODE file1.txt\n" "" ""
 unset INODE
 
+testing "missing" "$IN && ls does-not-exist 2>err ; grep -q 'ls:.*missing.*: No
+such file' err || echo missing error; $OUT" "" "" ""
+
 # Removing test dir for cleanup purpose
 rm -rf lstest
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index fbd2405c..4ae352af 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -583,9 +583,13 @@ void ls_main(void)
 
     dt = dirtree_add_node(0, *s, DIRTREE_STATLESS|DIRTREE_SYMFOLLOW*sym);
 
-    // note: double_list->prev temporarirly goes in dirtree->parent
-    if (dt) dlist_add_nomalloc((void *)&TT.files->child, (void *)dt);
-    else toys.exitval = 1;
+    // note: double_list->prev temporarily goes in dirtree->parent
+    if (dt) {
+      if (!dt->st.st_blksize && !dt->st.st_dev && !dt->st.st_ino) {
+        perror_msg_raw(*s);
+        free(dt);
+      } else dlist_add_nomalloc((void *)&TT.files->child, (void *)dt);
+    } else toys.exitval = 1;
   }
 
   // Convert double_list into dirtree.
-- 
2.23.0.187.g17f5b7556c-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to