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 DIRECTORY_EX_H
17 #define DIRECTORY_EX_H
18 
19 #include <string>
20 #include <vector>
21 #include <sys/stat.h>
22 #ifdef UTILS_CXX_RUST
23 #include "cxx.h"
24 #endif
25 
26 namespace OHOS {
27 
28 #ifdef UTILS_CXX_RUST
29 rust::String RustGetCurrentProcFullFileName();
30 rust::String RustGetCurrentProcPath();
31 rust::String RustExtractFilePath(const rust::String& fileFullName);
32 rust::String RustExtractFileName(const rust::String& fileFullName);
33 rust::String RustExtractFileExt(const rust::String& fileName);
34 rust::String RustExcludeTrailingPathDelimiter(const rust::String& path);
35 rust::String RustIncludeTrailingPathDelimiter(const rust::String& path);
36 bool RustPathToRealPath(const rust::String& path, rust::String& realPath);
37 void RustGetDirFiles(const rust::String& path, rust::vec<rust::String>& files);
38 #endif
39 
40 /**
41  * @brief Obtains the full absolute path of this program.
42  *
43  * <b>/proc/self/exe</b> indicates the program, and you can obtain its absolute
44  * path by using readlink().
45  */
46 std::string GetCurrentProcFullFileName();
47 
48 /**
49  * @brief Obtains the absolute path of this program.
50  */
51 std::string GetCurrentProcPath();
52 
53 /**
54  * @brief Obtains the path of a file based on the full path.
55  */
56 std::string ExtractFilePath(const std::string& fileFullName);
57 
58 /**
59  * @brief Obtains the name of a file based on the full path.
60  */
61 std::string ExtractFileName(const std::string& fileFullName);
62 
63 /**
64  * @brief Obtains the filename extension based on the full path.
65  *
66  */
67 std::string ExtractFileExt(const std::string& fileName);
68 
69 /**
70  * @brief Excludes the trailing path delimiter '/' from the <b>strPath</b>.
71  *
72  * If the path ends with '/', returns the path after removing '/'.
73  * Otherwise, returns the path directly.
74  */
75 std::string ExcludeTrailingPathDelimiter(const std::string& path);
76 
77 /**
78  * @brief Includes the trailing path delimiter '/' in the <b>strPath</b>.
79  *
80  * If the path ends with "/", returns the path.
81  * Otherwise, returns the path with an appended delimiter.
82  */
83 std::string IncludeTrailingPathDelimiter(const std::string& path);
84 
85 /**
86  * @brief Obtains the names of all files in the specified directory recursively.
87  *
88  * @param path Indicates the target directory.
89  * @param files Indicates the <b>std::vector</b> to store the file names.
90  */
91 void GetDirFiles(const std::string& path, std::vector<std::string>& files);
92 
93 /**
94  * @brief Checks whether a folder is empty.
95  *
96  * @return Returns <b>true</b> if the folder is empty;
97  * returns <b>false</b> otherwise.
98  */
99 bool IsEmptyFolder(const std::string& path);
100 
101 /**
102  * @brief Creates a directory recursively.
103  *
104  * The parent directory can be created at the same time when it does not exist.
105  *
106  * @note If there are errors such as 'Permission Denied', the creation may
107  * also fail.
108  * @return Returns <b>true</b> if the directory is created;
109  * returns <b>false</b> otherwise.
110  */
111 bool ForceCreateDirectory(const std::string& path);
112 
113 /**
114  * @brief Deletes a directory.
115  *
116  * All subdirectories and files in the specified directory will also be deleted.
117  *
118  * @note It is not necessarily successful to delete.
119  * @note If there are errors such as 'Permission Denied', the deletion may
120  * also fail.
121  * @return Returns <b>true</b> if the directory is deleted;
122  * returns <b>false</b> otherwise.
123  */
124 bool ForceRemoveDirectory(const std::string& path);
125 
126 /**
127  * @brief Removes the file specified by <b>fileName</b>.
128  *
129  * @return Returns <b>true</b> if the file is removed;
130  * returns <b>false</b> otherwise.
131  */
132 bool RemoveFile(const std::string& fileName);
133 
134 /**
135  * @brief Obtains the folder size, in bytes.
136  */
137 uint64_t GetFolderSize(const std::string& path);
138 
139 /**
140  * @brief Changes the access permissions on a file.
141  *
142  * @param mode Indicates the permissions on the file.
143  * For details, see <b>chmod()</b>.
144  * @return Returns <b>true</b> if the permissions are changed;
145  * returns <b>false</b> otherwise.
146  */
147 bool ChangeModeFile(const std::string& fileName, const mode_t& mode);
148 
149 /**
150  * @brief Changes the access permissions on a directory and all its
151  * subdirectories.
152  *
153  * @param mode Indicates the permissions. For details, see <b>chmod()</b>.
154  * @return Returns <b>true</b> if the permissions are changed;
155  * returns <b>false</b> otherwise.
156  */
157 bool ChangeModeDirectory(const std::string& path, const mode_t& mode);
158 
159 /**
160  * @brief Obtains the real path from a relative path.
161  *
162  * @return Returns <b>true</b> if the real path is obtained;
163  * returns <b>false</b> otherwise.
164  */
165 bool PathToRealPath(const std::string& path, std::string& realPath);
166 
167 #if defined(IOS_PLATFORM) || defined(_WIN32)
168 /**
169  * @brief Transforms a file name to that for Windows or macOS.
170  */
171 std::string TransformFileName(const std::string& fileName);
172 #endif
173 } // OHOS
174 #endif