From e0f2209f399bd6476f624c3393f1f48c288fc92f Mon Sep 17 00:00:00 2001 From: zkx Date: Fri, 21 Apr 2023 16:49:52 +0800 Subject: [PATCH] Add Native FileUri Class. Signed-off-by: zkx --- BUILD.gn | 14 +- bundle.json | 30 +-- .../innerkits/native/file_share/BUILD.gn | 1 + .../native/file_share/include/file_share.h | 34 +--- interfaces/innerkits/native/file_uri/BUILD.gn | 42 +++++ .../native/file_uri/include/file_uri.h | 43 +++++ .../native/file_uri/src/file_uri.cpp | 178 ++++++++++++++++++ interfaces/kits/js/BUILD.gn | 2 + .../kits/js/file_share/grant_uri_permission.h | 11 +- .../kits/js/file_uri/get_uri_from_path.cpp | 2 +- .../kits/js/file_uri/get_uri_from_path.h | 8 +- utils/include/common/common_const.h | 78 ++++++++ 12 files changed, 379 insertions(+), 64 deletions(-) create mode 100644 interfaces/innerkits/native/file_uri/BUILD.gn create mode 100644 interfaces/innerkits/native/file_uri/include/file_uri.h create mode 100644 interfaces/innerkits/native/file_uri/src/file_uri.cpp create mode 100644 utils/include/common/common_const.h diff --git a/BUILD.gn b/BUILD.gn index c51710244..20fc28927 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -13,11 +13,19 @@ import("//build/ohos.gni") -group("libremotefileshare") { +group("app_file_service_js_sdk") { + deps = [ + "interfaces/kits/js:fileshare", + "interfaces/kits/js:fileuri", + "interfaces/kits/js:remotefileshare", + ] +} + +group("app_file_service_native_sdk") { deps = [ "interfaces/innerkits/native/file_share:fileshare_native", - "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", - "//foundation/filemanagement/app_file_service/interfaces/kits/js:remotefileshare", + "interfaces/innerkits/native/file_uri:fileuri_native", + "interfaces/innerkits/remote_file_share/native:remote_file_share_native", ] } diff --git a/bundle.json b/bundle.json index 4f1a1d4c8..530396a8b 100644 --- a/bundle.json +++ b/bundle.json @@ -26,9 +26,8 @@ "group_type": { "base_group": [], "fwk_group": [ - "//foundation/filemanagement/app_file_service:libremotefileshare", - "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare", - "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri", + "//foundation/filemanagement/app_file_service:app_file_service_js_sdk", + "//foundation/filemanagement/app_file_service:app_file_service_native_sdk", "//foundation/filemanagement/app_file_service/interfaces/kits/js:backup" ], "service_group": [ @@ -42,19 +41,19 @@ { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share:fileshare_native", "header": { - "header_files": [ - "file_share.h" - ], - "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share/include" + "header_files": [ + "file_share.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share/include" } }, { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", "header": { - "header_files": [ - "remote_file_share.h" - ], - "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native" + "header_files": [ + "remote_file_share.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native" } }, { @@ -71,6 +70,15 @@ "impl/i_service_reverse.h" ] } + }, + { + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri:fileuri_native", + "header": { + "header_files": [ + "file_uri.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri/include" + } } ], "test": [ diff --git a/interfaces/innerkits/native/file_share/BUILD.gn b/interfaces/innerkits/native/file_share/BUILD.gn index 6f9b24ff6..90d88e4a0 100644 --- a/interfaces/innerkits/native/file_share/BUILD.gn +++ b/interfaces/innerkits/native/file_share/BUILD.gn @@ -18,6 +18,7 @@ config("file_share_config") { include_dirs = [ "include", "../common", + "../../../../utils/include/common", ] } diff --git a/interfaces/innerkits/native/file_share/include/file_share.h b/interfaces/innerkits/native/file_share/include/file_share.h index 61117c606..116994d45 100644 --- a/interfaces/innerkits/native/file_share/include/file_share.h +++ b/interfaces/innerkits/native/file_share/include/file_share.h @@ -16,8 +16,9 @@ #ifndef APP_FILE_SERVICE_FILE_SHARE #define APP_FILE_SERVICE_FILE_SHARE -#include #include + +#include "common_const.h" #include "want.h" namespace OHOS { @@ -31,37 +32,6 @@ using namespace std; enum SHARE_FILE_TYPE { DIR_TYPE = 0, FILE_TYPE }; -namespace { -const string FILE_SCHEME = "file"; -const string DATA_STORAGE_PATH = "/data/storage/"; -const string PACKAGE_NAME_FLAG = ""; -const string CURRENT_USER_ID_FLAG = ""; -const string DATA_APP_EL2_PATH = "/data/service/el2/"; -const string SHARE_R_PATH = "/r/"; -const string SHARE_RW_PATH = "/rw/"; -const string SHARE_PATH = "/share/"; - -const vector SANDBOX_PATH = { - "/data/storage/el1/bundle", - "/data/storage/el2/base", - "/data/storage/el1/database", - "/data/storage/el2/database", - "/data/storage/el1/base", - "/data/storage/ark-cache", - "/data/storage/ark-profile" -}; - -const vector LOWER_PATH = { - "/data/app/el1/bundle/public/", - "/data/app/el2//base/", - "/data/app/el1//database/", - "/data/app/el2//database/", - "/data/app/el1//base/", - "/data/local/ark-cache/", - "/data/local/ark-profile//" -}; -} - class FileShare { public: FileShare() {} diff --git a/interfaces/innerkits/native/file_uri/BUILD.gn b/interfaces/innerkits/native/file_uri/BUILD.gn new file mode 100644 index 000000000..780328171 --- /dev/null +++ b/interfaces/innerkits/native/file_uri/BUILD.gn @@ -0,0 +1,42 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +config("file_uri_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "../common", + "../../../../utils/include/common", + ] +} + +ohos_shared_library("fileuri_native") { + sources = [ "src/file_uri.cpp" ] + + public_configs = [ ":file_uri_config" ] + + external_deps = [ + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h new file mode 100644 index 000000000..20fb816f3 --- /dev/null +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef APP_FILE_SERVICE_FILE_URI_H +#define APP_FILE_SERVICE_FILE_URI_H + +#include "common_const.h" +#include "uri.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { +using namespace std; + +class FileUri : public OHOS::Uri { + string fileId_ = DEFAULT_VALUE; + string networkId_ = DEFAULT_VALUE; +public: + explicit FileUri(const string &uri): Uri(uri) {} + bool IsMediaUri(); + string GetNetworkId(); + static int32_t Open(const string &uri, uint32_t mode); + static string GetUriFromPath(const string &path); + static string GetUriFromPath(const string &path, string &bundleName); + ~FileUri() = default; +}; +} // namespace ModuleFileUri +} // namespace AppFileService +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp new file mode 100644 index 000000000..60c1e1ee0 --- /dev/null +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "file_uri.h" + +#include +#include + +#include "bundle_info.h" +#include "bundle_mgr_proxy.h" +#include "log.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { + +bool FileUri::IsMediaUri() +{ + string scheme = this->GetScheme(); + if (scheme == FILE_SCHEME) { + return this->GetAuthority() == MEDIA_BUNDLE_NAME; + } else if (scheme == DATA_SHARE_SCHEME) { + string path = this->GetPath(); + size_t len = MEDIA.length(); + if (path.length() > len) { + return path.substr(0, len) == MEDIA; + } + } + return false; +} + +static int32_t GetFragmentArgs(string &fragment, unordered_map &map) +{ + string keyValue; + size_t index = 0; + for (string remain = fragment.substr(index); index != string::npos;) { + index = remain.find('&', 1); + if (index == string::npos) { + keyValue = remain.substr(1); + } else { + keyValue = remain.substr(1, index - 1); + remain = remain.substr(index); + } + + size_t splitIndex = keyValue.find('='); + if (splitIndex == string::npos) { + return -EINVAL; + } + map[keyValue.substr(0, splitIndex)] = keyValue.substr(splitIndex + 1); + } + return 0; +} + +string FileUri::GetNetworkId() +{ + if (networkId_ != DEFAULT_VALUE) { + return networkId_; + } + string fragment = FRAGMENT_SYMBOLS + this->GetFragment(); + unordered_map map; + int32_t ret = GetFragmentArgs(fragment, map); + if (ret < 0) { + LOGE("Failed to GetFragmentArgs, fragment = %{private}s", fragment.c_str()); + return ""; + } + networkId_ = map.find(NETWORK_ID_TAG) != map.end() ? map[NETWORK_ID_TAG] : ""; + return networkId_; +} + +static sptr GetBundleMgrProxy() +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + LOGE("fail to get system ability mgr."); + return nullptr; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + LOGE("fail to get bundle manager proxy."); + return nullptr; + } + + return iface_cast(remoteObject); +} + +static string GetBundleNameSelf() +{ + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + LOGE("Bundle mgr proxy is null ptr."); + return nullptr; + } + OHOS::AppExecFwk::BundleInfo bundleInfo; + auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); + if (ret != ERR_OK) { + LOGE("Failed to get bundleNameSelf."); + return nullptr; + } + + return bundleInfo.name; +} + +static string GetFilePath(string &path, const string &bundleName, const uint32_t &mode) +{ + if (bundleName != GetBundleNameSelf()) { + if ((mode & O_WRONLY) == O_WRONLY || (mode & O_RDWR) == O_RDWR) { + path = PATH_SHARE + SHARE_RW_PATH + bundleName + path; + } else { + path = PATH_SHARE + SHARE_R_PATH + bundleName + path; + } + } + return path; +} + +int32_t FileUri::Open(const string &uri, uint32_t mode) +{ + FileUri fileUri(uri); + string scheme = fileUri.GetScheme(); + if (scheme != FILE_SCHEME) { + LOGE("Failed to Open, uri = %{private}s", uri.c_str()); + return -EINVAL; + } + string path = fileUri.GetPath(); + if (path.empty()) { + LOGE("Failed to GetPath, uri = %{private}s", uri.c_str()); + return -EINVAL; + } + string bundleName = fileUri.GetAuthority(); + string filePath = GetFilePath(path, bundleName, mode); + LOGI("Open: filePath is %{private}s", filePath.c_str()); + int32_t fd = open(filePath.c_str(), mode); + return fd; +} + +string FileUri::GetUriFromPath(const string &path) +{ + if (path.empty() || path.find_first_of(PATH_SYMBOL) != 0) { + LOGE("Failed to GetUriFromPath, path = %{private}s", path.c_str()); + return ""; + } + string bundleName = GetBundleNameSelf(); + LOGI("GetUriFromPath: bundleName = %{private}s", bundleName.c_str()); + return FILE_SCHEME + SCHEME_SYMBOLS + bundleName + path; +} + +string FileUri::GetUriFromPath(const string &path, string &bundleName) +{ + if (path.empty() || path.find_first_of(PATH_SYMBOL) != 0) { + LOGE("Failed to GetUriFromPath, path = %{private}s", path.c_str()); + return ""; + } + LOGI("GetUriFromPath: bundleName = %{private}s", bundleName.c_str()); + return FILE_SCHEME + SCHEME_SYMBOLS + bundleName + path; +} + +} // namespace ModuleFileUri +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index b64a78556..0b3e37383 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -40,6 +40,7 @@ ohos_shared_library("fileshare") { include_dirs = [ "//foundation/filemanagement/app_file_service/interfaces/kits/js", "//foundation/filemanagement/app_file_service/interfaces/kits/js/common", + "../../../utils/include/common", ] sources = [ @@ -72,6 +73,7 @@ ohos_shared_library("fileuri") { include_dirs = [ "//foundation/filemanagement/app_file_service/interfaces/kits/js", "//foundation/filemanagement/app_file_service/interfaces/kits/js/common", + "../../../utils/include/common", ] sources = [ diff --git a/interfaces/kits/js/file_share/grant_uri_permission.h b/interfaces/kits/js/file_share/grant_uri_permission.h index 65f9890dc..9af6d2172 100644 --- a/interfaces/kits/js/file_share/grant_uri_permission.h +++ b/interfaces/kits/js/file_share/grant_uri_permission.h @@ -16,6 +16,7 @@ #ifndef GRANT_URI_PERMISSION_H #define GRANT_URI_PERMISSION_H +#include "common_const.h" #include "filemgmt_libn.h" #include "iremote_broker.h" @@ -23,16 +24,6 @@ namespace OHOS { namespace AppFileService { namespace ModuleFileShare { -using namespace std; - -const string MEDIA_GRANT_URI_PERMISSION = "datashare:///media/file_operation/bundle_permission_insert_operation"; -const string MEDIALIBRARY_DATA_URI = "datashare:///media"; -const string MEDIA_FILEMODE_READONLY = "r"; -const string GRANT_URI_NAME = "file_share_grant_uri_permission"; -const string PERMISSION_BUNDLE_NAME = "bundle_name"; -const string PERMISSION_FILE_ID = "file_id"; -const string PERMISSION_MODE = "mode"; - class FileShareGrantToken : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileshare.grantUriPermission"); diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.cpp b/interfaces/kits/js/file_uri/get_uri_from_path.cpp index 56c1ee38a..c7948abc6 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -82,7 +82,7 @@ napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) return nullptr; } string packageName = GetBundleName(); - string uri = SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + packageName + path.get(); + string uri = FILE_SCHEME + SCHEME_SYMBOLS + packageName + path.get(); return NVal::CreateUTF8String(env, uri).val_; } diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.h b/interfaces/kits/js/file_uri/get_uri_from_path.h index 7dbea01cd..4003a6cb9 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.h +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -16,18 +16,12 @@ #ifndef GET_URI_FROM_PATH_H #define GET_URI_FROM_PATH_H -#include +#include "common_const.h" #include "filemgmt_libn.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { -using namespace std; - -const string SCHEME = "file"; -const char SCHEME_SEPARATOR = ':'; -const string PATH_SYMBOLS = "//"; -const string FRAGMENT_SYMBOLS = "#"; class GetUriFromPath final { public: diff --git a/utils/include/common/common_const.h b/utils/include/common/common_const.h new file mode 100644 index 000000000..5e3ffbdef --- /dev/null +++ b/utils/include/common/common_const.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef APP_FILE_SERVICE_COMMON_CONST_H +#define APP_FILE_SERVICE_COMMON_CONST_H + +#include + +namespace OHOS { +namespace AppFileService { +using namespace std; +const string FILE_SCHEME = "file"; +const string DATA_SHARE_SCHEME = "datashare"; +const string SCHEME_SYMBOLS = "://"; +const string PATH_EMPTY_SYMBOLS = "//"; +const string PATH_SYMBOL = "/"; +const string QUERY_SYMBOL = "?"; +const string FRAGMENT_SYMBOLS = "#"; +const string NETWORK_ID_TAG = "networkid"; +const string EQUAL_SIGN = "="; +const string MEDIA = "/media/"; +const string MEDIA_BUNDLE_NAME = "media"; +const string DEFAULT_VALUE = "default_value"; + +const std::string PATH_SHARE = "/data/storage/el2/share"; + +const string DATA_STORAGE_PATH = "/data/storage/"; +const string PACKAGE_NAME_FLAG = ""; +const string CURRENT_USER_ID_FLAG = ""; +const string DATA_APP_EL2_PATH = "/data/service/el2/"; +const string SHARE_R_PATH = "/r/"; +const string SHARE_RW_PATH = "/rw/"; +const string SHARE_PATH = "/share/"; + +const vector SANDBOX_PATH = { + "/data/storage/el1/bundle", + "/data/storage/el2/base", + "/data/storage/el1/database", + "/data/storage/el2/database", + "/data/storage/el1/base", + "/data/storage/ark-cache", + "/data/storage/ark-profile" +}; + +const vector LOWER_PATH = { + "/data/app/el1/bundle/public/", + "/data/app/el2//base/", + "/data/app/el1//database/", + "/data/app/el2//database/", + "/data/app/el1//base/", + "/data/local/ark-cache/", + "/data/local/ark-profile//" +}; + +const string MEDIA_GRANT_URI_PERMISSION = "datashare:///media/file_operation/bundle_permission_insert_operation"; +const string MEDIALIBRARY_DATA_URI = "datashare:///media"; +const string MEDIA_FILEMODE_READONLY = "r"; +const string GRANT_URI_NAME = "file_share_grant_uri_permission"; +const string PERMISSION_BUNDLE_NAME = "bundle_name"; +const string PERMISSION_FILE_ID = "file_id"; +const string PERMISSION_MODE = "mode"; + +} // namespace AppFileService +} // namespace OHOS + +#endif \ No newline at end of file -- Gitee