• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 << "' invalid." << NEW_LINE_PATH << filePath << 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: '" << info.limitKey << "' invalid limit key." << NEW_LINE_PATH << filePath << 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: '" << info.fileCluster << "' invalid ResType." << NEW_LINE_PATH << filePath << 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     vector<string> segs = FileEntry::FilePath(filePath).GetSegments();
105     copy(segs.begin(), segs.end(), back_inserter(segments));
106 
107     if (segments.size() >= (SEG_RESOURCE + 1) && segments[SEG_RESOURCE] != RESOURCES_DIR) {
108         cerr << "Error: don't contain '" << RESOURCES_DIR << "'." << NEW_LINE_PATH << filePath << endl;
109         return false;
110     }
111 
112     if (segments.size() >= (SEG_LIMIT_KEY + 1) && segments[SEG_LIMIT_KEY] == RAW_FILE_DIR) {
113         return true;
114     }
115 
116     if (segments.size() != SEG_MAX) {
117         cerr << "Error: segments != " << SEG_MAX << NEW_LINE_PATH << filePath << endl;
118         return false;
119     }
120     return true;
121 }
122 
IteratorArray(const Json::Value & array,function<bool (const string &)> callback) const123 bool IncrementList::IteratorArray(const Json::Value &array, function<bool(const string &)> callback) const
124 {
125     if (array.empty()) {
126         return true;
127     }
128 
129     if (!array.isArray()) {
130         cerr << "Error: "<< RESTOOL_LIST_FILE << ",not array." << endl;
131         return false;
132     }
133 
134     for (Json::ArrayIndex i = 0; i < array.size(); i++) {
135         if (!array[i].isString()) {
136             cerr << "Error: " << RESTOOL_LIST_FILE << ",not string." << endl;
137             return false;
138         }
139         if (callback && !callback(array[i].asString())) {
140             return false;
141         }
142     }
143     return true;
144 }
145 
ParseArray(const Json::Value & array,vector<FileIncrement> & fileList,bool isDeleted) const146 bool IncrementList::ParseArray(const Json::Value &array, vector<FileIncrement> &fileList, bool isDeleted) const
147 {
148     return IteratorArray(array, [this, &fileList, &isDeleted](const string &filePath) {
149         FileIncrement info;
150         info.isDeleted = isDeleted;
151         if (!GetFromPath(filePath, info)) {
152             return false;
153         }
154         fileList.push_back(info);
155         return true;
156     });
157 }
158 }
159 }
160 }