1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 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 INCLUDE_TUNING_BASE_FILE_UTILS_H 17 #define INCLUDE_TUNING_BASE_FILE_UTILS_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 #include <filesystem> 24 25 #include "contrib/minizip/unzip.h" 26 27 namespace SysTuning { 28 namespace base { 29 #define TS_PERMISSION_RW 0600 30 #define TS_PERMISSION_RWX 777 31 #define ZLIB_CMF 0x78 32 #define ZLIB_FLG 0x9C 33 #define ZLIB_MAGIC_NUM_LEN 2 34 constexpr uint32_t K_FILE_MODE_INVALID = 0xFFFFFFFF; 35 enum TraceParserStatus { 36 TRACE_PARSER_NORMAL = 0, 37 TRACE_PARSER_FILE_TYPE_ERROR = 1, 38 TRACE_PARSE_ERROR = 2, 39 TRACE_PARSER_ABNORMAL = 3 40 }; 41 42 void SetAnalysisResult(TraceParserStatus stat); 43 44 TraceParserStatus GetAnalysisResult(); 45 46 ssize_t Read(int32_t fd, uint8_t *dst, size_t dstSize); 47 48 int32_t OpenFile(const std::string &path, int32_t flags, uint32_t mode = K_FILE_MODE_INVALID); 49 50 std::string GetExecutionDirectoryPath(); 51 52 #if defined(is_linux) || defined(_WIN32) 53 std::vector<std::string> GetFilesNameFromDir(const std::string &path, bool onlyFileName = true); 54 #endif 55 56 bool UnZipFile(const std::string &zipFile, std::string &traceFile); 57 bool UnZlibFile(const std::string &zlibFile, std::string &traceFile); 58 59 bool LocalUnzip(const std::string &zipFile, const std::string &dstDir); 60 61 class LocalZip { 62 public: 63 explicit LocalZip(const std::string &file); 64 bool Unzip(std::string &traceFile); 65 bool Unzlib(std::string &traceFile); 66 67 private: 68 bool IsFileExist(); 69 bool IsZipFile(); 70 bool IsZlibFile(); 71 static bool CreateDir(const std::filesystem::path &dirName, bool del = false); 72 bool WriteFile(const unzFile &uzf, const std::filesystem::path &fileName); 73 bool WriteFile(std::ifstream &srcFile, std::ofstream &destFile); 74 std::string filePath_; 75 std::string tmpDir_; 76 uint32_t bufSize_ = 512000000; 77 uint8_t magicNumLen_ = 2; 78 std::unique_ptr<char[]> buf_; 79 }; 80 81 } // namespace base 82 } // namespace SysTuning 83 #endif // INCLUDE_TUNING_BASE_FILE_UTILS_H_ 84