• 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 "resource_directory.h"
17 
18 #include <iostream>
19 
20 #include "file_entry.h"
21 #include "resource_util.h"
22 #include "restool_errors.h"
23 #include "select_compile_parse.h"
24 
25 namespace OHOS {
26 namespace Global {
27 namespace Restool {
28 using namespace std;
ScanResources(const string & resourcesDir,function<bool (const DirectoryInfo &)> callback) const29 bool ResourceDirectory::ScanResources(const string &resourcesDir, function<bool(const DirectoryInfo&)> callback) const
30 {
31     FileEntry f(resourcesDir);
32     if (!f.Init()) {
33         return false;
34     }
35 
36     for (const auto &it : f.GetChilds()) {
37         string limitKey = it->GetFilePath().GetFilename();
38         if (ResourceUtil::IsIgnoreFile(limitKey, it->IsFile())) {
39             continue;
40         }
41 
42         if (it->IsFile()) {
43             PrintError(GetError(ERR_CODE_INVALID_RESOURCE_PATH)
44                 .FormatCause(it->GetFilePath().GetPath().c_str(), "not a directory"));
45             return false;
46         }
47 
48         if (limitKey == RAW_FILE_DIR || limitKey == RES_FILE_DIR) {
49             continue;
50         }
51 
52         if (!ScanResourceLimitKeyDir(it->GetFilePath().GetPath(), limitKey, callback)) {
53             return false;
54         }
55     }
56     return true;
57 }
58 
59 // below private
ScanResourceLimitKeyDir(const string & resourceTypeDir,const string & limitKey,function<bool (const DirectoryInfo &)> callback) const60 bool ResourceDirectory::ScanResourceLimitKeyDir(const string &resourceTypeDir, const string &limitKey,
61     function<bool(const DirectoryInfo&)> callback) const
62 {
63     vector<KeyParam> keyParams;
64     if (!KeyParser::Parse(limitKey, keyParams)) {
65         PrintError(GetError(ERR_CODE_INVALID_LIMIT_KEY).FormatCause(limitKey.c_str()).SetPosition(resourceTypeDir));
66         return false;
67     }
68     if (!SelectCompileParse::IsSelectCompile(keyParams)) {
69         return true;
70     }
71     FileEntry f(resourceTypeDir);
72     if (!f.Init()) {
73         return false;
74     }
75     for (const auto &it : f.GetChilds()) {
76         string dirPath = it->GetFilePath().GetPath();
77         string fileCluster = it->GetFilePath().GetFilename();
78         if (ResourceUtil::IsIgnoreFile(fileCluster, it->IsFile())) {
79             continue;
80         }
81 
82         if (it->IsFile()) {
83             PrintError(GetError(ERR_CODE_INVALID_RESOURCE_PATH).FormatCause(dirPath.c_str(), "not a directory"));
84             return false;
85         }
86 
87         ResType type = ResourceUtil::GetResTypeByDir(fileCluster);
88         if (type == ResType::INVALID_RES_TYPE) {
89             PrintError(GetError(ERR_CODE_INVALID_RESOURCE_DIR)
90                            .FormatCause(fileCluster.c_str(), ResourceUtil::GetAllResTypeDirs().c_str())
91                            .SetPosition(dirPath));
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