From c1e65357eb2f27dd78025bb8f13c63e3f5a53392 Mon Sep 17 00:00:00 2001 From: cuiruibin Date: Fri, 20 Jun 2025 19:03:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E8=8E=B7=E5=8F=96=E5=88=B0=E7=9A=84BM?= =?UTF-8?q?S=E5=AF=B9=E8=B1=A1=E4=BB=8E=E5=AD=90=E7=B1=BB=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=88=90=E7=88=B6=E7=B1=BB=20Signed-off-by:=20cuiruib?= =?UTF-8?q?in=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../native/file_share/src/file_share.cpp | 8 ++--- .../native/file_uri/src/file_uri.cpp | 12 ++++--- .../file_uri_native/file_uri_test.cpp | 34 +++++++++++++++++++ .../file_uri_ndk_test/file_uri_ndk_test.cpp | 6 ++-- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 36491aafb..20df5c555 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -167,10 +167,10 @@ static int32_t GetSharePath(const string &uri, FileShareInfo &info, uint32_t fla 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_; + shareRPath = DATA_APP_EL2_PATH + info.currentUid_ + SHARE_PATH + info.targetBundleName_ + SHARE_R_PATH + + networkId + BACKSLASH + info.providerBundleName_ + info.providerSandboxPath_; + shareRWPath = DATA_APP_EL2_PATH + info.currentUid_ + SHARE_PATH + info.targetBundleName_ + SHARE_RW_PATH + + networkId + BACKSLASH + info.providerBundleName_ + info.providerSandboxPath_; } if (!SandboxHelper::IsValidPath(shareRPath) || !SandboxHelper::IsValidPath(shareRWPath)) { LOGE("Invalid share path"); diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index b7674f81e..cc229ca47 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -134,13 +134,17 @@ string FileUri::GetRealPath() uri_.ToString().find(NETWORK_PARA) != string::npos) { string networkId = ""; SandboxHelper::GetNetworkIdFromUri(uri_.ToString(), networkId); - string pathShare = PATH_SHARE; if (!networkId.empty()) { - pathShare = PATH_SHARE + BACKSLASH + networkId; + realPath = PATH_SHARE + MODE_RW + networkId + BACKSLASH + bundleName + sandboxPath; + } else { + realPath = PATH_SHARE + MODE_RW + bundleName + sandboxPath; } - realPath = pathShare + MODE_RW + bundleName + sandboxPath; if (access(realPath.c_str(), F_OK) != 0) { - realPath = pathShare + MODE_R + bundleName + sandboxPath; + if (!networkId.empty()) { + realPath = PATH_SHARE + MODE_R + networkId + BACKSLASH + bundleName + sandboxPath; + } else { + realPath = PATH_SHARE + MODE_R + bundleName + sandboxPath; + } } } LOGD("GetRealPath return path is ,%{private}s", realPath.c_str()); diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index 468865cb2..44daa3842 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -272,6 +272,40 @@ namespace OHOS::AppFileService::ModuleFileUri { EXPECT_EQ(fileUri.GetRealPath(), fileStr); GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0005"; } + /** + * @tc.name: file_uri_test_00011 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7LW57 + */ + HWTEST_F(FileUriTest, File_uri_GetPath_0009, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0009"; + string uri = "file://docs/storage/Users/currentUser/Documents/1.txt?networkid=***"; + string fileStr = "/data/storage/el2/share/r/***/docs/storage/Users/currentUser/Documents/1.txt"; + FileUri fileUri(uri); + EXPECT_EQ(fileUri.GetRealPath(), fileStr); + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0009"; + } + /** + * @tc.name: file_uri_test_00012 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7LW57 + */ + HWTEST_F(FileUriTest, File_uri_GetPath_0010, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0010"; + string uri = "file://docs/storage/Users/currentUser/Documents/1.txt?networkid="; + string fileStr = "/data/storage/el2/share/r/docs/storage/Users/currentUser/Documents/1.txt"; + FileUri fileUri(uri); + EXPECT_EQ(fileUri.GetRealPath(), fileStr); + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0010"; + } /** * @tc.name: File_uri_GetPathBySA_0001 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 95c2c0d42..2e80653f4 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 @@ -153,7 +153,7 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_004, TestSize.Level1) fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; const char *fileUri = fileUriStr.c_str(); std::string filePathStr = - "/data/storage/el2/share/64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9/r/" + BUNDLE_A + + "/data/storage/el2/share/r/64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9/" + BUNDLE_A + "/data/storage/el2/distributedfiles/.remote_share/"; filePathStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri004.txt"; const char *filePath = filePathStr.c_str(); @@ -182,7 +182,7 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_005, TestSize.Level1) fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; const char *fileUri = fileUriStr.c_str(); const char filePath[] = - "/data/storage/el2/share/64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9/r/docs/storage/Users/" + "/data/storage/el2/share/r/64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9/docs/storage/Users/" "currentUser/Documents/GetPathFromUri005.txt"; char *result = nullptr; unsigned int length = fileUriStr.size(); @@ -211,7 +211,7 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_006, TestSize.Level1) fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; const char *fileUri = fileUriStr.c_str(); std::string filePathUri = - "/data/storage/el2/share/64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9/r/" + bundleB; + "/data/storage/el2/share/r/64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9/" + bundleB; filePathUri += "/data/storage/el2/distributedfiles/.remote_share/"; filePathUri += "data/storage/el2/base/haps/entry/files/GetPathFromUri006.txt"; const char *filePath = filePathUri.c_str(); -- Gitee