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