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> 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& extData = ""); 68 void DoShutdown(); 69 std::string GetCertName(); 70 bool WriteFully(int fd, const uint8_t *data, size_t size); 71 bool ReadFully(int fd, void* data, size_t size); 72 bool ReadFileToString(int fd, std::string &content); 73 bool CopyFile(const std::string &src, const std::string &dest, bool isAppend = false); 74 bool CopyDir(const std::string &srcPath, const std::string &dstPath); 75 bool WriteStringToFile(int fd, const std::string& content); 76 std::string GetLocalBoardId(); 77 bool CopyUpdaterLogs(const std::string &sLog, const std::string &dLog); 78 void CompressFiles(std::vector<std::string> &files, const std::string &zipFile); 79 void CompressLogs(const std::string &name); 80 bool CheckResultFail(); 81 void WriteDumpResult(const std::string &result, const std::string &fileName); 82 long long int GetDirSize(const std::string &folderPath); 83 size_t GetFileSize(const std::string &filePath); 84 long long int GetDirSizeForFile(const std::string &filePath); 85 bool DeleteOldFile(const std::string dest); 86 void SaveLogs(); 87 std::vector<std::string> ParseParams(int argc, char **argv); 88 bool CheckUpdateMode(const std::string &mode); 89 std::string DurationToString(std::vector<std::chrono::duration<double>> &durations, std::size_t pkgPosition, 90 int precision = 2); 91 std::string GetRealPath(const std::string &path); 92 std::string GetPartitionRealPath(const std::string &name); 93 void SetMessageToMisc(const std::string &miscCmd, const int message, const std::string headInfo); 94 bool CheckFaultInfo(const std::string &faultInfo); 95 void SetCmdToMisc(const std::string &miscCmd); 96 void AddUpdateInfoToMisc(const std::string headInfo, const std::optional<int> message); 97 void RemoveUpdateInfoFromMisc(const std::string &headInfo); 98 void SetFaultInfoToMisc(const std::string &faultInfo); 99 void GetTagValInStr(const std::string& str, const std::string &tag, std::string &val); 100 bool IsValidHexStr(const std::string &str); 101 void TrimString (std::string &str); 102 bool RestoreconPath(const std::string &path); 103 std::string TrimUpdateMode(const std::string &mode); 104 bool IsEsDevice(); 105 #ifndef __WIN32 106 void SetFileAttributes(const std::string& file, uid_t owner, gid_t group, mode_t mode); 107 #endif 108 109 #ifdef __cplusplus 110 #if __cplusplus 111 extern "C" { 112 #endif 113 #endif 114 int SetParameter(const char *key, const char *value); 115 #ifdef __cplusplus 116 #if __cplusplus 117 } 118 #endif 119 #endif 120 } // Utils 121 #ifdef __cplusplus 122 #if __cplusplus 123 extern "C" { 124 #endif 125 #endif 126 void InitLogger(const std::string &tag); 127 #ifdef __cplusplus 128 #if __cplusplus 129 } 130 #endif 131 #endif 132 } // Updater 133 #endif // UPDATER_UTILS_H 134