This is an automated email from the ASF dual-hosted git repository.

pengzheng pushed a commit to branch feature/refactor_bundle_cache
in repository https://gitbox.apache.org/repos/asf/celix.git


The following commit(s) were added to refs/heads/feature/refactor_bundle_cache 
by this push:
     new 8bad0fa8 Optimize celix_utils_deleteDirectory to eliminate recursion 
and dynamic memory allocation.
     new af8948ff Merge remote-tracking branch 
'apache/feature/refactor_bundle_cache' into feature/refactor_bundle_cache
8bad0fa8 is described below

commit 8bad0fa8cbfde8c199319809f4d4f005d2856044
Author: PengZheng <[email protected]>
AuthorDate: Fri Mar 31 21:43:38 2023 +0800

    Optimize celix_utils_deleteDirectory to eliminate recursion and dynamic 
memory allocation.
---
 libs/utils/src/celix_file_utils.c | 63 ++++++++++++++-------------------------
 1 file changed, 23 insertions(+), 40 deletions(-)

diff --git a/libs/utils/src/celix_file_utils.c 
b/libs/utils/src/celix_file_utils.c
index 030f31ec..66abddd4 100644
--- a/libs/utils/src/celix_file_utils.c
+++ b/libs/utils/src/celix_file_utils.c
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <dirent.h>
+#include <fts.h>
 #include <stdlib.h>
 #include <zip.h>
 #include <sys/time.h>
@@ -169,57 +170,39 @@ celix_status_t celix_utils_deleteDirectory(const char* 
path, const char** errorO
     }
 
     //file exist and is directory
-    DIR *dir;
-    dir = opendir(path);
-    if (dir == NULL) {
-        *errorOut = strerror(errno);
-        return CELIX_FILE_IO_EXCEPTION;
-    }
-
     celix_status_t status = CELIX_SUCCESS;
-    struct dirent* dent = NULL;
-    errno = 0;
-    dent = readdir(dir);
-    if (errno != 0) {
+    char *paths[] = { (char*)path, NULL };
+    FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, NULL);
+    if (fts == NULL) {
         status = CELIX_FILE_IO_EXCEPTION;
         *errorOut = strerror(errno);
-    }
-    while (status == CELIX_SUCCESS && dent != NULL) {
-        if ((strcmp((dent->d_name), ".") != 0) && (strcmp((dent->d_name), 
"..") != 0)) {
-            char* subdir = NULL;
-            asprintf(&subdir, "%s/%s", path, dent->d_name);
-
-            struct stat st;
-            // we don't follow symbol link, remove it directly
-            if (lstat(subdir, &st) == 0) {
-                if (S_ISDIR (st.st_mode)) {
-                    status = celix_utils_deleteDirectory(subdir, errorOut);
-                } else {
-                    if (remove(subdir) != 0) {
+    } else {
+        FTSENT *ent;
+        while ((ent = fts_read(fts)) != NULL) {
+            switch (ent->fts_info) {
+                case FTS_DP:
+                case FTS_F:
+                case FTS_SL:
+                case FTS_SLNONE:
+                    if (remove(ent->fts_accpath) != 0) {
                         status = CELIX_FILE_IO_EXCEPTION;
                         *errorOut = strerror(errno);
                     }
-                }
+                    break;
+                case FTS_DNR:
+                case FTS_ERR:
+                case FTS_NS:
+                    status = CELIX_FILE_IO_EXCEPTION;
+                    *errorOut = strerror(ent->fts_errno);
+                    break;
+                default:
+                    break;
             }
-            free(subdir);
             if (status != CELIX_SUCCESS) {
                 break;
             }
         }
-        errno = 0;
-        dent = readdir(dir);
-        if (errno != 0) {
-            status = CELIX_FILE_IO_EXCEPTION;
-            *errorOut = strerror(errno);
-        }
-    }
-    closedir(dir);
-
-    if (status == CELIX_SUCCESS) {
-        if (remove(path) != 0) {
-            status = CELIX_FILE_IO_EXCEPTION;
-            *errorOut = strerror(errno);
-        }
+        fts_close(fts);
     }
     return status;
 }

Reply via email to