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_600 = S_IRUSR | S_IWUSR;
57 constexpr mode_t FILE_PERM_640 = S_IRUSR | S_IWUSR | S_IRGRP;
58 constexpr mode_t FILE_PERM_660 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
59 constexpr mode_t FILE_PERM_666 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
60 
61 // file_ex.h
62 bool LoadStringFromFile(const std::string& filePath, std::string& content);
63 bool LoadLinesFromFile(const std::string& filePath, std::vector<std::string>& lines);
64 bool LoadStringFromFd(int fd, std::string& content);
65 bool SaveStringToFile(const std::string& filePath, const std::string& content, bool truncated = true);
66 bool SaveStringToFd(int fd, const std::string& content);
67 bool LoadBufferFromFile(const std::string& filePath, std::vector<char>& content);
68 bool SaveBufferToFile(const std::string& filePath, const std::vector<char>& content, bool truncated = true);
69 bool FileExists(const std::string& fileName);
70 bool FileExistsA(const std::string& fileName);
71 bool WriteBufferToFd(int fd, const char* buffer, size_t size);
72 int32_t CreateFile(const std::string &path, mode_t mode = DEFAULT_FILE_MODE);
73 
74 // directory_ex.h
75 std::string ExtractFilePath(const std::string& fileFullName);
76 std::string ExtractFileName(const std::string& fileFullName);
77 std::string ExtractFileExt(const std::string& fileName);
78 std::string IncludeTrailingPathDelimiter(const std::string& path);
79 std::string ExcludeTrailingPathDelimiter(const std::string& path);
80 void GetDirFiles(const std::string& path, std::vector<std::string>& files, bool isRecursive = true);
81 void GetDirDirs(const std::string& path, std::vector<std::string>& dirs);
82 bool ForceCreateDirectory(const std::string& path);
83 bool ForceCreateDirectory(const std::string& path, mode_t mode);
84 void RemoveFolderBeginWith(const std::string &path, const std::string &folderName);
85 bool ForceRemoveDirectory(const std::string& path, bool isNeedDeleteGivenDirSelf = true);
86 bool RemoveFile(const std::string& fileName);
87 uint64_t GetFolderSize(const std::string& path);
88 uint64_t GetFileSize(const std::string& path);
89 bool ChangeMode(const std::string& fileName, const mode_t& mode);
90 bool ChangeModeFile(const std::string& fileName, const mode_t& mode);
91 bool ChangeModeDirectory(const std::string& path, const mode_t& mode);
92 bool PathToRealPath(const std::string& path, std::string& realPath);
93 mode_t Umask(const mode_t& mode);
94 int Open(const std::string& path, const int flags, const mode_t mode);
95 void FormatPath2UnixStyle(std::string &path);
96 void CreateDirWithDefaultPerm(const std::string& path, uid_t aidRoot, uid_t aid_system);
97 int CopyFile(const std::string &src, const std::string &des);
98 int CopyFileFast(const std::string &src, const std::string &des);
99 bool IsDirectory(const std::string &path);
100 bool GetLastLine(std::istream &fin, std::string &line, uint32_t maxLen = 10240); // 10240 : max line len
101 std::string GetFirstLine(const std::string& path);
102 std::string GetParentDir(const std::string &path);
103 bool IsLegalPath(const std::string& path);
104 bool RenameFile(const std::string& src, const std::string& dest);
105 bool GetDirXattr(const std::string& dir, const std::string& name, std::string& value);
106 } // namespace FileUtil
107 } // namespace HiviewDFX
108 } // namespace OHOS
109 #endif // UTILITY_FILE_UTIL_H
110