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 39 class Timer { 40 public: Timer()41 Timer() : start_(std::chrono::steady_clock::now()) {} 42 duration()43 std::chrono::milliseconds duration() const 44 { 45 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_); 46 } 47 private: 48 std::chrono::steady_clock::time_point start_; 49 }; 50 std::ostream &operator<<(std::ostream &os, const Timer &timer); 51 } // namespace SysInstaller 52 } // namespace OHOS 53 #endif // SYS_INSTALLER_MODULE_UTILS_H 54