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_module_inc.h"
17 #include<iostream>
18 #include "factory_resource_compiler.h"
19 #include "file_entry.h"
20 #include "increment_index.h"
21 #include "restool_errors.h"
22
23 namespace OHOS {
24 namespace Global {
25 namespace Restool {
26 using namespace std;
ResourceModuleInc(const string & modulePath,const string & moduleOutput,const string & moduleName,const vector<string> & folder)27 ResourceModuleInc::ResourceModuleInc(const string &modulePath, const string &moduleOutput,
28 const string &moduleName, const vector<string> &folder)
29 : ResourceModule(modulePath, moduleOutput, moduleName), folder_(folder)
30 {
31 }
32
ScanResource(const vector<IncrementList::FileIncrement> & fileIncrements)33 uint32_t ResourceModuleInc::ScanResource(const vector<IncrementList::FileIncrement> &fileIncrements)
34 {
35 vector<string> skips;
36 for (const auto &fileIncrement : fileIncrements) {
37 skips.push_back(fileIncrement.filePath);
38 if (fileIncrement.dirType == ResType::ELEMENT) {
39 continue;
40 }
41 string filePathDel = FileEntry::FilePath(moduleOutput_).Append(RESOURCES_DIR)
42 .Append(fileIncrement.limitKey).Append(fileIncrement.fileCluster).Append(fileIncrement.filename).GetPath();
43 if (ResourceUtil::NeedConverToSolidXml(fileIncrement.dirType) &&
44 FileEntry::FilePath(filePathDel).GetExtension() == ".xml") {
45 filePathDel = FileEntry::FilePath(filePathDel).ReplaceExtension(".sxml").GetPath();
46 }
47 if (remove(filePathDel.c_str()) != 0) {
48 cerr << "Error: remove failed '" << filePathDel << "', reason: " << strerror(errno) << endl;
49 return RESTOOL_ERROR;
50 }
51 }
52
53 string indexPath = FileEntry::FilePath(moduleOutput_).Append(IncrementIndex::INDEX_FILE).GetPath();
54 IncrementIndex moduleIndex(indexPath, folder_);
55 moduleIndex.SetSkipPaths(skips);
56 if (!moduleIndex.Load(owner_)) {
57 return RESTOOL_ERROR;
58 }
59
60 for (const auto &fileIncrement : fileIncrements) {
61 unique_ptr<IResourceCompiler> resourceCompiler =
62 FactoryResourceCompiler::CreateCompiler(fileIncrement.dirType, moduleOutput_);
63 resourceCompiler->SetModuleName(moduleName_);
64 if (resourceCompiler->Compile(fileIncrement) != RESTOOL_SUCCESS) {
65 return RESTOOL_ERROR;
66 }
67
68 if (MergeResourceItem(owner_, resourceCompiler->GetResult(), true) != RESTOOL_SUCCESS) {
69 return RESTOOL_ERROR;
70 }
71 }
72 return RESTOOL_SUCCESS;
73 }
74
SaveIndex() const75 uint32_t ResourceModuleInc::SaveIndex() const
76 {
77 string indexPath = FileEntry::FilePath(moduleOutput_).Append(IncrementIndex::INDEX_FILE).GetPath();
78 IncrementIndex moduleIndex(indexPath, folder_);
79 if (!moduleIndex.Save(owner_)) {
80 return RESTOOL_ERROR;
81 }
82 return RESTOOL_SUCCESS;
83 }
84 }
85 }
86 }