From 45fd260a0ac4f7cdbd623de65f7e37c6fcacb162 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Fri, 21 Jul 2023 02:20:13 +0000 Subject: [PATCH 1/3] mediashare Signed-off-by: lvyuanyuan Change-Id: Ib87923eb3f1835efcbe0f31d081b07a2118ddd0a --- bundle.json | 9 - interfaces/common/include/common_func.h | 6 +- .../include/media_share.h} | 41 ++-- interfaces/common/src/common_func.cpp | 105 ++++++++- interfaces/common/src/media_share.cpp | 153 +++++++++++++ interfaces/innerkits/native/BUILD.gn | 1 + .../native/file_share/src/file_share.cpp | 38 ++++ .../native/file_uri/src/file_uri.cpp | 76 ------- interfaces/kits/js/BUILD.gn | 1 - .../kits/js/file_uri/get_uri_from_path.cpp | 70 +++++- .../kits/js/file_uri/get_uri_from_path.h | 8 + test/unittest/BUILD.gn | 1 - test/unittest/file_uri_native/BUILD.gn | 36 --- .../file_uri_native/file_uri_test.cpp | 214 ------------------ 14 files changed, 395 insertions(+), 364 deletions(-) rename interfaces/{innerkits/native/file_uri/include/file_uri.h => common/include/media_share.h} (41%) create mode 100644 interfaces/common/src/media_share.cpp delete mode 100644 interfaces/innerkits/native/file_uri/src/file_uri.cpp delete mode 100644 test/unittest/file_uri_native/BUILD.gn delete mode 100644 test/unittest/file_uri_native/file_uri_test.cpp diff --git a/bundle.json b/bundle.json index 94d46a748..cd037d76e 100644 --- a/bundle.json +++ b/bundle.json @@ -74,15 +74,6 @@ "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share/include" } }, - { - "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:fileuri_native", - "header": { - "header_files": [ - "file_uri.h" - ], - "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri/include" - } - }, { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:remote_file_share_native", "header": { diff --git a/interfaces/common/include/common_func.h b/interfaces/common/include/common_func.h index d90a148f9..0e841641a 100644 --- a/interfaces/common/include/common_func.h +++ b/interfaces/common/include/common_func.h @@ -23,9 +23,9 @@ namespace OHOS { namespace AppFileService { class CommonFunc { public: - static std::string GetSelfBundleName(); - static std::string GetUriFromPath(const std::string &path); - static bool CheckPublicDirPath(const std::string &sandboxPath); + static bool CheckValidPath(const std::string &filePath); + static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, + std::string &physicalPath); }; } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/common/include/media_share.h similarity index 41% rename from interfaces/innerkits/native/file_uri/include/file_uri.h rename to interfaces/common/include/media_share.h index eb76514c3..36624d792 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/common/include/media_share.h @@ -13,27 +13,34 @@ * limitations under the License. */ -#ifndef APP_FILE_SERVICE_FILE_URI_FILE_URI_H -#define APP_FILE_SERVICE_FILE_URI_FILE_URI_H - +#ifndef FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_MEDIA_SHARE_H +#define FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_MEDIA_SHARE_H + #include -#include "uri.h" +#include "datashare_helper.h" +#include "datashare_values_bucket.h" +#include "filemgmt_libn.h" +#include "iremote_broker.h" + namespace OHOS { namespace AppFileService { -namespace ModuleFileUri { -class FileUri { +class FileShareGrantToken : public IRemoteBroker { public: - std::string GetName(); - std::string GetPath(); - std::string ToString(); + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileshare.grantUriPermission"); + + FileShareGrantToken() = default; + virtual ~FileShareGrantToken() noexcept = default; +}; - explicit FileUri(const std::string &uriOrPath); - ~FileUri() = default; -private: - Uri uri_; +class MediaShare { +public: + static int InsertByDatashare(napi_env env, const DataShare::DataShareValuesBucket &valuesBucket, + bool isApi10); + static int GetDataShareInfo(const std::string &uri, const std::string &bundleName, + DataShare::DataShareValuesBucket &valuesBucket, bool &isApi10, const std::string &mode); + static std::string GetModeFromFlag(unsigned int flag); }; -} // ModuleFileUri -} // namespace AppFileService -} // namespace OHOS -#endif // APP_FILE_SERVICE_FILE_URI_FILE_URI_H \ No newline at end of file +} // namespace AppFileService +} // namespace OHOS + diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index c3aedeee5..80ec678b4 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -17,6 +17,7 @@ #include +<<<<<<< HEAD #include "bundle_info.h" #include "bundle_mgr_proxy.h" #include "ipc_skeleton.h" @@ -24,13 +25,18 @@ #include "system_ability_definition.h" #include "log.h" +======= +#include "log.h" +#include "json_utils.h" +#include "uri.h" +>>>>>>> 1b6d9eb (mediashare) using namespace std; namespace OHOS { namespace AppFileService { -using namespace OHOS::AppExecFwk; namespace { +<<<<<<< HEAD const std::string FILE_SCHEME_PREFIX = "file://"; const char BACKFLASH = '/'; const std::vector PUBLIC_DIR_PATHS = { @@ -105,6 +111,103 @@ string CommonFunc::GetUriFromPath(const string &path) string packageName = GetSelfBundleName(); realPath = FILE_SCHEME_PREFIX + packageName + realPath; return realPath; +======= + const string PACKAGE_NAME_FLAG = ""; + const string CURRENT_USER_ID_FLAG = ""; + const string PHYSICAL_PATH_KEY = "src-path"; + const string SANDBOX_PATH_KEY = "sandbox-path"; + const string MOUNT_PATH_MAP_KEY = "mount-path-map"; + const string SANDBOX_JSON_FILE_PATH = "/etc/app_file_service/file_share_sandbox.json"; +} +std::unordered_map CommonFunc::sandboxPathMap_; + +static string GetLowerPath(string &lowerPathHead, const string &lowerPathTail, + const string &userId, const string &bundleName) +{ + if (lowerPathHead.find(CURRENT_USER_ID_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(CURRENT_USER_ID_FLAG), + CURRENT_USER_ID_FLAG.length(), userId); + } + + if (lowerPathHead.find(PACKAGE_NAME_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(PACKAGE_NAME_FLAG), + PACKAGE_NAME_FLAG.length(), bundleName); + } + + return lowerPathHead + lowerPathTail; +} + +static void GetSandboxPathMap(unordered_map &sandboxPathMap) +{ + nlohmann::json jsonObj; + int ret = JsonUtils::GetJsonObjFromPath(jsonObj, SANDBOX_JSON_FILE_PATH); + if (ret != 0) { + LOGE("Get json object failed from %{public}s with %{public}d", SANDBOX_JSON_FILE_PATH.c_str(), ret); + return; + } + + if (jsonObj.find(MOUNT_PATH_MAP_KEY) == jsonObj.end()) { + LOGE("Json object find mount path map failed"); + return; + } + + nlohmann::json mountPathMap = jsonObj[MOUNT_PATH_MAP_KEY]; + for (size_t i = 0; i < mountPathMap.size(); i++) { + string srcPath = mountPathMap[i][PHYSICAL_PATH_KEY]; + string sandboxPath = mountPathMap[i][SANDBOX_PATH_KEY]; + sandboxPathMap[sandboxPath] = srcPath; + } + return; +} + +int32_t CommonFunc::GetPhysicalPath(const std::string &fileUri, const std::string &userId, + std::string &physicalPath) +{ + Uri uri(fileUri); + string bundleName = uri.GetAuthority(); + string sandboxPath = uri.GetPath(); + + string lowerPathTail = ""; + string lowerPathHead = ""; + + if (sandboxPathMap_.size() == 0) { + GetSandboxPathMap(sandboxPathMap_); + } + + for (auto it = sandboxPathMap_.begin(); it != sandboxPathMap_.end(); it++) { + string sandboxPathPrefix = it->first; + if (sandboxPath.length() >= sandboxPathPrefix.length()) { + string sandboxPathTemp = sandboxPath.substr(0, sandboxPathPrefix.length()); + if (sandboxPathTemp == sandboxPathPrefix) { + lowerPathHead = it->second; + lowerPathTail = sandboxPath.substr(sandboxPathPrefix.length()); + break; + } + } + } + + if (lowerPathHead == "") { + return -EINVAL; + } else { + physicalPath = GetLowerPath(lowerPathHead, lowerPathTail, userId, bundleName); + return 0; + } +} + +bool CommonFunc::CheckValidPath(const std::string &filePath) +{ + if (filePath.empty() || filePath.size() >= PATH_MAX) { + return false; + } + + char realPath[PATH_MAX]{'\0'}; + if (realpath(filePath.c_str(), realPath) != nullptr && + strncmp(realPath, filePath.c_str(), filePath.size()) == 0) { + return true; + } else { + return false; + } +>>>>>>> 1b6d9eb (mediashare) } } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/common/src/media_share.cpp b/interfaces/common/src/media_share.cpp new file mode 100644 index 000000000..9554db352 --- /dev/null +++ b/interfaces/common/src/media_share.cpp @@ -0,0 +1,153 @@ +/* + * 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 "media_share.h" + +#include "iremote_stub.h" +#include "log.h" + +using namespace std; +using namespace OHOS::DataShare; +namespace OHOS { +namespace AppFileService { +namespace { + const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; + const string MEDIA_GRANT_URI_PERMISSION = + "datashare:///media/bundle_permission_insert_operation/bundle_permission_insert_operation"; + const string MEDIA_API_VERSION_10 = "?api_version=10"; + const string PERMISSION_BUNDLE_NAME = "bundle_name"; + const string PERMISSION_FILE_ID = "file_id"; + const string PERMISSION_MODE = "mode"; + const string PERMISSION_TABLE_TYPE = "table_type"; + const string MEDIA_FILE_URI_PHOTO_PREFEX = "file://media/Photo/"; + const string MEDIA_FILE_URI_AUDIO_PREFEX = "file://media/Audio/"; + const string MEDIA_FILE_URI_VIDEO_PREFEX = "file://media/video/"; + const string MEDIA_FILE_URI_IMAGE_PREFEX = "file://media/image/"; + const string MEDIA_FILE_URI_FILE_PREFEX = "file://media/file/"; + const string MEDIA_FILE_URI_Audio_PREFEX = "file://media/audio/"; +} + +enum MediaFileTable { + FILE_TABLE = 0, + PHOTO_TABLE = 1, + AUDIO_TABLE = 2, +}; + +static int32_t GetMediaTypeAndApiFromUri(const std::string &uri, bool &isApi10) +{ + if (uri.find(MEDIA_FILE_URI_PHOTO_PREFEX) == 0) { + isApi10 = true; + return MediaFileTable::PHOTO_TABLE; + } else if (uri.find(MEDIA_FILE_URI_VIDEO_PREFEX) == 0 || + uri.find(MEDIA_FILE_URI_IMAGE_PREFEX) == 0) { + return MediaFileTable::PHOTO_TABLE; + } else if (uri.find(MEDIA_FILE_URI_AUDIO_PREFEX) == 0) { + isApi10 = true; + return MediaFileTable::AUDIO_TABLE; + } else if (uri.find(MEDIA_FILE_URI_Audio_PREFEX) == 0) { + return MediaFileTable::AUDIO_TABLE; + } else if (uri.find(MEDIA_FILE_URI_FILE_PREFEX) == 0) { + return MediaFileTable::FILE_TABLE; + } + + return MediaFileTable::FILE_TABLE; +} + +static bool IsAllDigits(string idStr) +{ + for (size_t i = 0; i < idStr.size(); i++) { + if (!isdigit(idStr[i])) { + return false; + } + } + return true; +} + +static string GetIdFromUri(string uri) +{ + string rowNum = ""; + size_t pos = uri.rfind('/'); + if (pos != string::npos) { + rowNum = uri.substr(pos + 1); + if (!IsAllDigits(rowNum)) { + rowNum = ""; + } + } + return rowNum; +} + +string CommonFunc::GetModeFromFlag(unsigned int flag) +{ + string mode = ""; + if (flag & OHOS::AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION) { + mode += "r"; + } + if (flag & OHOS::AAFwk::Want::FLAG_AUTH_WRITE_URI_PERMISSION) { + mode += "w"; + } + return mode; +} + +int CommonFunc::GetDataShareInfo(const string &uri, const string &bundleName, + DataShareValuesBucket &valuesBucket, bool &isApi10, const string &mode) +{ + string idStr = GetIdFromUri(uri); + if (idStr == "") { + LOGE("FileShare::GetJSArgs get fileId parameter failed!"); + return EINVAL; + } + + int32_t fileId = stoi(idStr); + int32_t filesType = GetMediaTypeAndApiFromUri(uri, isApi10); + valuesBucket.Put(PERMISSION_FILE_ID, fileId); + valuesBucket.Put(PERMISSION_BUNDLE_NAME, bundleName); + valuesBucket.Put(PERMISSION_MODE, mode); + valuesBucket.Put(PERMISSION_TABLE_TYPE, filesType); + + return 0; +} + +int CommonFunc::InsertByDatashare(napi_env env, const DataShareValuesBucket &valuesBucket, bool isApi10) +{ + int ret = -1; + std::shared_ptr dataShareHelper = nullptr; + sptr remote = new IRemoteStub(); + if (remote == nullptr) { + LOGE("FileShare::InsertByDatashare get remoteObject failed!"); + return -ENOMEM; + } + + dataShareHelper = DataShareHelper::Creator(remote->AsObject(), MEDIALIBRARY_DATA_URI); + if (!dataShareHelper) { + LOGE("FileShare::InsertByDatashare connect to datashare failed!"); + return -FileManagement::LibN::E_PERMISSION; + } + string uriStr = MEDIA_GRANT_URI_PERMISSION; + if (isApi10) { + uriStr += MEDIA_API_VERSION_10; + } + + Uri uri(uriStr); + ret = dataShareHelper->Insert(uri, valuesBucket); + if (ret < 0) { + LOGE("FileShare::InsertByDatashare insert failed with error code %{public}d!", ret); + return ret; + } + return ret; +} +} // namespace AppFileService +} // namespace OHOS + + diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 2810c8a4e..ae72c8108 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -35,6 +35,7 @@ config("file_uri_config") { ohos_shared_library("fileshare_native") { sources = [ "../../common/src/json_utils.cpp", + "../../common/src/media_share.cpp", "../../common/src/sandbox_helper.cpp", "file_share/src/file_share.cpp", ] diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 8045d8552..2160f1c94 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -25,6 +25,7 @@ #include "accesstoken_kit.h" #include "hap_token_info.h" #include "log.h" +#include "media_share.h" #include "sandbox_helper.h" #include "uri.h" @@ -46,6 +47,7 @@ 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 string MEDIA = "media"; } struct FileShareInfo { @@ -128,6 +130,26 @@ static int32_t GetShareFileType(FileShareInfo &info) return -ENOENT; } +static int32_t GetShareMediaInfo(const string &uri, const string &bundleName, uint32_t flag) +{ + DataShare::DataShareValuesBucket valuesBucket; + bool isApi10; + string mode = CommonFunc::GetModeFromFlag(flag); + int32_t ret = CommonFunc::GetDataShareInfo(uri, bundleName, valuesBucket, isApi10, mode); + if (ret != 0) { + LOGE("Failed to GetDataShareInfo %{public}d", ret); + return ret; + } + + ret = CommonFunc::InsertByDatashare(nullptr, valuesBucket, isApi10); + if (ret != 0) { + LOGE("Failed to InsertByDatashare %{public}d", ret); + return ret; + } + + return 0; +} + static int32_t GetFileShareInfo(const string &uri, uint32_t tokenId, uint32_t flag, FileShareInfo &info) { int32_t ret = 0; @@ -138,6 +160,16 @@ static int32_t GetFileShareInfo(const string &uri, uint32_t tokenId, uint32_t fl } GetProviderInfo(uri, info); + + if (info.providerBundleName_ == MEDIA) { + ret = GetShareMediaInfo(uri, info.targetBundleName_, flag); + if (ret != 0) { + LOGE("Failed to GetShareMediaInfo %{public}d", ret); + return ret; + } + + return 0; + } ret = SandboxHelper::GetPhysicalPath(uri, info.currentUid_, info.providerLowerPath_); if (ret != 0) { @@ -213,6 +245,7 @@ static int32_t PreparePreShareDir(FileShareInfo &info) int32_t CreateShareFile(const string &uri, uint32_t tokenId, uint32_t flag) { + LOGI("CreateShareFile Enter!"); FileShareInfo info; int32_t ret = GetFileShareInfo(uri, tokenId, flag, info); if (ret != 0) { @@ -220,6 +253,11 @@ int32_t CreateShareFile(const string &uri, uint32_t tokenId, uint32_t flag) return ret; } + if (info.providerBundleName_ == MEDIA) { + LOGI("Create Media Share File Successfully!"); + return 0; + } + if ((ret = PreparePreShareDir(info)) != 0) { LOGE("PreparePreShareDir failed"); return ret; diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp deleted file mode 100644 index d30bc0a55..000000000 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 "uri.h" - -#include "common_func.h" -#include "log.h" - -using namespace std; -namespace OHOS { -namespace AppFileService { -namespace ModuleFileUri { -const std::string PATH_SHARE = "/data/storage/el2/share"; -const std::string MODE_RW = "/rw/"; -const std::string MODE_R = "/r/"; -const std::string FILE_SCHEME_PREFIX = "file://"; -string FileUri::GetName() -{ - string sandboxPath = uri_.GetPath(); - size_t posLast = sandboxPath.find_last_of("/"); - if (posLast == string::npos) { - return sandboxPath; - } - - if (posLast == sandboxPath.size()) { - return ""; - } - - return sandboxPath.substr(posLast + 1); -} - -string FileUri::GetPath() -{ - string sandboxPath = uri_.GetPath(); - string realPath = sandboxPath; - string providerBundleName = uri_.GetAuthority(); - string targetBundleName = CommonFunc::GetSelfBundleName(); - if (CommonFunc::CheckPublicDirPath(realPath) || - ((targetBundleName != providerBundleName) && (providerBundleName != ""))) { - realPath = PATH_SHARE + MODE_RW + providerBundleName + sandboxPath; - if (access(realPath.c_str(), F_OK) != 0) { - realPath = PATH_SHARE + MODE_R + providerBundleName + sandboxPath; - } - } - - return realPath; -} - -string FileUri::ToString() -{ - return uri_.ToString(); -} - -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 diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index bc8a7129d..f46af5eb3 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -82,7 +82,6 @@ ohos_shared_library("fileuri") { ] sources = [ - "../../common/src/common_func.cpp", "file_uri/file_uri_n_exporter.cpp", "file_uri/get_uri_from_path.cpp", ] 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 da98d8611..0c84d6558 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -14,15 +14,71 @@ */ #include "get_uri_from_path.h" -#include "status_receiver_host.h" - -#include "common_func.h" +#include "bundle_info.h" +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" #include "log.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { using namespace OHOS::FileManagement::LibN; +using namespace OHOS::AppExecFwk; + +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 GetBundleName() +{ + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + LOGE("GetBundleName: bundle mgr proxy is nullptr."); + return nullptr; + } + + BundleInfo bundleInfo; + auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); + if (ret != ERR_OK) { + LOGE("GetBundleName: bundleName get fail. uid is %{public}d", uid); + return nullptr; + } + + return bundleInfo.name; +} + +static bool NormalizePath(string &path) +{ + if (path.size() <= 0) { + return false; + } + + if (path[0] != SCHEME_PATH_BEGIN) { + path.insert(0, 1, SCHEME_PATH_BEGIN); + } + + return true; +} napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) { @@ -39,13 +95,15 @@ napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) return nullptr; } - std::string uri = CommonFunc::GetUriFromPath(path.get()); - if (uri == "") { - LOGE("GetUriFromPath failed!"); + string realPath = path.get(); + if (!realPath.empty() && !NormalizePath(realPath)) { + LOGE("GetUriFromPath::NormalizePath failed!"); NError(EINVAL).ThrowErr(env); return nullptr; } + string packageName = GetBundleName(); + string uri = SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + packageName + realPath; 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 8adb88fb9..46ee4fc43 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.h +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -22,6 +22,14 @@ 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 = "#"; +const char SCHEME_PATH_BEGIN = '/'; + class GetUriFromPath final { public: static napi_value Sync(napi_env env, napi_callback_info info); diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 53d8db6d1..bce4a610a 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -15,7 +15,6 @@ group("unittest") { testonly = true deps = [ "file_share_native:file_share_test", - "file_uri_native:file_uri_test", "remote_file_share:remote_file_share_test", ] } diff --git a/test/unittest/file_uri_native/BUILD.gn b/test/unittest/file_uri_native/BUILD.gn deleted file mode 100644 index be6bb9caf..000000000 --- a/test/unittest/file_uri_native/BUILD.gn +++ /dev/null @@ -1,36 +0,0 @@ -# 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/test.gni") - -ohos_unittest("file_uri_test") { - module_out_path = "filemanagement/app_file_service" - sources = [ "file_uri_test.cpp" ] - - external_deps = [ - "ability_base:base", - "ability_base:want", - "ability_base:zuri", - "access_token:libaccesstoken_sdk", - "app_file_service:fileshare_native", - "app_file_service:fileuri_native", - "c_utils:utils", - "hilog:libhilog", - "ipc:ipc_core", - ] - - deps = [ - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - ] -} diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp deleted file mode 100644 index 6da553a98..000000000 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ /dev/null @@ -1,214 +0,0 @@ -/* - * 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 - -#include "accesstoken_kit.h" -#include "ipc_skeleton.h" -#include "uri.h" - -#include "common_func.h" -#include "file_share.h" -#include "log.h" - -using namespace std; -using namespace OHOS::Security::AccessToken; -using namespace OHOS::AppFileService; - -const string bundleA = "com.ohos.systemui"; -string CommonFunc::GetSelfBundleName() -{ - return bundleA; -} - -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: - static void SetUpTestCase(void) {}; - static void TearDownTestCase() {}; - void SetUp() {}; - void TearDown() {}; - }; - - /** - * @tc.name: file_uri_test_0000 - * @tc.desc: Test function of ToString() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_ToString_0000, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_ToString_0000"; - - string fileStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + bundleA + fileStr; - FileUri fileUri(fileStr); - EXPECT_EQ(fileUri.ToString(), uri); - - FileUri fileUri2(uri); - EXPECT_EQ(fileUri2.ToString(), uri); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_ToString_0000"; - } - - /** - * @tc.name: file_uri_test_0001 - * @tc.desc: Test function of GetName() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetName_0000, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetName_0000"; - - string fileStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + bundleA + fileStr; - FileUri fileUri(fileStr); - string name = fileUri.GetName(); - EXPECT_EQ(name, "test.txt"); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetName_0000"; - } - - /** - * @tc.name: file_uri_test_0002 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0000, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0000"; - string fileStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + bundleA + fileStr; - FileUri fileUri(uri); - string path = fileUri.GetPath(); - EXPECT_EQ(path, fileStr); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000"; - } - - /** - * @tc.name: file_uri_test_0003 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0001, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0001"; - string fileStr = "/Documents/test.txt"; - string uri = "file://" + bundleA + fileStr; - string rltStr = PATH_SHARE + MODE_R + bundleA + fileStr; - FileUri fileUri(uri); - string path = fileUri.GetPath(); - EXPECT_EQ(path, rltStr); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0001"; - } - - /** - * @tc.name: file_uri_test_0004 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0002, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0002"; - 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; - FileUri fileUri(uri); - string path = fileUri.GetPath(); - EXPECT_EQ(path, rltStr); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0002"; - } - - /** - * @tc.name: file_uri_test_0005 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0003, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0003"; - int32_t uid = -1; - uid = OHOS::IPCSkeleton::GetCallingUid(); - string bundleB = "com.ohos.settingsdata"; - string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleB + "/files/test.txt"; - int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT); - ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!"; - - string actStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + bundleB + actStr; - uint32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleA, 0); - - int32_t flag = 3; - int32_t ret = CreateShareFile(uri, tokenId, flag); - EXPECT_EQ(ret, E_OK); - - string rltStr = PATH_SHARE + MODE_R + bundleB + actStr; - FileUri fileUri(uri); - string path = fileUri.GetPath(); - EXPECT_EQ(path, rltStr); - - vector sharePathList; - sharePathList.push_back(uri); - ret = DeleteShareFile(tokenId, sharePathList); - EXPECT_EQ(ret, E_OK); - close(fd); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0003"; - } - - /** - * @tc.name: file_uri_test_0006 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0004, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004"; - string fileStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + fileStr; - FileUri fileUri(uri); - EXPECT_EQ(fileUri.ToString(), uri); - EXPECT_EQ(fileUri.GetName(), "test.txt"); - EXPECT_EQ(fileUri.GetPath(), fileStr); - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004"; - } -} \ No newline at end of file -- Gitee From 958cb929d299315f8f6002aedbfb7f3079ce5ba3 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Fri, 21 Jul 2023 06:13:09 +0000 Subject: [PATCH 2/3] =?UTF-8?q?mediauri=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: I68d09fe116b873238996f4989e59e142d9c4f0bc --- interfaces/common/include/media_share.h | 1 - interfaces/common/src/media_share.cpp | 1 + interfaces/innerkits/native/BUILD.gn | 3 +++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/interfaces/common/include/media_share.h b/interfaces/common/include/media_share.h index 36624d792..3784ebb7c 100644 --- a/interfaces/common/include/media_share.h +++ b/interfaces/common/include/media_share.h @@ -18,7 +18,6 @@ #include -#include "datashare_helper.h" #include "datashare_values_bucket.h" #include "filemgmt_libn.h" #include "iremote_broker.h" diff --git a/interfaces/common/src/media_share.cpp b/interfaces/common/src/media_share.cpp index 9554db352..06c3fd58d 100644 --- a/interfaces/common/src/media_share.cpp +++ b/interfaces/common/src/media_share.cpp @@ -15,6 +15,7 @@ #include "media_share.h" +#include "datashare_helper.h" #include "iremote_stub.h" #include "log.h" diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index ae72c8108..649325e28 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -48,6 +48,9 @@ ohos_shared_library("fileshare_native") { "ability_base:zuri", "access_token:libaccesstoken_sdk", "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "file_api:filemgmt_libn", "hilog:libhilog", ] -- Gitee From bbcdafc7beb53198528e55c97c5e241f97719eb2 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Fri, 21 Jul 2023 07:26:47 +0000 Subject: [PATCH 3/3] =?UTF-8?q?mediauri=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: I5a5f11b0c76fb83ab8223516402bff1d38fe5d55 --- interfaces/common/include/media_share.h | 8 +++--- interfaces/common/src/media_share.cpp | 9 +++--- interfaces/innerkits/native/BUILD.gn | 28 ++----------------- .../native/file_share/src/file_share.cpp | 6 ++-- 4 files changed, 13 insertions(+), 38 deletions(-) diff --git a/interfaces/common/include/media_share.h b/interfaces/common/include/media_share.h index 3784ebb7c..375ebdfc5 100644 --- a/interfaces/common/include/media_share.h +++ b/interfaces/common/include/media_share.h @@ -15,7 +15,7 @@ #ifndef FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_MEDIA_SHARE_H #define FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_MEDIA_SHARE_H - + #include #include "datashare_values_bucket.h" @@ -37,9 +37,9 @@ public: static int InsertByDatashare(napi_env env, const DataShare::DataShareValuesBucket &valuesBucket, bool isApi10); static int GetDataShareInfo(const std::string &uri, const std::string &bundleName, - DataShare::DataShareValuesBucket &valuesBucket, bool &isApi10, const std::string &mode); - static std::string GetModeFromFlag(unsigned int flag); + DataShare::DataShareValuesBucket &valuesBucket, bool &isApi10, const std::string &mode); + static std::string GetModeFromFlag(unsigned int flag); }; } // namespace AppFileService } // namespace OHOS - +#endif \ No newline at end of file diff --git a/interfaces/common/src/media_share.cpp b/interfaces/common/src/media_share.cpp index 06c3fd58d..c9c0de3ab 100644 --- a/interfaces/common/src/media_share.cpp +++ b/interfaces/common/src/media_share.cpp @@ -18,6 +18,7 @@ #include "datashare_helper.h" #include "iremote_stub.h" #include "log.h" +#include "want.h" using namespace std; using namespace OHOS::DataShare; @@ -89,7 +90,7 @@ static string GetIdFromUri(string uri) return rowNum; } -string CommonFunc::GetModeFromFlag(unsigned int flag) +string MediaShare::GetModeFromFlag(unsigned int flag) { string mode = ""; if (flag & OHOS::AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION) { @@ -101,7 +102,7 @@ string CommonFunc::GetModeFromFlag(unsigned int flag) return mode; } -int CommonFunc::GetDataShareInfo(const string &uri, const string &bundleName, +int MediaShare::GetDataShareInfo(const string &uri, const string &bundleName, DataShareValuesBucket &valuesBucket, bool &isApi10, const string &mode) { string idStr = GetIdFromUri(uri); @@ -120,7 +121,7 @@ int CommonFunc::GetDataShareInfo(const string &uri, const string &bundleName, return 0; } -int CommonFunc::InsertByDatashare(napi_env env, const DataShareValuesBucket &valuesBucket, bool isApi10) +int MediaShare::InsertByDatashare(napi_env env, const DataShareValuesBucket &valuesBucket, bool isApi10) { int ret = -1; std::shared_ptr dataShareHelper = nullptr; @@ -150,5 +151,3 @@ int CommonFunc::InsertByDatashare(napi_env env, const DataShareValuesBucket &val } } // namespace AppFileService } // namespace OHOS - - diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 649325e28..447ac6148 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -50,35 +50,11 @@ ohos_shared_library("fileshare_native") { "c_utils:utils", "data_share:datashare_common", "data_share:datashare_consumer", + "file_api:filemgmt_libhilog", "file_api:filemgmt_libn", "hilog:libhilog", - ] - - part_name = "app_file_service" - subsystem_name = "filemanagement" -} - -ohos_shared_library("fileuri_native") { - sources = [ - "../../common/src/common_func.cpp", - "../../common/src/json_utils.cpp", - "file_uri/src/file_uri.cpp", - ] - - public_configs = [ ":file_uri_config" ] - - external_deps = [ - "ability_base:base", - "ability_base:want", - "ability_base:zuri", - "ability_runtime:abilitykit_native", - "access_token:libaccesstoken_sdk", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "hilog:libhilog", "ipc:ipc_core", - "samgr:samgr_proxy", + "napi:ace_napi", ] part_name = "app_file_service" diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 2160f1c94..bc901fa11 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -134,14 +134,14 @@ static int32_t GetShareMediaInfo(const string &uri, const string &bundleName, ui { DataShare::DataShareValuesBucket valuesBucket; bool isApi10; - string mode = CommonFunc::GetModeFromFlag(flag); - int32_t ret = CommonFunc::GetDataShareInfo(uri, bundleName, valuesBucket, isApi10, mode); + string mode = MediaShare::GetModeFromFlag(flag); + int32_t ret = MediaShare::GetDataShareInfo(uri, bundleName, valuesBucket, isApi10, mode); if (ret != 0) { LOGE("Failed to GetDataShareInfo %{public}d", ret); return ret; } - ret = CommonFunc::InsertByDatashare(nullptr, valuesBucket, isApi10); + ret = MediaShare::InsertByDatashare(nullptr, valuesBucket, isApi10); if (ret != 0) { LOGE("Failed to InsertByDatashare %{public}d", ret); return ret; -- Gitee