1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#ifndef UPDATER_UTILS_H 16fb299fa2Sopenharmony_ci#define UPDATER_UTILS_H 17fb299fa2Sopenharmony_ci 18fb299fa2Sopenharmony_ci#include "utils_fs.h" 19fb299fa2Sopenharmony_ci#include "utils_common.h" 20fb299fa2Sopenharmony_ci#include <cerrno> 21fb299fa2Sopenharmony_ci#include <optional> 22fb299fa2Sopenharmony_ci#include <string> 23fb299fa2Sopenharmony_ci#include <sys/types.h> 24fb299fa2Sopenharmony_ci#include <vector> 25fb299fa2Sopenharmony_ci 26fb299fa2Sopenharmony_cinamespace Updater { 27fb299fa2Sopenharmony_cinamespace Utils { 28fb299fa2Sopenharmony_ciconstexpr int N_BIN = 2; 29fb299fa2Sopenharmony_ciconstexpr int N_OCT = 8; 30fb299fa2Sopenharmony_ciconstexpr int N_DEC = 10; 31fb299fa2Sopenharmony_ciconstexpr int N_HEX = 16; 32fb299fa2Sopenharmony_ciconstexpr int O_USER_GROUP_ID = 1000; 33fb299fa2Sopenharmony_ciconstexpr int ARGC_TWO_NUMS = 2; 34fb299fa2Sopenharmony_ciconstexpr int USER_ROOT_AUTHORITY = 0; 35fb299fa2Sopenharmony_ciconstexpr int USER_UPDATE_AUTHORITY = 6666; 36fb299fa2Sopenharmony_ciconstexpr int GROUP_SYS_AUTHORITY = 1000; 37fb299fa2Sopenharmony_ciconstexpr int GROUP_UPDATE_AUTHORITY = 6666; 38fb299fa2Sopenharmony_ciconstexpr int GROUP_ROOT_AUTHORITY = 0; 39fb299fa2Sopenharmony_ciconstexpr const char* ON_SERVER = "ON_SERVER"; 40fb299fa2Sopenharmony_citemplate<class T> 41fb299fa2Sopenharmony_ciT String2Int(const std::string &str, int base = N_HEX) 42fb299fa2Sopenharmony_ci{ 43fb299fa2Sopenharmony_ci static_assert(std::is_same_v<T, int> || std::is_same_v<T, size_t> || std::is_same_v<T, unsigned long long int>, 44fb299fa2Sopenharmony_ci "type should be int or size_t or unsigned long long int"); 45fb299fa2Sopenharmony_ci char *end = nullptr; 46fb299fa2Sopenharmony_ci if (str.empty()) { 47fb299fa2Sopenharmony_ci errno = EINVAL; 48fb299fa2Sopenharmony_ci return 0; 49fb299fa2Sopenharmony_ci } 50fb299fa2Sopenharmony_ci if (((str[0] == '0') && (str[1] == 'x')) || (str[1] == 'X')) { 51fb299fa2Sopenharmony_ci base = N_HEX; 52fb299fa2Sopenharmony_ci } 53fb299fa2Sopenharmony_ci T result = 0; 54fb299fa2Sopenharmony_ci if constexpr (std::is_same_v<T, int>) { 55fb299fa2Sopenharmony_ci result = strtol(str.c_str(), &end, base); 56fb299fa2Sopenharmony_ci } else if constexpr (std::is_same_v<T, size_t> || std::is_same_v<T, unsigned long long int>) { 57fb299fa2Sopenharmony_ci result = strtoull(str.c_str(), &end, base); 58fb299fa2Sopenharmony_ci } else { 59fb299fa2Sopenharmony_ci errno = EINVAL; 60fb299fa2Sopenharmony_ci } 61fb299fa2Sopenharmony_ci return result; 62fb299fa2Sopenharmony_ci} 63fb299fa2Sopenharmony_ciint32_t DeleteFile(const std::string& filename); 64fb299fa2Sopenharmony_cistd::vector<std::string> SplitString(const std::string &str, const std::string del = " \t"); 65fb299fa2Sopenharmony_cistd::string Trim(const std::string &str); 66fb299fa2Sopenharmony_cistd::string ConvertSha256Hex(const uint8_t* shaDigest, size_t length); 67fb299fa2Sopenharmony_civoid UpdaterDoReboot(const std::string& rebootTarget, const std::string &rebootReason, 68fb299fa2Sopenharmony_ci const std::string& extData = ""); 69fb299fa2Sopenharmony_civoid DoShutdown(const std::string &shutdownReason); 70fb299fa2Sopenharmony_cistd::string GetCertName(); 71fb299fa2Sopenharmony_cibool WriteFully(int fd, const uint8_t *data, size_t size); 72fb299fa2Sopenharmony_cibool ReadFully(int fd, void* data, size_t size); 73fb299fa2Sopenharmony_cibool ReadFileToString(int fd, std::string &content); 74fb299fa2Sopenharmony_cibool CopyFile(const std::string &src, const std::string &dest, bool isAppend = false); 75fb299fa2Sopenharmony_cibool CopyDir(const std::string &srcPath, const std::string &dstPath); 76fb299fa2Sopenharmony_cibool WriteStringToFile(int fd, const std::string& content); 77fb299fa2Sopenharmony_cistd::string GetLocalBoardId(); 78fb299fa2Sopenharmony_cibool CopyUpdaterLogs(const std::string &sLog, const std::string &dLog); 79fb299fa2Sopenharmony_civoid CompressFiles(std::vector<std::string> &files, const std::string &zipFile); 80fb299fa2Sopenharmony_civoid CompressLogs(const std::string &name); 81fb299fa2Sopenharmony_cibool CheckResultFail(); 82fb299fa2Sopenharmony_civoid WriteDumpResult(const std::string &result, const std::string &fileName); 83fb299fa2Sopenharmony_cilong long int GetDirSize(const std::string &folderPath); 84fb299fa2Sopenharmony_cisize_t GetFileSize(const std::string &filePath); 85fb299fa2Sopenharmony_cilong long int GetDirSizeForFile(const std::string &filePath); 86fb299fa2Sopenharmony_cibool DeleteOldFile(const std::string dest); 87fb299fa2Sopenharmony_civoid SaveLogs(); 88fb299fa2Sopenharmony_cistd::vector<std::string> ParseParams(int argc, char **argv); 89fb299fa2Sopenharmony_cibool CheckUpdateMode(const std::string &mode); 90fb299fa2Sopenharmony_cistd::string DurationToString(std::vector<std::chrono::duration<double>> &durations, std::size_t pkgPosition, 91fb299fa2Sopenharmony_ci int precision = 2); 92fb299fa2Sopenharmony_cistd::string GetRealPath(const std::string &path); 93fb299fa2Sopenharmony_cistd::string GetPartitionRealPath(const std::string &name); 94fb299fa2Sopenharmony_civoid SetMessageToMisc(const std::string &miscCmd, const int message, const std::string headInfo); 95fb299fa2Sopenharmony_cibool CheckFaultInfo(const std::string &faultInfo); 96fb299fa2Sopenharmony_civoid SetCmdToMisc(const std::string &miscCmd); 97fb299fa2Sopenharmony_civoid AddUpdateInfoToMisc(const std::string headInfo, const std::optional<int> message); 98fb299fa2Sopenharmony_civoid RemoveUpdateInfoFromMisc(const std::string &headInfo); 99fb299fa2Sopenharmony_civoid SetFaultInfoToMisc(const std::string &faultInfo); 100fb299fa2Sopenharmony_civoid GetTagValInStr(const std::string& str, const std::string &tag, std::string &val); 101fb299fa2Sopenharmony_cibool IsValidHexStr(const std::string &str); 102fb299fa2Sopenharmony_civoid TrimString (std::string &str); 103fb299fa2Sopenharmony_cibool RestoreconPath(const std::string &path); 104fb299fa2Sopenharmony_cistd::string TrimUpdateMode(const std::string &mode); 105fb299fa2Sopenharmony_cibool IsEsDevice(); 106fb299fa2Sopenharmony_ci#ifndef __WIN32 107fb299fa2Sopenharmony_civoid SetFileAttributes(const std::string& file, uid_t owner, gid_t group, mode_t mode); 108fb299fa2Sopenharmony_ci#endif 109fb299fa2Sopenharmony_ci 110fb299fa2Sopenharmony_ci#ifdef __cplusplus 111fb299fa2Sopenharmony_ci#if __cplusplus 112fb299fa2Sopenharmony_ciextern "C" { 113fb299fa2Sopenharmony_ci#endif 114fb299fa2Sopenharmony_ci#endif 115fb299fa2Sopenharmony_ciint SetParameter(const char *key, const char *value); 116fb299fa2Sopenharmony_ci#ifdef __cplusplus 117fb299fa2Sopenharmony_ci#if __cplusplus 118fb299fa2Sopenharmony_ci} 119fb299fa2Sopenharmony_ci#endif 120fb299fa2Sopenharmony_ci#endif 121fb299fa2Sopenharmony_ci} // Utils 122fb299fa2Sopenharmony_ci#ifdef __cplusplus 123fb299fa2Sopenharmony_ci#if __cplusplus 124fb299fa2Sopenharmony_ciextern "C" { 125fb299fa2Sopenharmony_ci#endif 126fb299fa2Sopenharmony_ci#endif 127fb299fa2Sopenharmony_civoid InitLogger(const std::string &tag); 128fb299fa2Sopenharmony_ci#ifdef __cplusplus 129fb299fa2Sopenharmony_ci#if __cplusplus 130fb299fa2Sopenharmony_ci} 131fb299fa2Sopenharmony_ci#endif 132fb299fa2Sopenharmony_ci#endif 133fb299fa2Sopenharmony_ci} // Updater 134fb299fa2Sopenharmony_ci#endif // UPDATER_UTILS_H 135