changeset 1591:a016421051e4 draft

Ashwini Sharma pointed out that "mkdir sub; ln -s . sub/up; du -L sub" shouldn't loop endlessly.
author Rob Landley <rob@landley.net>
date Mon, 01 Dec 2014 12:52:55 -0600
parents d5931155cafe
children c0dee3caad31
files tests/du.test toys/posix/du.c
diffstat 2 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/tests/du.test	Mon Dec 01 03:15:25 2014 -0600
+++ b/tests/du.test	Mon Dec 01 12:52:55 2014 -0600
@@ -18,6 +18,9 @@
 # allocated file space is zero.
 testing "du counts symlinks without following" "du -ks du_test" "8\tdu_test\n" "" ""
 testing "du -L follows symlinks" "du -ksL du_test" "16\tdu_test\n" "" ""
+ln -s . du_test/up
+testing "du -L avoid endless loop" "du -ksL du_test" "16\tdu_test\n" "" ""
+rm du_test/up
 # if -H and -L are specified, the last takes priority
 testing "du -HL follows symlinks" "du -ksHL du_test" "16\tdu_test\n" "" ""
 testing "du -H does not follow unspecified symlinks" "du -ksH du_test" "8\tdu_test\n" "" ""
--- a/toys/posix/du.c	Mon Dec 01 03:15:25 2014 -0600
+++ b/toys/posix/du.c	Mon Dec 01 12:52:55 2014 -0600
@@ -110,6 +110,15 @@
   if ((toys.optflags & FLAG_x) && (TT.st_dev != node->st.st_dev))
     return 0;
 
+  // Don't loop endlessly on recursive directory symlink
+  if (toys.optflags & FLAG_L) {
+    struct dirtree *try = node;
+
+    while ((try = try->parent))
+      if (node->st.st_dev==try->st.st_dev && node->st.st_ino==try->st.st_ino)
+        return 0;
+  }
+
   // Don't count hard links twice
   if (!(toys.optflags & FLAG_l) && !node->again)
     if (seen_inode(&TT.inodes, &node->st)) return 0;