• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #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 <unistd.h>
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace FileUtil {
27 namespace {
28 const char PATH_DELIMITER = '/';
29 }
IsFileExists(const std::string & file)30 bool IsFileExists(const std::string& file)
31 {
32     return access(file.c_str(), F_OK) == 0;
33 }
34 
IsFile(const std::string & file)35 bool IsFile(const std::string& file)
36 {
37     struct stat statBuf {};
38     return lstat(file.c_str(), &statBuf) == 0 ? S_ISREG(statBuf.st_mode) : false;
39 }
40 
IsDirectory(const std::string & dir)41 bool IsDirectory(const std::string& dir)
42 {
43     struct stat statBuf {};
44     return lstat(dir.c_str(), &statBuf) == 0 ? S_ISDIR(statBuf.st_mode) : false;
45 }
46 
RemoveFile(const std::string & file)47 bool RemoveFile(const std::string& file)
48 {
49     return !IsFileExists(file) || (remove(file.c_str()) == 0);
50 }
51 
RemoveDirectory(const std::string & dir)52 bool RemoveDirectory(const std::string& dir)
53 {
54     return !IsFileExists(dir) || (rmdir(dir.c_str()) == 0);
55 }
56 
ForceCreateDirectory(const std::string & dir)57 bool ForceCreateDirectory(const std::string& dir)
58 {
59     std::string::size_type index = 0;
60     do {
61         std::string subPath;
62         index = dir.find('/', index + 1); // (index + 1) means the next char traversed
63         if (index == std::string::npos) {
64             subPath = dir;
65         } else {
66             subPath = dir.substr(0, index);
67         }
68 
69         if (!IsFileExists(subPath) && mkdir(subPath.c_str(), S_IRWXU) != 0) {
70             return false;
71         }
72     } while (index != std::string::npos);
73     return IsFileExists(dir);
74 }
75 
ForceRemoveDirectory(const std::string & dir,bool isDeleteSelf)76 bool ForceRemoveDirectory(const std::string& dir, bool isDeleteSelf)
77 {
78     if (IsFile(dir)) {
79         return RemoveFile(dir);
80     } else if (IsDirectory(dir)) {
81         DIR* dirPtr = opendir(dir.c_str());
82         if (dirPtr == nullptr) {
83             return false;
84         }
85         struct dirent* dirInfo = nullptr;
86         while ((dirInfo = readdir(dirPtr)) != nullptr) {
87             // do not process the special dir
88             if (strcmp(dirInfo->d_name, ".") == 0 || strcmp(dirInfo->d_name, "..") == 0) {
89                 continue;
90             }
91             std::string filePath = GetFilePathByDir(dir, dirInfo->d_name);
92             if (!ForceRemoveDirectory(filePath)) {
93                 closedir(dirPtr);
94                 return false;
95             }
96         }
97         closedir(dirPtr);
98         if (isDeleteSelf && !RemoveDirectory(dir)) {
99             return false;
100         }
101     } else {
102         return false;
103     }
104     return true;
105 }
106 
GetDirFiles(const std::string & dir,std::vector<std::string> & files)107 void GetDirFiles(const std::string& dir, std::vector<std::string>& files)
108 {
109     DIR* dirPtr = opendir(dir.c_str());
110     if (dirPtr == nullptr) {
111         return;
112     }
113 
114     struct dirent* ent = nullptr;
115     while ((ent = readdir(dirPtr)) != nullptr) {
116         if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
117             continue;
118         }
119         std::string filePath = GetFilePathByDir(dir, ent->d_name);
120         if (IsFile(filePath)) { // do not process subdirectory
121             files.push_back(filePath);
122         }
123     }
124     closedir(dirPtr);
125 }
126 
GetDirSize(const std::string & dir)127 uint64_t GetDirSize(const std::string& dir)
128 {
129     std::vector<std::string> files;
130     GetDirFiles(dir, files);
131     uint64_t totalSize = 0;
132     struct stat statBuf {};
133     for (auto& file : files) {
134         if (stat(file.c_str(), &statBuf) == 0) {
135             totalSize += static_cast<uint64_t>(statBuf.st_size);
136         }
137     }
138     return totalSize;
139 }
140 
GetFileSize(const std::string & file)141 uint64_t GetFileSize(const std::string& file)
142 {
143     struct stat statBuf {};
144     return stat(file.c_str(), &statBuf) == 0 ? static_cast<uint64_t>(statBuf.st_size) : 0;
145 }
146 
SaveStringToFile(const std::string & file,const std::string & content,bool isTrunc)147 bool SaveStringToFile(const std::string& file, const std::string& content, bool isTrunc)
148 {
149     if (content.empty()) {
150         return true;
151     }
152 
153     std::ofstream os;
154     if (isTrunc) {
155         os.open(file.c_str(), std::ios::out | std::ios::trunc);
156     } else {
157         os.open(file.c_str(), std::ios::out | std::ios::app);
158     }
159     if (!os.is_open()) {
160         return false;
161     }
162 
163     os.write(content.c_str(), content.length());
164     if (os.fail()) {
165         os.close();
166         return false;
167     }
168     os.close();
169     return true;
170 }
171 
GetFilePathByDir(const std::string & dir,const std::string & fileName)172 std::string GetFilePathByDir(const std::string& dir, const std::string& fileName)
173 {
174     if (dir.empty()) {
175         return fileName;
176     }
177     std::string filePath = dir;
178     if (filePath.back() != '/') {
179         filePath.push_back(PATH_DELIMITER);
180     }
181     filePath.append(fileName);
182     return filePath;
183 }
184 } // namespace FileUtil
185 } // namespace HiviewDFX
186 } // namespace OHOS
187