diff --git a/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp b/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp index 9d375c2440dfa9d852cb9d701b76979b551c43fe..e337af6ee80ddf55f48efc830ba22888ba2c9872 100755 --- a/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/rmdirent.cpp @@ -34,6 +34,15 @@ using namespace std; using namespace OHOS::FileManagement::LibN; #ifdef __MUSL__ +static bool CheckDir(const string &path) +{ + std::error_code errCode; + if (!filesystem::is_directory(filesystem::status(path, errCode))) { + return false; + } + return true; +} + static NError RmDirent(const string &fpath) { std::filesystem::path strToPath(fpath); @@ -55,6 +64,19 @@ static NError RmDirent(const string &fpath) } #else +static bool CheckDir(const string &path) +{ + struct stat fileInformation; + if (stat(path.c_str(), &fileInformation) == 0) { + if (fileInformation.st_mode & S_IFDIR) { + return true; + } + } else { + HILOGE("Failed to stat file"); + } + return false; +} + static NError RmDirent(const string &fpath) { std::unique_ptr scandir_req = { @@ -122,6 +144,12 @@ napi_value Rmdirent::Sync(napi_env env, napi_callback_info info) return nullptr; } + if (!CheckDir(path.get())) { + HILOGE("Invalid path, not a directory"); + NError(ENOTDIR).ThrowErr(env); + return nullptr; + } + auto err = RmDirent(string(path.get())); if (err) { err.ThrowErr(env); @@ -147,6 +175,12 @@ napi_value Rmdirent::Async(napi_env env, napi_callback_info info) return nullptr; } + if (!CheckDir(path.get())) { + HILOGE("Invalid path, not a directory"); + NError(ENOTDIR).ThrowErr(env); + return nullptr; + } + auto cbExec = [tmpPath = string(path.get())]() -> NError { return RmDirent(tmpPath); };