diff --git a/interfaces/common/include/sandbox_helper.h b/interfaces/common/include/sandbox_helper.h index ac94fc32ca59c0d703a33788ee21c4cbe54270dc..64671d029774ff60a04bc9309a7bdcf537ea2417 100644 --- a/interfaces/common/include/sandbox_helper.h +++ b/interfaces/common/include/sandbox_helper.h @@ -40,6 +40,7 @@ public: static int32_t GetBackupPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath); static bool IsValidPath(const std::string &path); + static void GetNetworkIdFromUri(const std::string &fileUri, std::string &networkId); }; } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index 8850178f63da956d8b4630c3d4c9c15fedfd37b4..af783e4ac237cc6689595480f755c062036fb4fc 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -35,7 +35,7 @@ namespace AppFileService { using namespace OHOS::AppExecFwk; namespace { const std::string FILE_SCHEME_PREFIX = "file://"; -const char BACKFLASH = '/'; +const char BACKSLASH = '/'; const std::string FILE_MANAGER_URI_HEAD = "/storage/"; const std::string FILE_MANAGER_AUTHORITY = "docs"; const std::string MEDIA_FUSE_PATH_HEAD = "/data/storage/el2/media"; @@ -103,8 +103,8 @@ static void NormalizePath(string &path) return; } - if (path[0] != BACKFLASH) { - path.insert(0, 1, BACKFLASH); + if (path[0] != BACKSLASH) { + path.insert(0, 1, BACKSLASH); } } diff --git a/interfaces/common/src/sandbox_helper.cpp b/interfaces/common/src/sandbox_helper.cpp index 2dac229aeafd95590429adf28d3fcbde2edc312a..854ddcab08fc8641a05062c06367407563bad465 100644 --- a/interfaces/common/src/sandbox_helper.cpp +++ b/interfaces/common/src/sandbox_helper.cpp @@ -38,8 +38,8 @@ namespace { const string MOUNT_PATH_MAP_KEY = "mount-path-map"; const string SANDBOX_JSON_FILE_PATH = "/etc/app_file_service/file_share_sandbox.json"; const string BACKUP_SANDBOX_JSON_FILE_PATH = "/etc/app_file_service/backup_sandbox.json"; - const std::string SHAER_PATH_HEAD = "/mnt/hmdfs/"; - const std::string SHAER_PATH_MID = "/account/cloud_merge_view/files/"; + const std::string SHARE_PATH_HEAD = "/mnt/hmdfs/"; + const std::string SHARE_PATH_MID = "/account/cloud_merge_view/files/"; const string FILE_MANAGER_URI_HEAD = "/storage/"; const string FILE_MANAGER_AUTHORITY = "docs"; const string DLP_MANAGER_BUNDLE_NAME = "com.ohos.dlpmanager"; @@ -54,6 +54,8 @@ namespace { const std::string PATH_INVALID_FLAG1 = "../"; const std::string PATH_INVALID_FLAG2 = "/.."; const uint32_t PATH_INVALID_FLAG_LEN = 3; + const std::string NETWORK_PARA = "?networkid="; + const std::string AMPERSAND = "&"; } struct MediaUriInfo { @@ -307,7 +309,7 @@ static int32_t GetMediaPhysicalPath(const std::string &sandboxPath, const std::s return -EINVAL; } - physicalPath = SHAER_PATH_HEAD + userId + SHAER_PATH_MID + mediaUriInfo.mediaType + + physicalPath = SHARE_PATH_HEAD + userId + SHARE_PATH_MID + mediaUriInfo.mediaType + BACKSLASH + to_string(bucketNum) + BACKSLASH + mediaUriInfo.realName + mediaSuffix; return 0; } @@ -336,19 +338,20 @@ int32_t SandboxHelper::GetMediaSharePath(const std::vector &fileUri return 0; } -static void GetNetworkIdFromUri(const std::string &fileUri, string &networkId) +void SandboxHelper::GetNetworkIdFromUri(const std::string &fileUri, std::string &networkId) { - Uri uri(fileUri); - std::string networkIdInfo = uri.GetQuery(); - if (networkIdInfo.empty()) { - return; - } - - size_t posIndex = networkIdInfo.find('='); - if (posIndex == string::npos || posIndex == (networkIdInfo.size() - 1)) { - return; + std::string uri = fileUri; + size_t pos = uri.find(NETWORK_PARA); + if (pos != string::npos && pos > 0 && pos < uri.size() - NETWORK_PARA.size()) { + if (uri.substr(pos + NETWORK_PARA.size()).find(BACKSLASH) == string::npos) { + size_t endPos = uri.substr(pos + NETWORK_PARA.size()).find(AMPERSAND); + if (endPos != string::npos) { + networkId = uri.substr(pos + NETWORK_PARA.size(), endPos); + } else { + networkId = uri.substr(pos + NETWORK_PARA.size()); + } + } } - networkId = networkIdInfo.substr(posIndex + 1); } static void DoGetPhysicalPath(string &lowerPathTail, string &lowerPathHead, const string &sandboxPath, diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index ddaa5ea9fca4b00f8109232007003faf803a5821..36491aafb0d985b09c61268fdbbd5f5ea46f9458 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -52,6 +52,7 @@ const string SHARE_PATH = "/share/"; const string EXTERNAL_PATH = "file://docs/storage/External"; const string NETWORK_PARA = "networkid="; const int32_t DLP_COMMON = 0; +const std::string BACKSLASH = "/"; } struct FileShareInfo { @@ -158,11 +159,19 @@ static void DelSharePath(const string &delPath) static int32_t GetSharePath(const string &uri, FileShareInfo &info, uint32_t flag) { + string networkId = ""; + SandboxHelper::GetNetworkIdFromUri(uri, networkId); string shareRPath = DATA_APP_EL2_PATH + info.currentUid_ + SHARE_PATH + info.targetBundleName_ + SHARE_R_PATH + info.providerBundleName_ + info.providerSandboxPath_; string shareRWPath = DATA_APP_EL2_PATH + info.currentUid_ + SHARE_PATH + info.targetBundleName_ + SHARE_RW_PATH + info.providerBundleName_ + info.providerSandboxPath_; + if (!networkId.empty()) { + shareRPath = DATA_APP_EL2_PATH + info.currentUid_ + SHARE_PATH + info.targetBundleName_ + BACKSLASH + + networkId + SHARE_R_PATH + info.providerBundleName_ + info.providerSandboxPath_; + shareRWPath = DATA_APP_EL2_PATH + info.currentUid_ + SHARE_PATH + info.targetBundleName_ + BACKSLASH + + networkId + SHARE_RW_PATH + info.providerBundleName_ + info.providerSandboxPath_; + } if (!SandboxHelper::IsValidPath(shareRPath) || !SandboxHelper::IsValidPath(shareRWPath)) { LOGE("Invalid share path"); return -EINVAL; diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 5e58b6000d54d86a49a5ad1154eead3aa5e880e7..655e6dcb491a2f800da8af55dd6626c988b0ac81 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -39,7 +39,7 @@ const std::string FILE_SCHEME_PREFIX = "file://"; const std::string FILE_MANAGER_AUTHORITY = "docs"; const std::string MEDIA_AUTHORITY = "media"; const std::string NETWORK_PARA = "?networkid="; -const std::string BACKFLASH = "/"; +const std::string BACKSLASH = "/"; const std::string FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable"; const int DECODE_FORMAT_NUM = 16; const int DECODE_LEN = 2; @@ -132,9 +132,15 @@ string FileUri::GetRealPath() } if (((!bundleName.empty()) && (bundleName != BUNDLE_NAME)) || uri_.ToString().find(NETWORK_PARA) != string::npos) { - realPath = PATH_SHARE + MODE_RW + bundleName + sandboxPath; + string networkId = ""; + SandboxHelper::GetNetworkIdFromUri(uri_.ToString(), networkId); + string pathShare = PATH_SHARE; + if (!networkId.empty()) { + pathShare = PATH_SHARE + BACKSLASH + networkId; + } + realPath = pathShare + MODE_RW + bundleName + sandboxPath; if (access(realPath.c_str(), F_OK) != 0) { - realPath = PATH_SHARE + MODE_R + bundleName + sandboxPath; + realPath = pathShare + MODE_R + bundleName + sandboxPath; } } return realPath; @@ -192,7 +198,7 @@ bool FileUri::IsRemoteUri() { size_t pos = uri_.ToString().find(NETWORK_PARA); if (pos != string::npos && pos > 0 && pos < uri_.ToString().size() - NETWORK_PARA.size()) { - if (uri_.ToString().substr(pos + NETWORK_PARA.size()).find(BACKFLASH) == string::npos) { + if (uri_.ToString().substr(pos + NETWORK_PARA.size()).find(BACKSLASH) == string::npos) { return true; } } diff --git a/test/unittest/file_share_native/file_share_test.cpp b/test/unittest/file_share_native/file_share_test.cpp index 22d26eb8661bbf8f75c1deec673c53d007a157bc..fef359af4a970116a3a9e82d9795d6f06d4f34e3 100644 --- a/test/unittest/file_share_native/file_share_test.cpp +++ b/test/unittest/file_share_native/file_share_test.cpp @@ -736,4 +736,99 @@ HWTEST_F(FileShareTest, File_share_GetBackupPhysicalPath_0007, testing::ext::Tes GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetBackupPhysicalPath_0007"; } +/** + * @tc.name: File_share_GetNetworkIdFromUri_001 + * @tc.desc: Test function of GetNetworkIdFromUri() interface for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetNetworkIdFromUri_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetNetworkIdFromUri_001"; + std::string fileUri = "file://docs/storage/Users/currentUser/test.jpg?networkid=***"; + std::string result = "***"; + std::string networkId; + SandboxHelper::GetNetworkIdFromUri(fileUri, networkId); + EXPECT_EQ(result, networkId); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetNetworkIdFromUri_001"; +} + +/** + * @tc.name: File_share_GetNetworkIdFromUri_002 + * @tc.desc: Test function of GetNetworkIdFromUri() interface for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetNetworkIdFromUri_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetNetworkIdFromUri_002"; + std::string fileUri = "file://docs/storage/Users/currentUser/test.jpg?networkid="; + std::string result = ""; + std::string networkId; + SandboxHelper::GetNetworkIdFromUri(fileUri, networkId); + EXPECT_EQ(result, networkId); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetNetworkIdFromUri_002"; +} + +/** + * @tc.name: File_share_GetNetworkIdFromUri_003 + * @tc.desc: Test function of GetNetworkIdFromUri() interface for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetNetworkIdFromUri_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetNetworkIdFromUri_003"; + std::string fileUri = "file://docs/storage/Users/currentUser/test.jpg?networkid=123456&789"; + std::string result = "123456"; + std::string networkId; + SandboxHelper::GetNetworkIdFromUri(fileUri, networkId); + EXPECT_EQ(result, networkId); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetNetworkIdFromUri_003"; +} + +/** + * @tc.name: File_share_GetNetworkIdFromUri_004 + * @tc.desc: Test function of GetNetworkIdFromUri() interface for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetNetworkIdFromUri_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetNetworkIdFromUri_004"; + std::string fileUri = "file://docs/storage/Users/currentUser/test.jpg?networkid=123456/test/1.txt"; + std::string result = ""; + std::string networkId; + SandboxHelper::GetNetworkIdFromUri(fileUri, networkId); + EXPECT_EQ(result, networkId); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetNetworkIdFromUri_004"; +} + +/** + * @tc.name: File_share_GetNetworkIdFromUri_005 + * @tc.desc: Test function of GetNetworkIdFromUri() interface for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetNetworkIdFromUri_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetNetworkIdFromUri_005"; + std::string fileUri = "file://docs/storage/Users/currentUser/test.jpg?query=123456789&?networkid=123456; + std::string result = "123456"; + std::string networkId; + SandboxHelper::GetNetworkIdFromUri(fileUri, networkId); + EXPECT_EQ(result, networkId); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetNetworkIdFromUri_005"; +} + } // namespace \ No newline at end of file diff --git a/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp b/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp index d3903b13c9ed4be5c7d6ee2c241d32a25797b331..f8d7b5d14240549f2106f4c11ca883019179aaec 100644 --- a/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp +++ b/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp @@ -178,7 +178,6 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_005, TestSize.Level1) { GTEST_LOG_(INFO) << "get_path_from_uri_test_005 start"; std::string fileUriStr = "file://docs/storage/Users/currentUser/Documents/GetPathFromUri005.txt"; - fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; const char *fileUri = fileUriStr.c_str(); const char filePath[] = "/data/storage/el2/share/r/docs/storage/Users/currentUser/Documents/GetPathFromUri005.txt"; char *result = nullptr;