增加删除文件夹的通用函数
authorCaisking <Casiking@zhihuishixun.com>
Thu, 24 Jul 2025 06:54:50 +0000 (14:54 +0800)
committerCaisking <Casiking@zhihuishixun.com>
Fri, 22 Aug 2025 03:09:14 +0000 (11:09 +0800)
code/sdk/zhihui/utils/custom_utils.c
code/sdk/zhihui/utils/custom_utils.h

index 8c4677b..d951c16 100644 (file)
 // void save_popup_windows_log_c(int type){}
 // void system_init_uart(void){}
 #endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <dirent.h>
+#include <sys/stat.h>
 #include <unistd.h>
 #include "custom_runtime.h"
 #include "pm_definition.h"
@@ -2075,6 +2079,91 @@ int uapi_delete_files(const char *file_path)
         
 }
 
+/*删除文件夹*/
+int uapi_delete_folder(const char *folder_path) {
+    // 参数检查
+    if (folder_path == NULL || strlen(folder_path) == 0) {
+        fprintf(stderr, "Error: Invalid folder path\n");
+        return -1;
+    }
+
+    // 安全检查:防止删除根目录
+    if (strcmp(folder_path, "/") == 0 ||
+        strcmp(folder_path, ".") == 0 ||
+        strcmp(folder_path, "..") == 0) {
+        fprintf(stderr, "Error: Deleting root directory or current/parent directory is prohibited\n");
+        return -1;
+    }
+
+    // 检查路径是否存在
+    struct stat statbuf;
+    if (stat(folder_path, &statbuf) != 0) {
+        // 路径不存在,可能已被删除,视为成功
+        return 0;
+    }
+
+    // 如果不是目录,返回错误
+    if (!S_ISDIR(statbuf.st_mode)) {
+        fprintf(stderr, "Error: Path '%s' is not a directory\n", folder_path);
+        return -1;
+    }
+
+    // 打开目录
+    DIR *dir = opendir(folder_path);
+    if (dir == NULL) {
+        fprintf(stderr, "Error: Failed to open directory '%s'\n", folder_path);
+        return -1;
+    }
+
+    // 递归删除目录中的所有内容
+    struct dirent *entry;
+    char subpath[1024]; // 假设路径长度不超过1024字节
+
+    while ((entry = readdir(dir)) != NULL) {
+        // 跳过当前目录和上级目录
+        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
+            continue;
+        }
+
+        // 构建子路径
+        snprintf(subpath, sizeof(subpath), "%s/%s", folder_path, entry->d_name);
+
+        // 获取子项信息
+        if (stat(subpath, &statbuf) != 0) {
+            fprintf(stderr, "Warning: Failed to stat '%s', skipping\n", subpath);
+            continue;
+        }
+
+        // 如果是目录,递归删除
+        if (S_ISDIR(statbuf.st_mode)) {
+            if (uapi_delete_folder(subpath) != 0) {
+                fprintf(stderr, "Error: Failed to delete subdirectory '%s'\n", subpath);
+                closedir(dir);
+                return -1;
+            }
+        }
+        // 如果是文件或符号链接,直接删除
+        else {
+            if (unlink(subpath) != 0) {
+                fprintf(stderr, "Error: Failed to delete file '%s'\n", subpath);
+                closedir(dir);
+                return -1;
+            }
+        }
+    }
+
+    // 关闭目录
+    closedir(dir);
+
+    // 删除空目录
+    if (rmdir(folder_path) != 0) {
+        fprintf(stderr, "Error: Failed to remove directory '%s'\n", folder_path);
+        return -1;
+    }
+
+    return 0; // 成功
+}
+
 void uapi_check_tar_file_count(char *folderpath)
 {
     DIR *dir;
index 9389c1c..c5c1f89 100644 (file)
@@ -294,6 +294,7 @@ errcode_t do_custom_fileoperate_service_check_msg(int cmd, void *params, int par
 
 
 int uapi_delete_files(const char *file_path);
+int uapi_delete_folder(const char *folder_path);
 void uapi_restore_factory_setting(void);
 
 errcode_t custom_i2c_init(uint8_t bus_index);