From 924a6b64fded6c050a5dd35c84c73107ce224966 Mon Sep 17 00:00:00 2001 From: cuiruibin Date: Tue, 8 Apr 2025 09:10:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AA=92=E4=BD=93=E7=B1=BB=E5=9E=8BURI?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=88=86=E5=B8=83=E5=BC=8FURI=20Signed-off-b?= =?UTF-8?q?y:=20cuiruibin=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interfaces/common/include/sandbox_helper.h | 1 + interfaces/common/src/sandbox_helper.cpp | 24 +++++ .../src/remote_file_share.cpp | 99 +++++++++++++++---- 3 files changed, 106 insertions(+), 18 deletions(-) diff --git a/interfaces/common/include/sandbox_helper.h b/interfaces/common/include/sandbox_helper.h index c0fa99864..d299929c3 100644 --- a/interfaces/common/include/sandbox_helper.h +++ b/interfaces/common/include/sandbox_helper.h @@ -33,6 +33,7 @@ public: static std::string Encode(const std::string &uri); static std::string Decode(const std::string &uri); static bool CheckValidPath(const std::string &filePath); + static int32_t GetMediaSharePath(const std::vector &fileUris, std::vector &physicalPaths); static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath); static int32_t GetBackupPhysicalPath(const std::string &fileUri, const std::string &userId, diff --git a/interfaces/common/src/sandbox_helper.cpp b/interfaces/common/src/sandbox_helper.cpp index 3ec9256e4..b1e291590 100644 --- a/interfaces/common/src/sandbox_helper.cpp +++ b/interfaces/common/src/sandbox_helper.cpp @@ -15,6 +15,7 @@ #include "sandbox_helper.h" +#include #include #include #include @@ -27,6 +28,8 @@ using namespace std; namespace OHOS { namespace AppFileService { +typedef void (*ConvertFileUriToMntPath)(const std::vector &fileUris, + std::vector &physicalPaths); namespace { const string PACKAGE_NAME_FLAG = ""; const string CURRENT_USER_ID_FLAG = ""; @@ -308,6 +311,27 @@ static int32_t GetMediaPhysicalPath(const std::string &sandboxPath, const std::s return 0; } +int32_t SandboxHelper::GetMediaSharePath(const std::vector &fileUris, + std::vector &physicalPaths) +{ + void *handle = dlopen("libmedia_library_handler.z.so", RTLD_LAZY); + if (!handle) { + LOGE("dlopen libmedia_library_handler.z.so failed, errno = %{public}s", dlerror()); + return -1; + } + char *error; + ConvertFileUriToMntPath convertFileUriToMntPath = (ConvertFileUriToMntPath)dlsym(handle, "ConvertFileUriToMntPath"); + if ((error = dlerror()) != NULL) { + LOGE("GetMediaSharePath dlsym failed, errno = %{public}s", error); + return -2; + } + convertFileUriToMntPath(fileUris, physicalPaths); + if (fileUris.size() != physicalPaths.size()) { + return -3; + } + return 0; +} + static void GetNetworkIdFromUri(const std::string &fileUri, string &networkId) { Uri uri(fileUri); 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 568d8a573..0e210f1dd 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 @@ -44,6 +44,7 @@ const int HMDFS_CID_SIZE = 64; const int USER_ID_INIT = 100; const unsigned HMDFS_IOC = 0xf2; const std::string FILE_SCHEME = "file"; +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/"; @@ -268,11 +269,13 @@ int RemoteFileShare::CreateSharePath(const int &fd, static int GetDistributedPath(Uri &uri, const int &userId, std::string &distributedPath, - const std::string &bundleName) + const std::string &bundleName, + const std::string &networkId) { - distributedPath = DST_PATH_HEAD + std::to_string(userId) + DST_PATH_MID + bundleName + - REMOTE_SHARE_PATH_DIR + SandboxHelper::Decode(uri.GetPath()); + distributedPath = DST_PATH_HEAD + std::to_string(userId) + DST_PATH_MID + bundleName + REMOTE_SHARE_PATH_DIR + + BACKSLASH + networkId + SandboxHelper::Decode(uri.GetPath()); if (distributedPath.size() >= PATH_MAX) { + LOGE("Path is too long with %{public}zu", distributedPath.size()); return -EINVAL; } @@ -341,8 +344,8 @@ static void SetHmdfsUriInfo(struct HmdfsUriInfo &hui, const std::string &networkId, const std::string &bundleName) { - hui.uriStr = FILE_SCHEME + "://" + bundleName + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + - uri.GetPath() + networkId; + hui.uriStr = FILE_SCHEME + "://" + bundleName + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + BACKSLASH + + networkId + uri.GetPath() + NETWORK_PARA + networkId; hui.fileSize = fileSize; return; @@ -351,7 +354,7 @@ static void SetHmdfsUriInfo(struct HmdfsUriInfo &hui, static int32_t SetPublicDirHmdfsInfo(const std::string &physicalPath, const std::string &uriStr, struct HmdfsUriInfo &hui, const std::string &networkId) { - hui.uriStr = uriStr + networkId; + hui.uriStr = uriStr + NETWORK_PARA + networkId; struct stat buf = {}; if (stat(physicalPath.c_str(), &buf) != 0) { LOGE("Failed to get physical path stat with %{public}d", -errno); @@ -393,7 +396,7 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int bundleName = MEDIA_BUNDLE_NAME; } - std::string networkId = NETWORK_PARA + GetLocalNetworkId(); + std::string networkId = GetLocalNetworkId(); if (bundleName == FILE_MANAGER_AUTHORITY) { (void)SetPublicDirHmdfsInfo(physicalPath, uriStr, hui, networkId); LOGD("GetDfsUriFromLocal successfully"); @@ -401,9 +404,8 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int } std::string distributedPath; - int ret = GetDistributedPath(uri, userId, distributedPath, bundleName); + int ret = GetDistributedPath(uri, userId, distributedPath, bundleName, networkId); if (ret != 0) { - LOGE("Path is too long with %{public}d", ret); return ret; } @@ -426,20 +428,86 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int return 0; } +static int32_t UriCategoryByType(const std::vector &uriList, + std::vector &mediaUriList, + std::vector &otherUriList) +{ + if (uriList.size() == 0) { + return -EINVAL; + } + for (auto &uriStr : uriList) { + Uri uri(uriStr); + std::string bundleName = uri.GetAuthority(); + if (bundleName == MEDIA_AUTHORITY) { + mediaUriList.push_back(uriStr); + } else { + otherUriList.push_back(uriStr); + } + } + return 0; +} + +static int32_t GetMediaDfsUrisFromLocal(const std::vector &uriList, + const int32_t &userId, + std::unordered_map &uriToDfsUriMaps) +{ + LOGI("GetMediaDfsUrisFromLocal start"); + std::string ioctlDir = SHAER_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID; + UniqueFd dirFd(open(ioctlDir.c_str(), O_RDONLY)); + if (dirFd < 0) { + LOGE("Open share path failed with %{public}d", errno); + return errno; + } + std::vector physicalPaths; + int getPhysicalPathRet = SandboxHelper::GetMediaSharePath(uriList, physicalPaths); + if (getPhysicalPathRet != 0) { + return -EINVAL; + } + std::string networkId = GetLocalNetworkId(); + std::string bundleName = MEDIA_BUNDLE_NAME; + for (size_t i = 0; i < uriList.size(); i++) { + Uri uri(uriList[i]); + LOGD("GetDfsUriFromLocal begin, uri: %{private}s", uriList[i].c_str()); + std::string distributedPath; + int ret = GetDistributedPath(uri, userId, distributedPath, bundleName, networkId); + if (ret != 0) { + return ret; + } + struct HmdfsDstInfo hdi; + InitHmdfsInfo(hdi, physicalPaths[i], distributedPath, bundleName); + ret = ioctl(dirFd, HMDFS_IOC_GET_DST_PATH, &hdi); + if (ret != 0 && GetMergePathFd(hdi, dirFd, userId) != 0) { + return errno; + } + HmdfsUriInfo dfsUriInfo; + SetHmdfsUriInfo(dfsUriInfo, uri, hdi.size, networkId, bundleName); + uriToDfsUriMaps.insert({uriList[i], dfsUriInfo}); + } + LOGI("GetDfsUrisFromLocal successfully"); + return 0; +} + int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector &uriList, const int32_t &userId, std::unordered_map &uriToDfsUriMaps) { LOGI("GetDfsUrisFromLocal start"); + std::vector otherUriList; + std::vector mediaUriList; + int ret = UriCategoryByType(uriList, mediaUriList, otherUriList); + if (ret == 0 && mediaUriList.size() != 0) { + ret = GetMediaDfsUrisFromLocal(mediaUriList, userId, uriToDfsUriMaps); + } + if (ret != 0) + return ret; std::string ioctlDir = SHAER_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID; UniqueFd dirFd(open(ioctlDir.c_str(), O_RDONLY)); if (dirFd < 0) { LOGE("Open share path failed with %{public}d", errno); return errno; } - LOGI("open ioctlDir end"); - std::string networkId = NETWORK_PARA + GetLocalNetworkId(); - for (auto &uriStr : uriList) { + std::string networkId = GetLocalNetworkId(); + for (auto &uriStr : otherUriList) { Uri uri(uriStr); std::string bundleName = uri.GetAuthority(); LOGD("GetDfsUriFromLocal begin, uri: %{private}s", uriStr.c_str()); @@ -448,21 +516,16 @@ int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector &uri LOGE("Failed to get physical path"); return -EINVAL; } - if (bundleName == MEDIA_AUTHORITY) { - bundleName = MEDIA_BUNDLE_NAME; - } if (bundleName == FILE_MANAGER_AUTHORITY) { HmdfsUriInfo dfsUriInfo; (void)SetPublicDirHmdfsInfo(physicalPath, uriStr, dfsUriInfo, networkId); uriToDfsUriMaps.insert({uriStr, dfsUriInfo}); - LOGD("GetDfsUriFromLocal successfully"); continue; } std::string distributedPath; - int ret = GetDistributedPath(uri, userId, distributedPath, bundleName); + ret = GetDistributedPath(uri, userId, distributedPath, bundleName, networkId); if (ret != 0) { - LOGE("Path is too long with %{public}d", ret); return ret; } struct HmdfsDstInfo hdi; -- Gitee