• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 #include "file_util.h"
16 
17 #include <dirent.h>
18 #include <fstream>
19 #include <iostream>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <sys/xattr.h>
23 #include <unistd.h>
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace FileUtil {
28 namespace {
29 const char PATH_DELIMITER = '/';
30 constexpr mode_t FILE_PERM_600 = S_IRUSR | S_IWUSR;
31 constexpr uint32_t BUF_SIZE_256 = 256;
32 }
IsFileExists(const std::string & file)33 bool IsFileExists(const std::string& file)
34 {
35     return access(file.c_str(), F_OK) == 0;
36 }
37 
IsFile(const std::string & file)38 bool IsFile(const std::string& file)
39 {
40     struct stat statBuf {};
41     return lstat(file.c_str(), &statBuf) == 0 ? S_ISREG(statBuf.st_mode) : false;
42 }
43 
IsDirectory(const std::string & dir)44 bool IsDirectory(const std::string& dir)
45 {
46     struct stat statBuf {};
47     return lstat(dir.c_str(), &statBuf) == 0 ? S_ISDIR(statBuf.st_mode) : false;
48 }
49 
RemoveFile(const std::string & file)50 bool RemoveFile(const std::string& file)
51 {
52     return !IsFileExists(file) || (remove(file.c_str()) == 0);
53 }
54 
RemoveDirectory(const std::string & dir)55 bool RemoveDirectory(const std::string& dir)
56 {
57     return !IsFileExists(dir) || (rmdir(dir.c_str()) == 0);
58 }
59 
ForceCreateDirectory(const std::string & dir)60 bool ForceCreateDirectory(const std::string& dir)
61 {
62     std::string::size_type index = 0;
63     do {
64         std::string subPath;
65         index = dir.find('/', index + 1); // (index + 1) means the next char traversed
66         if (index == std::string::npos) {
67             subPath = dir;
68         } else {
69             subPath = dir.substr(0, index);
70         }
71 
72         if (!IsFileExists(subPath) && mkdir(subPath.c_str(), S_IRWXU) != 0) {
73             return false;
74         }
75     } while (index != std::string::npos);
76     return IsFileExists(dir);
77 }
78 
ForceRemoveDirectory(const std::string & dir,bool isDeleteSelf)79 bool ForceRemoveDirectory(const std::string& dir, bool isDeleteSelf)
80 {
81     if (IsFile(dir)) {
82         return RemoveFile(dir);
83     } else if (IsDirectory(dir)) {
84         DIR* dirPtr = opendir(dir.c_str());
85         if (dirPtr == nullptr) {
86             return false;
87         }
88         struct dirent* dirInfo = nullptr;
89         while ((dirInfo = readdir(dirPtr)) != nullptr) {
90             // do not process the special dir
91             if (strcmp(dirInfo->d_name, ".") == 0 || strcmp(dirInfo->d_name, "..") == 0) {
92                 continue;
93             }
94             std::string filePath = GetFilePathByDir(dir, dirInfo->d_name);
95             if (!ForceRemoveDirectory(filePath)) {
96                 closedir(dirPtr);
97                 return false;
98             }
99         }
100         closedir(dirPtr);
101         if (isDeleteSelf && !RemoveDirectory(dir)) {
102             return false;
103         }
104     } else {
105         return false;
106     }
107     return true;
108 }
109 
GetDirFiles(const std::string & dir,std::vector<std::string> & files)110 void GetDirFiles(const std::string& dir, std::vector<std::string>& files)
111 {
112     DIR* dirPtr = opendir(dir.c_str());
113     if (dirPtr == nullptr) {
114         return;
115     }
116 
117     struct dirent* ent = nullptr;
118     while ((ent = readdir(dirPtr)) != nullptr) {
119         if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
120             continue;
121         }
122         std::string filePath = GetFilePathByDir(dir, ent->d_name);
123         if (IsFile(filePath)) { // do not process subdirectory
124             files.push_back(filePath);
125         }
126     }
127     closedir(dirPtr);
128 }
129 
GetDirSize(const std::string & dir)130 uint64_t GetDirSize(const std::string& dir)
131 {
132     std::vector<std::string> files;
133     GetDirFiles(dir, files);
134     uint64_t totalSize = 0;
135     struct stat statBuf {};
136     for (auto& file : files) {
137         if (stat(file.c_str(), &statBuf) == 0) {
138             totalSize += static_cast<uint64_t>(statBuf.st_size);
139         }
140     }
141     return totalSize;
142 }
143 
GetFileSize(const std::string & file)144 uint64_t GetFileSize(const std::string& file)
145 {
146     struct stat statBuf {};
147     return stat(file.c_str(), &statBuf) == 0 ? static_cast<uint64_t>(statBuf.st_size) : 0;
148 }
149 
ChangeMode(const std::string & file,const mode_t & mode)150 bool ChangeMode(const std::string& file, const mode_t& mode)
151 {
152     return (chmod(file.c_str(), mode) == 0);
153 }
154 
CreateFile(const std::string & file,const mode_t & mode)155 bool CreateFile(const std::string& file, const mode_t& mode)
156 {
157     if (IsFileExists(file)) {
158         return true;
159     }
160     std::ofstream fout(file);
161     if (!fout.is_open()) {
162         return false;
163     }
164     fout.flush();
165     fout.close();
166     return ChangeMode(file, mode);
167 }
168 
SaveStringToFile(const std::string & file,const std::string & content,bool isTrunc)169 bool SaveStringToFile(const std::string& file, const std::string& content, bool isTrunc)
170 {
171     if (content.empty()) {
172         return true;
173     }
174 
175     if (!CreateFile(file, FILE_PERM_600)) {
176         return false;
177     }
178     std::ofstream os;
179     if (isTrunc) {
180         os.open(file.c_str(), std::ios::out | std::ios::trunc);
181     } else {
182         os.open(file.c_str(), std::ios::out | std::ios::app);
183     }
184     if (!os.is_open()) {
185         return false;
186     }
187 
188     os.write(content.c_str(), content.length());
189     if (os.fail()) {
190         os.close();
191         return false;
192     }
193     os.close();
194     return true;
195 }
196 
GetFilePathByDir(const std::string & dir,const std::string & fileName)197 std::string GetFilePathByDir(const std::string& dir, const std::string& fileName)
198 {
199     if (dir.empty()) {
200         return fileName;
201     }
202     std::string filePath = dir;
203     if (filePath.back() != '/') {
204         filePath.push_back(PATH_DELIMITER);
205     }
206     filePath.append(fileName);
207     return filePath;
208 }
209 
LoadLinesFromFile(const std::string & filePath,std::vector<std::string> & lines)210 bool LoadLinesFromFile(const std::string& filePath, std::vector<std::string>& lines)
211 {
212     std::ifstream file(filePath);
213     if (file.is_open()) {
214         std::string line;
215         while (std::getline(file, line)) {
216             lines.emplace_back(line);
217         }
218         file.close();
219         return true;
220     }
221     return false;
222 }
223 
SetDirXattr(const std::string & dir,const std::string & name,const std::string & value)224 bool SetDirXattr(const std::string& dir, const std::string& name, const std::string& value)
225 {
226     return setxattr(dir.c_str(), name.c_str(), value.c_str(), strlen(value.c_str()), 0) == 0;
227 }
228 
GetDirXattr(const std::string & dir,const std::string & name,std::string & value)229 bool GetDirXattr(const std::string& dir, const std::string& name, std::string& value)
230 {
231     char buf[BUF_SIZE_256] = {0};
232     if (getxattr(dir.c_str(), name.c_str(), buf, sizeof(buf) - 1) == -1) {
233         return false;
234     }
235     value = buf;
236     return true;
237 }
238 } // namespace FileUtil
239 } // namespace HiviewDFX
240 } // namespace OHOS
241