1 /* 2 * Copyright (c) 2021 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 UTILITY_FILE_UTIL_H 17 #define UTILITY_FILE_UTIL_H 18 19 #ifdef _WIN32 20 using mode_t = unsigned short; 21 using pid_t = int; 22 using tid_t = int; 23 using uid_t = int; 24 #define S_IRWXU 00700 25 #define S_IXGRP 00010 26 #define S_IXOTH 00001 27 #define S_IRWXG 00070 28 29 #define S_IRUSR 00400 30 #define S_IWUSR 00200 31 #define S_IRGRP 00040 32 #define S_IWGRP 00020 33 #define S_IROTH 00004 34 #define F_OK 0 35 #ifndef S_ISDIR 36 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR) 37 #include <io.h> 38 # define access _access 39 #endif 40 #else 41 #include <dirent.h> 42 #include <sys/stat.h> 43 #endif 44 45 #include <sstream> 46 #include <string> 47 #include <vector> 48 49 namespace OHOS { 50 namespace HiviewDFX { 51 namespace FileUtil { 52 constexpr mode_t DEFAULT_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; // -rw-rw-r-- 53 constexpr mode_t FILE_PERM_755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; 54 constexpr mode_t FILE_PERM_775 = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; 55 constexpr mode_t FILE_PERM_770 = S_IRWXU | S_IRWXG; 56 constexpr mode_t FILE_PERM_660 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; 57 // file_ex.h 58 bool LoadStringFromFile(const std::string& filePath, std::string& content); 59 bool LoadStringFromFd(int fd, std::string& content); 60 bool SaveStringToFile(const std::string& filePath, const std::string& content, bool truncated = true); 61 bool SaveStringToFd(int fd, const std::string& content); 62 bool LoadBufferFromFile(const std::string& filePath, std::vector<char>& content); 63 bool SaveBufferToFile(const std::string& filePath, const std::vector<char>& content, bool truncated = true); 64 bool FileExists(const std::string& fileName); 65 bool FileExistsA(const std::string& fileName); 66 bool WriteBufferToFd(int fd, const char* buffer, size_t size); 67 int32_t CreateFile(const std::string &path, mode_t mode = DEFAULT_FILE_MODE); 68 69 // directory_ex.h 70 std::string ExtractFilePath(const std::string& fileFullName); 71 std::string ExtractFileName(const std::string& fileFullName); 72 std::string IncludeTrailingPathDelimiter(const std::string& path); 73 std::string ExcludeTrailingPathDelimiter(const std::string& path); 74 void GetDirFiles(const std::string& path, std::vector<std::string>& files); 75 bool ForceCreateDirectory(const std::string& path); 76 bool ForceCreateDirectory(const std::string& path, mode_t mode); 77 void RemoveFolderBeginWith(const std::string &path, const std::string &folderName); 78 bool ForceRemoveDirectory(const std::string& path, bool isNeedDeleteGivenDirSelf = true); 79 bool RemoveFile(const std::string& fileName); 80 uint64_t GetFolderSize(const std::string& path); 81 uint64_t GetFileSize(const std::string& path); 82 bool ChangeMode(const std::string& fileName, const mode_t& mode); 83 bool ChangeModeFile(const std::string& fileName, const mode_t& mode); 84 bool ChangeModeDirectory(const std::string& path, const mode_t& mode); 85 bool PathToRealPath(const std::string& path, std::string& realPath); 86 mode_t Umask(const mode_t& mode); 87 int Open(const std::string& path, const int flags, const mode_t mode); 88 void FormatPath2UnixStyle(std::string &path); 89 void CreateDirWithDefaultPerm(const std::string& path, uid_t aidRoot, uid_t aid_system); 90 int CopyFile(const std::string &src, const std::string &des); 91 bool IsDirectory(const std::string &path); 92 bool GetLastLine(std::istream &fin, std::string &line, uint32_t maxLen = 10240); // 10240 : max line len 93 std::string GetParentDir(const std::string &path); 94 bool IsLegalPath(const std::string& path); 95 bool RenameFile(const std::string& src, const std::string& dest); 96 } // namespace FileUtil 97 } // namespace HiviewDFX 98 } // namespace OHOS 99 #endif // UTILITY_FILE_UTIL_H 100