diff --git a/app_file_service.gni b/app_file_service.gni index 8a49035045e68705ecf5eaa89a6d1a304a0184e9..ba957f7505f8fd745d26cb89bd19cfad78889f96 100644 --- a/app_file_service.gni +++ b/app_file_service.gni @@ -17,9 +17,5 @@ path_base = "//commonlibrary/c_utils/base" path_napi = "//foundation/arkui/napi" app_file_service_path = "//foundation/filemanagement/app_file_service" declare_args() { - sandbox_manarer = false - if (defined(global_parts_info) && - defined(global_parts_info.accesscontrol_sandbox_manager)) { - sandbox_manarer = true - } + app_file_service_feature_sandbox_manarer = true } diff --git a/bundle.json b/bundle.json index f3659913c22757a7cc5ff36257501c8611fb3c58..05117536b5ca6e25d67aa78c7c522a62c4bb3e87 100644 --- a/bundle.json +++ b/bundle.json @@ -37,6 +37,7 @@ "ipc", "init", "napi", + "os_account", "safwk", "samgr", "storage_service", diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index 8cdb66b62e73a593a77a435b680edac172cbd0ae..ea022122609e58ca478475f84657608464caa613 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -31,10 +31,30 @@ namespace OHOS { namespace AppFileService { using namespace OHOS::AppExecFwk; namespace { - const std::string FILE_SCHEME_PREFIX = "file://"; - const char BACKFLASH = '/'; - const std::string FILE_MANAGER_URI_HEAD = "/storage/"; - const std::string FILE_MANAGER_AUTHORITY = "docs"; +const std::string FILE_SCHEME_PREFIX = "file://"; +const char BACKFLASH = '/'; +const std::string FILE_MANAGER_URI_HEAD = "/storage/"; +const std::string FILE_HAP_URI_HEAD = "/data/storage"; +const std::string FILE_MANAGER_AUTHORITY = "docs"; +const std::string FILE_APP_DATA = "appdata"; +const std::string FILE_BASE = "base/"; +const std::string FILE_USERS = "Users"; +const std::string FILE_MNT_DATA = "/mnt/data/fuse"; +const std::string FILE_DLP_PACKAGE_NAME = "com.ohos.dlpmanager"; +} // namespace + +static string GetNextDirName(string &path) +{ + if (path.find(BACKFLASH == 0)) { + path = path.substr(1); + } + string dirname = ""; + if (path.find(BACKFLASH) != string::npos) { + size_t pos = path.find(BACKFLASH); + dirname = path.substr(0, pos); + path = path.substr(pos); + } + return dirname; } static sptr GetBundleMgrProxy() { @@ -83,15 +103,44 @@ static void NormalizePath(string &path) } } +static bool GetRealPathIsUri(string &path) +{ + string realPath = path; + if (realPath.find(FILE_MANAGER_URI_HEAD) == 0) { + realPath = realPath.substr(FILE_MANAGER_URI_HEAD.size()); + string pathType = GetNextDirName(realPath); + if (pathType != FILE_USERS) { + return false; + } + string userName = GetNextDirName(realPath); + if (GetNextDirName(realPath) == FILE_APP_DATA) { + string encryptionLevel = GetNextDirName(realPath); + string dataPart = GetNextDirName(realPath); + string bundleName = GetNextDirName(realPath); + if (bundleName.empty()) { + return false; + } + path = FILE_SCHEME_PREFIX + bundleName + FILE_HAP_URI_HEAD + BACKFLASH + encryptionLevel + BACKFLASH + + dataPart + SandboxHelper::Encode(realPath); + return true; + } + } + return false; +} + string CommonFunc::GetUriFromPath(const string &path) { string realPath = path; NormalizePath(realPath); - - string packageName = (path.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : GetSelfBundleName(); + if (GetRealPathIsUri(realPath)) { + return realPath; + } + if (realPath.find(FILE_MNT_DATA) == 0) { + return FILE_SCHEME_PREFIX + FILE_DLP_PACKAGE_NAME + SandboxHelper::Encode(realPath); + } + string packageName = (realPath.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : GetSelfBundleName(); realPath = FILE_SCHEME_PREFIX + packageName + SandboxHelper::Encode(realPath); return realPath; } } // namespace AppFileService } // namespace OHOS - diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 6ae1aa14c4dd074a485db3f0168c7c25993867b4..3391007fb08a6c60ff4c381431060201525e51c4 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -52,7 +52,8 @@ ohos_shared_library("fileshare_native") { ] public_configs = [ ":file_share_config" ] - + deps = + [ "${app_file_service_path}/interfaces/innerkits/native:fileuri_native" ] external_deps = [ "ability_base:base", "ability_base:want", @@ -65,11 +66,10 @@ ohos_shared_library("fileshare_native") { "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", + "os_account:os_account_innerkits", ] - defines = [] - if (sandbox_manarer) { + if (app_file_service_feature_sandbox_manarer) { external_deps += [ "sandbox_manager:libsandbox_manager_sdk" ] - defines += [ "SANDBOX_MANAGER" ] } part_name = "app_file_service" subsystem_name = "filemanagement" @@ -101,6 +101,7 @@ ohos_shared_library("fileuri_native") { "hilog:libhilog", "init:libbegetutil", "ipc:ipc_core", + "os_account:os_account_innerkits", "samgr:samgr_proxy", ] diff --git a/interfaces/innerkits/native/file_share/include/file_permission.h b/interfaces/innerkits/native/file_share/include/file_permission.h index 407673abf0779ab782c9fea0d2155c87310ee793..3c8953c92d9d8d31cde6aabbbea4c6501f7c0374 100644 --- a/interfaces/innerkits/native/file_share/include/file_permission.h +++ b/interfaces/innerkits/native/file_share/include/file_permission.h @@ -16,19 +16,15 @@ #ifndef APP_FILE_SERVICE_FILE_PERMISSION #define APP_FILE_SERVICE_FILE_PERMISSION +#include "sandbox_manager_kit.h" #include #include #include -#ifdef SANDBOX_MANAGER -#include "sandbox_manager_kit.h" -#endif namespace OHOS { namespace AppFileService { using namespace std; -#ifdef SANDBOX_MANAGER using namespace AccessControl::SandboxManager; -#endif constexpr const int32_t MAX_ARRAY_SIZE = 500; typedef enum OperationMode { READ_MODE = 1 << 0, @@ -74,8 +70,11 @@ public: static int32_t DeactivatePermission(const vector &uriPolicies, deque &errorResults); static int32_t CheckPersistentPermission(const vector &uriPolicies, vector &errorResults); - static string GetPathByPermission(const std::string &permission); -#ifdef SANDBOX_MANAGER + static string GetPathByPermission(const std::string &userName, const std::string &permission); + static int32_t CheckUriPersistentPermission(uint32_t tokenId, + const vector &uriPolicies, + vector &errorResults); + private: static void ParseErrorResults(const vector &resultCodes, const vector &pathPolicies, diff --git a/interfaces/innerkits/native/file_share/src/file_permission.cpp b/interfaces/innerkits/native/file_share/src/file_permission.cpp index 3c41d02b052fdb20c363dfec2c5162c10830f520..0932b46086ef8b66627401d305c68fba4057a136 100644 --- a/interfaces/innerkits/native/file_share/src/file_permission.cpp +++ b/interfaces/innerkits/native/file_share/src/file_permission.cpp @@ -12,22 +12,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "file_permission.h" -#include "log.h" -#include "parameter.h" -#include "uri.h" -#include -#include -#ifdef SANDBOX_MANAGER #include "accesstoken_kit.h" #include "bundle_constants.h" +#include "file_uri.h" #include "hap_token_info.h" #include "ipc_skeleton.h" +#include "log.h" #include "n_error.h" #include "sandbox_helper.h" #include "sandbox_manager_err_code.h" #include "tokenid_kit.h" -#endif +#include "uri.h" +#include +#include +#include namespace OHOS { namespace AppFileService { @@ -43,6 +43,7 @@ const std::string SANDBOX_STORAGE_PATH = "/storage/Users/currentUser/"; const std::string DOWNLOAD_PATH = "/storage/Users/currentUser/Download"; const std::string DESKTOP_PATH = "/storage/Users/currentUser/Desktop"; const std::string DOCUMENTS_PATH = "/storage/Users/currentUser/Documents"; +const std::string CURRENTUSER = "currentUser"; const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; @@ -51,7 +52,6 @@ const std::unordered_map permissionPathMap = { {READ_WRITE_DOWNLOAD_PERMISSION, DOWNLOAD_PATH}, {READ_WRITE_DESKTOP_PERMISSION, DESKTOP_PATH}, {READ_WRITE_DOCUMENTS_PERMISSION, DOCUMENTS_PATH}}; -#ifdef SANDBOX_MANAGER namespace { bool CheckValidUri(const string &uriStr) { @@ -169,8 +169,8 @@ vector FilePermission::GetPathPolicyInfoFromUriPolicyInfo(const vect { vector pathPolicies; for (auto uriPolicy : uriPolicies) { - Uri uri(uriPolicy.uri); - string path = SandboxHelper::Decode(uri.GetPath()); + AppFileService::ModuleFileUri::FileUri fileUri(uriPolicy.uri); + string path = fileUri.GetRealPath(); if (!CheckValidUri(uriPolicy.uri) || access(path.c_str(), F_OK) != 0) { LOGE("Not correct uri!"); PolicyErrorResult result = {uriPolicy.uri, PolicyErrorCode::INVALID_PATH, INVALID_PATH_MESSAGE}; @@ -190,7 +190,7 @@ vector FilePermission::GetPathPolicyInfoFromUriPolicyInfo(const vect for (const auto &uriPolicy : uriPolicies) { Uri uri(uriPolicy.uri); string path = SandboxHelper::Decode(uri.GetPath()); - if (!CheckValidUri(uriPolicy.uri) || access(path.c_str(), F_OK) != 0) { + if (!CheckValidUri(uriPolicy.uri)) { LOGE("Not correct uri!"); errorResults.emplace_back(false); } else { @@ -202,7 +202,7 @@ vector FilePermission::GetPathPolicyInfoFromUriPolicyInfo(const vect return pathPolicies; } -static bool CheckPermission(uint64_t tokenCaller, const string &permission) +static bool CheckPermission(uint32_t tokenCaller, const string &permission) { return Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission) == Security::AccessToken::PermissionState::PERMISSION_GRANTED; @@ -246,13 +246,11 @@ int32_t FilePermission::CheckUriPersistentPermission(uint32_t tokenId, ParseErrorResults(resultCodes, errorResults); return errorCode; } -#endif int32_t FilePermission::PersistPermission(const vector &uriPolicies, deque &errorResults) { int errorCode = 0; -#ifdef SANDBOX_MANAGER vector pathPolicies = GetPathPolicyInfoFromUriPolicyInfo(uriPolicies, errorResults); vector resultCodes; int32_t sandboxManagerErrorCode = SandboxManagerKit::PersistPolicy(pathPolicies, resultCodes); @@ -264,7 +262,6 @@ int32_t FilePermission::PersistPermission(const vector &uriPolici if (errorCode == EPERM) { ParseErrorResults(resultCodes, pathPolicies, errorResults); } -#endif return errorCode; } @@ -272,7 +269,6 @@ int32_t FilePermission::RevokePermission(const vector &uriPolicie deque &errorResults) { int errorCode = 0; -#ifdef SANDBOX_MANAGER vector pathPolicies = GetPathPolicyInfoFromUriPolicyInfo(uriPolicies, errorResults); vector resultCodes; int32_t sandboxManagerErrorCode = SandboxManagerKit::UnPersistPolicy(pathPolicies, resultCodes); @@ -284,7 +280,6 @@ int32_t FilePermission::RevokePermission(const vector &uriPolicie if (errorCode == EPERM) { ParseErrorResults(resultCodes, pathPolicies, errorResults); } -#endif return errorCode; } @@ -292,7 +287,6 @@ int32_t FilePermission::ActivatePermission(const vector &uriPolic deque &errorResults) { int errorCode = 0; -#ifdef SANDBOX_MANAGER vector pathPolicies = GetPathPolicyInfoFromUriPolicyInfo(uriPolicies, errorResults); vector resultCodes; int32_t sandboxManagerErrorCode = SandboxManagerKit::StartAccessingPolicy(pathPolicies, resultCodes); @@ -304,7 +298,6 @@ int32_t FilePermission::ActivatePermission(const vector &uriPolic if (errorCode == EPERM) { ParseErrorResults(resultCodes, pathPolicies, errorResults); } -#endif return errorCode; } @@ -312,7 +305,6 @@ int32_t FilePermission::DeactivatePermission(const vector &uriPol deque &errorResults) { int errorCode = 0; -#ifdef SANDBOX_MANAGER vector pathPolicies = GetPathPolicyInfoFromUriPolicyInfo(uriPolicies, errorResults); vector resultCodes; int32_t sandboxManagerErrorCode = SandboxManagerKit::StopAccessingPolicy(pathPolicies, resultCodes); @@ -324,27 +316,26 @@ int32_t FilePermission::DeactivatePermission(const vector &uriPol if (errorCode == EPERM) { ParseErrorResults(resultCodes, pathPolicies, errorResults); } -#endif return errorCode; } int32_t FilePermission::CheckPersistentPermission(const vector &uriPolicies, vector &errorResults) { int errorCode = 0; -#ifdef SANDBOX_MANAGER - auto tokenId = IPCSkeleton::GetCallingTokenID(); + uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); errorCode = CheckUriPersistentPermission(tokenId, uriPolicies, errorResults); -#endif return errorCode; } -string FilePermission::GetPathByPermission(const std::string &permission) +string FilePermission::GetPathByPermission(const std::string &userName, const std::string &permission) { -#ifdef SANDBOX_MANAGER if (permissionPathMap.find(permission) != permissionPathMap.end()) { - return permissionPathMap.at(permission); + string path = permissionPathMap.at(permission); + if (!userName.empty()) { + path = path.replace(path.find(CURRENTUSER), CURRENTUSER.length(), userName); + } + return path; } -#endif return ""; } } // namespace AppFileService diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h index 1f8f50410c7bbeebf2718b2df408b00983d10b69..7a0f839fd0b617da19293b02e455f9a5acef3b5c 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -28,6 +28,7 @@ public: std::string GetPath(); std::string GetRealPathBySA(const std::string &targetBundleName = ""); std::string GetRealPath(); + std::string GetRealPathBySA(const std::string &targetBundleName = ""); std::string ToString(); std::string GetFullDirectoryUri(); bool IsRemoteUri(); diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 11fb261c6e7149035d616d5cccae4802df690925..69b1cb81fa35afd6b8ef14b424cb0a6c0bb8476c 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -15,16 +15,18 @@ #include "file_uri.h" -#include +#include #include #include +#include #include "uri.h" #include "common_func.h" #include "log.h" -#include "sandbox_helper.h" +#include "os_account_manager.h" #include "parameter.h" +#include "sandbox_helper.h" using namespace std; namespace OHOS { @@ -38,17 +40,37 @@ const std::string FILE_MANAGER_AUTHORITY = "docs"; const std::string MEDIA_AUTHORITY = "media"; const std::string NETWORK_PARA = "?networkid="; const std::string BACKFLASH = "/"; -const std::string FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable"; -static bool CheckFileManagerFullMountEnable() +const std::string FILE_MANAGER_URI_HEAD = "/storage/Users/"; +const std::string DEFAULT_USERNAME = "currentUser"; +const std::string FILE_HAP_URI_HEAD = "/data/storage/"; +const std::string FILE_APP_DATA = "appdata"; +const std::string FILE_MNT_DATA = "/mnt/data/fuse"; +const std::string FILE_DLP_PACKAGE_NAME = "com.ohos.dlpmanager"; +std::string BUNDLE_NAME = ""; +std::mutex g_globalMutex; +static string GetNextDirName(string &path) { - char value[] = "false"; - int retSystem = GetParameter(FULL_MOUNT_ENABLE_PARAMETER.c_str(), "false", value, sizeof(value)); - if (retSystem > 0 && !strcmp(value, "true")) { - LOGD("The full mount enable parameter is true"); - return true; + if (path.find(BACKFLASH) == 0) { + path = path.substr(1); } - LOGD("The full mount enable parameter is false"); - return false; + string dirname = ""; + if (path.find(BACKFLASH) != string::npos) { + size_t pos = path.find(BACKFLASH); + dirname = path.substr(0, pos); + path = path.substr(pos); + } + return dirname; +} + +static std::string GetUserName() +{ + std::string userName; + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if (errCode != ERR_OK || userName.empty()) { + LOGE("Get userName"); + } + userName = DEFAULT_USERNAME; + return userName; } string FileUri::GetName() @@ -66,6 +88,18 @@ string FileUri::GetName() return sandboxPath.substr(posLast + 1); } +string GetOpenPath(string &path, string bundleName) +{ + if (path.find(FILE_HAP_URI_HEAD) == 0) { + path = path.substr(FILE_HAP_URI_HEAD.size()); + string encryptionLevel = GetNextDirName(path); + string dataPart = GetNextDirName(path); + return FILE_MANAGER_URI_HEAD + GetUserName() + BACKFLASH + FILE_APP_DATA + BACKFLASH + encryptionLevel + + BACKFLASH + dataPart + BACKFLASH + bundleName + path; + } + return path; +} + string FileUri::GetPath() { string sandboxPath = SandboxHelper::Decode(uri_.GetPath()); @@ -86,19 +120,47 @@ 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 && - (access(realPath.c_str(), F_OK) == 0 || CheckFileManagerFullMountEnable())) { + if (bundleName == FILE_MANAGER_AUTHORITY && uri_.ToString().find(NETWORK_PARA) == string::npos) { return realPath; } - - if (((bundleName != "") && (bundleName != CommonFunc::GetSelfBundleName())) || - uri_.ToString().find(NETWORK_PARA) != string::npos) { + if (bundleName == FILE_DLP_PACKAGE_NAME && realPath.find(FILE_DLP_PACKAGE_NAME) == 0) { + return realPath; + } + if (bundleName == MEDIA_AUTHORITY || + (bundleName == FILE_MANAGER_AUTHORITY && uri_.ToString().find(NETWORK_PARA) != string::npos)) { realPath = PATH_SHARE + MODE_RW + bundleName + sandboxPath; if (access(realPath.c_str(), F_OK) != 0) { realPath = PATH_SHARE + MODE_R + bundleName + sandboxPath; } + return realPath; + } + string callBundleName; + { + std::lock_guard lock(g_globalMutex); + if (BUNDLE_NAME == "") { + BUNDLE_NAME = CommonFunc::GetSelfBundleName(); + } + callBundleName = BUNDLE_NAME; + } + if (((bundleName != "") && (bundleName != callBundleName)) || uri_.ToString().find(NETWORK_PARA) != string::npos) { + realPath = GetOpenPath(realPath, bundleName); } + + return realPath; +} + +string FileUri::GetRealPathBySA(const std::string &targetBundleName) +{ + string sandboxPath = SandboxHelper::Decode(uri_.GetPath()); + string realPath = sandboxPath; + string bundleName = uri_.GetAuthority(); + if (bundleName == FILE_MANAGER_AUTHORITY || bundleName == targetBundleName) { + return realPath; + } + if (bundleName == FILE_DLP_PACKAGE_NAME && realPath.find(FILE_DLP_PACKAGE_NAME) == 0) { + return realPath; + } + realPath = GetOpenPath(realPath, bundleName); return realPath; } @@ -162,10 +224,10 @@ bool FileUri::CheckUriFormat(const std::string &uri) return true; } -FileUri::FileUri(const string &uriOrPath): uri_( - (uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath) -) -{} +FileUri::FileUri(const string &uriOrPath) + : uri_((uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath)) +{ } -} // namespace AppFileService -} // namespace OHOS \ No newline at end of file +} // namespace ModuleFileUri +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h b/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h index 652273fbfb21e187fbd8ba745668f5061c82f325..aca2e5c4618757200e5d9b4ef929d3938d4c8361 100644 --- a/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h +++ b/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h @@ -24,7 +24,7 @@ namespace OHOS { namespace AppFileService { namespace ModuleRemoteFileShare { namespace { - const std::string SHARE_ALL_DEVICE = "0"; +const std::string SHARE_ALL_DEVICE = "0"; } struct HmdfsUriInfo { @@ -35,8 +35,10 @@ struct HmdfsUriInfo { class RemoteFileShare { public: RemoteFileShare() {} - static int CreateSharePath(const int &fd, std::string &sharePath, - const int &userId, const std::string &deviceId = SHARE_ALL_DEVICE); + static int CreateSharePath(const int &fd, + std::string &sharePath, + const int &userId, + const std::string &deviceId = SHARE_ALL_DEVICE); static int32_t GetDfsUriFromLocal(const std::string &uriStr, const int32_t &userId, struct HmdfsUriInfo &hui); static int32_t GetDfsUrisFromLocal(const std::vector &uriList, const int32_t &userId, 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 5961fe8c7d0e4931f784b94af8b05f5c308fe9bd..5975b1f4d28b052a7e9acfd8fb36fd9e868b9b08 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,13 +15,13 @@ #include "remote_file_share.h" +#include #include +#include #include #include #include #include -#include -#include #include "log.h" #ifdef ENABLE_DEVICE_MANAGER @@ -30,8 +30,8 @@ #endif #include "sandbox_helper.h" #include "securec.h" -#include "uri.h" #include "unique_fd.h" +#include "uri.h" namespace OHOS { namespace AppFileService { @@ -62,7 +62,7 @@ namespace { const std::string REMOTE_SHARE_PATH_MID = "hmdfs/"; } -#define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl) +#define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl) #define HMDFS_IOC_GET_DST_PATH _IOR(HMDFS_IOC, 3, unsigned int) struct HmdfsShareControl { @@ -175,8 +175,7 @@ static bool DeleteShareDir(const std::string &PACKAGE_PATH, const std::string &S return result; } -static int CreateShareFile(struct HmdfsShareControl &shareControl, const char* file, - const std::string &deviceId) +static int CreateShareFile(struct HmdfsShareControl &shareControl, const char *file, const std::string &deviceId) { int32_t dirFd = open(file, O_RDONLY); if (dirFd < 0) { @@ -200,12 +199,13 @@ 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); + return (fd < 0) || (userId < USER_ID_INIT) || (deviceId != SHARE_ALL_DEVICE && deviceId.size() != HMDFS_CID_SIZE); } -int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, - const int &userId, const std::string &deviceId) +int RemoteFileShare::CreateSharePath(const int &fd, + std::string &sharePath, + const int &userId, + const std::string &deviceId) { struct HmdfsShareControl shareControl; shareControl.fd = fd; @@ -265,10 +265,14 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, return 0; } -static int GetDistributedPath(Uri &uri, const int &userId, std::string &distributedPath, const std::string &bundleName) +static int GetDistributedPath(Uri &uri, + const int &userId, + std::string &distributedPath, + 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 + + BACKFLASH + networkId + SandboxHelper::Decode(uri.GetPath()); if (distributedPath.size() >= PATH_MAX) { return -EINVAL; } @@ -293,8 +297,10 @@ static std::string GetPhysicalPath(Uri &uri, const std::string &userId) return physicalPath; } -static void InitHmdfsInfo(struct HmdfsDstInfo &hdi, const std::string &physicalPath, - const std::string &distributedPath, const std::string &bundleName) +static void InitHmdfsInfo(struct HmdfsDstInfo &hdi, + const std::string &physicalPath, + const std::string &distributedPath, + const std::string &bundleName) { hdi.localLen = physicalPath.size() + 1; hdi.localPathIndex = reinterpret_cast(physicalPath.c_str()); @@ -312,7 +318,7 @@ static std::string GetLocalNetworkId() { const std::string LOCAL = "local"; std::string networkId = LOCAL; - #ifdef ENABLE_DEVICE_MANAGER +#ifdef ENABLE_DEVICE_MANAGER auto callback = std::make_shared(); int32_t ret = DeviceManager::GetInstance().InitDeviceManager(PACKAGE_NAME, callback); if (ret != 0) { @@ -326,36 +332,37 @@ static std::string GetLocalNetworkId() if (ret != 0 || networkId.empty()) { return ""; } - #endif +#endif return networkId; } +static bool NotSupportIoctl(const std::string &path) +{ + struct stat buf = {}; + if (stat(path.c_str(), &buf) != 0) { + LOGE("Get path stat failed, err %{private}d" errno); + return true; + } + if (S_ISDIR(buf.st_mode)) { + LOGE("Dir Not Support GetDfsUriFromLocal"); + return true; + } + return false; +} + static void SetHmdfsUriInfo(struct HmdfsUriInfo &hui, Uri &uri, uint64_t fileSize, 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 + BACKFLASH + + networkId + uri.GetPath() + NETWORK_PARA + networkId; hui.fileSize = fileSize; return; } -static int32_t SetPublicDirHmdfsInfo(const std::string &physicalPath, const std::string &uriStr, - struct HmdfsUriInfo &hui, const std::string &networkId) -{ - hui.uriStr = uriStr + networkId; - 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 = static_cast(buf.st_size); - return 0; -} - static int32_t GetMergePathFd(HmdfsDstInfo &hdi, UniqueFd &dirFd, const int32_t &userId) { LOGI("Open merge path start"); @@ -388,15 +395,17 @@ 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"); return 0; } - + if (NotSupportIoctl(physicalPath)) { + return -EINVAL; + } 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; @@ -432,8 +441,7 @@ int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector &uri LOGE("Open share path failed with %{public}d", errno); return errno; } - LOGI("open ioctlDir end"); - std::string networkId = NETWORK_PARA + GetLocalNetworkId(); + std::string networkId = GetLocalNetworkId(); for (auto &uriStr : uriList) { Uri uri(uriStr); std::string bundleName = uri.GetAuthority(); @@ -454,9 +462,11 @@ int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector &uri LOGD("GetDfsUriFromLocal successfully"); continue; } - + if (NotSupportIoctl(physicalPath)) { + return -EINVAL; + } 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; diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 57417adbf2f489f51b28af8caba30d08bc64c6a3..8e84e3847187919fd8116c1d087fdf2329db1e2e 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -38,7 +38,8 @@ ohos_shared_library("fileshare") { "file_share/grant_permissions.cpp", "file_share/grant_uri_permission.cpp", ] - + deps = + [ "${app_file_service_path}/interfaces/innerkits/native:fileuri_native" ] external_deps = [ "ability_base:want", "ability_base:zuri", @@ -60,10 +61,8 @@ ohos_shared_library("fileshare") { "ipc:ipc_core", "napi:ace_napi", ] - defines = [] - if (sandbox_manarer) { + if (app_file_service_feature_sandbox_manarer) { external_deps += [ "sandbox_manager:libsandbox_manager_sdk" ] - defines += [ "SANDBOX_MANAGER" ] } relative_install_dir = "module" diff --git a/interfaces/kits/js/file_share/grant_permissions.cpp b/interfaces/kits/js/file_share/grant_permissions.cpp index ba2386395adf95c63a4eec11943365bfc2484012..f8c3448c5bc5c378aaaa199036fb779e02e70bf4 100644 --- a/interfaces/kits/js/file_share/grant_permissions.cpp +++ b/interfaces/kits/js/file_share/grant_permissions.cpp @@ -23,7 +23,6 @@ #include "js_native_api.h" #include "log.h" #include "n_napi.h" -#include "parameter.h" #include "tokenid_kit.h" namespace OHOS { @@ -32,30 +31,6 @@ namespace ModuleFileShare { using namespace OHOS::FileManagement::LibN; using namespace std; -namespace { -const std::string FILE_ACCESS_PERMISSION = "ohos.permission.FILE_ACCESS_PERSIST"; -const std::string FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable"; - -static bool CheckPermission(const string &permission) -{ - Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); - return Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission) == - Security::AccessToken::PermissionState::PERMISSION_GRANTED; -} - -static bool CheckFileManagerFullMountEnable() -{ - char value[] = "false"; - int retSystem = GetParameter(FULL_MOUNT_ENABLE_PARAMETER.c_str(), "false", value, sizeof(value)); - if (retSystem > 0 && !strcmp(value, "true")) { - LOGE("The full mount enable parameter is true"); - return true; - } - LOGD("The full mount enable parameter is false"); - return false; -} -} // namespace - static napi_value GetErrData(napi_env env, deque &errorResults) { napi_value res = nullptr; @@ -154,16 +129,6 @@ static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector< napi_value PersistPermission(napi_env env, napi_callback_info info) { - if (!CheckFileManagerFullMountEnable()) { - LOGE("The device doesn't support this api"); - NError(E_DEVICENOTSUPPORT).ThrowErr(env); - return nullptr; - } - if (!CheckPermission(FILE_ACCESS_PERMISSION)) { - LOGE("PersistPermission has not ohos permission!"); - NError(E_PERMISSION).ThrowErr(env); - return nullptr; - } NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE)) { LOGE("PersistPermission Number of arguments unmatched"); @@ -198,16 +163,6 @@ napi_value PersistPermission(napi_env env, napi_callback_info info) napi_value RevokePermission(napi_env env, napi_callback_info info) { - if (!CheckFileManagerFullMountEnable()) { - LOGE("The device doesn't support this api"); - NError(E_DEVICENOTSUPPORT).ThrowErr(env); - return nullptr; - } - if (!CheckPermission(FILE_ACCESS_PERMISSION)) { - LOGE("RevokePermission has not ohos permission!"); - NError(E_PERMISSION).ThrowErr(env); - return nullptr; - } NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE)) { LOGE("RevokePermission Number of arguments unmatched"); @@ -242,16 +197,6 @@ napi_value RevokePermission(napi_env env, napi_callback_info info) napi_value ActivatePermission(napi_env env, napi_callback_info info) { - if (!CheckFileManagerFullMountEnable()) { - LOGE("The device doesn't support this api"); - NError(E_DEVICENOTSUPPORT).ThrowErr(env); - return nullptr; - } - if (!CheckPermission(FILE_ACCESS_PERMISSION)) { - LOGE("PersistPermission has not ohos permission!"); - NError(E_PERMISSION).ThrowErr(env); - return nullptr; - } NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE)) { LOGE("ActivatePermission Number of arguments unmatched"); @@ -286,16 +231,6 @@ napi_value ActivatePermission(napi_env env, napi_callback_info info) napi_value DeactivatePermission(napi_env env, napi_callback_info info) { - if (!CheckFileManagerFullMountEnable()) { - LOGE("The device doesn't support this api"); - NError(E_DEVICENOTSUPPORT).ThrowErr(env); - return nullptr; - } - if (!CheckPermission(FILE_ACCESS_PERMISSION)) { - LOGE("PersistPermission has not ohos permission!"); - NError(E_PERMISSION).ThrowErr(env); - return nullptr; - } NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE)) { LOGE("DeactivatePermission Number of arguments unmatched"); @@ -330,16 +265,6 @@ napi_value DeactivatePermission(napi_env env, napi_callback_info info) napi_value CheckPersistentPermission(napi_env env, napi_callback_info info) { - if (!CheckFileManagerFullMountEnable()) { - LOGE("The device doesn't support this api"); - NError(E_DEVICENOTSUPPORT).ThrowErr(env); - return nullptr; - } - if (!CheckPermission(FILE_ACCESS_PERMISSION)) { - LOGE("PersistPermission has not ohos permission!"); - NError(E_PERMISSION).ThrowErr(env); - return nullptr; - } NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE)) { LOGE("ActivatePermission Number of arguments unmatched"); diff --git a/interfaces/kits/ndk/fileshare/src/BUILD.gn b/interfaces/kits/ndk/fileshare/src/BUILD.gn index 7ddf573550075d475af04958fab39847223de05f..6a8dbd2d3a755cc2a3b5a1eb9b63312e65cbc696 100644 --- a/interfaces/kits/ndk/fileshare/src/BUILD.gn +++ b/interfaces/kits/ndk/fileshare/src/BUILD.gn @@ -43,6 +43,9 @@ ohos_shared_library("ohfileshare") { "init:libbegetutil", "ipc:ipc_core", ] + if (app_file_service_feature_sandbox_manarer) { + external_deps += [ "sandbox_manager:libsandbox_manager_sdk" ] + } output_extension = "so" relative_install_dir = "ndk" part_name = "app_file_service" diff --git a/test/fuzztest/fileshare_fuzzer/BUILD.gn b/test/fuzztest/fileshare_fuzzer/BUILD.gn index d9f20cbec624f1cb86f0d3463106880ec3e26c33..53a2cad2503b1e78d26a9b58a7770c806a5d914f 100644 --- a/test/fuzztest/fileshare_fuzzer/BUILD.gn +++ b/test/fuzztest/fileshare_fuzzer/BUILD.gn @@ -40,10 +40,12 @@ ohos_fuzztest("FileShareFuzzTest") { deps = [ "${app_file_service_path}/interfaces/innerkits/native:fileshare_native", ] - external_deps = [ "access_token:libaccesstoken_sdk", "access_token:libnativetoken", "access_token:libtoken_setproc", ] + if (app_file_service_feature_sandbox_manarer) { + external_deps += [ "sandbox_manager:libsandbox_manager_sdk" ] + } } diff --git a/test/unittest/file_permission_native/src/file_permission_test.cpp b/test/unittest/file_permission_native/src/file_permission_test.cpp index 0347b1ada4ffacb46cdac9213b1de45cd5160898..ecf9281e2cd92f60f1dd1484cf75e29fff2a85a7 100644 --- a/test/unittest/file_permission_native/src/file_permission_test.cpp +++ b/test/unittest/file_permission_native/src/file_permission_test.cpp @@ -464,7 +464,7 @@ HWTEST_F(FilePermissionTest, GetPathByPermission_test_0001, testing::ext::TestSi GTEST_LOG_(INFO) << "FileShareTest-begin GetPathByPermission_test_0001"; std::string PermissionName = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; std::string sandboxPath = "/storage/Users/currentUser/Download"; - std::string path = FilePermission::GetPathByPermission(PermissionName); + std::string path = FilePermission::GetPathByPermission("currentUser", PermissionName); if (CheckFileManagerFullMountEnable()) { EXPECT_EQ(path, sandboxPath); } else { @@ -485,7 +485,7 @@ HWTEST_F(FilePermissionTest, GetPathByPermission_test_0002, testing::ext::TestSi GTEST_LOG_(INFO) << "FileShareTest-begin GetPathByPermission_test_0002"; std::string PermissionName = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; std::string sandboxPath = "/storage/Users/currentUser/Desktop"; - std::string path = FilePermission::GetPathByPermission(PermissionName); + std::string path = FilePermission::GetPathByPermission("currentUser", PermissionName); if (CheckFileManagerFullMountEnable()) { EXPECT_EQ(path, sandboxPath); } else { @@ -506,7 +506,7 @@ HWTEST_F(FilePermissionTest, GetPathByPermission_test_0003, testing::ext::TestSi GTEST_LOG_(INFO) << "FileShareTest-begin GetPathByPermission_test_0003"; std::string PermissionName = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; std::string sandboxPath = "/storage/Users/currentUser/Documents"; - std::string path = FilePermission::GetPathByPermission(PermissionName); + std::string path = FilePermission::GetPathByPermission("currentUser", PermissionName); if (CheckFileManagerFullMountEnable()) { EXPECT_EQ(path, sandboxPath); } else { diff --git a/test/unittest/file_share_native/file_share_test.cpp b/test/unittest/file_share_native/file_share_test.cpp index dea3a408a889cb8fa84c0f756442c9f605cc9ea4..2cd55d06f1b89b62f0aeac7ebe47076cce8be7ad 100644 --- a/test/unittest/file_share_native/file_share_test.cpp +++ b/test/unittest/file_share_native/file_share_test.cpp @@ -25,7 +25,6 @@ #include "access_token_error.h" #include "accesstoken_kit.h" -#include "file_permission.h" #include "file_share.h" #include "ipc_skeleton.h" #include "log.h" diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index d02975a69b832afa24b4a19ca48117819f8c57f8..14aef7cbeaae42ed75cb9b9d9cdc2f6129c0daa2 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -54,10 +54,6 @@ static bool CheckFileManagerFullMountEnable() } namespace OHOS::AppFileService::ModuleFileUri { - const string PATH_SHARE = "/data/storage/el2/share"; - const string MODE_RW = "/rw/"; - const string MODE_R = "/r/"; - const int E_OK = 0; class FileUriTest : public testing::Test { public: @@ -159,7 +155,7 @@ namespace OHOS::AppFileService::ModuleFileUri { if (CheckFileManagerFullMountEnable()) { EXPECT_EQ(path, fileStrRealPath); } else { - EXPECT_EQ(path, PATH_SHARE + MODE_R + fileStrPath); + EXPECT_EQ(path, fileStrRealPath); }; GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0001"; } @@ -178,7 +174,7 @@ namespace OHOS::AppFileService::ModuleFileUri { string fileStr = "/data/storage/el2/base/files/test.txt"; string bundleB = "com.demo.b"; string uri = "file://" + bundleB + fileStr; - string rltStr = PATH_SHARE + MODE_R + bundleB + fileStr; + string rltStr = "/storage/Users/currentUser/appdata/el2/base/" + bundleB + "/files/test.txt"; FileUri fileUri(uri); string path = fileUri.GetRealPath(); EXPECT_EQ(path, rltStr); @@ -204,23 +200,12 @@ namespace OHOS::AppFileService::ModuleFileUri { string actStr = "/data/storage/el2/base/files/test.txt"; string uri = "file://" + bundleB + actStr; - uint32_t tokenId = AccessTokenKit::GetHapTokenID(uid, BUNDLE_A, 0); - - int32_t flag = 3; - vector uriList(1, uri); - vector retList; - int32_t ret = FileShare::CreateShareFile(uriList, tokenId, flag, retList); - EXPECT_EQ(ret, E_OK); - string rltStr = PATH_SHARE + MODE_R + bundleB + actStr; + string rltStr = "/storage/Users/currentUser/appdata/el2/base/" + bundleB + "/files/test.txt"; FileUri fileUri(uri); string path = fileUri.GetRealPath(); EXPECT_EQ(path, rltStr); - vector sharePathList; - sharePathList.push_back(uri); - ret = FileShare::DeleteShareFile(tokenId, sharePathList); - EXPECT_EQ(ret, E_OK); close(fd); GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0003"; } 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 307dfb9f7127aea1c8c1c382686238355bd29106..a50f02f0572b824dd8e4309bce54f12c75ebfeec 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 @@ -104,8 +104,7 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_002, TestSize.Level1) EXPECT_EQ(ret, ERR_OK); GTEST_LOG_(INFO) << result; if (!CheckFileManagerFullMountEnable()) { - const char filePath[] = - "/data/storage/el2/share/r/docs/storage/Users/currentUser/Documents/GetPathFromUri002.txt"; + const char filePath[] = "/storage/Users/currentUser/Documents/GetPathFromUri002.txt"; EXPECT_EQ(strcmp(result, filePath), 0); } else { const char filePath[] = "/storage/Users/currentUser/Documents/GetPathFromUri002.txt"; @@ -125,7 +124,7 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_003, TestSize.Level1) { GTEST_LOG_(INFO) << "get_path_from_uri_test_003 start"; const char fileUri[] = "file://demoa/data/storage/el2/base/files/GetPathFromUri003.txt"; - std::string filePathStr = "/data/storage/el2/share/r/demoa/data/storage/el2/base/files/GetPathFromUri003.txt"; + std::string filePathStr = "/storage/Users/currentUser/appdata/el2/base/demoa/files/GetPathFromUri003.txt"; const char *filePath = filePathStr.c_str(); char *result = nullptr; unsigned int length = strlen(fileUri); @@ -152,8 +151,7 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_004, TestSize.Level1) fileUriStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri004.txt"; fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; const char *fileUri = fileUriStr.c_str(); - std::string filePathStr = - "/data/storage/el2/share/r/" + BUNDLE_A + "/data/storage/el2/distributedfiles/.remote_share/"; + std::string filePathStr = "/storage/Users/currentUser/appdata/el2/distributedfiles/" + BUNDLE_A + "/.remote_share/"; filePathStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri004.txt"; const char *filePath = filePathStr.c_str(); char *result = nullptr; @@ -178,9 +176,8 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_005, TestSize.Level1) { GTEST_LOG_(INFO) << "get_path_from_uri_test_005 start"; std::string fileUriStr = "file://docs/storage/Users/currentUser/Documents/GetPathFromUri005.txt"; - fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; const char *fileUri = fileUriStr.c_str(); - const char filePath[] = "/data/storage/el2/share/r/docs/storage/Users/currentUser/Documents/GetPathFromUri005.txt"; + const char filePath[] = "/storage/Users/currentUser/Documents/GetPathFromUri005.txt"; char *result = nullptr; unsigned int length = fileUriStr.size(); FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result); @@ -207,8 +204,8 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_006, TestSize.Level1) fileUriStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri006.txt"; fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; const char *fileUri = fileUriStr.c_str(); - std::string filePathUri = "/data/storage/el2/share/r/" + bundleB; - filePathUri += "/data/storage/el2/distributedfiles/.remote_share/"; + std::string filePathUri = "/storage/Users/currentUser/appdata/el2/distributedfiles/" + bundleB; + filePathUri += "/.remote_share/"; filePathUri += "data/storage/el2/base/haps/entry/files/GetPathFromUri006.txt"; const char *filePath = filePathUri.c_str(); char *result = nullptr; @@ -232,8 +229,8 @@ HWTEST_F(NDKFileUriTest, get_path_from_uri_test_006, TestSize.Level1) HWTEST_F(NDKFileUriTest, get_uri_from_path_test_001, TestSize.Level1) { GTEST_LOG_(INFO) << "get_uri_from_path_test_001 start"; - const char filePath[] = "storage/Users/currentUser/Documents/GetPathFromUri001.txt"; - const char fileUri[] = "file://com.example.filesharea/storage/Users/currentUser/Documents/GetPathFromUri001.txt"; + const char filePath[] = "/storage/Users/currentUser/Documents/GetPathFromUri001.txt"; + const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetPathFromUri001.txt"; char *result = nullptr; unsigned int length = strlen(filePath); FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result); @@ -246,6 +243,81 @@ HWTEST_F(NDKFileUriTest, get_uri_from_path_test_001, TestSize.Level1) GTEST_LOG_(INFO) << "get_uri_from_path_test_001 end"; } +/** + * @tc.number: get_uri_from_path_test_002 + * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri + * @tc.desc: Set path and get uri + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_uri_from_path_test_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_uri_from_path_test_002 start"; + const char filePath[] = + "/storage/Users/currentUser/appdata/el2/base/com.example.filesharea/haps/entry/files/GetPathFromUri001.txt"; + const char fileUri[] = "file://com.example.filesharea/data/storage/el2/base/haps/entry/files/GetPathFromUri001.txt"; + char *result = nullptr; + unsigned int length = strlen(filePath); + FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result); + EXPECT_EQ(ret, ERR_OK); + if (result != nullptr) { + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, fileUri), 0); + FreeResult(&result); + } + GTEST_LOG_(INFO) << "get_uri_from_path_test_002 end"; +} + +/** + * @tc.number: get_uri_from_path_test_003 + * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri + * @tc.desc: Set path and get uri + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_uri_from_path_test_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_uri_from_path_test_003 start"; + const char filePath[] = + "/storage/Users/currentUser/appdatas/el2/base/com.example.filesharea/haps/entry/files/GetPathFromUri001.txt"; + const char fileUri[] = + "file://docs/storage/Users/currentUser/appdatas/el2/base/com.example.filesharea/haps/entry/files/" + "GetPathFromUri001.txt"; + char *result = nullptr; + unsigned int length = strlen(filePath); + FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result); + EXPECT_EQ(ret, ERR_OK); + if (result != nullptr) { + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, fileUri), 0); + FreeResult(&result); + } + GTEST_LOG_(INFO) << "get_uri_from_path_test_003 end"; +} + +/** + * @tc.number: get_uri_from_path_test_004 + * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri + * @tc.desc: Set path and get uri + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_uri_from_path_test_004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_uri_from_path_test_004 start"; + const char filePath[] = + "/storage/Users/currentUser/appdatas/el2/base/com.example.filesharea/haps/entry/files/GetPathFromUri001.txt"; + const char fileUri[] = + "file://docs/storage/Users/currentUser/appdatas/el2/base/com.example.filesharea/haps/entry/files/" + "GetPathFromUri001.txt"; + char *result = nullptr; + unsigned int length = strlen(filePath); + FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result); + EXPECT_EQ(ret, ERR_OK); + if (result != nullptr) { + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, fileUri), 0); + FreeResult(&result); + } + GTEST_LOG_(INFO) << "get_uri_from_path_test_004 end"; +} /** * @tc.number: get_full_directory_uri_test_001 * @tc.name: Test function of OH_FileUri_GetFullDirectoryUri() interface for unknown path