diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 73cf7abaa48a2edf2fcb6d24177bc27259d51233..e810fd1a4e399a6da5237b7f86a96b990e5ea9c4 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" @@ -36,6 +37,12 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +namespace { + const std::vector PUBLIC_DIR_PATHS = { + "/Documents" + }; +} + void InitOpenMode(napi_env env, napi_value exports) { char propertyName[] = "OpenMode"; @@ -175,6 +182,16 @@ string CommonFunc::GetModeFromFlags(unsigned int flags) return mode; } +bool CommonFunc::CheckPublicDirPath(const std::string &sandboxPath) +{ + for (const std::string &path : PUBLIC_DIR_PATHS) { + if (sandboxPath.find(path) == 0) { + return true; + } + } + return false; +} + tuple, unique_ptr> CommonFunc::GetCopyPathArg(napi_env env, napi_value srcPath, napi_value dstPath) diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 46821e21d21e31206e912b2d024fb9eb329f0ccf..fc97865fc78d281479a12eada361e9e7b97fb76c 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -71,6 +71,7 @@ struct CommonFunc { napi_value dstPath); static void fs_req_cleanup(uv_fs_t* req); static std::string GetModeFromFlags(unsigned int flags); + static bool CheckPublicDirPath(const std::string &path); }; } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index aacb92aabdc348b1fe726b1865ed740790b942fa..7360218b4b88a8b881b05851a51d9c99326c8ac1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -104,24 +104,6 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) } #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) -static string DealWithUriWithName(string str) -{ - static uint32_t meetCount = 6; - uint32_t count = 0; - uint32_t index; - for (index = 0; index < str.length(); index++) { - if (str[index] == '/') { - count++; - } - if (count == meetCount) { - break; - } - } - if (count == meetCount) { - str = str.substr(0, index); - } - return str; -} static int OpenFileByDatashare(string path, unsigned int flags) { @@ -138,7 +120,6 @@ static int OpenFileByDatashare(string path, unsigned int flags) HILOGE("Failed to connect to datashare"); return -E_PERMISSION; } - path = DealWithUriWithName(path); Uri uri(path); fd = dataShareHelper->OpenFile(uri, CommonFunc::GetModeFromFlags(flags)); return fd; @@ -182,7 +163,7 @@ static string GetBundleNameSelf() static string GetPathFromFileUri(string path, string bundleName, unsigned int mode) { - if (bundleName != GetBundleNameSelf()) { + if (CommonFunc::CheckPublicDirPath(path) || bundleName != GetBundleNameSelf()) { if ((mode & O_WRONLY) == O_WRONLY || (mode & O_RDWR) == O_RDWR) { path = PATH_SHARE + MODE_RW + bundleName + path; } else {