From 9c785b85c5f85ea074d46a1d0e0dbfa1040fcc3f Mon Sep 17 00:00:00 2001 From: futurezhou Date: Sun, 29 Jan 2023 16:54:18 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E9=97=A8=E7=A6=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: futurezhou --- .../js/src/mod_fs/properties/rmdirent.cpp | 48 +++++++++++++++++++ .../js/src/mod_fs/properties/truncate.cpp | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp b/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp index 2c31c76a7..43ecec7ca 100755 --- a/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp @@ -31,6 +31,7 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +#ifdef __MUSL__ static NError RmDirent(const string &fpath) { std::filesystem::path strToPath(fpath); @@ -43,6 +44,53 @@ static NError RmDirent(const string &fpath) return NError(ERRNO_NOERR); } +#else +static NError RmDirent(const string &fpath) +{ + if (rmdir(fpath.c_str()) == 0) { + return NError(ERRNO_NOERR); + } + auto dir = opendir(fpath.c_str()); + if (!dir) { + return NError(errno); + } + 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; + } + } else { + if (unlink(filePath.c_str()) != 0) { + closedir(dir); + return NError(errno); + } + } + entry = readdir(dir); + } + closedir(dir); + if (rmdir(fpath.c_str()) != 0) { + return NError(errno); + } + return NError(ERRNO_NOERR); +} +#endif + napi_value Rmdirent::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index f6eb9e560..dabaa2173 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -186,5 +186,5 @@ napi_value Truncate::Async(napi_env env, napi_callback_info info) return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; } } -} +} } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file -- Gitee