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 "module_combine.h"
17 #include<algorithm>
18 #include<iostream>
19 #include "file_entry.h"
20 #include "key_manager.h"
21 #include "resource_util.h"
22 #include "solid_xml.h"
23
24 namespace OHOS {
25 namespace Global {
26 namespace Restool {
27 using namespace std;
ModuleCombine(const string & modulePath,const string & outputPath)28 ModuleCombine::ModuleCombine(const string &modulePath, const string &outputPath)
29 : modulePath_(modulePath), outputPath_(outputPath)
30 {
31 }
32
Combine()33 bool ModuleCombine::Combine()
34 {
35 string resourceDir = FileEntry::FilePath(modulePath_).Append(RESOURCES_DIR).GetPath();
36 ResourceDirectory resourceDirectory;
37 if (!resourceDirectory.ScanResources(resourceDir, [this](const DirectoryInfo &info) -> bool {
38 return CombineDirectory(info);
39 })) {
40 return false;
41 }
42 return true;
43 }
44
45 // below private
CombineDirectory(const DirectoryInfo & directoryInfo)46 bool ModuleCombine::CombineDirectory(const DirectoryInfo &directoryInfo)
47 {
48 string outputFolder = FileEntry::FilePath(outputPath_).Append(RESOURCES_DIR)
49 .Append(directoryInfo.limitKey).Append(directoryInfo.fileCluster).GetPath();
50 if (!ResourceUtil::CreateDirs(outputFolder)) {
51 return false;
52 }
53
54 map<string, string> sxmlPaths;
55 FileEntry f(directoryInfo.dirPath);
56 if (!f.Init()) {
57 return false;
58 }
59 for (const auto &entry : f.GetChilds()) {
60 string src = entry->GetFilePath().GetPath();
61 if (!entry->IsFile()) {
62 cerr << "Error: " << src << " is directory." << endl;
63 return false;
64 }
65
66 string filename = entry->GetFilePath().GetFilename();
67 string dst = FileEntry::FilePath(outputFolder).Append(filename).GetPath();
68 if (ResourceUtil::FileExist(dst)) {
69 continue;
70 }
71
72 if (entry->GetFilePath().GetExtension() == ".sxml") {
73 sxmlPaths.emplace(src, dst);
74 continue;
75 }
76
77 auto result = find_if (XmlKeyNode::KEY_TO_FILE_NAME.begin(), XmlKeyNode::KEY_TO_FILE_NAME.end(),
78 [&filename](const auto &iter) {
79 return filename == iter.second;
80 });
81 if (result != XmlKeyNode::KEY_TO_FILE_NAME.end()) {
82 continue;
83 }
84
85 if (!ResourceUtil::CopyFleInner(src, dst)) {
86 return true;
87 }
88 }
89 return CombineSolidXml(directoryInfo.dirPath, outputFolder, sxmlPaths);
90 }
91
CombineSolidXml(const string & src,const string & dst,const map<string,string> & sxmlPaths)92 bool ModuleCombine::CombineSolidXml(const string &src, const string &dst, const map<string, string> &sxmlPaths)
93 {
94 if (sxmlPaths.empty()) {
95 return true;
96 }
97
98 KeyManager oldKeyManager;
99 if (!oldKeyManager.LoadKey(src)) {
100 return false;
101 }
102
103 KeyManager newKeyManager;
104 if (!newKeyManager.LoadKey(dst)) {
105 return false;
106 }
107
108 for (const auto &sxmlPath : sxmlPaths) {
109 SolidXml solidXml(sxmlPath.first, oldKeyManager.GetKeys());
110 if (!solidXml.FlushNodeKeys(sxmlPath.second, newKeyManager.GetKeys())) {
111 return false;
112 }
113 }
114
115 if (!newKeyManager.SaveKey(dst)) {
116 cerr << "Error: GenerateToOther " << modulePath_ << " fail." << endl;
117 return false;
118 }
119 return true;
120 }
121 }
122 }
123 }
124