• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INNER_BUNDLE_CLONE_COMMON_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INNER_BUNDLE_CLONE_COMMON_H
18 
19 #include <string>
20 #include "app_log_wrapper.h"
21 #include "bundle_constants.h"
22 #include "bundle_service_constants.h"
23 #include "string_ex.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 constexpr const char* CLONE_DIR_PATH_PREFIX = "clone";
29 constexpr const char* PLUS_SIGN = "+";
30 constexpr const char* MINUS_SIGN = "-";
31 }
32 
33 class BundleCloneCommonHelper {
34 public:
GetCloneDataDir(const std::string & bundleName,const int32_t appIndex)35     static std::string GetCloneDataDir(const std::string &bundleName, const int32_t appIndex)
36     {
37         return std::string(PLUS_SIGN) + CLONE_DIR_PATH_PREFIX +
38             MINUS_SIGN + std::to_string(appIndex) + PLUS_SIGN + bundleName;
39     }
40 
GetCloneBundleIdKey(const std::string & bundleName,const int32_t appIndex)41     static std::string GetCloneBundleIdKey(const std::string &bundleName, const int32_t appIndex)
42     {
43         return std::to_string(appIndex) + CLONE_DIR_PATH_PREFIX + Constants::FILE_UNDERLINE + bundleName;
44     }
45 
ParseCloneDataDir(const std::string & cloneDirName,std::string & bundleName,int32_t & appIndex)46     static bool ParseCloneDataDir(const std::string &cloneDirName, std::string &bundleName, int32_t &appIndex)
47     {
48         if (cloneDirName.find(ServiceConstants::CLONE_PREFIX) != 0) {
49             APP_LOGE("prefix invalid %{public}s", cloneDirName.c_str());
50             return false;
51         }
52         std::string tempStr = cloneDirName.substr(strlen(ServiceConstants::CLONE_PREFIX));
53         size_t plusPos = tempStr.find(PLUS_SIGN);
54         if (plusPos == std::string::npos) {
55             APP_LOGE("plus sign not found %{public}s", cloneDirName.c_str());
56             return false;
57         }
58         if (!OHOS::StrToInt(tempStr.substr(0, plusPos), appIndex)) {
59             APP_LOGE("StrToInt failed %{public}s", cloneDirName.c_str());
60             return false;
61         }
62         bundleName = tempStr.substr(plusPos + 1);
63         return true;
64     }
65 };
66 } // namespace AppExecFwk
67 } // namespace OHOS
68 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INNER_BUNDLE_CLONE_COMMON_H