Added 'depth limit' option to pnd_disco searchpaths, so can search recursively foreve...
authorskeezix <skeezix@flotsam-vm.(none)>
Wed, 18 May 2011 01:33:29 +0000 (21:33 -0400)
committerskeezix <skeezix@flotsam-vm.(none)>
Wed, 18 May 2011 01:33:29 +0000 (21:33 -0400)
lib/pnd_discovery.c
lib/pnd_pathiter.h

index 8afbb38..e1fc0ef 100644 (file)
@@ -11,6 +11,7 @@
 
 #define _XOPEN_SOURCE 500
 #define __USE_XOPEN_EXTENDED
 
 #define _XOPEN_SOURCE 500
 #define __USE_XOPEN_EXTENDED
+#define _GNU_SOURCE
 #include <ftw.h> /* for nftw, tree walker */
 
 #include "pnd_container.h"
 #include <ftw.h> /* for nftw, tree walker */
 
 #include "pnd_container.h"
@@ -70,7 +71,10 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
     if ( logit ) {
       pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
     }
     if ( logit ) {
       pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
     }
-    return ( 0 ); // skip directories and other non-regular files
+    if ( ftwbuf -> level >= pathiter_depthlimit ) {
+      return ( FTW_SKIP_SUBTREE );
+    }
+    return ( FTW_CONTINUE ); // skip directories and other non-regular files
   }
 
   // PND/PNZ file and others may be valid as well .. but lets leave that for now
   }
 
   // PND/PNZ file and others may be valid as well .. but lets leave that for now
@@ -86,7 +90,7 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
     if ( logit ) {
       pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
     }
     if ( logit ) {
       pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
     }
-    return ( 0 );
+    return ( FTW_CONTINUE );
   }
 
   // potentially a valid application
   }
 
   // potentially a valid application
@@ -114,13 +118,13 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
     // try to locate the PXML portion
     if ( ! pnd_pnd_seek_pxml ( f ) ) {
       fclose ( f );
     // try to locate the PXML portion
     if ( ! pnd_pnd_seek_pxml ( f ) ) {
       fclose ( f );
-      return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
+      return ( FTW_CONTINUE ); // pnd or not, but not to spec. Pwn'd the pnd?
     }
 
     // accrue it into a buffer
     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
       fclose ( f );
     }
 
     // accrue it into a buffer
     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
       fclose ( f );
-      return ( 0 );
+      return ( FTW_CONTINUE );
     }
 
     //printf ( "buffer is %s\n", pxmlbuf );
     }
 
     //printf ( "buffer is %s\n", pxmlbuf );
@@ -170,7 +174,7 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
 
   // pxmlh is useful?
   if ( ! pxmlapps ) {
 
   // pxmlh is useful?
   if ( ! pxmlapps ) {
-    return ( 0 ); // continue tree walk
+    return ( FTW_CONTINUE ); // continue tree walk
   }
 
   // for ovr-file
   }
 
   // for ovr-file
@@ -392,7 +396,7 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
   // free up the applist
   free ( pxmlapps );
 
   // free up the applist
   free ( pxmlapps );
 
-  return ( 0 ); // continue the tree walk
+  return ( FTW_CONTINUE ); // continue the tree walk
 }
 
 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
 }
 
 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
@@ -413,7 +417,7 @@ pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
     nftw ( buffer,               // path to descend
           pnd_disco_callback,   // callback to do processing
           10,                   // no more than X open fd's at once
     nftw ( buffer,               // path to descend
           pnd_disco_callback,   // callback to do processing
           10,                   // no more than X open fd's at once
-          FTW_PHYS );           // do not follow symlinks
+          FTW_PHYS | FTW_ACTIONRETVAL );   // do not follow symlinks
 
   }
   SEARCHPATH_POST
 
   }
   SEARCHPATH_POST
index 5e13aa8..d3fabf0 100644 (file)
@@ -13,6 +13,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <ctype.h> // for isdigit()
 
 // this really should be replaced by a function pair .. one to
 // start a new search and one to go to next, bailing when done. Maybe
 
 // this really should be replaced by a function pair .. one to
 // start a new search and one to go to next, bailing when done. Maybe
 // func with a callback. Whatever.
 
 
 // func with a callback. Whatever.
 
 
+// will be set to 0xFFFF for no limit currently set, or a smaller number
+// if that is the depth limit
+unsigned int pathiter_depthlimit;
+
+
 #if 1 // globbing is performed
 
 // only iterates actual existing paths; if you want to return
 #if 1 // globbing is performed
 
 // only iterates actual existing paths; if you want to return
@@ -29,6 +35,7 @@
   char *end, *colon;                              \
   char *head = searchpath;                        \
   char chunk [ FILENAME_MAX ];                    \
   char *end, *colon;                              \
   char *head = searchpath;                        \
   char chunk [ FILENAME_MAX ];                    \
+  char *lt;                                       \
                                                   \
   /*fprintf ( stderr, "sp %s\n", searchpath );*/  \
                                                   \
                                                   \
   /*fprintf ( stderr, "sp %s\n", searchpath );*/  \
                                                   \
       strncpy ( chunk, head, FILENAME_MAX - 1 );  \
     }                                             \
                                                   \
       strncpy ( chunk, head, FILENAME_MAX - 1 );  \
     }                                             \
                                                   \
-    /*fprintf ( stderr, "-> %s\n", chunk ); */    \
+    pathiter_depthlimit = 0xFFFF;                \
+                                                  \
+    if ( ( lt = strchr ( chunk, '<' ) ) ) {       \
+      if ( isdigit ( *(lt+1) ) ) {                \
+        pathiter_depthlimit = atoi ( lt + 1 );    \
+      }                                           \
+      *lt = '\0';                                 \
+    }                                             \
+                                                  \
+    /*fprintf ( stderr, "-> chunk %s limit %d\n", chunk, pathiter_depthlimit );*/ \
                                                   \
     struct stat statbuf;                          \
     wordexp_t _p;                                 \
                                                   \
     struct stat statbuf;                          \
     wordexp_t _p;                                 \