• 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 "utils_fs.h"
19 #include "utils_common.h"
20 #include <cerrno>
21 #include <optional>
22 #include <string>
23 #include <sys/types.h>
24 #include <vector>
25 
26 namespace Updater {
27 enum SlotType {
28     NOT_AB,
29     SLOT_A,
30     SLOT_B,
31 };
32 namespace Utils {
33 constexpr int N_BIN = 2;
34 constexpr int N_OCT = 8;
35 constexpr int N_DEC = 10;
36 constexpr int N_HEX = 16;
37 constexpr int O_USER_GROUP_ID = 1000;
38 constexpr int ARGC_TWO_NUMS = 2;
39 constexpr int USER_ROOT_AUTHORITY = 0;
40 constexpr int USER_UPDATE_AUTHORITY = 6666;
41 constexpr int GROUP_SYS_AUTHORITY = 1000;
42 constexpr int GROUP_UPDATE_AUTHORITY = 6666;
43 constexpr int GROUP_ROOT_AUTHORITY = 0;
44 constexpr const char* ON_SERVER = "ON_SERVER";
45 template<class T>
46 T String2Int(const std::string &str, int base = N_HEX)
47 {
48     static_assert(std::is_same_v<T, int> || std::is_same_v<T, size_t> || std::is_same_v<T, unsigned long long int>,
49                   "type should be int or size_t or unsigned long long int");
50     char *end = nullptr;
51     if (str.empty()) {
52         errno = EINVAL;
53         return 0;
54     }
55     if (((str[0] == '0') && (str[1] == 'x')) || (str[1] == 'X')) {
56         base = N_HEX;
57     }
58     T result = 0;
59     if constexpr (std::is_same_v<T, int>) {
60         result = strtol(str.c_str(), &end, base);
61     } else if constexpr (std::is_same_v<T, size_t> || std::is_same_v<T, unsigned long long int>) {
62         result = strtoull(str.c_str(), &end, base);
63     } else {
64         errno = EINVAL;
65     }
66     return result;
67 }
68 int32_t DeleteFile(const std::string& filename);
69 std::vector<std::string> SplitString(const std::string &str, const std::string del = " \t");
70 std::string Trim(const std::string &str);
71 std::string ConvertSha256Hex(const uint8_t* shaDigest, size_t length);
72 void UpdaterDoReboot(const std::string& rebootTarget, const std::string &rebootReason,
73     const std::string& extData = "");
74 void DoShutdown(const std::string &shutdownReason);
75 std::string GetCertName();
76 bool WriteFully(int fd, const uint8_t *data, size_t size);
77 bool ReadFully(int fd, void* data, size_t size);
78 bool ReadFileToString(int fd, std::string &content);
79 bool ReadStringFromProcFile(const std::string &filePath, std::string &content);
80 bool CopyFile(const std::string &src, const std::string &dest, bool isAppend = false);
81 bool CopyDir(const std::string &srcPath, const std::string &dstPath);
82 bool WriteStringToFile(int fd, const std::string& content);
83 std::string GetLocalBoardId();
84 bool CopyUpdaterLogs(const std::string &sLog, const std::string &dLog);
85 void CompressFiles(std::vector<std::string> &files, const std::string &zipFile);
86 void CompressLogs(const std::string &name);
87 bool CheckResultFail();
88 void WriteDumpResult(const std::string &result, const std::string &fileName);
89 long long int GetDirSize(const std::string &folderPath);
90 size_t GetFileSize(const std::string &filePath);
91 long long int GetDirSizeForFile(const std::string &filePath);
92 bool DeleteOldFile(const std::string dest);
93 void SaveLogs();
94 std::vector<std::string> ParseParams(int argc, char **argv);
95 bool CheckUpdateMode(const std::string &mode);
96 std::string DurationToString(std::vector<std::chrono::duration<double>> &durations, std::size_t pkgPosition,
97     int precision = 2);
98 std::string GetRealPath(const std::string &path);
99 std::string GetPartitionRealPath(const std::string &name);
100 void SetMessageToMisc(const std::string &miscCmd, const int message, const std::string headInfo);
101 bool CheckFaultInfo(const std::string &faultInfo);
102 void SetCmdToMisc(const std::string &miscCmd);
103 void AddUpdateInfoToMisc(const std::string headInfo, const std::optional<int> message);
104 void RemoveUpdateInfoFromMisc(const std::string &headInfo);
105 void SetFaultInfoToMisc(const std::string &faultInfo);
106 void GetTagValInStr(const std::string& str, const std::string &tag, std::string &val);
107 bool IsValidHexStr(const std::string &str);
108 void TrimString (std::string &str);
109 bool RestoreconPath(const std::string &path);
110 std::string TrimUpdateMode(const std::string &mode);
111 bool IsEsDevice();
112 bool ConvertToUnsignedLongLong(const std::string &str, uint64_t &value);
113 bool ConvertToLongLong(const std::string &str, int64_t &value);
114 bool ConvertToLong(const std::string &str, int32_t &value);
115 bool ConvertToUnsignedLong(const std::string &str, uint32_t &value, int base = 0);
116 bool ConvertToDouble(const std::string &str, double &value);
117 bool ConvertToFloat(const std::string &str, float &value);
118 bool IsVabDevice();
119 bool SetUpdateSlot(int updateSlot);
120 bool SetUpdateSuffix(std::string stringsuffix);
121 int GetUpdateSlot();
122 std::string GetUpdateSuffix();
123 std::string GetUpdateActiveSuffix();
124 std::vector<pid_t> GetAllTids(pid_t pid);
125 std::string VectorToString(const std::vector<pid_t> &pids);
126 #ifndef __WIN32
127 void SetFileAttributes(const std::string& file, uid_t owner, gid_t group, mode_t mode);
128 #endif
129 
130 #ifdef __cplusplus
131 #if __cplusplus
132 extern "C" {
133 #endif
134 #endif
135 int SetParameter(const char *key, const char *value);
136 #ifdef __cplusplus
137 #if __cplusplus
138 }
139 #endif
140 #endif
141 } // Utils
142 #ifdef __cplusplus
143 #if __cplusplus
144 extern "C" {
145 #endif
146 #endif
147 void InitLogger(const std::string &tag);
148 #ifdef __cplusplus
149 #if __cplusplus
150 }
151 #endif
152 #endif
153 } // Updater
154 #endif // UPDATER_UTILS_H
155