1 /*
2 * Copyright (c) 2025 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 #include <string>
16
17 #include "b_error/b_error.h"
18 #include "b_resources/b_constants.h"
19 #include "filemgmt_libn.h"
20
21 #include "incremental_backup_data.h"
22 #include "parse_inc_info_from_js.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26 using namespace LibN;
CheckDataList(const LibN::NVal & data)27 bool Parse::CheckDataList(const LibN::NVal &data)
28 {
29 LibN::NVal name = data.GetProp(BConstants::BUNDLE_NAME);
30 if (name.val_ == nullptr) {
31 HILOGE("name.val is nullptr");
32 return false;
33 }
34 auto [succ, str, ignore] = name.ToUTF8String();
35 if (!succ) {
36 HILOGE("convert name failed");
37 return false;
38 }
39
40 LibN::NVal time = data.GetProp(BConstants::LAST_INCREMENTAL_TIME);
41 if (time.val_ == nullptr) {
42 HILOGE("time.val is nullptr");
43 return false;
44 }
45 tie(succ, ignore) = time.ToInt64();
46 if (!succ) {
47 HILOGE("convert time failed");
48 return false;
49 }
50 return true;
51 }
52
ParseDataList(napi_env env,const napi_value & value)53 std::tuple<bool, std::vector<BIncrementalData>> Parse::ParseDataList(napi_env env, const napi_value& value)
54 {
55 uint32_t size = 0;
56 napi_status status = napi_get_array_length(env, value, &size);
57 if (status != napi_ok) {
58 HILOGE("Get array length failed!");
59 return {false, {}};
60 }
61 if (size == 0) {
62 HILOGI("array length is zero!");
63 return {true, {}};
64 }
65
66 napi_value result;
67 std::vector<BIncrementalData> backupData;
68 for (uint32_t i = 0; i < size; i++) {
69 status = napi_get_element(env, value, i, &result);
70 if (status != napi_ok) {
71 HILOGE("Get element failed! index is :%{public}u", i);
72 return {false, {}};
73 } else {
74 NVal element(env, result);
75 if (!CheckDataList(element)) {
76 HILOGE("bundles are invalid!");
77 return {false, {}};
78 }
79 IncrementalBackupData data(element);
80 backupData.emplace_back(data.bundleName,
81 data.lastIncrementalTime,
82 data.manifestFd,
83 data.parameters,
84 data.priority);
85 }
86 }
87 return {true, backupData};
88 }
89
VerifyAndParseParams(napi_env env,LibN::NFuncArg & funcArg,bool & isPreciseScan,std::vector<BIncrementalData> & bundleNames)90 bool Parse::VerifyAndParseParams(napi_env env, LibN::NFuncArg &funcArg,
91 bool &isPreciseScan, std::vector<BIncrementalData> &bundleNames)
92 {
93 if (!funcArg.InitArgs(NARG_CNT::TWO)) {
94 HILOGE("Number of arguments unmatched.");
95 NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env);
96 return false;
97 }
98
99 NVal jsBundleBool(env, funcArg[NARG_POS::FIRST]);
100 bool succ;
101 tie(succ, isPreciseScan) = jsBundleBool.ToBool();
102 if (!succ) {
103 HILOGE("First argument is not bool.");
104 NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get isPreciseScan.").GetCode()).ThrowErr(env);
105 return false;
106 }
107 tie(succ, bundleNames) = ParseDataList(env, funcArg[NARG_POS::SECOND]);
108 if (!succ) {
109 HILOGE("bundles array invalid.");
110 NError(BError(BError::Codes::SDK_INVAL_ARG, "bundles array invalid.").GetCode()).ThrowErr(env);
111 return false;
112 }
113 if (bundleNames.empty()) {
114 HILOGI("BundleName list is empty.");
115 NError(BError(BError::Codes::SDK_INVAL_ARG, "BundleName list is empty.").GetCode()).ThrowErr(env);
116 return false;
117 }
118 return true;
119 }
120 } // namespace OHOS::FileManagement::Backup