From bb4603c56c16f3fd6e093fa60980d9827814ae24 Mon Sep 17 00:00:00 2001 From: Caisking Date: Thu, 24 Jul 2025 14:54:50 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4=E6=96=87?= =?utf8?q?=E4=BB=B6=E5=A4=B9=E7=9A=84=E9=80=9A=E7=94=A8=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../drivers/peripheral/utils/custom_utils.c | 89 +++++++++++++++++++ .../drivers/peripheral/utils/custom_utils.h | 1 + 2 files changed, 90 insertions(+) diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.c b/code/sdk/drivers/peripheral/utils/custom_utils.c index d8c8eb2b6..9a798ca5c 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.c +++ b/code/sdk/drivers/peripheral/utils/custom_utils.c @@ -46,7 +46,11 @@ #include "system/include/SystemLogModelC.h" #include "ConfigModelC.h" #endif +#include +#include +#include #include +#include #include #include "custom_runtime.h" #include "pm_definition.h" @@ -2127,6 +2131,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; diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.h b/code/sdk/drivers/peripheral/utils/custom_utils.h index 0d26b3137..10ac2cdd7 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.h +++ b/code/sdk/drivers/peripheral/utils/custom_utils.h @@ -317,6 +317,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); void custom_i2c_reset(uint8_t i2cbus, uint16_t i2c_clock); -- 2.17.1