diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index 14095f08aca26ad4c087bc533169eff3e1c30daf..9cd4c0d289d05f30a596b1f8acb31993e562eea7 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -52,6 +52,7 @@ const std::string BACKSLASH = "/"; const std::string DISTRIBUTED_DIR_PATH = "/data/storage/el2/distributedfiles"; const std::string DST_PATH_HEAD = "/data/service/el2/"; const std::string DST_PATH_MID = "/hmdfs/account/data/"; +const std::string DST_PATH_MID_FILES = "/hmdfs/account/files/"; const std::string SHARE_PATH_HEAD = "/mnt/hmdfs/"; const std::string SHARE_PATH_MID = "/account/merge_view/services/"; const std::string LOWER_SHARE_PATH_HEAD = "/mnt/hmdfs/"; @@ -305,8 +306,7 @@ static int GetMediaDistributedDir(const int &userId, std::string &distributedDir return E_OK; } -static int GetDistributedDir(const int &userId, std::string &distributedDir, const std::string &bundleName, - const std::string &networkId, Uri uri) +static std::string GetSandboxDir(Uri uri) { std::string sandboxPath = SandboxHelper::Decode(uri.GetPath()); std::string sandboxDir = ""; @@ -318,9 +318,32 @@ static int GetDistributedDir(const int &userId, std::string &distributedDir, con sandboxDir = SANDBOX_CLOUD_DIR; } else { LOGE("Get distributed dir failed."); + return ""; + } + return sandboxDir; +} + +static bool IsDistributedFileDir(Uri uri) +{ + std::string sandboxDir = GetSandboxDir(uri); + return sandboxDir == DISTRIBUTED_DIR_PATH ? true : false; +} + +static bool IsCloudFileDir(Uri uri) +{ + std::string sandboxDir = GetSandboxDir(uri); + return sandboxDir == SANDBOX_CLOUD_DIR ? true : false; +} + +static int GetDistributedDir(const int &userId, std::string &distributedDir, const std::string &bundleName, + const std::string &networkId, Uri uri) +{ + std::string sandboxDir = GetSandboxDir(uri); + if (sandboxDir == "") { + LOGE("Get sandbox dir failed"); return -EINVAL; } - + distributedDir = DST_PATH_HEAD + std::to_string(userId) + DST_PATH_MID + bundleName + REMOTE_SHARE_PATH_DIR + BACKSLASH + networkId + sandboxDir; if (distributedDir.size() >= PATH_MAX) { @@ -337,7 +360,7 @@ static int32_t GetMediaPhysicalDir(const int32_t &userId, std::string &physicalD LOGE("bundleName is not media type."); return -EINVAL; } - physicalDir = SHARE_PATH_HEAD + std::to_string(userId) + MEDIA_PATH_PREFIX_TWO + MEDIA_PHOTO_PATH; + physicalDir = DST_PATH_HEAD + std::to_string(userId) + DST_PATH_MID_FILES + MEDIA_PHOTO_PATH; if (physicalDir.size() >= PATH_MAX) { LOGE("Path is too long with %{public}zu", physicalDir.size()); return -EINVAL; @@ -432,6 +455,18 @@ static void SetHmdfsUriInfo(struct HmdfsUriInfo &hui, return; } +static int32_t SetFileSize(const std::string &physicalPath, struct HmdfsUriInfo &hui) +{ + struct stat buf = {}; + if (stat(physicalPath.c_str(), &buf) != E_OK) { + int32_t ret = -errno; + LOGE("Failed to get physical path stat with %{public}d", ret); + return ret; + } + hui.fileSize = static_cast(buf.st_size); + return E_OK; +} + static int32_t SetPublicDirHmdfsInfo(const std::string &physicalPath, const std::string &uriStr, struct HmdfsUriInfo &hui, const std::string &networkId) { @@ -704,14 +739,13 @@ static int32_t SetHmdfsUriDirInfo(struct HmdfsUriInfo &hui, Uri &uri, const std: { hui.uriStr = FILE_SCHEME + "://" + bundleName + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + BACKSLASH + networkId + uri.GetPath() + NETWORK_PARA + networkId; - struct stat buf = {}; - if (stat(physicalPath.c_str(), &buf) != E_OK) { - int32_t ret = -errno; - LOGE("Failed to get physical path stat with %{public}d", ret); - return ret; - } - hui.fileSize = static_cast(buf.st_size); - return E_OK; + return SetFileSize(physicalPath, hui); +} + +static int32_t SetDistributedfilesHmdfsUriDirInfo(struct HmdfsUriInfo &hui, Uri &uri, const std::string &physicalPath) +{ + hui.uriStr = uri.ToString(); + return SetFileSize(physicalPath, hui); } static int32_t SetMediaHmdfsUriDirInfo(struct HmdfsUriInfo &hui, Uri &uri, const std::string &physicalPath, @@ -729,14 +763,7 @@ static int32_t SetMediaHmdfsUriDirInfo(struct HmdfsUriInfo &hui, Uri &uri, const } hui.uriStr = FILE_SCHEME + "://" + MEDIA_BUNDLE_NAME + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + BACKSLASH + networkId + mediaSandboxPath + NETWORK_PARA + networkId; - - struct stat buf = {}; - if (stat(physicalPath.c_str(), &buf) != E_OK) { - LOGE("Failed to get physical path stat with %{public}d", -errno); - return -errno; - } - hui.fileSize = static_cast(buf.st_size); - return E_OK; + return SetFileSize(physicalPath, hui); } @@ -785,7 +812,7 @@ static int32_t GetMediaDfsUrisDirFromLocal(const std::vector &uriLi return E_OK; } -static int32_t CheckIfNeedMount(const int &userId, const std::string &bundleName, const std::string &networkId, Uri uri) +static int32_t DoMount(const int &userId, const std::string &bundleName, const std::string &networkId, Uri uri) { std::string distributedDir; int32_t ret = GetDistributedDir(userId, distributedDir, bundleName, networkId, uri); @@ -808,12 +835,32 @@ static int32_t CheckIfNeedMount(const int &userId, const std::string &bundleName return E_OK; } +static int32_t CheckIfNeedMount(const std::string &bundleName, const std::string &networkId, Uri uri, + const std::string &physicalPath, std::unordered_map &uriToDfsUriMaps) +{ + HmdfsUriInfo dfsUriInfo; + std::string uriStr = uri.ToString(); + if (bundleName == FILE_MANAGER_AUTHORITY) { + (void)SetPublicDirHmdfsInfo(physicalPath, uriStr, dfsUriInfo, networkId); + uriToDfsUriMaps.insert({uriStr, dfsUriInfo}); + LOGI("bundleName: %{public}s, dfsUri: %{private}s", uriStr.c_str(), dfsUriInfo.uriStr.c_str()); + return E_OK; + } + if (IsDistributedFileDir(uri) || IsCloudFileDir(uri)) { + (void)SetDistributedfilesHmdfsUriDirInfo(dfsUriInfo, uri, physicalPath); + uriToDfsUriMaps.insert({uriStr, dfsUriInfo}); + LOGI("bundleName: %{public}s, dfsUri: %{private}s", uriStr.c_str(), dfsUriInfo.uriStr.c_str()); + return E_OK; + } + return -EINVAL; +} + int32_t RemoteFileShare::GetDfsUrisDirFromLocal(const std::vector &uriList, const int32_t &userId, std::unordered_map &uriToDfsUriMaps) { std::vector otherUriList; std::vector mediaUriList; - int ret = UriCategoryByType(uriList, mediaUriList, otherUriList); + int32_t ret = UriCategoryByType(uriList, mediaUriList, otherUriList); if (ret == E_OK && mediaUriList.size() != 0) { ret = GetMediaDfsUrisDirFromLocal(mediaUriList, userId, uriToDfsUriMaps); } @@ -831,20 +878,16 @@ int32_t RemoteFileShare::GetDfsUrisDirFromLocal(const std::vector & } std::string bundleName = uri.GetAuthority(); - if (bundleName == FILE_MANAGER_AUTHORITY) { - HmdfsUriInfo dfsUriInfo; - (void)SetPublicDirHmdfsInfo(physicalPath, uriStr, dfsUriInfo, networkId); - uriToDfsUriMaps.insert({uriStr, dfsUriInfo}); - LOGI("localUri: %{private}s, dfsUri: %{private}s", uriStr.c_str(), dfsUriInfo.uriStr.c_str()); + ret = CheckIfNeedMount(bundleName, networkId, uri, physicalPath, uriToDfsUriMaps); + if (ret == E_OK) { continue; } - ret = CheckIfNeedMount(userId, bundleName, networkId, uri); + ret = DoMount(userId, bundleName, networkId, uri); if (ret != E_OK) { LOGE("Failed to mount, %{public}d", ret); return ret; } - HmdfsUriInfo dfsUriInfo; (void)SetHmdfsUriDirInfo(dfsUriInfo, uri, physicalPath, networkId, bundleName); uriToDfsUriMaps.insert({uriStr, dfsUriInfo}); diff --git a/test/unittest/remote_file_share/remote_file_share_test.cpp b/test/unittest/remote_file_share/remote_file_share_test.cpp index fcbacc2b7961cb2f2832b23d9647367a3f25de15..9cbe14a736000dad9cbdd32d366d6a830f90e7cc 100644 --- a/test/unittest/remote_file_share/remote_file_share_test.cpp +++ b/test/unittest/remote_file_share/remote_file_share_test.cpp @@ -35,6 +35,7 @@ namespace { const int E_OK = 0; const int USER_ID = 100; const int32_t TEST_CHAR = 95; + const int32_t NO_SUCH_FILE_ERROR = -2; class RemoteFileShareTest : public testing::Test { public: @@ -1228,32 +1229,168 @@ namespace { } /** - * @tc.name: Remote_file_share_CheckIfNeedMount_0001 - * @tc.desc: Test function of CheckIfNeedMount() + * @tc.name: Remote_file_share_DoMount_0001 + * @tc.desc: Test function of DoMount() * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ - HWTEST_F(RemoteFileShareTest, Remote_file_share_CheckIfNeedMount_0001, testing::ext::TestSize.Level1) + HWTEST_F(RemoteFileShareTest, Remote_file_share_DoMount_0001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CheckIfNeedMount_0001"; + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_DoMount_0001"; OHOS::Uri uri("file://com.demo.a/data/storage/el2/base/remote_share.txt"); string bundleName = "com.demo.a"; string networkId = "network123"; int32_t usrId = USER_ID; - int32_t ret = CheckIfNeedMount(usrId, bundleName, networkId, uri); + int32_t ret = DoMount(usrId, bundleName, networkId, uri); EXPECT_EQ(ret, -EINVAL); OHOS::Uri uri1("file://com.demo.a/data/storage/el22/base/remote_share.txt"); - ret = CheckIfNeedMount(usrId, bundleName, networkId, uri1); + ret = DoMount(usrId, bundleName, networkId, uri1); EXPECT_EQ(ret, -EINVAL); - OHOS::Uri uri2("file://meida/data/storage/el2/base/remote_share.txt"); - ret = CheckIfNeedMount(usrId, bundleName, networkId, uri2); + OHOS::Uri uri2("file://media/data/storage/el2/base/remote_share.txt"); + ret = DoMount(usrId, bundleName, networkId, uri2); EXPECT_EQ(ret, -EINVAL); + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_DoMount_0001"; + } + + /** + * @tc.name: Remote_file_share_SetFileSize_0001 + * @tc.desc: Test function of SetFileSize() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_SetFileSize_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_SetFileSize_0001"; + + HmdfsUriInfo hui; + hui.fileSize = 0; + string physicalPath = "/data/app/el2/100/base/com.demo.a/remote_share.txt"; + + int32_t ret = SetFileSize(physicalPath, hui); + EXPECT_EQ(ret, NO_SUCH_FILE_ERROR); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_SetFileSize_0001"; + } + + /** + * @tc.name: Remote_file_share_SetDistributedfilesHmdfsUriDirInfo_0001 + * @tc.desc: Test function of SetDistributedfilesHmdfsUriDirInfo() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_SetDistributedfilesHmdfsUriDirInfo_0001, + testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_SetDistributedfilesHmdfsUriDirInfo_0001"; + + HmdfsUriInfo hui; + OHOS::Uri uri("file://com.demo.a/data/storage/el2/base/remote_share.txt"); + string physicalPath = "/data/app/el2/100/base/com.demo.a/remote_share.txt"; + + int32_t ret = SetDistributedfilesHmdfsUriDirInfo(hui, uri, physicalPath); + EXPECT_EQ(hui.uriStr, uri.ToString()); + EXPECT_EQ(ret, NO_SUCH_FILE_ERROR); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_SetDistributedfilesHmdfsUriDirInfo_0001"; + } + + /** + * @tc.name: Remote_file_share_CheckIfNeedMount_0001 + * @tc.desc: Test function of CheckIfNeedMount() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CheckIfNeedMount_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CheckIfNeedMount_0001"; + + string bundleName = "docs"; + string networkId = "networkId123"; + OHOS::Uri uri("file://docs/storage/Users/currentUser/Document/Subject1/Subject2/1.txt"); + string physicalPath = "/data/app/el2/100/base/docs/remote_share.txt"; + unordered_map uriToDfsUriMaps; + + int32_t ret = CheckIfNeedMount(bundleName, networkId, uri, physicalPath, uriToDfsUriMaps); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CheckIfNeedMount_0001"; } + + /** + * @tc.name: Remote_file_share_CheckIfNeedMount_0002 + * @tc.desc: Test function of CheckIfNeedMount() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CheckIfNeedMount_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CheckIfNeedMount_0002"; + + string bundleName = "com.demo.a"; + string networkId = "networkId123"; + OHOS::Uri uri("file://com.demo.a/data/storage/el2/distributedfiles/remote_share.txt"); + string physicalPath = "/data/app/el2/100/base/com.demo.a/remote_share.txt"; + unordered_map uriToDfsUriMaps; + + int32_t ret = CheckIfNeedMount(bundleName, networkId, uri, physicalPath, uriToDfsUriMaps); + EXPECT_EQ(ret, E_OK); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CheckIfNeedMount_0002"; + } + + /** + * @tc.name: Remote_file_share_CheckIfNeedMount_0003 + * @tc.desc: Test function of CheckIfNeedMount() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CheckIfNeedMount_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CheckIfNeedMount_0003"; + + string bundleName = "com.demo.a"; + string networkId = "networkId123"; + OHOS::Uri uri("file://docs/data/storage/el2/cloud/remote_share.txt"); + string physicalPath = "/data/app/el2/100/cloud/com.demo.a/remote_share.txt"; + unordered_map uriToDfsUriMaps; + + int32_t ret = CheckIfNeedMount(bundleName, networkId, uri, physicalPath, uriToDfsUriMaps); + EXPECT_EQ(ret, E_OK); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CheckIfNeedMount_0003"; + } + + /** + * @tc.name: Remote_file_share_CheckIfNeedMount_0004 + * @tc.desc: Test function of CheckIfNeedMount() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CheckIfNeedMount_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CheckIfNeedMount_0004"; + + string bundleName = "com.demo.a"; + string networkId = "networkId123"; + OHOS::Uri uri("file://com.demo.a/data/storage/el2/base/remote_share.txt"); + string physicalPath = "/data/app/el2/100/base/com.demo.a/remote_share.txt"; + unordered_map uriToDfsUriMaps; + + int32_t ret = CheckIfNeedMount(bundleName, networkId, uri, physicalPath, uriToDfsUriMaps); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CheckIfNeedMount_0004"; + } }