changeset 580:4877cff01b25

dirtree logic cleanup: switch DIRTREE_NORECURSE and DIRTREE_NOSAVE to DIRTREE_RECURSE and DIRTREE_SAVE.
author Rob Landley <rob@landley.net>
date Sun, 20 May 2012 15:00:19 -0500
parents 6a7d64f51b1b
children 1e07220fd3b6
files lib/dirtree.c lib/lib.h toys/ls.c toys/mdev.c
diffstat 4 files changed, 24 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/lib/dirtree.c	Sun May 20 14:58:39 2012 -0500
+++ b/lib/dirtree.c	Sun May 20 15:00:19 2012 -0500
@@ -68,14 +68,14 @@
 
 // Default callback, filters out "." and "..".
 
-int dirtree_isdotdot(struct dirtree *catch)
+int dirtree_notdotdot(struct dirtree *catch)
 {
 	// Should we skip "." and ".."?
 	if (catch->name[0]=='.' && (!catch->name[1] ||
 			(catch->name[1]=='.' && !catch->name[2])))
-				return DIRTREE_NOSAVE|DIRTREE_NORECURSE;
+				return 0;
 
-	return 0;
+	return DIRTREE_SAVE|DIRTREE_RECURSE;
 }
 
 // Handle callback for a node in the tree. Returns saved node(s) or NULL.
@@ -91,11 +91,11 @@
 {
 	int flags;
 
-	if (!callback) callback = dirtree_isdotdot;
+	if (!callback) callback = dirtree_notdotdot;
 
 	flags = callback(new);
 	if (S_ISDIR(new->st.st_mode)) {
-		if (!(flags & DIRTREE_NORECURSE)) {
+		if (flags & DIRTREE_RECURSE) {
 			new->data = openat (new->parent ? new->parent->data : AT_FDCWD,
 				new->name, 0);
 			dirtree_recurse(new, callback);
@@ -104,7 +104,7 @@
 		if (flags & DIRTREE_COMEAGAIN) flags = callback(new);
 	}
 	// If this had children, it was callback's job to free them already.
-	if (flags & DIRTREE_NOSAVE) {
+	if (!(flags & DIRTREE_SAVE)) {
 		free(new);
 		new = NULL;
 	}
--- a/lib/lib.h	Sun May 20 14:58:39 2012 -0500
+++ b/lib/lib.h	Sun May 20 15:00:19 2012 -0500
@@ -48,16 +48,17 @@
 // Values returnable from callback function (bitfield, or them together)
 // Default with no callback is 0
 
-// Do not add this node to the tree
-#define DIRTREE_NOSAVE       1
-// Do not recurse into children
-#define DIRTREE_NORECURSE    2
-// Call again after handling all children (Directories only. Sets linklen = -1)
+// Add this node to the tree
+#define DIRTREE_SAVE         1
+// Recurse into children
+#define DIRTREE_RECURSE      2
+// Call again after handling all children of this directory
+// (Ignored for non-directories, sets linklen = -1 before second call.)
 #define DIRTREE_COMEAGAIN    4
 // Follow symlinks to directories
 #define DIRTREE_SYMFOLLOW    8
-// Abort recursive dirtree.  (Forces NOSAVE and NORECURSE on this entry.)
-#define DIRTREE_ABORT      (256|DIRTREE_NOSAVE|DIRTREE_NORECURSE)
+// Don't look at any more files in this directory.
+#define DIRTREE_ABORT      256
 
 #define DIRTREE_ABORTVAL ((struct dirtree *)1)
 
@@ -72,7 +73,7 @@
 
 struct dirtree *dirtree_add_node(int dirfd, char *name);
 char *dirtree_path(struct dirtree *node, int *plen);
-int dirtree_isdotdot(struct dirtree *catch);
+int dirtree_notdotdot(struct dirtree *catch);
 struct dirtree *handle_callback(struct dirtree *new,
 	int (*callback)(struct dirtree *node));
 void dirtree_recurse(struct dirtree *node,
--- a/toys/ls.c	Sun May 20 14:58:39 2012 -0500
+++ b/toys/ls.c	Sun May 20 15:00:19 2012 -0500
@@ -139,11 +139,10 @@
 // TODO should -1f print here to handle enormous dirs without runing
 // out of mem?
 
-    if (flags & FLAG_a) return DIRTREE_NORECURSE;
-    if (!(flags & FLAG_A) && new->name[0]=='.')
-        return DIRTREE_NOSAVE|DIRTREE_NORECURSE;
+    if (flags & FLAG_a) return 0;
+    if (!(flags & FLAG_A) && new->name[0]=='.') return 0;
 
-    return dirtree_isdotdot(new)|DIRTREE_NORECURSE;
+    return dirtree_notdotdot(new);
 }
 
 // Display a list of dirtree entries, according to current format
@@ -289,7 +288,7 @@
     for (ul = 0; ul<dtlen; free(sort[ul++])) {
 // TODO follow symlinks when?
         if ((flags & FLAG_d) || !S_ISDIR(sort[ul]->st.st_mode)
-            || dirtree_isdotdot(sort[ul])) continue;
+            || !dirtree_notdotdot(sort[ul])) continue;
 
         // Recurse into dirs if at top of the tree or given -R
         if (!indir->parent || (flags & FLAG_R))
--- a/toys/mdev.c	Sun May 20 14:58:39 2012 -0500
+++ b/toys/mdev.c	Sun May 20 15:00:19 2012 -0500
@@ -36,6 +36,8 @@
 #include "toys.h"
 #include "lib/xregcomp.h"
 
+// todo, open() block devices to trigger partition scanning.
+
 // mknod in /dev based on a path like "/sys/block/hda/hda1"
 static void make_device(char *path)
 {
@@ -180,7 +182,7 @@
 {
 	// Entries in /sys/class/block aren't char devices, so skip 'em.  (We'll
 	// get block devices out of /sys/block.)
-	if(!strcmp(node->name, "block")) return DIRTREE_NOSAVE|DIRTREE_NORECURSE;
+	if(!strcmp(node->name, "block")) return 0;
 
 	// Does this directory have a "dev" entry in it?
 	// This is path based because the hotplug callbacks are
@@ -194,9 +196,8 @@
 
 	// Circa 2.6.25 the entries more than 2 deep are all either redundant
 	// (mouse#, event#) or unnamed (every usb_* entry is called "device").
-    if (node->parent && node->parent->parent)
-		return DIRTREE_NOSAVE|DIRTREE_NORECURSE;
-	return DIRTREE_NOSAVE;
+
+	return (node->parent && node->parent->parent) ? 0 : DIRTREE_RECURSE;
 }
 
 void mdev_main(void)