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 #include "file_util.h"
17 #include <algorithm>
18 #include <cstdio>
19 #include <cstring>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <fstream>
23 #include <iostream>
24 #include <iterator>
25 #include <pthread.h>
26 #include <sys/prctl.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29
30 #include <sys/syscall.h>
31
32 #include "directory_ex.h"
33 #include "file_ex.h"
34 namespace OHOS {
35 namespace HiviewDFX {
36 namespace FileUtil {
37 using namespace std;
LoadStringFromFile(const std::string & filePath,std::string & content)38 bool LoadStringFromFile(const std::string& filePath, std::string& content)
39 {
40 return OHOS::LoadStringFromFile(filePath, content);
41 }
42
LoadStringFromFd(int fd,std::string & content)43 bool LoadStringFromFd(int fd, std::string& content)
44 {
45 return OHOS::LoadStringFromFd(fd, content);
46 }
47
SaveStringToFile(const std::string & filePath,const std::string & content,bool truncated)48 bool SaveStringToFile(const std::string& filePath, const std::string& content, bool truncated)
49 {
50 return OHOS::SaveStringToFile(filePath, content, truncated);
51 }
52
SaveStringToFd(int fd,const std::string & content)53 bool SaveStringToFd(int fd, const std::string& content)
54 {
55 return OHOS::SaveStringToFd(fd, content);
56 }
57
LoadBufferFromFile(const std::string & filePath,std::vector<char> & content)58 bool LoadBufferFromFile(const std::string& filePath, std::vector<char>& content)
59 {
60 return OHOS::LoadBufferFromFile(filePath, content);
61 }
62
SaveBufferToFile(const std::string & filePath,const std::vector<char> & content,bool truncated)63 bool SaveBufferToFile(const std::string& filePath, const std::vector<char>& content, bool truncated)
64 {
65 return OHOS::SaveBufferToFile(filePath, content, truncated);
66 }
67
FileExists(const std::string & fileName)68 bool FileExists(const std::string& fileName)
69 {
70 return OHOS::FileExists(fileName);
71 }
72
ExtractFilePath(const std::string & fileFullName)73 std::string ExtractFilePath(const std::string& fileFullName)
74 {
75 return OHOS::ExtractFilePath(fileFullName);
76 }
77
ExtractFileName(const std::string & fileFullName)78 std::string ExtractFileName(const std::string& fileFullName)
79 {
80 return OHOS::ExtractFileName(fileFullName);
81 }
82
IncludeTrailingPathDelimiter(const std::string & path)83 std::string IncludeTrailingPathDelimiter(const std::string& path)
84 {
85 return OHOS::IncludeTrailingPathDelimiter(path);
86 }
87
ExcludeTrailingPathDelimiter(const std::string & path)88 std::string ExcludeTrailingPathDelimiter(const std::string& path)
89 {
90 return OHOS::ExcludeTrailingPathDelimiter(path);
91 }
92
GetDirFiles(const std::string & path,std::vector<std::string> & files)93 void GetDirFiles(const std::string& path, std::vector<std::string>& files)
94 {
95 return OHOS::GetDirFiles(path, files);
96 }
97
ForceCreateDirectory(const std::string & path)98 bool ForceCreateDirectory(const std::string& path)
99 {
100 return OHOS::ForceCreateDirectory(path);
101 }
102
ForceCreateDirectory(const string & path,mode_t mode)103 bool ForceCreateDirectory(const string& path, mode_t mode)
104 {
105 string::size_type index = 0;
106 do {
107 index = path.find('/', index + 1);
108 string subPath = (index == string::npos) ? path : path.substr(0, index);
109 if (access(subPath.c_str(), F_OK) != 0) {
110 if (mkdir(subPath.c_str(), mode) != 0) {
111 return false;
112 }
113 }
114 } while (index != string::npos);
115 return access(path.c_str(), F_OK) == 0;
116 }
117
ForceRemoveDirectory(const std::string & path,bool isNeedDeleteGivenDirSelf)118 bool ForceRemoveDirectory(const std::string& path, bool isNeedDeleteGivenDirSelf)
119 {
120 return OHOS::ForceRemoveDirectory(path);
121 }
122
GetFileSize(const std::string & path)123 uint64_t GetFileSize(const std::string& path)
124 {
125 struct stat st;
126 return stat(path.c_str(), &st) ? 0 : static_cast<uint64_t>(st.st_size);
127 }
128
RemoveFile(const std::string & fileName)129 bool RemoveFile(const std::string& fileName)
130 {
131 return OHOS::RemoveFile(fileName);
132 }
133
GetFolderSize(const std::string & path)134 uint64_t GetFolderSize(const std::string& path)
135 {
136 return OHOS::GetFolderSize(path);
137 }
138
139 // inner function, and param is legitimate
ChangeMode(const string & fileName,const mode_t & mode)140 bool ChangeMode(const string& fileName, const mode_t& mode)
141 {
142 return (chmod(fileName.c_str(), mode) == 0);
143 }
144
ChangeModeFile(const string & fileName,const mode_t & mode)145 bool ChangeModeFile(const string& fileName, const mode_t& mode)
146 {
147 if (access(fileName.c_str(), F_OK) != 0) {
148 return false;
149 }
150
151 return ChangeMode(fileName, mode);
152 }
153
ChangeModeDirectory(const std::string & path,const mode_t & mode)154 bool ChangeModeDirectory(const std::string& path, const mode_t& mode)
155 {
156 return OHOS::ChangeModeDirectory(path, mode);
157 }
158
PathToRealPath(const std::string & path,std::string & realPath)159 bool PathToRealPath(const std::string& path, std::string& realPath)
160 {
161 return OHOS::PathToRealPath(path, realPath);
162 }
163
Umask(const mode_t & mode)164 mode_t Umask(const mode_t& mode)
165 {
166 return umask(mode);
167 }
168
Open(const std::string & path,const int flags,const mode_t mode)169 int Open(const std::string& path, const int flags, const mode_t mode)
170 {
171 return open(path.c_str(), flags, mode);
172 }
173
CreateDirWithDefaultPerm(const std::string & path,uid_t aidRoot,uid_t aidSystem)174 void CreateDirWithDefaultPerm(const std::string& path, uid_t aidRoot, uid_t aidSystem)
175 {
176 FileUtil::ForceCreateDirectory(path);
177 chown(path.c_str(), aidRoot, aidSystem);
178 }
179
FormatPath2UnixStyle(std::string & path)180 void FormatPath2UnixStyle(std::string &path)
181 {
182 // unimplemented
183 }
184
RemoveFolderBeginWith(const std::string & path,const std::string & folderName)185 void RemoveFolderBeginWith(const std::string &path, const std::string &folderName)
186 {
187 // unimplemented
188 }
189
WriteBufferToFd(int fd,const char * buffer,size_t size)190 bool WriteBufferToFd(int fd, const char* buffer, size_t size)
191 {
192 if (fd < 0) {
193 return false;
194 }
195
196 if (buffer == nullptr) {
197 return false;
198 }
199
200 ssize_t writeSize = size;
201 if (writeSize != TEMP_FAILURE_RETRY(write(fd, buffer, size))) {
202 return false;
203 }
204
205 return true;
206 }
207
CreateFile(const std::string & path,mode_t mode)208 int CreateFile(const std::string &path, mode_t mode)
209 {
210 if (FileExists(path)) {
211 return 0;
212 } else {
213 std::ofstream fout(path);
214 if (!fout.is_open()) {
215 return -1;
216 }
217 fout.flush();
218 fout.close();
219 if (ChangeMode(path, mode) != 0) {
220 return -1;
221 }
222 }
223 return 0;
224 }
225
CopyFile(const std::string & src,const std::string & des)226 int CopyFile(const std::string &src, const std::string &des)
227 {
228 std::ifstream fin(src, ios::binary);
229 std::ofstream fout(des, ios::binary);
230 if (!fin.is_open()) {
231 return -1;
232 }
233 if (!fout.is_open()) {
234 return -1;
235 }
236 fout << fin.rdbuf();
237 if (fout.fail()) {
238 fout.clear();
239 }
240 fout.flush();
241 return 0;
242 }
243
IsDirectory(const std::string & path)244 bool IsDirectory(const std::string &path)
245 {
246 struct stat statBuffer;
247 if (stat(path.c_str(), &statBuffer) == 0 && S_ISDIR(statBuffer.st_mode)) {
248 return true;
249 }
250 return false;
251 }
252
GetLastLine(std::istream & fin,std::string & line,int maxLen)253 bool GetLastLine(std::istream &fin, std::string &line, int maxLen)
254 {
255 if (fin.tellg() <= 0) {
256 return false;
257 } else {
258 fin.seekg(-1, fin.cur);
259 }
260 uint32_t count = 0;
261 while (fin.good() && fin.peek() == fin.widen('\n') && fin.tellg() > 0 && count < maxLen) {
262 fin.seekg(-1, fin.cur);
263 count++;
264 }
265 if (!fin.good() || count >= maxLen) {
266 return false;
267 }
268 if (fin.tellg() == 0) {
269 return true;
270 }
271 count = 0;
272 while (fin.good() && fin.peek() != fin.widen('\n') && fin.tellg() > 0 && count < maxLen) {
273 fin.seekg(-1, fin.cur);
274 count++;
275 }
276 if (!fin.good() || count >= maxLen) {
277 return false;
278 }
279 if (fin.tellg() != 0) {
280 fin.seekg(1, fin.cur);
281 }
282 auto oldPos = fin.tellg();
283 getline(fin, line);
284 fin.seekg(oldPos);
285 return true;
286 }
287
GetParentDir(const std::string & path)288 std::string GetParentDir(const std::string &path)
289 {
290 string str = ExtractFilePath(path);
291 if (str.empty()) {
292 return "";
293 }
294 return str.substr(0, str.size() - 1);
295 }
296
IsLegalPath(const std::string & path)297 bool IsLegalPath(const std::string& path)
298 {
299 if (path.find("./") != std::string::npos ||
300 path.find("../") != std::string::npos) {
301 return false;
302 }
303 return true;
304 }
305
RenameFile(const std::string & src,const std::string & dest)306 bool RenameFile(const std::string& src, const std::string& dest)
307 {
308 if (std::rename(src.c_str(), dest.c_str()) == 0) {
309 return true;
310 }
311 return false;
312 }
313 } // namespace FileUtil
314 } // namespace HiviewDFX
315 } // namespace OHOS
316