1/* 2 * Copyright (c) 2024-2024 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 "thread_pool.h" 26#include "signature_tools_log.h" 27 28namespace OHOS { 29namespace SignatureTools { 30 31class FileUtils { 32public: 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 /** 70 * Read byte from input file. 71 * 72 * @param file input file 73 * @param offset offset 74 * @param length length 75 * @param ret data bytes 76 * @return 0:success <0:error 77 */ 78 static int ReadFileByOffsetAndLength(std::ifstream& file, size_t offset, size_t length, std::string& ret); 79 /** 80 * Read byte from input stream. 81 * 82 * @param input input stream 83 * @param offset offset 84 * @param length length 85 * @ret data bytes 86 * @return 0:success <0 :error 87 */ 88 static int ReadInputByOffsetAndLength(std::ifstream& input, size_t offset, size_t length, std::string& ret); 89 /** 90 * Read byte from input stream. 91 * 92 * @param input InputStream 93 * @param length length 94 * @ret data bytes 95 * @return 0:success <0 :error 96 */ 97 static int ReadInputByLength(std::ifstream& input, size_t length, std::string& ret); 98 /** 99 * Write data in file to output stream 100 * 101 * @param inFile input file path. 102 * @param out output file path. 103 * @param offset file read offset 104 * @param size file read size 105 * @return true, if write successfully. 106 */ 107 static bool AppendWriteFileByOffsetToFile(std::ifstream& input, std::ofstream& out, size_t offset, size_t size); 108 static bool AppendWriteFileToFile(const std::string& inputFile, const std::string& outputFile); 109 static bool AppendWriteByteToFile(const std::string& bytes, const std::string& outputFile); 110 111 static int WriteInputToOutPut(std::ifstream& input, std::ofstream& output, size_t length); 112 static bool WriteInputToOutPut(const std::string& input, const std::string& output); 113 static bool IsSpaceEnough(const std::string& filePath, const int64_t requiredSpace); 114 /** 115* Write byte array data to output file. 116* 117* @param bytes byte array data. 118* @param outFile output file path. 119* @return true, if write successfully. 120*/ 121 static bool WriteByteToOutFile(const std::string& bytes, const std::string& outFile); 122 /** 123* Write byte array data to output file. 124* 125* @param bytes byte array data. 126* @param outFile output file path. 127* @return true, if write successfully. 128*/ 129 static bool WriteByteToOutFile(const std::string& bytes, std::ofstream& outFile); 130 static bool WriteByteToOutFile(const std::vector<int8_t>& bytes, std::ofstream& outFile); 131 /** 132 * regex filename 133 * 134 * @param name filename 135 * @return boolean 136 */ 137 static bool IsRunnableFile(const std::string& name); 138 static int64_t GetFileLen(const std::string& file); 139 static void DelDir(const std::string& file); 140}; 141} // namespace SignatureTools 142} // namespace OHOS 143#endif // SIGNATRUETOOLS_FILE_UTILS_H