1 /* 2 * Copyright (c) 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 RESSCHED_COMMON_INCLUDE_RES_SCHED_FILE_UTIL 17 #define RESSCHED_COMMON_INCLUDE_RES_SCHED_FILE_UTIL 18 #include <fcntl.h> 19 #include <string> 20 #include <unistd.h> 21 22 namespace OHOS { 23 namespace ResourceSchedule { 24 namespace ResCommonUtil { 25 26 /** 27 * @brief Write reclaim status into given pid's node. 28 * 29 * @param pid Indicates the pid will write reclaim status 30 * 31 */ 32 void WriteFileReclaim(int32_t pid); 33 34 /** 35 * @brief Obtains the real path from a relative path. 36 * 37 * @param filePath the relative path 38 * @param realPath the real path 39 * @return 'true' if obtain succeed. 40 */ 41 bool GetRealPath(const std::string& filePath, std::string& realPath); 42 43 /** 44 * @brief Judje path exists. 45 * 46 * @param path path 47 * @return 'true' if exist. 48 */ 49 bool PathOrFileExists(const std::string& path); 50 51 /** 52 * @brief traverse all sub path and file path in given file path. 53 * 54 * @param filePath file path 55 * @param iters the traverse result 56 * @return 'true' if dir is vaild 57 */ 58 bool DirIterator(const std::string& filePath, std::vector<std::string>& iters); 59 60 /** 61 * @brief Checks weather a folder is empty. 62 63 * 64 * @param filePath file path will checked 65 * @return 'true' if dir floder is empty. 66 */ 67 bool IsEmptyDir(const std::string& filePath); 68 69 /** 70 * @brief Checks weather a string is floder dir. 71 * 72 * @param filePath file path will checked 73 * @return 'true' if filePath is dir. 74 */ 75 bool IsDir(const std::string& filePath); 76 77 /** 78 * @brief Create dir with mode. 79 * 80 * @param dir dir will create 81 * @param mode the dir's mode 82 * @return 'true' if dir create succeed. 83 */ 84 bool CreateDir(const std::string& dir, const mode_t& mode); 85 86 /** 87 * @brief Create file with mode. 88 * 89 * @param filePath will create 90 * @param mode the dir's mode 91 * @return 'true' if file create succeed. 92 */ 93 bool CreateFile(const std::string& filePath, const mode_t& mode); 94 95 /** 96 * @brief Remove dir. 97 * 98 * @param dir dir will removed 99 * @return 'true' if dir remove succeed. 100 */ 101 bool RemoveDirs(const std::string& dir); 102 103 /** 104 * @brief Remove file. 105 * 106 * @param filePath file will removed 107 * @return 'true' if dir remove succeed. 108 */ 109 bool RemoveFile(const std::string& filePath); 110 111 /** 112 * @brief Extract file name from file path. 113 * 114 * @param filePath the file path 115 * @return file name. 116 */ 117 std::string ExtractFileName(const std::string& filePath); 118 119 /** 120 * @brief Check weather a file path is BLK mode. 121 * 122 * @param filePath the file path 123 * @return 'true' if file path is BLK mode. 124 */ 125 bool IsBLKPath(const std::string& filePath); 126 127 /** 128 * @brief Write a string to a file. 129 * 130 * @param filePath Indicates the path of target file 131 * @param content Indicates the string object to write 132 * @param truncated Indicates wheather to truncate the original file 133 * @return 'true' if the string is written successfully. 134 */ 135 bool SaveStringToFile(const std::string& filePath, const std::string& content, bool truncated = true); 136 137 /** 138 * @brief Read Lines from file. 139 * 140 * @param filePath Indicates the path of target file 141 * @param lines the lines of the file 142 * @return 'true' if read successfully. 143 */ 144 bool ReadLinesFromFile(const std::string& filePath, std::vector<std::string>& lines); 145 146 /** 147 * @brief Copy file from src to des. 148 * 149 * @param src the file will be copied 150 * @param tes the target path 151 * @return 'true' if copy successfully. 152 */ 153 bool CopyFile(const std::string& src, const std::string& des); 154 155 /** 156 * @brief Get the highest priority config file absolute path 157 * 158 * @param configPath the relative path of the config 159 * @param realConfigPath the absolute path of the highest priority path 160 * @return 'true' if the config is exist. 161 */ 162 bool GetRealConfigPath(const std::string& configPath, std::string& realConfigPath); 163 164 /** 165 * @brief check path is valid 166 * 167 * @param path The path that needs to be checked 168 * @return 'true' if the path is valid. 169 */ 170 bool IsValidPath(const std::string& path); 171 172 /** 173 * @brief get file size 174 * 175 * @param path The file path 176 * @return the size of the file. 177 */ 178 uint64_t GetFileSize(const std::string& path); 179 180 /** 181 * @brief get device valid size 182 * 183 * @param path The partition of path 184 * @return the remain size of partition. 185 */ 186 double GetDeviceVailidSize(const std::string& path); 187 } 188 } // namespace ResourceSchedule 189 } // namespace OHOS 190 191 #endif // RESSCHED_COMMON_INCLUDE_RES_SCHED_FILE_UTIL