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 "applypatch/command_function.h"
17 #include "command_process.h"
18
19 namespace Updater {
RegistBlockUpdateCommandFunction(void)20 extern "C" __attribute__((constructor)) void RegistBlockUpdateCommandFunction(void)
21 {
22 const std::unordered_map<std::string, std::function<std::unique_ptr<CommandFunction>()>> COMMANDFUNC = {
23 { "abort", []() { return std::make_unique<AbortCommandFn>(); } },
24 { "bsdiff", []() { return std::make_unique<DiffAndMoveCommandFn>(); } },
25 { "new", []() { return std::make_unique<NewCommandFn>(); } },
26 { "pkgdiff", []() { return std::make_unique<DiffAndMoveCommandFn>(); } },
27 { "zero", []() { return std::make_unique<ZeroAndEraseCommandFn>(); } },
28 { "erase", []() { return std::make_unique<ZeroAndEraseCommandFn>(); } },
29 { "free", []() { return std::make_unique<FreeCommandFn>(); } },
30 { "move", []() { return std::make_unique<DiffAndMoveCommandFn>(); } },
31 { "stash", []() { return std::make_unique<StashCommandFn>(); } },
32 { "copy", []() { return std::make_unique<DiffAndMoveCommandFn>(); } }
33 };
34 for (auto &iter : COMMANDFUNC) {
35 CommandFunctionFactory::GetInstance().RegistCommandFunction(iter.first, iter.second());
36 }
37 }
38
GetInstance()39 CommandFunctionFactory &CommandFunctionFactory::GetInstance()
40 {
41 static CommandFunctionFactory instance;
42 return instance;
43 }
44
GetCommandFunction(std::string command)45 CommandFunction* CommandFunctionFactory::GetCommandFunction(std::string command)
46 {
47 auto iter = commandFunctionMap_.find(command);
48 if (iter != commandFunctionMap_.end()) {
49 return iter->second.get();
50 }
51 return nullptr;
52 }
53
RegistCommandFunction(std::string command,std::unique_ptr<CommandFunction> commandFunction)54 void CommandFunctionFactory::RegistCommandFunction(std::string command,
55 std::unique_ptr<CommandFunction> commandFunction)
56 {
57 commandFunctionMap_.emplace(command, std::move(commandFunction));
58 }
59 }