From aa097437ee576a536d45b12627cecedf1b22766e Mon Sep 17 00:00:00 2001 From: cuiruibin Date: Tue, 21 Nov 2023 14:59:36 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E6=96=B0=E5=A2=9EPC=E7=AB=AFUri=E8=BD=AC?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E6=B2=99=E7=AE=B1=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cuiruibin --- bundle.json | 3 +- interfaces/innerkits/native/BUILD.gn | 2 + .../native/file_uri/src/file_uri.cpp | 64 +++++++- test/unittest/file_uri_native/BUILD.gn | 2 + .../file_uri_native/file_uri_test.cpp | 148 +++++++++++++++++- 5 files changed, 210 insertions(+), 9 deletions(-) diff --git a/bundle.json b/bundle.json index 0d0c382c7..e0eb0a57d 100644 --- a/bundle.json +++ b/bundle.json @@ -37,7 +37,8 @@ "napi", "safwk", "samgr", - "storage_service" + "storage_service", + "os_account" ], "third_party": [ "bounds_checking_function", diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index a66a94ad7..a60af1fa7 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -80,7 +80,9 @@ ohos_shared_library("fileuri_native") { "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", + "os_account:os_account_innerkits", "samgr:samgr_proxy", ] diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index fe9591efb..c662fd4e6 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -15,16 +15,17 @@ #include "file_uri.h" -#include #include #include +#include #include "uri.h" #include "common_func.h" #include "log.h" +#include "os_account_manager.h" +#include "parameter.h" #include "sandbox_helper.h" - using namespace std; namespace OHOS { namespace AppFileService { @@ -36,6 +37,59 @@ 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 STORAGE_USERS = "storage/Users/"; +const std::string EL1_APPDATA = "/appdata/el1/"; +const std::string EL2_APPDATA = "/appdata/el2/"; +const std::string APP_EL1_SANDBOX_HEAD = "/data/storage/el1/base"; +const std::string APP_EL2_SANDBOX_HEAD = "/data/storage/el2/base"; +const std::string DEFAULT_USERNAME = "currentUser"; +const char *g_fileManagerFullMountEnableParameter = "const.filemanager.full_mout.enable"; +bool CheckFileManagerFullMountEnable() +{ + char value[] = "false"; + int retSystem = GetParameter(g_fileManagerFullMountEnableParameter, "false", value, sizeof(value)); + if (retSystem > 0 && !std::strcmp(value, "true")) { + return true; + } + LOGD("Not supporting all mounts"); + return false; +} +bool checkInvalidUri(const string &bundleName) +{ + if (bundleName == MEDIA_AUTHORITY) { + return true; + } + if (bundleName == CommonFunc::GetSelfBundleName()) { + return true; + } + return false; +} +static string GetUserName() +{ + std::string userName; + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if (errCode != ERR_OK || userName.empty()) { + LOGD("Get userName Failed"); + userName = DEFAULT_USERNAME; + } + return userName; +} +static string GetPathFromShareUri(string &realPath, std::string &bundleName) +{ + if(bundleName == FILE_MANAGER_AUTHORITY){ + return realPath; + } + string securityLevel = EL1_APPDATA; + string sandboxHead = APP_EL1_SANDBOX_HEAD; + if (realPath.find(APP_EL1_SANDBOX_HEAD) == string::npos) { + securityLevel = EL2_APPDATA; + sandboxHead = APP_EL2_SANDBOX_HEAD; + } + size_t posBegin = realPath.find(sandboxHead) + sandboxHead.size(); + string fileLowerPath = realPath.substr(posBegin, realPath.size() - 1); + string userName = GetUserName(); + return STORAGE_USERS + userName + securityLevel + bundleName + fileLowerPath; +} string FileUri::GetName() { string sandboxPath = SandboxHelper::Decode(uri_.GetPath()); @@ -68,8 +122,10 @@ string FileUri::GetRealPath() string sandboxPath = SandboxHelper::Decode(uri_.GetPath()); string realPath = sandboxPath; string bundleName = uri_.GetAuthority(); - if (bundleName == FILE_MANAGER_AUTHORITY && - uri_.ToString().find(NETWORK_PARA) == string::npos && + if (CheckFileManagerFullMountEnable() && !checkInvalidUri(bundleName)) { + return GetPathFromShareUri(realPath, bundleName); + } + if (bundleName == FILE_MANAGER_AUTHORITY && uri_.ToString().find(NETWORK_PARA) == string::npos && access(realPath.c_str(), F_OK) == 0) { return realPath; } diff --git a/test/unittest/file_uri_native/BUILD.gn b/test/unittest/file_uri_native/BUILD.gn index 554e71fc6..2fb40de9f 100644 --- a/test/unittest/file_uri_native/BUILD.gn +++ b/test/unittest/file_uri_native/BUILD.gn @@ -27,7 +27,9 @@ ohos_unittest("file_uri_test") { "app_file_service:fileuri_native", "c_utils:utils", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", + "os_account:os_account_innerkits", ] deps = [ diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index e9efac2a1..9f703cee5 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -26,7 +26,8 @@ #include "common_func.h" #include "file_share.h" #include "log.h" - +#include "os_account_manager.h" +#include "parameter.h" using namespace std; using namespace OHOS::Security::AccessToken; using namespace OHOS::AppFileService; @@ -45,7 +46,7 @@ namespace OHOS::AppFileService::ModuleFileUri { const string MODE_RW = "/rw/"; const string MODE_R = "/r/"; const int E_OK = 0; - + const char* g_fileManagerFullMoutEnableParameter = "const.filemanager.full_mout.enable"; class FileUriTest : public testing::Test { public: static void SetUpTestCase(void) {}; @@ -53,7 +54,6 @@ namespace OHOS::AppFileService::ModuleFileUri { void SetUp() {}; void TearDown() {}; }; - /** * @tc.name: file_uri_test_0000 * @tc.desc: Test function of ToString() interface for SUCCESS. @@ -193,7 +193,7 @@ namespace OHOS::AppFileService::ModuleFileUri { vector retList; int32_t ret = FileShare::CreateShareFile(uriList, tokenId, flag, retList); EXPECT_EQ(ret, E_OK); - + string rltStr = PATH_SHARE + MODE_R + bundleB + actStr; FileUri fileUri(uri); string path = fileUri.GetRealPath(); @@ -250,4 +250,144 @@ namespace OHOS::AppFileService::ModuleFileUri { EXPECT_EQ(folderUriObject.GetFullDirectoryUri(), folderDirectoryUri); GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000"; } + /** + * @tc.name: file_uri_test_0008 + * @tc.desc: Test function of GetRealPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ + HWTEST_F(FileUriTest, File_uri_GetRealPath_0005, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetRealPath_0005"; + string fileUri = "file://media/data/storage/el2/base/files/1.txt"; + string filePath = PATH_SHARE + MODE_R + "media/data/storage/el2/base/files/1.txt"; + FileUri folderUriObject(fileUri); + EXPECT_EQ(folderUriObject.GetRealPath(), filePath); + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetRealPath_0005"; + } + /** + * @tc.name: file_uri_test_0009 + * @tc.desc: Test function of GetRealPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ + HWTEST_F(FileUriTest, File_uri_GetRealPath_0006, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetRealPath_0006"; + string fileUri = "file://com.demo.a/data/storage/el1/base/files/1.txt"; + string userName; + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if (errCode != ERR_OK) { + GTEST_LOG_(INFO) << "GetOsAccountShortName failed File_uri_GetRealPath_0006"; + } + string filePath = "storage/Users/" + userName + "/appdata/el1/com.demo.a/files/1.txt"; + string RealPath = PATH_SHARE + MODE_R + "com.demo.a/data/storage/el1/base/files/1.txt"; + FileUri folderUriObject(fileUri); + char value[] = "false"; + int retSystem = GetParameter(g_fileManagerFullMoutEnableParameter, "false", value, sizeof(value)); + if (retSystem > 0 && !std::strcmp(value, "true")) { + EXPECT_EQ(folderUriObject.GetRealPath(), filePath); + } else { + EXPECT_EQ(folderUriObject.GetRealPath(), RealPath); + } + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetRealPath_0006"; + } + /** + * @tc.name: file_uri_test_0010 + * @tc.desc: Test function of GetRealPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ + HWTEST_F(FileUriTest, File_uri_GetRealPath_0007, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetRealPath_0007"; + string fileUri = "file://com.demo.a/data/storage/el2/base/files/1.txt"; + string filePath = PATH_SHARE + MODE_R + "com.demo.a/data/storage/el2/base/files/1.txt"; + FileUri folderUriObject(fileUri); + EXPECT_EQ(folderUriObject.GetRealPath(), filePath); + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetRealPath_0007"; + } + /** + * @tc.name: file_uri_test_0011 + * @tc.desc: Test function of GetRealPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ + HWTEST_F(FileUriTest, File_uri_GetRealPath_0008, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetRealPath_0008"; + string fileUri = "file://com.demo.a/data/storage/el2/base/files/1.txt"; + string userName; + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if (errCode != ERR_OK) { + GTEST_LOG_(INFO) << "GetOsAccountShortName failed File_uri_GetRealPath_0008"; + } + string filePath = "storage/Users/" + userName + "/appdata/el2/com.demo.a/files/1.txt"; + string RealPath = PATH_SHARE + MODE_R + "com.demo.a/data/storage/el2/base/files/1.txt"; + FileUri folderUriObject(fileUri); + char value[] = "false"; + int retSystem = GetParameter(g_fileManagerFullMoutEnableParameter, "false", value, sizeof(value)); + if (retSystem > 0 && !std::strcmp(value, "true")) { + EXPECT_EQ(folderUriObject.GetRealPath(), filePath); + } else { + EXPECT_EQ(folderUriObject.GetRealPath(), RealPath); + } + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetRealPath_0008"; + } + /** + * @tc.name: file_uri_test_0012 + * @tc.desc: Test function of GetRealPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ + HWTEST_F(FileUriTest, File_uri_GetRealPath_0009, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetRealPath_0009"; + string fileUri = "file://docs/storage/Users/user/files/1.txt"; + string filePath = "/storage/Users/user/files/1.txt"; + string RealPath = PATH_SHARE + MODE_R + "docs/storage/Users/user/files/1.txt"; + FileUri folderUriObject(fileUri); + char value[] = "false"; + int retSystem = GetParameter(g_fileManagerFullMoutEnableParameter, "false", value, sizeof(value)); + if (retSystem > 0 && !std::strcmp(value, "true")) { + EXPECT_EQ(folderUriObject.GetRealPath(), filePath); + } else { + EXPECT_EQ(folderUriObject.GetRealPath(), RealPath); + } + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetRealPath_0009"; + } + /** + * @tc.name: file_uri_test_0012 + * @tc.desc: Test function of GetRealPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ + HWTEST_F(FileUriTest, File_uri_GetRealPath_0010, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetRealPath_0010"; + string fileUri = "file://docs/storage/Users/user/Download/files/1.txt"; + string filePath = "/storage/Users/user/Download/files/1.txt"; + string RealPath = PATH_SHARE + MODE_R + "docs/storage/Users/user/Download/files/1.txt"; + FileUri folderUriObject(fileUri); + char value[] = "false"; + int retSystem = GetParameter(g_fileManagerFullMoutEnableParameter, "false", value, sizeof(value)); + if (retSystem > 0 && !std::strcmp(value, "true")) { + EXPECT_EQ(folderUriObject.GetRealPath(), filePath); + } else { + EXPECT_EQ(folderUriObject.GetRealPath(), RealPath); + } + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetRealPath_0010"; + } } \ No newline at end of file -- Gitee