• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
23 namespace OHOS {
24 
25 /**
26  * @brief Get the full absolute path to the current program.
27  *
28  * /proc/self/exe represents the current program, and its source path can be
29  * read with the readlink function to get the absolute path of the current
30  * program.
31  */
32 std::string GetCurrentProcFullFileName();
33 std::string GetCurrentProcFullFileName();
34 
35 /**
36  * @brief Get the absolute path of the current program.
37  */
38 std::string GetCurrentProcPath();
39 
40 /**
41  * @brief Obtain the path to the corresponding file by the full path.
42  */
43 std::string ExtractFilePath(const std::string& fileFullName);
44 
45 /**
46  * @brief Obtain the name to the corresponding file by the full path.
47  */
48 std::string ExtractFileName(const std::string& fileFullName);
49 
50 /**
51  * @brief Obtain the filename extension to the corresponding file by the full
52  * path.
53  */
54 std::string ExtractFileExt(const std::string& fileName);
55 
56 /**
57  * @brief Exclude the end '/' from the strPath.
58  *
59  * Determine whether the path has ended with '/', and returns the path after
60  * removing '/', otherwise returns the path directly.
61  */
62 std::string ExcludeTrailingPathDelimiter(const std::string& path);
63 
64 /**
65  * @brief Include the end '/' from the strPath.
66  *
67  * Determine whether the path has ended with "/", and returns the path after
68  * adding '/', otherwise returns the path directly.
69  */
70 std::string IncludeTrailingPathDelimiter(const std::string& path);
71 
72 /**
73  * @brief Get names of all files under `path` recursively.
74  *
75  * @param path Input path.
76  * @param files Target `std::vector` to store the file names.
77  */
78 void GetDirFiles(const std::string& path, std::vector<std::string>& files);
79 
80 /**
81  * @brief Judge if the path is empty.
82  *
83  * @return Return true if is empty, else false.
84  */
85 bool IsEmptyFolder(const std::string& path);
86 
87 /**
88  * @brief Create the dir recursively.
89  *
90  * Parent directory can be created at the same time when it does not exist.
91  *
92  * @note If there are problems such as 'Permission Denied', the creation may
93  * also fail.
94  * @return Return true if create success, else false.
95  */
96 bool ForceCreateDirectory(const std::string& path);
97 
98 /**
99  * @brief Delete the specified dir.
100  *
101  * All subdirs and files will also be deleted.
102  *
103  * @note It is not necessarily successful to delete.
104  * @note If there are problems such as 'Permission Denied', the deletion may
105  * also fail.
106  * @return Return true if delete success, else false.
107  */
108 bool ForceRemoveDirectory(const std::string& path);
109 
110 /**
111  * @brief Remove the file specified by fileName.
112  *
113  * @return Return true if remove success, else false.
114  */
115 bool RemoveFile(const std::string& fileName);
116 
117 /**
118  * @brief Get the folder size(bytes).
119  */
120 uint64_t GetFolderSize(const std::string& path);
121 
122 /**
123  * @brief Change the file authority.
124  *
125  * @param mode Specify the changed permissions, see chmod().
126  * @return Return true if change success, else false.
127  */
128 bool ChangeModeFile(const std::string& fileName, const mode_t& mode);
129 
130 /**
131  * @brief Change authority of the directory specified by path and all of its
132  * subdirectories.
133  *
134  * @param mode Specify the changed permissions, see chmod().
135  * @return Return true if change success, else false.
136  */
137 bool ChangeModeDirectory(const std::string& path, const mode_t& mode);
138 
139 /**
140  * @brief Get real path from relative path.
141  *
142  * @return Return true if get success, else false.
143  */
144 bool PathToRealPath(const std::string& path, std::string& realPath);
145 } // OHOS
146 #endif
147