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