1 /* 2 * Copyright (c) 2023 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 16 #ifndef SYS_INSTALLER_MODULE_UTILS_H 17 #define SYS_INSTALLER_MODULE_UTILS_H 18 19 #include <sstream> 20 #include <string> 21 #include <sys/stat.h> 22 #include <unistd.h> 23 #include <vector> 24 25 namespace OHOS { 26 namespace SysInstaller { 27 bool CreateDirIfNeeded(const std::string &path, mode_t mode); 28 bool CheckPathExists(const std::string &path); 29 bool CheckFileSuffix(const std::string &file, const std::string &suffix); 30 std::string GetFileName(const std::string &file); 31 std::string GetHmpName(const std::string &filePath); 32 bool WaitForFile(const std::string &path, const std::chrono::nanoseconds &timeout); 33 bool StartsWith(const std::string &str, const std::string &prefix); 34 bool ReadFullyAtOffset(int fd, uint8_t *data, size_t count, off_t offset); 35 uint16_t ReadLE16(const uint8_t *buff); 36 uint32_t ReadLE32(const uint8_t *buff); 37 std::string GetRealPath(const std::string &path); 38 void Revert(const std::string &hmpName, bool reboot); 39 bool IsHotSa(int32_t saId); 40 bool IsRunning(int32_t saId); 41 bool IsHotHmpPackage(int32_t type); 42 bool IsHotHmpPackage(const std::string &hmpName); 43 bool CheckBootComplete(void); 44 void RemoveSpecifiedDir(const std::string &path); 45 std::string GetDeviceSaSdkVersion(void); 46 int GetDeviceApiVersion(void); 47 std::string GetContentFromZip(const std::string &zipPath, const std::string &fileName); 48 bool CheckAndUpdateRevertResult(const std::string &hmpPath, const std::string &resultInfo, const std::string &keyWord); 49 void KillProcessOnArkWeb(void); 50 bool InstallHmpBundle(const std::string &hmpPath, bool revert); 51 52 class Timer { 53 public: Timer()54 Timer() : start_(std::chrono::steady_clock::now()) {} 55 duration()56 std::chrono::milliseconds duration() const 57 { 58 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_); 59 } 60 private: 61 std::chrono::steady_clock::time_point start_; 62 }; 63 std::ostream &operator<<(std::ostream &os, const Timer &timer); 64 } // namespace SysInstaller 65 } // namespace OHOS 66 #endif // SYS_INSTALLER_MODULE_UTILS_H 67