From 8d95074b7d034188af8542aaea0306d3670d71be Mon Sep 17 00:00:00 2001
From: Rob Landley <rob@landley.net>
Date: Mon, 7 Mar 2016 16:02:47 -0600
Subject: Cleanup pass on the dirtree infrastructure, in preparation for making
 rm -r handle infinite depth. Fix docs, tweak dirtree_handle_callback()
 semantics, remove dirtree_start() and don't export dirtree_handle_callback(),
 instead offer dirtree_flagread(). (dirtree_read() is a wrapper around
 dirtree_flagread passing 0 for flags.)

---
 toys/posix/chgrp.c |  4 ++--
 toys/posix/cp.c    |  7 +++----
 toys/posix/du.c    |  2 +-
 toys/posix/find.c  |  2 +-
 toys/posix/ls.c    | 11 +++++++----
 5 files changed, 14 insertions(+), 12 deletions(-)

(limited to 'toys/posix')

diff --git a/toys/posix/chgrp.c b/toys/posix/chgrp.c
index 6b95c6ad..e0690c93 100644
--- a/toys/posix/chgrp.c
+++ b/toys/posix/chgrp.c
@@ -94,8 +94,8 @@ void chgrp_main(void)
     TT.group = xgetgrnamid(TT.group_name)->gr_gid;
 
   for (s=toys.optargs+1; *s; s++)
-    dirtree_handle_callback(dirtree_start(*s, toys.optflags&(FLAG_H|FLAG_L)),
-      do_chgrp);;
+    dirtree_flagread(*s, DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)),
+      do_chgrp);
 
   if (CFG_TOYBOX_FREE && ischown) free(own);
 }
diff --git a/toys/posix/cp.c b/toys/posix/cp.c
index cb7e6e3b..0e6a2efa 100644
--- a/toys/posix/cp.c
+++ b/toys/posix/cp.c
@@ -430,12 +430,11 @@ void cp_main(void)
       if (rc) rc = rename(src, TT.destname);
     }
 
-    // Skip nonexistent sources
+    // Copy if we didn't mv, skipping nonexistent sources
     if (rc) {
-      if (errno!=EXDEV ||
-        !(new = dirtree_start(src, toys.optflags&(FLAG_H|FLAG_L))))
+      if (errno!=EXDEV || dirtree_flagread(src, DIRTREE_SHUTUP+
+        DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)), TT.callback))
           perror_msg("bad '%s'", src);
-      else dirtree_handle_callback(new, TT.callback);
     }
     if (destdir) free(TT.destname);
   }
diff --git a/toys/posix/du.c b/toys/posix/du.c
index 77c7b6e2..2797b3f4 100644
--- a/toys/posix/du.c
+++ b/toys/posix/du.c
@@ -153,7 +153,7 @@ void du_main(void)
 
   // Loop over command line arguments, recursing through children
   for (args = toys.optc ? toys.optargs : noargs; *args; args++)
-    dirtree_handle_callback(dirtree_start(*args, toys.optflags&(FLAG_H|FLAG_L)),
+    dirtree_flagread(*args, DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)),
       do_du);
   if (toys.optflags & FLAG_c) print(TT.total*512, 0);
 
diff --git a/toys/posix/find.c b/toys/posix/find.c
index a9c35f41..303aada2 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -556,7 +556,7 @@ void find_main(void)
 
   // Loop through paths
   for (i = 0; i < len; i++)
-    dirtree_handle_callback(dirtree_start(ss[i], toys.optflags&(FLAG_H|FLAG_L)),
+    dirtree_flagread(ss[i], DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)),
       do_find);
 
   execdir(0, 1);
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index 4dcbe888..799631b1 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -540,12 +540,15 @@ void ls_main(void)
   if (toys.optflags & FLAG_d) toys.optflags &= ~FLAG_R;
 
   // Iterate through command line arguments, collecting directories and files.
-  // Non-absolute paths are relative to current directory.
-  TT.files = dirtree_start(0, 0);
+  // Non-absolute paths are relative to current directory. Top of tree is
+  // a dummy node to collect command line arguments into pseudo-directory.
+  TT.files = dirtree_add_node(0, 0, 0);
   TT.files->dirfd = AT_FDCWD;
   for (s = *toys.optargs ? toys.optargs : noargs; *s; s++) {
-    dt = dirtree_start(*s, !(toys.optflags&(FLAG_l|FLAG_d|FLAG_F)) ||
-                            (toys.optflags&(FLAG_L|FLAG_H)));
+    int sym = !(toys.optflags&(FLAG_l|FLAG_d|FLAG_F))
+      || (toys.optflags&(FLAG_L|FLAG_H));
+
+    dt = dirtree_add_node(0, *s, DIRTREE_SYMFOLLOW*sym);
 
     // note: double_list->prev temporarirly goes in dirtree->parent
     if (dt) dlist_add_nomalloc((void *)&TT.files->child, (void *)dt);
-- 
cgit v1.2.3