1 /* 2 * Copyright (c) 2025-2025 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 SIGNATRUETOOLS_FILE_UTILS_H 16 #define SIGNATRUETOOLS_FILE_UTILS_H 17 #include <string> 18 #include <vector> 19 #include <regex> 20 #include <cassert> 21 #include <sys/statfs.h> 22 #include <cstdlib> 23 #include <iostream> 24 25 #include "byte_buffer.h" 26 #include "signature_tools_log.h" 27 28 namespace OHOS { 29 namespace SignatureTools { 30 31 class FileUtils { 32 public: 33 static const int NUM_TWO; 34 static const int NUM_THREE; 35 static const int NUM_FOUR; 36 static const std::string LIBS_PATH_PREFIX; 37 static constexpr int FILE_BUFFER_BLOCK = 1024 * 1024; 38 FileUtils() = delete; 39 // judge is or not empty 40 static bool IsEmpty(std::string cs); 41 // get file suffix 42 static std::string GetSuffix(std::string filePath); 43 // verify file type 44 static bool ValidFileType(const std::string& filePath, const std::initializer_list<std::string> types); 45 static bool IsValidFile(std::string file); 46 /** 47 * write content to output file 48 * @param content data to write 49 * @param output path 50 * @return 0:success 51 */ 52 static int Write(const std::string& content, const std::string& output); 53 /** 54 * Read byte from input stream. 55 * 56 * @param input Input stream 57 * @param ret File content 58 * @return 0:success <0:error 59 */ 60 static int Read(std::ifstream& input, std::string& ret); 61 /** 62 *Read byte from input file. 63 * 64 * @param file Which file to read 65 * @param ret byte content 66 * @return 0:success <0:error 67 */ 68 static int ReadFile(const std::string& path, std::string& ret); 69 static bool ReadFileToByteBuffer(const std::string& file, ByteBuffer& buffer); 70 static bool isElfFile(const std::string& filePath); 71 /** 72 * Read byte from input file. 73 * 74 * @param file input file 75 * @param offset offset 76 * @param length length 77 * @param ret data bytes 78 * @return 0:success <0:error 79 */ 80 static int ReadFileByOffsetAndLength(std::ifstream& file, size_t offset, size_t length, std::string& ret); 81 /** 82 * Read byte from input stream. 83 * 84 * @param input input stream 85 * @param offset offset 86 * @param length length 87 * @ret data bytes 88 * @return 0:success <0 :error 89 */ 90 static int ReadInputByOffsetAndLength(std::ifstream& input, size_t offset, size_t length, std::string& ret); 91 /** 92 * Read byte from input stream. 93 * 94 * @param input InputStream 95 * @param length length 96 * @ret data bytes 97 * @return 0:success <0 :error 98 */ 99 static int ReadInputByLength(std::ifstream& input, size_t length, std::string& ret); 100 /** 101 * Write data in file to output stream 102 * 103 * @param inFile input file path. 104 * @param out output file path. 105 * @param offset file read offset 106 * @param size file read size 107 * @return true, if write successfully. 108 */ 109 static int WriteInputToOutPut(std::ifstream& input, std::ofstream& output, size_t length); 110 static bool WriteInputToOutPut(const std::string& input, const std::string& output); 111 112 /** 113 * regex filename 114 * 115 * @param name filename 116 * @return boolean 117 */ 118 static bool IsRunnableFile(const std::string& name); 119 static int64_t GetFileLen(const std::string& file); 120 static void DelDir(const std::string& file); 121 static bool CopyTmpFileAndDel(const std::string& tmpFile, const std::string& output); 122 }; 123 } // namespace SignatureTools 124 } // namespace OHOS 125 #endif // SIGNATRUETOOLS_FILE_UTILS_H