lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [lucy-commits] svn commit: r1337370 - in /lucy/trunk/clownfish/src: CFCUtil.c CFCUtil.h
Date Fri, 11 May 2012 20:44:51 GMT
Author: marvin
Date: Fri May 11 20:44:51 2012
New Revision: 1337370

URL: http://svn.apache.org/viewvc?rev=1337370&view=rev
Log:
Add CFCUtil_walk().

Modified:
    lucy/trunk/clownfish/src/CFCUtil.c
    lucy/trunk/clownfish/src/CFCUtil.h

Modified: lucy/trunk/clownfish/src/CFCUtil.c
URL: http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCUtil.c?rev=1337370&r1=1337369&r2=1337370&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCUtil.c (original)
+++ lucy/trunk/clownfish/src/CFCUtil.c Fri May 11 20:44:51 2012
@@ -313,6 +313,43 @@ CFCUtil_make_path(const char *path) {
     return true;
 }
 
+void
+CFCUtil_walk(const char *path, CFCUtil_walk_callback_t callback,
+             void *context) {
+    // If it's a valid file system entry, invoke the callback.
+    struct stat stat_buf;
+    int stat_check = stat(path, &stat_buf);
+    if (stat_check == -1) {
+        return;
+    }
+    callback(path, context);
+
+    // Recurse into directories.
+    if (!(stat_buf.st_mode & S_IFDIR)) {
+        return;
+    }
+    void   *dirhandle   = CFCUtil_opendir(path);
+    size_t  dir_len     = strlen(path);
+    size_t  subpath_cap = dir_len * 2;
+    char   *subpath     = (char*)MALLOCATE(subpath_cap);
+    const char *entry   = NULL;
+    while (NULL != (entry = CFCUtil_dirnext(dirhandle))) {
+        if (strcmp(entry, ".") == 0 || strcmp(entry, "..") == 0) {
+            continue;
+        }
+        size_t name_len = strlen(entry);
+        size_t needed = dir_len + 1 + name_len + 1;
+        if (needed > subpath_cap) {
+            subpath_cap = needed;
+            subpath = (char*)REALLOCATE(subpath, subpath_cap);
+        }
+        sprintf(subpath, "%s" CFCUTIL_PATH_SEP "%s", path, entry);
+        CFCUtil_walk(subpath, callback, context);
+    }
+    FREEMEM(subpath);
+    CFCUtil_closedir(dirhandle, path);
+}
+
 /******************************** WINDOWS **********************************/
 #ifdef _WIN32
 

Modified: lucy/trunk/clownfish/src/CFCUtil.h
URL: http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCUtil.h?rev=1337370&r1=1337369&r2=1337370&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCUtil.h (original)
+++ lucy/trunk/clownfish/src/CFCUtil.h Fri May 11 20:44:51 2012
@@ -170,6 +170,15 @@ CFCUtil_make_dir(const char *dir);
 int
 CFCUtil_make_path(const char *path);
 
+/* Walk the file system, recursing into subdirectories.  Invoke the supplied
+ * callback for each valid, accessible file system entry.
+ */
+typedef void
+(*CFCUtil_walk_callback_t)(const char *path, void *context);
+void
+CFCUtil_walk(const char *dir, CFCUtil_walk_callback_t callback,
+             void *context);
+
 /* Print an error message to stderr and exit.
  */
 void



Mime
View raw message