1 /* 2 * Copyright (c) 2022-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 16 #ifndef OHOS_FILEMGMT_BACKUP_B_FILE_H 17 #define OHOS_FILEMGMT_BACKUP_B_FILE_H 18 19 #include <memory> 20 #include <string> 21 22 #include "unique_fd.h" 23 #include "json/json.h" 24 25 #include "b_anony/b_anony.h" 26 27 namespace OHOS::FileManagement::Backup { 28 using namespace std; 29 class BFile { 30 public: 31 /** 32 * @brief 一次性读取文件全部内容 33 * 34 * @param fd 文件描述符 35 * @return std::unique_ptr<char[]> 文件全部内容,保证最后一个字节为'\0' 36 * @throw std::system_error IO异常 37 */ 38 static std::unique_ptr<char[]> ReadFile(const UniqueFd &fd); 39 40 /** 41 * @brief linux sendfile 二次封装 42 * @param outFd 参数是待写入内容的文件描述符 43 * @param inFd 参数是待读出内容的文件描述符 44 * @throw std::system_error IO异常 45 */ 46 static void SendFile(int outFd, int inFd); 47 48 /** 49 * @brief linux write 二次封装 50 * @param fd 参数是待写入内容的文件描述符 51 * @param str 待写入文件的字符串 52 * @throw std::system_error IO异常 53 */ 54 static void Write(const UniqueFd &fd, const string &str); 55 56 /** 57 * @brief copy file from old path to new path 58 * 59 * @param from old path 60 * @param to new path 61 * @return true copy succeess 62 * @return false some error occur 63 */ 64 static bool CopyFile(const string &from, const string &to); 65 66 /** 67 * @brief move file from old path to new path 68 * 69 * @param from old path 70 * @param to new path 71 * @return true move succeess 72 * @return false some error occur 73 */ 74 static bool MoveFile(const string &from, const string &to); 75 76 /** 77 * @brief get real path 78 * 79 * @param path src path 80 * @param realPath real path 81 * @return true get real path success 82 * @return false some error occur 83 */ 84 static bool GetRealPath(const string &path, string &realPath); 85 86 /** 87 * @brief check if string is endswith suffix 88 * 89 * @param str str 90 * @param suffix suffix str 91 * @return true str is endswith suffix 92 * @return false str is not endswith suffix 93 */ 94 static bool EndsWith(const string &str, const string &suffix); 95 private: 96 }; 97 } // namespace OHOS::FileManagement::Backup 98 99 #endif // OHOS_FILEMGMT_BACKUP_B_FILE_H