1 /*
2 * Copyright (c) 2021 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 #include "increment_list.h"
17 #include<algorithm>
18 #include<iostream>
19 #include "file_entry.h"
20 #include "key_parser.h"
21
22 namespace OHOS {
23 namespace Global {
24 namespace Restool {
25 using namespace std;
26 const string IncrementList::RESTOOL_LIST_FILE = "restool_list.json";
IncrementList(const string & listPath,const vector<string> & folder)27 IncrementList::IncrementList(const string &listPath, const vector<string> &folder)
28 : listPath_(listPath), folder_(folder)
29 {
30 }
31
Parse(std::vector<FileIncrement> & fileList) const32 bool IncrementList::Parse(std::vector<FileIncrement> &fileList) const
33 {
34 Json::Value listJson;
35 if (!ResourceUtil::OpenJsonFile(listPath_, listJson)) {
36 return false;
37 }
38
39 if (ParseArray(listJson["del"], fileList, true) &&
40 ParseArray(listJson["fix"], fileList, false)) {
41 return true;
42 }
43 return false;
44 }
45
46 // below private
GetFromPath(const string & filePath,FileIncrement & info) const47 bool IncrementList::GetFromPath(const string &filePath, FileIncrement &info) const
48 {
49 int32_t priority = GetPriority(filePath);
50 if (priority < 0) {
51 cerr << "Error: " << RESTOOL_LIST_FILE << " '" << filePath << "' invalid." << endl;
52 return false;
53 }
54 info.rootPath = folder_[priority];
55 string rootPath = FileEntry::FilePath(info.rootPath).Append("").GetPath();
56 info.relativePath = filePath.substr(rootPath.length());
57 vector<string> segments;
58 if (!ParseSegment(info.relativePath, segments)) {
59 return false;
60 }
61
62 info.dirPath = FileEntry::FilePath(filePath).GetParent().GetPath();
63 info.filePath = filePath;
64 info.limitKey = segments[SEG_LIMIT_KEY];
65 if (info.limitKey == RAW_FILE_DIR) {
66 info.dirType = ResType::INVALID_RES_TYPE;
67 return true;
68 }
69 if (!KeyParser::Parse(info.limitKey, info.keyParams)) {
70 cerr << "Error: '" << filePath << "' '" << info.limitKey << "' invalid limit key." << endl;
71 return false;
72 }
73
74 info.fileCluster = segments[SEG_FILE_CLUSTER];
75 info.dirType = ResourceUtil::GetResTypeByDir(info.fileCluster);
76 if (info.dirType == ResType::INVALID_RES_TYPE) {
77 cerr << "Error: '" << filePath << "' '" << info.fileCluster << "' invalid ResType." << endl;
78 return false;
79 }
80 info.filename = segments[SEG_FILE_NAME];
81 return true;
82 }
83
GetPriority(const string & filePath) const84 int32_t IncrementList::GetPriority(const string &filePath) const
85 {
86 auto result = find_if(folder_.begin(), folder_.end(), [&filePath](auto &iter) {
87 string rootPath = FileEntry::FilePath(iter).Append("").GetPath();
88 if (filePath.length() <= rootPath.length()) {
89 return false;
90 }
91 if (filePath.compare(0, rootPath.length(), rootPath.c_str()) == 0) {
92 return true;
93 }
94 return true;
95 });
96 if (result == folder_.end()) {
97 return -1;
98 }
99 return (result - folder_.begin());
100 }
101
ParseSegment(const string & filePath,vector<string> & segments) const102 bool IncrementList::ParseSegment(const string &filePath, vector<string> &segments) const
103 {
104 for (const auto &it : FileEntry::FilePath(filePath).GetSegments()) {
105 segments.push_back(it);
106 }
107
108 if (segments.size() >= (SEG_RESOURCE + 1) && segments[SEG_RESOURCE] != RESOURCES_DIR) {
109 cerr << "Error: '" << filePath << "' don't contain '" << RESOURCES_DIR << "'" << endl;
110 return false;
111 }
112
113 if (segments.size() >= (SEG_LIMIT_KEY + 1) && segments[SEG_LIMIT_KEY] == RAW_FILE_DIR) {
114 return true;
115 }
116
117 if (segments.size() != SEG_MAX) {
118 cerr << "Error: '" << filePath << "' segments != " << SEG_MAX << endl;
119 return false;
120 }
121 return true;
122 }
123
IteratorArray(const Json::Value & array,function<bool (const string &)> callback) const124 bool IncrementList::IteratorArray(const Json::Value &array, function<bool(const string &)> callback) const
125 {
126 if (array.empty()) {
127 return true;
128 }
129
130 if (!array.isArray()) {
131 cerr << "Error: "<< RESTOOL_LIST_FILE << ",not array." << endl;
132 return false;
133 }
134
135 for (Json::ArrayIndex i = 0; i < array.size(); i++) {
136 if (!array[i].isString()) {
137 cerr << "Error: " << RESTOOL_LIST_FILE << ",not string." << endl;
138 return false;
139 }
140 if (callback && !callback(array[i].asString())) {
141 return false;
142 }
143 }
144 return true;
145 }
146
ParseArray(const Json::Value & array,vector<FileIncrement> & fileList,bool isDeleted) const147 bool IncrementList::ParseArray(const Json::Value &array, vector<FileIncrement> &fileList, bool isDeleted) const
148 {
149 return IteratorArray(array, [this, &fileList, &isDeleted](const string &filePath) {
150 FileIncrement info;
151 info.isDeleted = isDeleted;
152 if (!GetFromPath(filePath, info)) {
153 return false;
154 }
155 fileList.push_back(info);
156 return true;
157 });
158 }
159 }
160 }
161 }