154aa6d63Sopenharmony_ci/* 254aa6d63Sopenharmony_ci * Copyright (c) 2024-2024 Huawei Device Co., Ltd. 354aa6d63Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 454aa6d63Sopenharmony_ci * you may not use this file except in compliance with the License. 554aa6d63Sopenharmony_ci * You may obtain a copy of the License at 654aa6d63Sopenharmony_ci * 754aa6d63Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 854aa6d63Sopenharmony_ci * 954aa6d63Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1054aa6d63Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1154aa6d63Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1254aa6d63Sopenharmony_ci * See the License for the specific language governing permissions and 1354aa6d63Sopenharmony_ci * limitations under the License. 1454aa6d63Sopenharmony_ci */ 1554aa6d63Sopenharmony_ci#ifndef SIGNATRUETOOLS_FILE_UTILS_H 1654aa6d63Sopenharmony_ci#define SIGNATRUETOOLS_FILE_UTILS_H 1754aa6d63Sopenharmony_ci#include <string> 1854aa6d63Sopenharmony_ci#include <vector> 1954aa6d63Sopenharmony_ci#include <regex> 2054aa6d63Sopenharmony_ci#include <cassert> 2154aa6d63Sopenharmony_ci#include <sys/statfs.h> 2254aa6d63Sopenharmony_ci#include <cstdlib> 2354aa6d63Sopenharmony_ci#include <iostream> 2454aa6d63Sopenharmony_ci 2554aa6d63Sopenharmony_ci#include "thread_pool.h" 2654aa6d63Sopenharmony_ci#include "signature_tools_log.h" 2754aa6d63Sopenharmony_ci 2854aa6d63Sopenharmony_cinamespace OHOS { 2954aa6d63Sopenharmony_cinamespace SignatureTools { 3054aa6d63Sopenharmony_ci 3154aa6d63Sopenharmony_ciclass FileUtils { 3254aa6d63Sopenharmony_cipublic: 3354aa6d63Sopenharmony_ci static const int NUM_TWO; 3454aa6d63Sopenharmony_ci static const int NUM_THREE; 3554aa6d63Sopenharmony_ci static const int NUM_FOUR; 3654aa6d63Sopenharmony_ci static const std::string LIBS_PATH_PREFIX; 3754aa6d63Sopenharmony_ci static constexpr int FILE_BUFFER_BLOCK = 1024 * 1024; 3854aa6d63Sopenharmony_ci FileUtils() = delete; 3954aa6d63Sopenharmony_ci // judge is or not empty 4054aa6d63Sopenharmony_ci static bool IsEmpty(std::string cs); 4154aa6d63Sopenharmony_ci // get file suffix 4254aa6d63Sopenharmony_ci static std::string GetSuffix(std::string filePath); 4354aa6d63Sopenharmony_ci // verify file type 4454aa6d63Sopenharmony_ci static bool ValidFileType(const std::string& filePath, const std::initializer_list<std::string> types); 4554aa6d63Sopenharmony_ci static bool IsValidFile(std::string file); 4654aa6d63Sopenharmony_ci /** 4754aa6d63Sopenharmony_ci* write content to output file 4854aa6d63Sopenharmony_ci* @param content data to write 4954aa6d63Sopenharmony_ci* @param output path 5054aa6d63Sopenharmony_ci* @return 0:success 5154aa6d63Sopenharmony_ci*/ 5254aa6d63Sopenharmony_ci static int Write(const std::string& content, const std::string& output); 5354aa6d63Sopenharmony_ci /** 5454aa6d63Sopenharmony_ci* Read byte from input stream. 5554aa6d63Sopenharmony_ci* 5654aa6d63Sopenharmony_ci* @param input Input stream 5754aa6d63Sopenharmony_ci* @param ret File content 5854aa6d63Sopenharmony_ci* @return 0:success <0:error 5954aa6d63Sopenharmony_ci*/ 6054aa6d63Sopenharmony_ci static int Read(std::ifstream& input, std::string& ret); 6154aa6d63Sopenharmony_ci /** 6254aa6d63Sopenharmony_ci*Read byte from input file. 6354aa6d63Sopenharmony_ci* 6454aa6d63Sopenharmony_ci* @param file Which file to read 6554aa6d63Sopenharmony_ci* @param ret byte content 6654aa6d63Sopenharmony_ci* @return 0:success <0:error 6754aa6d63Sopenharmony_ci*/ 6854aa6d63Sopenharmony_ci static int ReadFile(const std::string& path, std::string& ret); 6954aa6d63Sopenharmony_ci /** 7054aa6d63Sopenharmony_ci * Read byte from input file. 7154aa6d63Sopenharmony_ci * 7254aa6d63Sopenharmony_ci * @param file input file 7354aa6d63Sopenharmony_ci * @param offset offset 7454aa6d63Sopenharmony_ci * @param length length 7554aa6d63Sopenharmony_ci * @param ret data bytes 7654aa6d63Sopenharmony_ci * @return 0:success <0:error 7754aa6d63Sopenharmony_ci */ 7854aa6d63Sopenharmony_ci static int ReadFileByOffsetAndLength(std::ifstream& file, size_t offset, size_t length, std::string& ret); 7954aa6d63Sopenharmony_ci /** 8054aa6d63Sopenharmony_ci * Read byte from input stream. 8154aa6d63Sopenharmony_ci * 8254aa6d63Sopenharmony_ci * @param input input stream 8354aa6d63Sopenharmony_ci * @param offset offset 8454aa6d63Sopenharmony_ci * @param length length 8554aa6d63Sopenharmony_ci * @ret data bytes 8654aa6d63Sopenharmony_ci * @return 0:success <0 :error 8754aa6d63Sopenharmony_ci */ 8854aa6d63Sopenharmony_ci static int ReadInputByOffsetAndLength(std::ifstream& input, size_t offset, size_t length, std::string& ret); 8954aa6d63Sopenharmony_ci /** 9054aa6d63Sopenharmony_ci * Read byte from input stream. 9154aa6d63Sopenharmony_ci * 9254aa6d63Sopenharmony_ci * @param input InputStream 9354aa6d63Sopenharmony_ci * @param length length 9454aa6d63Sopenharmony_ci * @ret data bytes 9554aa6d63Sopenharmony_ci * @return 0:success <0 :error 9654aa6d63Sopenharmony_ci */ 9754aa6d63Sopenharmony_ci static int ReadInputByLength(std::ifstream& input, size_t length, std::string& ret); 9854aa6d63Sopenharmony_ci /** 9954aa6d63Sopenharmony_ci * Write data in file to output stream 10054aa6d63Sopenharmony_ci * 10154aa6d63Sopenharmony_ci * @param inFile input file path. 10254aa6d63Sopenharmony_ci * @param out output file path. 10354aa6d63Sopenharmony_ci * @param offset file read offset 10454aa6d63Sopenharmony_ci * @param size file read size 10554aa6d63Sopenharmony_ci * @return true, if write successfully. 10654aa6d63Sopenharmony_ci */ 10754aa6d63Sopenharmony_ci static bool AppendWriteFileByOffsetToFile(std::ifstream& input, std::ofstream& out, size_t offset, size_t size); 10854aa6d63Sopenharmony_ci static bool AppendWriteFileToFile(const std::string& inputFile, const std::string& outputFile); 10954aa6d63Sopenharmony_ci static bool AppendWriteByteToFile(const std::string& bytes, const std::string& outputFile); 11054aa6d63Sopenharmony_ci 11154aa6d63Sopenharmony_ci static int WriteInputToOutPut(std::ifstream& input, std::ofstream& output, size_t length); 11254aa6d63Sopenharmony_ci static bool WriteInputToOutPut(const std::string& input, const std::string& output); 11354aa6d63Sopenharmony_ci static bool IsSpaceEnough(const std::string& filePath, const int64_t requiredSpace); 11454aa6d63Sopenharmony_ci /** 11554aa6d63Sopenharmony_ci* Write byte array data to output file. 11654aa6d63Sopenharmony_ci* 11754aa6d63Sopenharmony_ci* @param bytes byte array data. 11854aa6d63Sopenharmony_ci* @param outFile output file path. 11954aa6d63Sopenharmony_ci* @return true, if write successfully. 12054aa6d63Sopenharmony_ci*/ 12154aa6d63Sopenharmony_ci static bool WriteByteToOutFile(const std::string& bytes, const std::string& outFile); 12254aa6d63Sopenharmony_ci /** 12354aa6d63Sopenharmony_ci* Write byte array data to output file. 12454aa6d63Sopenharmony_ci* 12554aa6d63Sopenharmony_ci* @param bytes byte array data. 12654aa6d63Sopenharmony_ci* @param outFile output file path. 12754aa6d63Sopenharmony_ci* @return true, if write successfully. 12854aa6d63Sopenharmony_ci*/ 12954aa6d63Sopenharmony_ci static bool WriteByteToOutFile(const std::string& bytes, std::ofstream& outFile); 13054aa6d63Sopenharmony_ci static bool WriteByteToOutFile(const std::vector<int8_t>& bytes, std::ofstream& outFile); 13154aa6d63Sopenharmony_ci /** 13254aa6d63Sopenharmony_ci * regex filename 13354aa6d63Sopenharmony_ci * 13454aa6d63Sopenharmony_ci * @param name filename 13554aa6d63Sopenharmony_ci * @return boolean 13654aa6d63Sopenharmony_ci */ 13754aa6d63Sopenharmony_ci static bool IsRunnableFile(const std::string& name); 13854aa6d63Sopenharmony_ci static int64_t GetFileLen(const std::string& file); 13954aa6d63Sopenharmony_ci static void DelDir(const std::string& file); 14054aa6d63Sopenharmony_ci}; 14154aa6d63Sopenharmony_ci} // namespace SignatureTools 14254aa6d63Sopenharmony_ci} // namespace OHOS 14354aa6d63Sopenharmony_ci#endif // SIGNATRUETOOLS_FILE_UTILS_H