• 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 
ForceCreateDirectory(const std::string & path)96 bool ForceCreateDirectory(const std::string& path)
97 {
98     return OHOS::ForceCreateDirectory(path);
99 }
100 
ForceCreateDirectory(const string & path,mode_t mode)101 bool ForceCreateDirectory(const string& path, mode_t mode)
102 {
103     string::size_type index = 0;
104     do {
105         index = path.find('/', index + 1);
106         string subPath = (index == string::npos) ? path : path.substr(0, index);
107         if (access(subPath.c_str(), F_OK) != 0) {
108             if (mkdir(subPath.c_str(), mode) != 0) {
109                 return false;
110             }
111         }
112     } while (index != string::npos);
113     return access(path.c_str(), F_OK) == 0;
114 }
115 
ForceRemoveDirectory(const std::string & path,bool isNeedDeleteGivenDirSelf)116 bool ForceRemoveDirectory(const std::string& path, bool isNeedDeleteGivenDirSelf)
117 {
118     return OHOS::ForceRemoveDirectory(path);
119 }
120 
GetFileSize(const std::string & path)121 uint64_t GetFileSize(const std::string& path)
122 {
123     struct stat st;
124     return stat(path.c_str(), &st) ? 0 : static_cast<uint64_t>(st.st_size);
125 }
126 
RemoveFile(const std::string & fileName)127 bool RemoveFile(const std::string& fileName)
128 {
129     return OHOS::RemoveFile(fileName);
130 }
131 
GetFolderSize(const std::string & path)132 uint64_t GetFolderSize(const std::string& path)
133 {
134     return OHOS::GetFolderSize(path);
135 }
136 
137 // inner function, and param is legitimate
ChangeMode(const string & fileName,const mode_t & mode)138 bool ChangeMode(const string& fileName, const mode_t& mode)
139 {
140     return (chmod(fileName.c_str(), mode) == 0);
141 }
142 
ChangeModeFile(const string & fileName,const mode_t & mode)143 bool ChangeModeFile(const string& fileName, const mode_t& mode)
144 {
145     if (access(fileName.c_str(), F_OK) != 0) {
146         return false;
147     }
148 
149     return ChangeMode(fileName, mode);
150 }
151 
ChangeModeDirectory(const std::string & path,const mode_t & mode)152 bool ChangeModeDirectory(const std::string& path, const mode_t& mode)
153 {
154     return OHOS::ChangeModeDirectory(path, mode);
155 }
156 
PathToRealPath(const std::string & path,std::string & realPath)157 bool PathToRealPath(const std::string& path, std::string& realPath)
158 {
159     return OHOS::PathToRealPath(path, realPath);
160 }
161 
Umask(const mode_t & mode)162 mode_t Umask(const mode_t& mode)
163 {
164     return umask(mode);
165 }
166 
Open(const std::string & path,const int flags,const mode_t mode)167 int Open(const std::string& path, const int flags, const mode_t mode)
168 {
169     return open(path.c_str(), flags, mode);
170 }
171 
CreateDirWithDefaultPerm(const std::string & path,uid_t aidRoot,uid_t aidSystem)172 void CreateDirWithDefaultPerm(const std::string& path, uid_t aidRoot, uid_t aidSystem)
173 {
174     FileUtil::ForceCreateDirectory(path);
175     chown(path.c_str(), aidRoot, aidSystem);
176 }
177 
FormatPath2UnixStyle(std::string & path)178 void FormatPath2UnixStyle(std::string &path)
179 {
180     // unimplemented
181 }
182 
RemoveFolderBeginWith(const std::string & path,const std::string & folderName)183 void RemoveFolderBeginWith(const std::string &path, const std::string &folderName)
184 {
185     // unimplemented
186 }
187 
WriteBufferToFd(int fd,const char * buffer,size_t size)188 bool WriteBufferToFd(int fd, const char* buffer, size_t size)
189 {
190     if (fd < 0) {
191         return false;
192     }
193 
194     if (buffer == nullptr) {
195         return false;
196     }
197 
198     ssize_t writeSize = size;
199     if (writeSize != TEMP_FAILURE_RETRY(write(fd, buffer, size))) {
200         return false;
201     }
202 
203     return true;
204 }
205 
CreateFile(const std::string & path,mode_t mode)206 int CreateFile(const std::string &path, mode_t mode)
207 {
208     if (FileExists(path)) {
209         return 0;
210     } else {
211         std::ofstream fout(path);
212         if (!fout.is_open()) {
213             return -1;
214         }
215         fout.flush();
216         fout.close();
217         if (ChangeMode(path, mode) != 0) {
218             return -1;
219         }
220     }
221     return 0;
222 }
223 
CopyFile(const std::string & src,const std::string & des)224 int CopyFile(const std::string &src, const std::string &des)
225 {
226     std::ifstream fin(src, ios::binary);
227     std::ofstream fout(des, ios::binary);
228     if (!fin.is_open()) {
229         return -1;
230     }
231     if (!fout.is_open()) {
232         return -1;
233     }
234     fout << fin.rdbuf();
235     if (fout.fail()) {
236         fout.clear();
237     }
238     fout.flush();
239     return 0;
240 }
241 
IsDirectory(const std::string & path)242 bool IsDirectory(const std::string &path)
243 {
244     struct stat statBuffer;
245     if (stat(path.c_str(), &statBuffer) == 0 && S_ISDIR(statBuffer.st_mode)) {
246         return true;
247     }
248     return false;
249 }
250 
GetLastLine(std::istream & fin,std::string & line,uint32_t maxLen)251 bool GetLastLine(std::istream &fin, std::string &line, uint32_t maxLen)
252 {
253     if (fin.tellg() <= 0) {
254         return false;
255     } else {
256         fin.seekg(-1, fin.cur);
257     }
258     uint32_t count = 0;
259     while (fin.good() && fin.peek() == fin.widen('\n') && fin.tellg() > 0 && count < maxLen) {
260         fin.seekg(-1, fin.cur);
261         count++;
262     }
263     if (!fin.good() || count >= maxLen) {
264         return false;
265     }
266     if (fin.tellg() == 0) {
267         return true;
268     }
269     count = 0;
270     while (fin.good() && fin.peek() != fin.widen('\n') && fin.tellg() > 0 && count < maxLen) {
271         fin.seekg(-1, fin.cur);
272         count++;
273     }
274     if (!fin.good() || count >= maxLen) {
275         return false;
276     }
277     if (fin.tellg() != 0) {
278         fin.seekg(1, fin.cur);
279     }
280     auto oldPos = fin.tellg();
281     getline(fin, line);
282     fin.seekg(oldPos);
283     return true;
284 }
285 
GetParentDir(const std::string & path)286 std::string GetParentDir(const std::string &path)
287 {
288     string str = ExtractFilePath(path);
289     if (str.empty()) {
290         return "";
291     }
292     return str.substr(0, str.size() - 1);
293 }
294 
IsLegalPath(const std::string & path)295 bool IsLegalPath(const std::string& path)
296 {
297     if (path.find("./") != std::string::npos ||
298         path.find("../") != std::string::npos) {
299         return false;
300     }
301     return true;
302 }
303 
RenameFile(const std::string & src,const std::string & dest)304 bool RenameFile(const std::string& src, const std::string& dest)
305 {
306     if (std::rename(src.c_str(), dest.c_str()) == 0) {
307         return true;
308     }
309     return false;
310 }
311 } // namespace FileUtil
312 } // namespace HiviewDFX
313 } // namespace OHOS
314