diff --git a/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp b/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp index 43ecec7cae87926f9b24b11126381a014e7aa175..828afcd80a0aab60fbf6e3701fddcb45f7a87762 100755 --- a/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp @@ -23,7 +23,9 @@ #include #include +#include "common_func.h" #include "filemgmt_libhilog.h" +#include "uv.h" namespace OHOS { namespace FileManagement { @@ -35,57 +37,66 @@ using namespace OHOS::FileManagement::LibN; static NError RmDirent(const string &fpath) { std::filesystem::path strToPath(fpath); - std::uintmax_t num = std::filesystem::remove_all(strToPath); + std::error_code errCode; + std::uintmax_t num = std::filesystem::remove_all(strToPath, errCode); + if (errCode) { + HILOGE("Failed to remove directory, error code: %{public}d", errCode.value()); + return NError(errCode.value()); + } if (!num || std::filesystem::exists(strToPath)) { - HILOGE("Failed to remove file or directory by path"); - return NError(errno); + HILOGE("Failed to remove directory, dirPath does not exist"); + return NError(ENOENT); } - return NError(ERRNO_NOERR); } #else static NError RmDirent(const string &fpath) { - if (rmdir(fpath.c_str()) == 0) { - return NError(ERRNO_NOERR); + std::unique_ptr scandir_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!scandir_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); } - auto dir = opendir(fpath.c_str()); - if (!dir) { - return NError(errno); + int ret = 0; + ret = uv_fs_scandir(nullptr, scandir_req.get(), fpath.c_str(), 0, nullptr); + if (ret < 0) { + HILOGE("Failed to scandir, ret: %{public}d", ret); + return NError(ret); } - struct dirent* entry = readdir(dir); - while (entry) { - if (strncmp(entry->d_name, ".", strlen(".")) == 0 || - strncmp(entry->d_name, "..", strlen("..")) == 0) { - entry = readdir(dir); - continue; - } - struct stat fileInformation; - string filePath = fpath + '/'; - filePath.insert(filePath.length(), entry->d_name); - if (stat(filePath.c_str(), &fileInformation) != 0) { - closedir(dir); - HILOGE("Failed to close directory"); - return NError(errno); - } - if ((fileInformation.st_mode & S_IFMT) == S_IFDIR) { - auto err = RmDirent(filePath); - if (err) { - closedir(dir); - return err; + uv_dirent_t dent; + while (uv_fs_scandir_next(scandir_req.get(), &dent) != UV_EOF) { + string filePath = fpath + "/" + string(dent.name); + if (dent.type == UV_DIRENT_FILE) { + std::unique_ptr unlink_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!unlink_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); } - } else { - if (unlink(filePath.c_str()) != 0) { - closedir(dir); - return NError(errno); + ret = uv_fs_unlink(nullptr, unlink_req.get(), filePath.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to unlink file, ret: %{public}d", ret); + return NError(ret); + } + } else if (dent.type == UV_DIRENT_DIR) { + auto rmDirentRes = RmDirent(filePath); + if (rmDirentRes) { + return rmDirentRes; } } - entry = readdir(dir); } - closedir(dir); - if (rmdir(fpath.c_str()) != 0) { - return NError(errno); + std::unique_ptr rmdir_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!rmdir_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + ret = uv_fs_rmdir(nullptr, rmdir_req.get(), fpath.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to rmdir empty dir, ret: %{public}d", ret); + return NError(ret); } return NError(ERRNO_NOERR); }