• 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 #ifndef UPDATER_UTILS_H
16 #define UPDATER_UTILS_H
17 
18 #include <cerrno>
19 #include <string>
20 #include <sys/types.h>
21 #include <vector>
22 
23 namespace Updater {
24 namespace Utils {
25 constexpr int N_BIN = 2;
26 constexpr int N_OCT = 8;
27 constexpr int N_DEC = 10;
28 constexpr int N_HEX = 16;
29 constexpr int O_USER_GROUP_ID = 1000;
30 constexpr int ARGC_TWO_NUMS = 2;
31 constexpr int USER_ROOT_AUTHORITY = 0;
32 constexpr int USER_UPDATE_AUTHORITY = 6666;
33 constexpr int GROUP_SYS_AUTHORITY = 1000;
34 constexpr int GROUP_UPDATE_AUTHORITY = 6666;
35 template<class T>
36 T String2Int(const std::string &str, int base = N_HEX)
37 {
38     char *end = nullptr;
39     if (str.empty()) {
40         errno = EINVAL;
41         return 0;
42     }
43     if (((str[0] == '0') && (str[1] == 'x')) || (str[1] == 'X')) {
44         base = N_HEX;
45     }
46     T result = strtoull(str.c_str(), &end, base);
47     return result;
48 }
49 int32_t DeleteFile(const std::string& filename);
50 int MkdirRecursive(const std::string &pathName, mode_t mode);
51 int64_t GetFilesFromDirectory(const std::string &path, std::vector<std::string> &files, bool isRecursive = false);
52 std::vector<std::string> SplitString(const std::string &str, const std::string del = " \t");
53 std::string Trim(const std::string &str);
54 std::string ConvertSha256Hex(const uint8_t* shaDigest, size_t length);
55 void DoReboot(const std::string& rebootTarget, const std::string& extData = "");
56 void DoShutdown();
57 std::string GetCertName();
58 bool WriteFully(int fd, const uint8_t *data, size_t size);
59 bool ReadFully(int fd, void* data, size_t size);
60 bool ReadFileToString(int fd, std::string &content);
61 bool CopyFile(const std::string &src, const std::string &dest);
62 bool WriteStringToFile(int fd, const std::string& content);
63 std::string GetLocalBoardId();
64 bool CopyUpdaterLogs(const std::string &sLog, const std::string &dLog);
65 void CompressLogs(const std::string &name);
66 bool CheckDumpResult();
67 void WriteDumpResult(const std::string &result);
68 bool PathToRealPath(const std::string &path, std::string &realPath);
69 void UsSleep(int usec);
70 bool IsUpdaterMode();
71 bool RemoveDir(const std::string &path);
72 bool IsFileExist(const std::string &path);
73 bool IsDirExist(const std::string &path);
74 } // Utils
75 } // Updater
76 #endif // UPDATER_UTILS_H
77