From 998da8829825fff24cabfec95ca9ae22e0795e1b Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Fri, 14 Jul 2023 15:08:24 +0800 Subject: [PATCH] return uri directly when the uri is in public dir Signed-off-by: zhangkaixiang Change-Id: I6be7297c19a964fe1c021522cb63863bdfb9d324 --- .../src/remote_file_share.cpp | 105 ++++++++++++------ 1 file changed, 71 insertions(+), 34 deletions(-) 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 bfcc1446b..01d104dd4 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 @@ -15,19 +15,19 @@ #include "remote_file_share.h" +#include #include +#include #include #include #include #include -#include -#include +#include -#include "directory_ex.h" -#include "securec.h" #include "common_func.h" -#include "uri.h" #include "log.h" +#include "securec.h" +#include "uri.h" namespace OHOS { namespace AppFileService { @@ -46,6 +46,9 @@ namespace { const std::string LOWER_SHARE_PATH_MID = "/account/device_view/local/services/"; const std::string SHARE_PATH_DIR = "/.share"; const std::string REMOTE_SHARE_PATH_DIR = "/.remote_share"; + const std::vector PUBLIC_DIR_PATHS = { + "/Documents" + }; } #define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl) @@ -128,25 +131,25 @@ static std::string GetLowerSharePath(const int &userId, const std::string &packa return LOWER_SHARE_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID + packageName; } -static bool DeleteShareDir(const std::string &PACKAGE_PATH, const std::string &SHARE_PATH) +static bool DeleteShareDir(const std::string &packagePath, const std::string &sharePath) { bool result = true; - if (access(SHARE_PATH.c_str(), F_OK) == 0) { - int ret = rmdir(SHARE_PATH.c_str()); + if (access(sharePath.c_str(), F_OK) == 0) { + int ret = rmdir(sharePath.c_str()); if (ret != 0) { LOGE("RemoteFileShare::DeleteShareDir, delete dir failed with %{public}d", errno); result = false; } else { - LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", SHARE_PATH.c_str()); + LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", sharePath.c_str()); } } - if (access(PACKAGE_PATH.c_str(), F_OK) == 0) { - int ret = rmdir(PACKAGE_PATH.c_str()); + if (access(packagePath.c_str(), F_OK) == 0) { + int ret = rmdir(packagePath.c_str()); if (ret != 0) { LOGE("RemoteFileShare::DeleteShareDir, delete dir failed with %{public}d", errno); result = false; } else { - LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", PACKAGE_PATH.c_str()); + LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", packagePath.c_str()); } } return result; @@ -180,7 +183,7 @@ static int CreateShareFile(struct HmdfsShareControl &shareControl, const char* f static int CheckInputValidity(const int &fd, const int &userId, const std::string &deviceId) { return (fd < 0) || (userId < USER_ID_INIT) || (deviceId != SHARE_ALL_DEVICE && - deviceId.size() != HMDFS_CID_SIZE); + deviceId.size() != HMDFS_CID_SIZE); } int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, @@ -200,30 +203,30 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, return errno; } - const std::string PACKAGE_PATH = GetLowerSharePath(userId, processName); - const std::string LOWER_SHARE_PATH = PACKAGE_PATH + SHARE_PATH_DIR; - if (CreateShareDir(PACKAGE_PATH) != 0) + const std::string packagePath = GetLowerSharePath(userId, processName); + const std::string lowerSharePath = packagePath + SHARE_PATH_DIR; + if (CreateShareDir(packagePath) != 0) return errno; - if (CreateShareDir(LOWER_SHARE_PATH) != 0) { - DeleteShareDir(PACKAGE_PATH, LOWER_SHARE_PATH); + if (CreateShareDir(lowerSharePath) != 0) { + DeleteShareDir(packagePath, lowerSharePath); return errno; } - const std::string SHARE_PATH = GetSharePath(userId, processName) + SHARE_PATH_DIR; + const std::string ioctlDir = GetSharePath(userId, processName) + SHARE_PATH_DIR; char realPath[PATH_MAX] = {'\0'}; - if (!realpath(SHARE_PATH.c_str(), realPath)) { + if (!realpath(ioctlDir.c_str(), realPath)) { LOGE("RemoteFileShare::CreateSharePath, realpath failed with %{public}d", errno); - DeleteShareDir(PACKAGE_PATH, LOWER_SHARE_PATH); + DeleteShareDir(packagePath, lowerSharePath); return errno; } - std::string file_name = GetFileName(shareControl.fd); - if (file_name == "") { + std::string fileName = GetFileName(shareControl.fd); + if (fileName == "") { LOGE("RemoteFileShare::CreateSharePath, get error file name"); - DeleteShareDir(PACKAGE_PATH, LOWER_SHARE_PATH); + DeleteShareDir(packagePath, lowerSharePath); return EBADF; } - sharePath = SHARE_PATH + "/" + file_name; + sharePath = ioctlDir + "/" + fileName; if (CreateShareFile(shareControl, realPath, deviceId) != 0) { LOGE("RemoteFileShare::CreateSharePath, create share file failed with %{public}d", errno); @@ -232,7 +235,7 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, return 0; } sharePath = ""; - DeleteShareDir(PACKAGE_PATH, LOWER_SHARE_PATH); + DeleteShareDir(packagePath, lowerSharePath); return errno; } LOGI("RemoteFileShare::CreateSharePath, create %{public}s successfully", sharePath.c_str()); @@ -280,32 +283,66 @@ static void InitHmdfsInfo(struct HmdfsDstInfo &hdi, const std::string &physicalP const std::string &distributedPath, const std::string &bundleName) { hdi.localLen = physicalPath.size() + 1; - hdi.localPathIndex = (uint64_t)(physicalPath.c_str()); + hdi.localPathIndex = reinterpret_cast(physicalPath.c_str()); hdi.distributedLen = distributedPath.size() + 1; - hdi.distributedPathIndex = (uint64_t)(distributedPath.c_str()); + hdi.distributedPathIndex = reinterpret_cast(distributedPath.c_str()); hdi.bundleNameLen = bundleName.size() + 1; - hdi.bundleNameIndex = (uint64_t)(bundleName.c_str()); + hdi.bundleNameIndex = reinterpret_cast(bundleName.c_str()); - hdi.size = (uint64_t)&hdi.size; + hdi.size = reinterpret_cast(&hdi.size); } -static void SetHmdfsUriInfo(struct HmdfsUriInfo &hdi, Uri &uri, uint64_t fileSize) +static void SetHmdfsUriInfo(struct HmdfsUriInfo &hui, Uri &uri, uint64_t fileSize) { std::string bundleName = uri.GetAuthority(); std::string path = uri.GetPath(); - hdi.uriStr = FILE_SCHEME + "://" + bundleName + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + path; - hdi.fileSize = fileSize; + hui.uriStr = FILE_SCHEME + "://" + bundleName + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + path; + hui.fileSize = fileSize; return; } +static int32_t SetPublicDirPathSize(const std::string &physicalPath, struct HmdfsUriInfo &hui) +{ + struct stat buf = {}; + if (stat(physicalPath.c_str(), &buf) != 0) { + LOGE("Failed to get physical path stat with %{public}d", -errno); + return -errno; + } + hui.fileSize = buf.st_size; + return 0; +} + +static bool CheckPublicDirPath(Uri &uri, const std::string &userId, struct HmdfsUriInfo &hui) +{ + std::string sandboxPath = uri.GetPath(); + for (const std::string &path : PUBLIC_DIR_PATHS) { + if (sandboxPath.find(path) == 0) { + LOGD("Check public dir path successfully with path prefix %{public}s", path.c_str()); + std::string physicalPath = GetPhysicalPath(uri, userId); + hui.uriStr = uri.ToString(); + if (physicalPath == "") { + LOGE("CheckPublicDirPath failed to get physical path"); + return false; + } + (void)SetPublicDirPathSize(physicalPath, hui); + return true; + } + } + return false; +} + int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int32_t &userId, struct HmdfsUriInfo &hui) { Uri uri(uriStr); LOGD("GetDfsUriFromLocal begin with %{public}s", uriStr.c_str()); + if (CheckPublicDirPath(uri, std::to_string(userId), hui)) { + return 0; + } + std::string physicalPath = GetPhysicalPath(uri, std::to_string(userId)); if (physicalPath == "") { LOGE("Failed to get physical path"); @@ -329,7 +366,7 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int int32_t dirFd = open(ioctlDir.c_str(), O_RDONLY); if (dirFd < 0) { LOGE("Open share path failed with %{public}d", errno); - return errno; + return -errno; } ret = ioctl(dirFd, HMDFS_IOC_GET_DST_PATH, &hdi); -- Gitee