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