• 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 <cstdio>
17 #include <string>
18 #include <unistd.h>
19 #include <vector>
20 #include "fs_manager/mount.h"
21 #include "log.h"
22 #include "updater/updater_const.h"
23 #include "update_processor.h"
24 #include "update_processor_stream.h"
25 #include "utils.h"
26 
27 using namespace Updater;
28 #ifndef UPDATER_UT
main(int argc,char ** argv)29 int main(int argc, char **argv)
30 {
31     InitLogger("UPDATER_BINARY");
32     if (argc < MINIMAL_ARGC_LIMIT || argc > MAXIMAL_ARGC_LIMIT) {
33         LOG(ERROR) << "Invalid arguments:" << argc;
34         return EXIT_INVALID_ARGS;
35     }
36     bool retry = false;
37     int pipeFd;
38     std::string packagePath;
39     if (argc == 2) { // 2: package, pipe
40         packagePath = argv[0];
41         pipeFd = static_cast<int>(std::strtol(argv[1], nullptr, DECIMAL));
42     } else if (argc == 3) { // 3 a: package, pipe, retry
43         if (strcmp(argv[2], "retry") == 0) { // 2: retry index
44             retry = true;
45         }
46         packagePath = argv[0];
47         pipeFd = static_cast<int>(std::strtol(argv[1], nullptr, DECIMAL));
48     } else { // 4 a: binary, package, pipe, retry=1/retry=0
49         packagePath = argv[1];
50         pipeFd = static_cast<int>(std::strtol(argv[2], nullptr, DECIMAL)); // 2: pipe index
51         retry = strcmp(argv[3], "retry=0") == 0 ? false : true; // 3: retry index
52     }
53 
54     // Re-load fstab to child process.
55     LoadFstab();
56 
57     // 根据 packagePath 的后缀选择执行不同的函数
58     std::string fileExtension;
59     size_t dotPosition = packagePath.find_last_of(".");
60     if (dotPosition == std::string::npos) {
61         LOG(ERROR) << "Invalid packagePath: No extension found in " << packagePath;
62         return EXIT_INVALID_ARGS;
63     }
64     fileExtension = packagePath.substr(dotPosition + 1);
65     std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
66     if (fileExtension == "zip") {
67         return ProcessUpdater(retry, pipeFd, packagePath, Utils::GetCertName());
68     } else if (fileExtension == "bin") {
69         return ProcessUpdaterStream(retry, pipeFd, packagePath, Utils::GetCertName());
70     } else {
71         LOG(ERROR) << "Invalid packagePath:" << packagePath;
72         return EXIT_INVALID_ARGS;
73     }
74 }
75 #endif
76