diff --git a/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp b/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp index 2c31c76a7bc615534b917bd95e5b67d852b1b701..43ecec7cae87926f9b24b11126381a014e7aa175 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 f6eb9e5600269176754e1ce03c77f4776015831f..dabaa217385e3ab898bc06e99222a1b8dbdd0eb2 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