140f5d65dSopenharmony_ci/*
240f5d65dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
340f5d65dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
440f5d65dSopenharmony_ci * you may not use this file except in compliance with the License.
540f5d65dSopenharmony_ci * You may obtain a copy of the License at
640f5d65dSopenharmony_ci *
740f5d65dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
840f5d65dSopenharmony_ci *
940f5d65dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1040f5d65dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1140f5d65dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1240f5d65dSopenharmony_ci * See the License for the specific language governing permissions and
1340f5d65dSopenharmony_ci * limitations under the License.
1440f5d65dSopenharmony_ci */
1540f5d65dSopenharmony_ci
1640f5d65dSopenharmony_ci#include "common_func.h"
1740f5d65dSopenharmony_ci
1840f5d65dSopenharmony_ci#include <mutex>
1940f5d65dSopenharmony_ci#include <vector>
2040f5d65dSopenharmony_ci
2140f5d65dSopenharmony_ci#include <singleton.h>
2240f5d65dSopenharmony_ci#include "bundle_mgr_client.h"
2340f5d65dSopenharmony_ci#include "bundle_mgr_proxy.h"
2440f5d65dSopenharmony_ci#include "ipc_skeleton.h"
2540f5d65dSopenharmony_ci#include "iservice_registry.h"
2640f5d65dSopenharmony_ci#include "system_ability_definition.h"
2740f5d65dSopenharmony_ci
2840f5d65dSopenharmony_ci#include "log.h"
2940f5d65dSopenharmony_ci#include "sandbox_helper.h"
3040f5d65dSopenharmony_ci
3140f5d65dSopenharmony_ciusing namespace std;
3240f5d65dSopenharmony_ci
3340f5d65dSopenharmony_cinamespace OHOS {
3440f5d65dSopenharmony_cinamespace AppFileService {
3540f5d65dSopenharmony_ciusing namespace OHOS::AppExecFwk;
3640f5d65dSopenharmony_cinamespace {
3740f5d65dSopenharmony_ci    const std::string FILE_SCHEME_PREFIX = "file://";
3840f5d65dSopenharmony_ci    const char BACKFLASH = '/';
3940f5d65dSopenharmony_ci    const std::string FILE_MANAGER_URI_HEAD = "/storage/";
4040f5d65dSopenharmony_ci    const std::string FILE_MANAGER_AUTHORITY = "docs";
4140f5d65dSopenharmony_ci    std::string g_bundleName = "";
4240f5d65dSopenharmony_ci    std::mutex g_globalMutex;
4340f5d65dSopenharmony_ci}
4440f5d65dSopenharmony_cistatic sptr<BundleMgrProxy> GetBundleMgrProxy()
4540f5d65dSopenharmony_ci{
4640f5d65dSopenharmony_ci    sptr<ISystemAbilityManager> systemAbilityManager =
4740f5d65dSopenharmony_ci        SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
4840f5d65dSopenharmony_ci    if (!systemAbilityManager) {
4940f5d65dSopenharmony_ci        LOGE("fail to get system ability mgr.");
5040f5d65dSopenharmony_ci        return nullptr;
5140f5d65dSopenharmony_ci    }
5240f5d65dSopenharmony_ci
5340f5d65dSopenharmony_ci    sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
5440f5d65dSopenharmony_ci    if (!remoteObject) {
5540f5d65dSopenharmony_ci        LOGE("fail to get bundle manager proxy.");
5640f5d65dSopenharmony_ci        return nullptr;
5740f5d65dSopenharmony_ci    }
5840f5d65dSopenharmony_ci
5940f5d65dSopenharmony_ci    return iface_cast<BundleMgrProxy>(remoteObject);
6040f5d65dSopenharmony_ci}
6140f5d65dSopenharmony_ci
6240f5d65dSopenharmony_cibool CommonFunc::GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName)
6340f5d65dSopenharmony_ci{
6440f5d65dSopenharmony_ci    auto bmsClient = DelayedSingleton<AppExecFwk::BundleMgrClient>::GetInstance();
6540f5d65dSopenharmony_ci    if (bmsClient == nullptr) {
6640f5d65dSopenharmony_ci        LOGE("bundleMgrClient is nullptr.");
6740f5d65dSopenharmony_ci        return false;
6840f5d65dSopenharmony_ci    }
6940f5d65dSopenharmony_ci    auto bmsRet = bmsClient->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dirName);
7040f5d65dSopenharmony_ci    if (bmsRet != ERR_OK) {
7140f5d65dSopenharmony_ci        LOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d", bmsRet);
7240f5d65dSopenharmony_ci        return false;
7340f5d65dSopenharmony_ci    }
7440f5d65dSopenharmony_ci    return true;
7540f5d65dSopenharmony_ci}
7640f5d65dSopenharmony_ci
7740f5d65dSopenharmony_cistring CommonFunc::GetSelfBundleName()
7840f5d65dSopenharmony_ci{
7940f5d65dSopenharmony_ci    sptr<BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
8040f5d65dSopenharmony_ci    if (!bundleMgrProxy) {
8140f5d65dSopenharmony_ci        LOGE("GetSelfBundleName: bundle mgr proxy is nullptr.");
8240f5d65dSopenharmony_ci        return "";
8340f5d65dSopenharmony_ci    }
8440f5d65dSopenharmony_ci
8540f5d65dSopenharmony_ci    BundleInfo bundleInfo;
8640f5d65dSopenharmony_ci    auto ret = bundleMgrProxy->GetBundleInfoForSelf(0, bundleInfo);
8740f5d65dSopenharmony_ci    if (ret != ERR_OK) {
8840f5d65dSopenharmony_ci        LOGE("GetSelfBundleName: bundleName get fail.");
8940f5d65dSopenharmony_ci        return "";
9040f5d65dSopenharmony_ci    }
9140f5d65dSopenharmony_ci    string dirName = "" ;
9240f5d65dSopenharmony_ci    if (GetDirByBundleNameAndAppIndex(bundleInfo.name, bundleInfo.appIndex, dirName)) {
9340f5d65dSopenharmony_ci        return dirName;
9440f5d65dSopenharmony_ci    }
9540f5d65dSopenharmony_ci    return bundleInfo.name;
9640f5d65dSopenharmony_ci}
9740f5d65dSopenharmony_ci
9840f5d65dSopenharmony_cistatic void NormalizePath(string &path)
9940f5d65dSopenharmony_ci{
10040f5d65dSopenharmony_ci    if (path.size() == 0) {
10140f5d65dSopenharmony_ci        return;
10240f5d65dSopenharmony_ci    }
10340f5d65dSopenharmony_ci
10440f5d65dSopenharmony_ci    if (path[0] != BACKFLASH) {
10540f5d65dSopenharmony_ci        path.insert(0, 1, BACKFLASH);
10640f5d65dSopenharmony_ci    }
10740f5d65dSopenharmony_ci}
10840f5d65dSopenharmony_ci
10940f5d65dSopenharmony_cistring CommonFunc::GetUriFromPath(const string &path)
11040f5d65dSopenharmony_ci{
11140f5d65dSopenharmony_ci    string realPath = path;
11240f5d65dSopenharmony_ci    NormalizePath(realPath);
11340f5d65dSopenharmony_ci
11440f5d65dSopenharmony_ci    {
11540f5d65dSopenharmony_ci        std::lock_guard<std::mutex> lock(g_globalMutex);
11640f5d65dSopenharmony_ci        if (g_bundleName == "") {
11740f5d65dSopenharmony_ci            g_bundleName = GetSelfBundleName();
11840f5d65dSopenharmony_ci        }
11940f5d65dSopenharmony_ci    }
12040f5d65dSopenharmony_ci    string packageName = (path.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : g_bundleName;
12140f5d65dSopenharmony_ci    realPath = FILE_SCHEME_PREFIX + packageName + SandboxHelper::Encode(realPath);
12240f5d65dSopenharmony_ci    return realPath;
12340f5d65dSopenharmony_ci}
12440f5d65dSopenharmony_ci} // namespace AppFileService
12540f5d65dSopenharmony_ci} // namespace OHOS
12640f5d65dSopenharmony_ci
127