From 9f0f43a74b2dcaf2e2c78eb08cd2b72a1daee6e7 Mon Sep 17 00:00:00 2001 From: cuiruibin Date: Wed, 19 Jun 2024 10:38:57 +0800 Subject: [PATCH] =?UTF-8?q?innerApi=E6=8E=A5=E5=8F=A3=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E6=8C=81=E4=B9=85=E5=8C=96=E6=8E=88=E6=9D=83=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=20Signed-off-by:=20cuiruibin=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_file_service.gni | 6 +- bundle.json | 1 + interfaces/common/src/common_func.cpp | 63 ++++++++-- interfaces/innerkits/native/BUILD.gn | 9 +- .../file_share/include/file_permission.h | 17 +-- .../native/file_share/src/file_permission.cpp | 51 ++++---- .../native/file_uri/include/file_uri.h | 1 + .../native/file_uri/src/file_uri.cpp | 100 +++++++++++---- .../include/remote_file_share.h | 8 +- .../src/remote_file_share.cpp | 117 ++++++++---------- interfaces/kits/js/BUILD.gn | 7 +- .../kits/js/file_share/grant_permissions.cpp | 75 ----------- interfaces/kits/ndk/fileshare/src/BUILD.gn | 3 + test/fuzztest/fileshare_fuzzer/BUILD.gn | 4 +- test/unittest/file_permission_native/BUILD.gn | 5 +- .../src/file_permission_test.cpp | 6 +- .../file_share_native/file_share_test.cpp | 1 - .../file_uri_native/file_uri_test.cpp | 21 +--- .../file_uri_ndk_test/file_uri_ndk_test.cpp | 94 ++++++++++++-- 19 files changed, 328 insertions(+), 261 deletions(-) diff --git a/app_file_service.gni b/app_file_service.gni index 8a4903504..ba957f750 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 4cb7192e5..43e804932 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 8cdb66b62..ea0221226 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 6ae1aa14c..3391007fb 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 492c2d173..70dfef23f 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, @@ -85,10 +84,6 @@ private: deque &errorResults); static vector GetPathPolicyInfoFromUriPolicyInfo(const vector &uriPolicies, vector &errorResults); - static int32_t CheckUriPersistentPermission(uint64_t tokenId, - const vector &uriPolicies, - vector &errorResults); -#endif }; } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/innerkits/native/file_share/src/file_permission.cpp b/interfaces/innerkits/native/file_share/src/file_permission.cpp index a0f6d9164..23cf3d745 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) { @@ -165,8 +165,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}; @@ -186,7 +186,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 { @@ -198,7 +198,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; @@ -209,7 +209,7 @@ static bool IsFileManagerUri(const string &pathStr) return pathStr.find(DOWNLOAD_PATH) == 0 || pathStr.find(DESKTOP_PATH) == 0 || pathStr.find(DOCUMENTS_PATH) == 0; } -static bool CheckFileManagerUriPermission(uint64_t providerTokenId, const string &pathStr) +static bool CheckFileManagerUriPermission(uint32_t providerTokenId, const string &pathStr) { return (IsFileManagerUri(pathStr) && CheckPermission(providerTokenId, FILE_ACCESS_MANAGER_PERMISSION)) || (pathStr.find(DOWNLOAD_PATH) == 0 && CheckPermission(providerTokenId, READ_WRITE_DOWNLOAD_PERMISSION)) || @@ -217,7 +217,7 @@ static bool CheckFileManagerUriPermission(uint64_t providerTokenId, const string (pathStr.find(DOCUMENTS_PATH) == 0 && CheckPermission(providerTokenId, READ_WRITE_DOCUMENTS_PERMISSION)); } -int32_t FilePermission::CheckUriPersistentPermission(uint64_t tokenId, +int32_t FilePermission::CheckUriPersistentPermission(uint32_t tokenId, const vector &uriPolicies, vector &errorResults) { @@ -237,13 +237,11 @@ int32_t FilePermission::CheckUriPersistentPermission(uint64_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); @@ -251,7 +249,6 @@ int32_t FilePermission::PersistPermission(const vector &uriPolici if (errorCode == EPERM) { ParseErrorResults(resultCodes, pathPolicies, errorResults); } -#endif return errorCode; } @@ -259,7 +256,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); @@ -267,7 +263,6 @@ int32_t FilePermission::RevokePermission(const vector &uriPolicie if (errorCode == EPERM) { ParseErrorResults(resultCodes, pathPolicies, errorResults); } -#endif return errorCode; } @@ -275,7 +270,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); @@ -283,7 +277,6 @@ int32_t FilePermission::ActivatePermission(const vector &uriPolic if (errorCode == EPERM) { ParseErrorResults(resultCodes, pathPolicies, errorResults); } -#endif return errorCode; } @@ -291,7 +284,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); @@ -299,27 +291,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 8f175da91..8ae2f8a11 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -27,6 +27,7 @@ public: std::string GetName(); std::string GetPath(); 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 2973a554a..7dc157b24 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 "sandbox_helper.h" +#include "os_account_manager.h" #include "parameter.h" +#include "sandbox_helper.h" using namespace std; namespace OHOS { @@ -38,17 +39,36 @@ 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 = ""; +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 +86,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()); @@ -83,19 +115,41 @@ 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) { 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) { realPath = PATH_SHARE + MODE_RW + bundleName + sandboxPath; if (access(realPath.c_str(), F_OK) != 0) { realPath = PATH_SHARE + MODE_R + bundleName + sandboxPath; } + return realPath; + } + if (BUNDLE_NAME == "") { + BUNDLE_NAME = CommonFunc::GetSelfBundleName(); + } + if (((bundleName != "") && (bundleName != BUNDLE_NAME)) || 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; } @@ -145,10 +199,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 c151f991b..6a87ec628 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 dc97b33fd..a3de9dad3 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 { @@ -40,27 +40,29 @@ namespace ModuleRemoteFileShare { using namespace OHOS::DistributedHardware; #endif namespace { - 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 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 SHAER_PATH_HEAD = "/mnt/hmdfs/"; - const std::string SHAER_PATH_MID = "/account/merge_view/services/"; - const std::string LOWER_SHARE_PATH_HEAD = "/mnt/hmdfs/"; - 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::string MEDIA_AUTHORITY = "media"; - const std::string FILE_MANAGER_AUTHORITY = "docs"; - const std::string PACKAGE_NAME = "get_dfs_uri_from_local"; - const std::string NETWORK_PARA = "?networkid="; - const std::string MEDIA_BUNDLE_NAME = "com.ohos.medialibrary.medialibrarydata"; -} - -#define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl) +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 BACKFLASH = "/"; +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 SHAER_PATH_HEAD = "/mnt/hmdfs/"; +const std::string SHAER_PATH_MID = "/account/merge_view/services/"; +const std::string LOWER_SHARE_PATH_HEAD = "/mnt/hmdfs/"; +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::string MEDIA_AUTHORITY = "media"; +const std::string FILE_MANAGER_AUTHORITY = "docs"; +const std::string PACKAGE_NAME = "get_dfs_uri_from_local"; +const std::string NETWORK_PARA = "?networkid="; +const std::string MEDIA_BUNDLE_NAME = "com.ohos.medialibrary.medialibrarydata"; +const std::string FILE_MANAGER_BUNDLE_NAME = "com.ohos.UserFile.ExternalFileManager"; +} // namespace + +#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 { @@ -173,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) { @@ -198,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; @@ -263,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; } @@ -291,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()); @@ -310,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) { @@ -324,7 +332,7 @@ static std::string GetLocalNetworkId() if (ret != 0 || networkId.empty()) { return ""; } - #endif +#endif return networkId; } @@ -334,26 +342,13 @@ 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 + 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; -} - int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int32_t &userId, struct HmdfsUriInfo &hui) { Uri uri(uriStr); @@ -368,15 +363,13 @@ 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; + bundleName = FILE_MANAGER_BUNDLE_NAME; } 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; @@ -413,7 +406,7 @@ int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector &uri LOGE("Open share path failed with %{public}d", errno); return errno; } - std::string networkId = NETWORK_PARA + GetLocalNetworkId(); + std::string networkId = GetLocalNetworkId(); for (auto &uriStr : uriList) { Uri uri(uriStr); std::string bundleName = uri.GetAuthority(); @@ -428,15 +421,11 @@ int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector &uri } if (bundleName == FILE_MANAGER_AUTHORITY) { - HmdfsUriInfo dfsUriInfo; - (void)SetPublicDirHmdfsInfo(physicalPath, uriStr, dfsUriInfo, networkId); - uriToDfsUriMaps.insert({uriStr, dfsUriInfo}); - LOGD("GetDfsUriFromLocal successfully"); - continue; + bundleName = FILE_MANAGER_BUNDLE_NAME; } 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 d1c52b8a8..bbd2aaa47 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 ba2386395..f8c3448c5 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 7ddf57355..6a8dbd2d3 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 d9f20cbec..53a2cad25 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/BUILD.gn b/test/unittest/file_permission_native/BUILD.gn index 4a82a959a..457d4d8c8 100644 --- a/test/unittest/file_permission_native/BUILD.gn +++ b/test/unittest/file_permission_native/BUILD.gn @@ -12,7 +12,7 @@ # limitations under the License. import("//build/test.gni") - +import("//foundation/filemanagement/app_file_service/app_file_service.gni") ohos_unittest("file_permission_test") { branch_protector_ret = "pac_ret" sanitize = { @@ -44,6 +44,9 @@ ohos_unittest("file_permission_test") { "ipc:ipc_core", "samgr:samgr_proxy", ] + if (app_file_service_feature_sandbox_manarer) { + external_deps += [ "sandbox_manager:libsandbox_manager_sdk" ] + } deps = [ "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", 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 08b636101..5938cc845 100644 --- a/test/unittest/file_permission_native/src/file_permission_test.cpp +++ b/test/unittest/file_permission_native/src/file_permission_test.cpp @@ -427,7 +427,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 { @@ -448,7 +448,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 { @@ -469,7 +469,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 dea3a408a..2cd55d06f 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 d02975a69..14aef7cbe 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 307dfb9f7..a50f02f05 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 -- Gitee