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 "resource_directory.h"
17 #include<iostream>
18 #include "file_entry.h"
19 #include "resource_util.h"
20 #include "select_compile_parse.h"
21
22 namespace OHOS {
23 namespace Global {
24 namespace Restool {
25 using namespace std;
ScanResources(const string & resourcesDir,function<bool (const DirectoryInfo &)> callback) const26 bool ResourceDirectory::ScanResources(const string &resourcesDir, function<bool(const DirectoryInfo&)> callback) const
27 {
28 FileEntry f(resourcesDir);
29 if (!f.Init()) {
30 return false;
31 }
32
33 for (const auto &it : f.GetChilds()) {
34 string limitKey = it->GetFilePath().GetFilename();
35 if (ResourceUtil::IsIgnoreFile(limitKey, it->IsFile())) {
36 continue;
37 }
38
39 if (it->IsFile()) {
40 cerr << "Error: '" << it->GetFilePath().GetPath() << "' not directory." << endl;
41 return false;
42 }
43
44 if (limitKey == RAW_FILE_DIR || limitKey == RES_FILE_DIR) {
45 continue;
46 }
47
48 if (!ScanResourceLimitKeyDir(it->GetFilePath().GetPath(), limitKey, callback)) {
49 return false;
50 }
51 }
52 return true;
53 }
54
55 // below private
ScanResourceLimitKeyDir(const string & resourceTypeDir,const string & limitKey,function<bool (const DirectoryInfo &)> callback) const56 bool ResourceDirectory::ScanResourceLimitKeyDir(const string &resourceTypeDir, const string &limitKey,
57 function<bool(const DirectoryInfo&)> callback) const
58 {
59 vector<KeyParam> keyParams;
60 if (!KeyParser::Parse(limitKey, keyParams)) {
61 cerr << "Error: invalid limit key '" << limitKey << "'." << NEW_LINE_PATH << resourceTypeDir << endl;
62 return false;
63 }
64 if (!SelectCompileParse::IsSelectCompile(keyParams)) {
65 return true;
66 }
67 FileEntry f(resourceTypeDir);
68 if (!f.Init()) {
69 return false;
70 }
71 for (const auto &it : f.GetChilds()) {
72 string dirPath = it->GetFilePath().GetPath();
73 string fileCluster = it->GetFilePath().GetFilename();
74 if (ResourceUtil::IsIgnoreFile(fileCluster, it->IsFile())) {
75 continue;
76 }
77
78 if (it->IsFile()) {
79 cerr << "Error: '" << dirPath << "' not directory." << endl;
80 return false;
81 }
82
83 ResType type = ResourceUtil::GetResTypeByDir(fileCluster);
84 if (type == ResType::INVALID_RES_TYPE) {
85 string array("[ ");
86 for (auto item : g_fileClusterMap) {
87 array.append("'" + item.first + "' ");
88 }
89 array.append("]");
90 cerr << "Error: '" << dirPath << "', invalid directory name '";
91 cerr << fileCluster << "', must in " << array << endl;
92 return false;
93 }
94 DirectoryInfo info = { limitKey, fileCluster, dirPath, keyParams, type };
95 if (callback && !callback(info)) {
96 return false;
97 }
98 }
99 return true;
100 }
101 }
102 }
103 }
104