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