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