From d2ed8af111eed6ac6fbb0fe0b631fc83d0c81a81 Mon Sep 17 00:00:00 2001 From: "zhangkaixiang5@huawei.com" Date: Thu, 2 Feb 2023 20:39:10 +0800 Subject: [PATCH] Add CreateShareFile and DeleteShareFile Interface. Update IsMediaUri and GetBundleName Function. Signed-off-by: zhangkaixiang5@huawei.com --- BUILD.gn | 1 + bundle.json | 9 + interfaces/innerkits/native/common/log.h | 36 ++ .../innerkits/native/file_share/BUILD.gn | 40 ++ .../native/file_share/include/file_share.h | 77 ++++ .../native/file_share/src/file_share.cpp | 357 ++++++++++++++++++ interfaces/kits/js/@ohos.remotefileshare.d.ts | 6 +- interfaces/kits/js/BUILD.gn | 10 +- .../js/file_share/grant_uri_permission.cpp | 3 +- .../kits/js/file_uri/get_uri_from_path.cpp | 44 ++- 10 files changed, 574 insertions(+), 9 deletions(-) create mode 100644 interfaces/innerkits/native/common/log.h create mode 100644 interfaces/innerkits/native/file_share/BUILD.gn create mode 100644 interfaces/innerkits/native/file_share/include/file_share.h create mode 100644 interfaces/innerkits/native/file_share/src/file_share.cpp diff --git a/BUILD.gn b/BUILD.gn index 2c041cd26..4233fa935 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -15,6 +15,7 @@ import("//build/ohos.gni") group("libremotefileshare") { 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", ] diff --git a/bundle.json b/bundle.json index 8e6c15411..fa6637b89 100644 --- a/bundle.json +++ b/bundle.json @@ -32,6 +32,15 @@ "service_group": [] }, "inner_kits": [ + { + "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" + } + }, { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", "header": { diff --git a/interfaces/innerkits/native/common/log.h b/interfaces/innerkits/native/common/log.h new file mode 100644 index 000000000..7353db72b --- /dev/null +++ b/interfaces/innerkits/native/common/log.h @@ -0,0 +1,36 @@ +/* + * 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 LOG_H +#define LOG_H +#include "hilog/log.h" + +namespace OHOS { +namespace AppFileService { +const unsigned int APP_LOG_DOMAIN = 0xD004313; +const char APP_LOG_TAG[] = "AppFileService"; +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, APP_LOG_DOMAIN, APP_LOG_TAG}; + +#define PRINT_LOG(Level, fmt, ...) \ + OHOS::HiviewDFX::HiLog::Level(OHOS::AppFileService::LOG_LABEL, "[%{public}s:%{public}d] " fmt, \ + __FUNCTION__, __LINE__, ##__VA_ARGS__) + +#define LOGI(fmt, ...) PRINT_LOG(Info, fmt, ##__VA_ARGS__) +#define LOGW(fmt, ...) PRINT_LOG(Warn, fmt, ##__VA_ARGS__) +#define LOGE(fmt, ...) PRINT_LOG(Error, fmt, ##__VA_ARGS__) +#define LOGF(fmt, ...) PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) +} // namespace AppFileService +} // namespace OHOS + +#endif // LOG_H \ No newline at end of file diff --git a/interfaces/innerkits/native/file_share/BUILD.gn b/interfaces/innerkits/native/file_share/BUILD.gn new file mode 100644 index 000000000..6f9b24ff6 --- /dev/null +++ b/interfaces/innerkits/native/file_share/BUILD.gn @@ -0,0 +1,40 @@ +# 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_share_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "../common", + ] +} + +ohos_shared_library("fileshare_native") { + sources = [ "src/file_share.cpp" ] + + public_configs = [ ":file_share_config" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} diff --git a/interfaces/innerkits/native/file_share/include/file_share.h b/interfaces/innerkits/native/file_share/include/file_share.h new file mode 100644 index 000000000..9e741cc4b --- /dev/null +++ b/interfaces/innerkits/native/file_share/include/file_share.h @@ -0,0 +1,77 @@ +/* + * 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_SHARE +#define APP_FILE_SERVICE_FILE_SHARE + +#include +#include +#include "want.h" + +namespace OHOS { +namespace AppFileService { +using namespace std; + +#define DIR_MODE (S_IRWXU | S_IXGRP | S_IXOTH) +#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) +#define READ_URI_PERMISSION OHOS::AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION +#define WRITE_URI_PERMISSION OHOS::AAFwk::Want::FLAG_AUTH_WRITE_URI_PERMISSION + +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", + "/data/storage/el2/distributedfiles" +}; + +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//", + "/data/service/el2//hmdfs/account/data/" +}; +} + +class FileShare { +public: + FileShare() {} + static int32_t CreateShareFile(const string &uri, int32_t tokenId, int32_t flag); + static int32_t DeleteShareFile(int32_t tokenId, vector sharePathList); + ~FileShare() {} +}; +} // namespace AppFileService +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp new file mode 100644 index 000000000..df600bde5 --- /dev/null +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -0,0 +1,357 @@ +/* + * 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_share.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "accesstoken_kit.h" +#include "hap_token_info.h" +#include "log.h" +#include "uri.h" + +namespace OHOS { +namespace AppFileService { + +struct FileShareInfo { + string providerBundleName_; + string targetBundleName_; + string providerLowerPath_; + string providerSandboxPath_; + vector sharePath_; + vector isExist_; + string currentUid_; + SHARE_FILE_TYPE type_; +}; + +static int32_t GetTargetInfo(int32_t tokenId, string &bundleName, string ¤tUid) +{ + Security::AccessToken::HapTokenInfo hapInfo; + int32_t result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo); + if (result != 0) { + LOGE("Failed to get hap token info %{public}d", result); + return -EINVAL; + } + bundleName = hapInfo.bundleName; + currentUid = to_string(hapInfo.userID); + return 0; +} + +static void GetProviderBundleName(string uriStr, string &bundleName) +{ + Uri uri(uriStr); + bundleName = uri.GetAuthority(); +} + +static bool IsExistDir(const string &path) +{ + struct stat buf = {}; + if (stat(path.c_str(), &buf) != 0) { + return false; + } + return S_ISDIR(buf.st_mode); +} + +static bool IsExistFile(const string &path) +{ + struct stat buf = {}; + if (stat(path.c_str(), &buf) != 0) { + LOGE("Get path stat failed, path: %s err %{public}d", path.c_str(), errno); + return false; + } + return S_ISREG(buf.st_mode); +} + +static int32_t GetLowerPath(string &lowerPathHead, const string &lowerPathTail, FileShareInfo &info) +{ + if (lowerPathHead.empty()) { + return -EINVAL; + } + + if (lowerPathHead.find(CURRENT_USER_ID_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(CURRENT_USER_ID_FLAG), + CURRENT_USER_ID_FLAG.length(), info.currentUid_); + } + + if (lowerPathHead.find(PACKAGE_NAME_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(PACKAGE_NAME_FLAG), + PACKAGE_NAME_FLAG.length(), info.providerBundleName_); + } + + info.providerLowerPath_ = lowerPathHead + lowerPathTail; + return 0; +} + +static int32_t GetProviderPath(const string &uriStr, int32_t tokenId, FileShareInfo &info) +{ + Uri uri(uriStr); + string pathInProvider = uri.GetPath(); + string::size_type pos = pathInProvider.find(DATA_STORAGE_PATH); + if (pos == string::npos) { + return -EINVAL; + } + + size_t num = SANDBOX_PATH.size(); + string lowerPathTail = "", lowerPathHead = ""; + for (size_t i = 0; i < num; i++) { + if (pathInProvider.length() >= SANDBOX_PATH[i].length()) { + string sandboxPathTemp = pathInProvider.substr(0, SANDBOX_PATH[i].length()); + if (sandboxPathTemp == SANDBOX_PATH[i]) { + lowerPathHead = LOWER_PATH[i]; + lowerPathTail = pathInProvider.substr(SANDBOX_PATH[i].length()); + break; + } + } + } + + int32_t ret = GetLowerPath(lowerPathHead, lowerPathTail, info); + if (ret != 0) { + LOGE("Get lower path failed with %{public}d", ret); + return ret; + } + + info.providerSandboxPath_ = pathInProvider; + return 0; +} + +static void GetSharePath(FileShareInfo &info, int32_t flag) +{ + string shareRPath = DATA_APP_EL2_PATH + info.currentUid_ + SHARE_PATH +info.targetBundleName_ + + SHARE_R_PATH + info.providerBundleName_ + info.providerSandboxPath_; + string shareRWPath = DATA_APP_EL2_PATH + info.currentUid_ + SHARE_PATH +info.targetBundleName_ + + SHARE_RW_PATH + info.providerBundleName_ + info.providerSandboxPath_; + if ((flag & WRITE_URI_PERMISSION) == WRITE_URI_PERMISSION) { + info.sharePath_.push_back(shareRWPath); + info.sharePath_.push_back(shareRPath); + info.isExist_.push_back(false); + info.isExist_.push_back(false); + } else if ((flag & READ_URI_PERMISSION) == READ_URI_PERMISSION) { + info.sharePath_.push_back(shareRPath); + info.isExist_.push_back(false); + } +} + +static int32_t GetShareFileType(FileShareInfo &info) +{ + if (IsExistFile(info.providerLowerPath_)) { + info.type_ = FILE_TYPE; + return 0; + } else if (IsExistDir(info.providerLowerPath_)) { + info.type_ = DIR_TYPE; + return 0; + } + return -ENOENT; +} + +static int32_t GetFileShareInfo(const string &uri, int32_t tokenId, int32_t flag, FileShareInfo &info) +{ + int32_t ret = 0; + ret = GetTargetInfo(tokenId, info.targetBundleName_, info.currentUid_); + if (ret != 0) { + LOGE("Failed to get target info %{public}d", ret); + return ret; + } + + GetProviderBundleName(uri, info.providerBundleName_); + ret = GetProviderPath(uri, tokenId, info); + if (ret != 0) { + LOGE("Failed to get lower path %{public}d", ret); + return ret; + } + + ret = GetShareFileType(info); + if (ret != 0) { + LOGE("Failed to get share file type %{public}d", ret); + return ret; + } + GetSharePath(info, flag); + return 0; +} + +static bool MakeDir(const string &path) +{ + string::size_type index = 0; + string subPath; + do { + index = path.find('/', index + 1); + if (index == string::npos) { + subPath = path; + } else { + subPath = path.substr(0, index); + } + + if (access(subPath.c_str(), 0) != 0) { + if (mkdir(subPath.c_str(), DIR_MODE) != 0) { + LOGE("Failed to make dir with %{public}d", errno); + return false; + } + } + } while (index != string::npos); + + return true; +} + +static bool RemoveDir(const string& path) +{ + stack dirs, delDirs; + dirs.push(path); + while (dirs.size() > 0) { + string curPath = dirs.top(); + dirs.pop(); + DIR *curDir = opendir(curPath.c_str()); + struct dirent *ptr = readdir(curDir); + while (ptr != nullptr) { + string subPath = curPath + "/" + string(ptr->d_name); + if (ptr->d_type == DT_DIR && strcmp(ptr->d_name, ".") != 0 && + strcmp(ptr->d_name, "..") != 0) { + dirs.push(subPath); + delDirs.push(subPath); + } else if (ptr->d_type != DT_DIR && remove(subPath.c_str()) != 0) { + umount2(subPath.c_str(), MNT_DETACH); + remove(subPath.c_str()); + LOGE("Failed to remove dir with %{public}d", errno); + } + ptr = readdir(curDir); + } + closedir(curDir); + } + + while (delDirs.size() > 0) { + string curPath = delDirs.top(); + if (remove(curPath.c_str()) != 0) { + LOGE("Failed to remove dir with %{public}d", errno); + } + delDirs.pop(); + } + + return true; +} + +static bool DeleteDir(const string &path) +{ + if (IsExistDir(path)) { + return RemoveDir(path); + } + return false; +} + +static int32_t PreparePreShareDir(FileShareInfo &info) +{ + for (size_t i = 0; i < info.sharePath_.size(); i++) { + if (access(info.sharePath_[i].c_str(), F_OK) != 0) { + string sharePathDir = info.sharePath_[i]; + size_t posLast = info.sharePath_[i].find_last_of("/"); + sharePathDir = info.sharePath_[i].substr(0, posLast); + if (!MakeDir(sharePathDir.c_str())) { + LOGE("Make dir failed with %{public}d", errno); + return -errno; + } + } else { + LOGE("File %{public}s already exists", info.sharePath_[i].c_str()); + info.isExist_[i] = true; + } + } + return 0; +} + +int32_t FileShare::CreateShareFile(const string &uri, int32_t tokenId, int32_t flag) +{ + FileShareInfo info; + int32_t ret = GetFileShareInfo(uri, tokenId, flag, info); + if (ret != 0) { + LOGE("Failed to get FileShareInfo with %{public}d", ret); + return ret; + } + + if ((ret = PreparePreShareDir(info)) != 0) { + LOGE("PreparePreShareDir failed"); + return ret; + } + + for (size_t i = 0; i < info.sharePath_.size(); i++) { + if (info.isExist_[i]) { + continue; + } + + if (info.type_ == FILE_TYPE) { + if ((ret = creat(info.sharePath_[i].c_str(), FILE_MODE)) < 0) { + LOGE("Create file failed with %{public}d", errno); + return -errno; + } + close(ret); + } else if (mkdir(info.sharePath_[i].c_str(), DIR_MODE) != 0) { + LOGE("Create dir failed with %{public}d", errno); + return -errno; + } + + if (mount(info.providerLowerPath_.c_str(), info.sharePath_[i].c_str(), + nullptr, MS_BIND, nullptr) != 0) { + LOGE("Mount failed with %{public}d", errno); + return -errno; + } + } + LOGI("Create Share File Successfully!"); + return 0; +} + +static void UmountDelUris(vector sharePathList, string currentUid, string bundleNameSelf) +{ + for (size_t i = 0; i < sharePathList.size(); i++) { + Uri uri(sharePathList[i]); + string path = uri.GetPath(); + string bundleName = uri.GetAuthority(); + string delRPath = DATA_APP_EL2_PATH + currentUid + SHARE_PATH + bundleNameSelf + SHARE_R_PATH + + bundleName + path; + string delRWPath = DATA_APP_EL2_PATH + currentUid + SHARE_PATH + bundleNameSelf + SHARE_RW_PATH + + bundleName + path; + if (access(delRPath.c_str(), F_OK) == 0) { + if (umount2(delRPath.c_str(), MNT_DETACH) != 0) { + LOGE("UmountdelRPath, umount failed with %{public}d", errno); + } + } + if (access(delRWPath.c_str(), F_OK) == 0) { + if (umount2(delRWPath.c_str(), MNT_DETACH) != 0) { + LOGE("UmountdelRWPath, umount failed with %{public}d", errno); + } + } + } +} + +int32_t FileShare::DeleteShareFile(int32_t tokenId, vector sharePathList) +{ + string bundleName, currentUid; + int32_t ret = GetTargetInfo(tokenId, bundleName, currentUid); + if (ret != 0) { + LOGE("Failed to delete share file %{public}d", -EINVAL); + return -EINVAL; + } + UmountDelUris(sharePathList, currentUid, bundleName); + + string sharePath = DATA_APP_EL2_PATH + currentUid + SHARE_PATH + bundleName; + if ((access(sharePath.c_str(), F_OK) == 0) && !DeleteDir(sharePath)) { + LOGE("Delete dir failed with %{public}d", errno); + return -errno; + } + + LOGI("Delete Share File Successfully!"); + return 0; +} +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/@ohos.remotefileshare.d.ts b/interfaces/kits/js/@ohos.remotefileshare.d.ts index 40d946ae0..28bc3eb38 100644 --- a/interfaces/kits/js/@ohos.remotefileshare.d.ts +++ b/interfaces/kits/js/@ohos.remotefileshare.d.ts @@ -1,5 +1,5 @@ /* -* Copyright (C) 2021-2022 Huawei Device Co., Ltd. +* Copyright (C) 2021-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 @@ -18,7 +18,7 @@ import {AsyncCallback, Callback} from "./basic"; /** * Provides remote file share APIs * - * @since 8 + * @since 10 * @sysCap N/A * @devices phone, tablet */ @@ -26,7 +26,7 @@ declare namespace Remotefileshare { /** * Create the remote share path of src share file. * - * @since 8 + * @since 10 */ function createSharePath(fd: number, cid: string, callback: AsyncCallback): void; function createSharePath(fd: number, cid: string): Promise; diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index c14bbd22d..ee378513e 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -83,11 +83,17 @@ ohos_shared_library("fileuri") { ] deps = [ - "//foundation/bundlemanager/bundle_framework/interfaces/kits/native/bundle:bundle_ndk", "//foundation/filemanagement/file_api/utils/filemgmt_libn:filemgmt_libn", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "ability_runtime:abilitykit_native", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] relative_install_dir = "module/file" diff --git a/interfaces/kits/js/file_share/grant_uri_permission.cpp b/interfaces/kits/js/file_share/grant_uri_permission.cpp index 0dad0642b..dc8c1860b 100644 --- a/interfaces/kits/js/file_share/grant_uri_permission.cpp +++ b/interfaces/kits/js/file_share/grant_uri_permission.cpp @@ -24,6 +24,7 @@ using namespace OHOS::DataShare; using namespace OHOS::FileManagement::LibN; +using namespace OHOS::DistributedFS::ModuleRemoteUri; namespace OHOS { namespace AppFileService { @@ -52,7 +53,7 @@ namespace ModuleFileShare { return rowNum; } - static string GetModeFromFlag(int flag) + static string GetModeFromFlag(unsigned int flag) { string mode = ""; if (flag & OHOS::AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION) { 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 abba57d32..30c77385a 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -13,18 +13,56 @@ * limitations under the License. */ #include "get_uri_from_path.h" + +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" #include "log.h" -#include "native_interface_bundle.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() { - OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo(); - return nativeApplicationInfo.bundleName; + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + LOGE("GetBundleName: bundle mgr proxy is nullptr."); + return nullptr; + } + + string bundleName; + if (!bundleMgrProxy->GetBundleNameForUid(uid, bundleName)) { + LOGE("GetBundleName: bundleName get fail. uid is %{public}d", uid); + return nullptr; + } + + return bundleName; } napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) -- Gitee