1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "common/libs/utils/files.h"
18
19 #include <android-base/logging.h>
20
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <ftw.h>
24 #include <libgen.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <array>
30 #include <cerrno>
31 #include <chrono>
32 #include <climits>
33 #include <cstdio>
34 #include <cstdlib>
35 #include <cstring>
36 #include <fstream>
37 #include <ios>
38 #include <iosfwd>
39 #include <istream>
40 #include <memory>
41 #include <ostream>
42 #include <ratio>
43 #include <string>
44 #include <vector>
45
46 #include <android-base/macros.h>
47
48 #include "common/libs/fs/shared_fd.h"
49
50 namespace cuttlefish {
51
FileExists(const std::string & path,bool follow_symlinks)52 bool FileExists(const std::string& path, bool follow_symlinks) {
53 struct stat st;
54 return (follow_symlinks ? stat : lstat)(path.c_str(), &st) == 0;
55 }
56
FileHasContent(const std::string & path)57 bool FileHasContent(const std::string& path) {
58 return FileSize(path) > 0;
59 }
60
DirectoryContents(const std::string & path)61 std::vector<std::string> DirectoryContents(const std::string& path) {
62 std::vector<std::string> ret;
63 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(path.c_str()), closedir);
64 CHECK(dir != nullptr) << "Could not read from dir \"" << path << "\"";
65 if (dir) {
66 struct dirent *ent;
67 while ((ent = readdir(dir.get()))) {
68 ret.push_back(ent->d_name);
69 }
70 }
71 return ret;
72 }
73
DirectoryExists(const std::string & path,bool follow_symlinks)74 bool DirectoryExists(const std::string& path, bool follow_symlinks) {
75 struct stat st;
76 if ((follow_symlinks ? stat : lstat)(path.c_str(), &st) == -1) {
77 return false;
78 }
79 if ((st.st_mode & S_IFMT) != S_IFDIR) {
80 return false;
81 }
82 return true;
83 }
84
EnsureDirectoryExists(const std::string & directory_path)85 Result<void> EnsureDirectoryExists(const std::string& directory_path) {
86 if (!DirectoryExists(directory_path)) {
87 LOG(DEBUG) << "Setting up " << directory_path;
88 if (mkdir(directory_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) <
89 0 &&
90 errno != EEXIST) {
91 return CF_ERRNO("Failed to create dir: \"" << directory_path);
92 }
93 }
94 return {};
95 }
96
IsDirectoryEmpty(const std::string & path)97 bool IsDirectoryEmpty(const std::string& path) {
98 auto direc = ::opendir(path.c_str());
99 if (!direc) {
100 LOG(ERROR) << "IsDirectoryEmpty test failed with " << path
101 << " as it failed to be open" << std::endl;
102 return false;
103 }
104
105 decltype(::readdir(direc)) sub = nullptr;
106 int cnt {0};
107 while ( (sub = ::readdir(direc)) ) {
108 cnt++;
109 if (cnt > 2) {
110 LOG(ERROR) << "IsDirectoryEmpty test failed with " << path
111 << " as it exists but not empty" << std::endl;
112 return false;
113 }
114 }
115 return true;
116 }
117
RecursivelyRemoveDirectory(const std::string & path)118 bool RecursivelyRemoveDirectory(const std::string& path) {
119 // Copied from libbase TemporaryDir destructor.
120 auto callback = [](const char* child, const struct stat*, int file_type,
121 struct FTW*) -> int {
122 switch (file_type) {
123 case FTW_D:
124 case FTW_DP:
125 case FTW_DNR:
126 if (rmdir(child) == -1) {
127 PLOG(ERROR) << "rmdir " << child;
128 }
129 break;
130 case FTW_NS:
131 default:
132 if (rmdir(child) != -1) {
133 break;
134 }
135 // FALLTHRU (for gcc, lint, pcc, etc; and following for clang)
136 FALLTHROUGH_INTENDED;
137 case FTW_F:
138 case FTW_SL:
139 case FTW_SLN:
140 if (unlink(child) == -1) {
141 PLOG(ERROR) << "unlink " << child;
142 }
143 break;
144 }
145 return 0;
146 };
147
148 return nftw(path.c_str(), callback, 128, FTW_DEPTH | FTW_MOUNT | FTW_PHYS) ==
149 0;
150 }
151
AbsolutePath(const std::string & path)152 std::string AbsolutePath(const std::string& path) {
153 if (path.empty()) {
154 return {};
155 }
156 if (path[0] == '/') {
157 return path;
158 }
159 if (path[0] == '~') {
160 LOG(WARNING) << "Tilde expansion in path " << path <<" is not supported";
161 return {};
162 }
163
164 std::array<char, PATH_MAX> buffer{};
165 if (!realpath(".", buffer.data())) {
166 LOG(WARNING) << "Could not get real path for current directory \".\""
167 << ": " << strerror(errno);
168 return {};
169 }
170 return std::string{buffer.data()} + "/" + path;
171 }
172
FileSize(const std::string & path)173 off_t FileSize(const std::string& path) {
174 struct stat st;
175 if (stat(path.c_str(), &st) == -1) {
176 return 0;
177 }
178 return st.st_size;
179 }
180
MakeFileExecutable(const std::string & path)181 bool MakeFileExecutable(const std::string& path) {
182 LOG(DEBUG) << "Making " << path << " executable";
183 return chmod(path.c_str(), S_IRWXU) == 0;
184 }
185
186 // TODO(schuffelen): Use std::filesystem::last_write_time when on C++17
FileModificationTime(const std::string & path)187 std::chrono::system_clock::time_point FileModificationTime(const std::string& path) {
188 struct stat st;
189 if (stat(path.c_str(), &st) == -1) {
190 return std::chrono::system_clock::time_point();
191 }
192 std::chrono::seconds seconds(st.st_mtim.tv_sec);
193 return std::chrono::system_clock::time_point(seconds);
194 }
195
RenameFile(const std::string & old_name,const std::string & new_name)196 bool RenameFile(const std::string& old_name, const std::string& new_name) {
197 LOG(DEBUG) << "Renaming " << old_name << " to " << new_name;
198 if(rename(old_name.c_str(), new_name.c_str())) {
199 LOG(ERROR) << "File rename failed due to " << strerror(errno);
200 return false;
201 }
202
203 return true;
204 }
205
RemoveFile(const std::string & file)206 bool RemoveFile(const std::string& file) {
207 LOG(DEBUG) << "Removing file " << file;
208 return remove(file.c_str()) == 0;
209 }
210
ReadFile(const std::string & file)211 std::string ReadFile(const std::string& file) {
212 std::string contents;
213 std::ifstream in(file, std::ios::in | std::ios::binary);
214 in.seekg(0, std::ios::end);
215 if (in.fail()) {
216 // TODO(schuffelen): Return a failing Result instead
217 return "";
218 }
219 contents.resize(in.tellg());
220 in.seekg(0, std::ios::beg);
221 in.read(&contents[0], contents.size());
222 in.close();
223 return(contents);
224 }
225
CurrentDirectory()226 std::string CurrentDirectory() {
227 char* path = getcwd(nullptr, 0);
228 if (path == nullptr) {
229 PLOG(ERROR) << "`getcwd(nullptr, 0)` failed";
230 return "";
231 }
232 std::string ret(path);
233 free(path);
234 return ret;
235 }
236
SparseFileSizes(const std::string & path)237 FileSizes SparseFileSizes(const std::string& path) {
238 auto fd = SharedFD::Open(path, O_RDONLY);
239 if (!fd->IsOpen()) {
240 LOG(ERROR) << "Could not open \"" << path << "\": " << fd->StrError();
241 return {};
242 }
243 off_t farthest_seek = fd->LSeek(0, SEEK_END);
244 LOG(VERBOSE) << "Farthest seek: " << farthest_seek;
245 if (farthest_seek == -1) {
246 LOG(ERROR) << "Could not lseek in \"" << path << "\": " << fd->StrError();
247 return {};
248 }
249 off_t data_bytes = 0;
250 off_t offset = 0;
251 while (offset < farthest_seek) {
252 off_t new_offset = fd->LSeek(offset, SEEK_HOLE);
253 if (new_offset == -1) {
254 // ENXIO is returned when there are no more blocks of this type coming.
255 if (fd->GetErrno() == ENXIO) {
256 break;
257 } else {
258 LOG(ERROR) << "Could not lseek in \"" << path << "\": " << fd->StrError();
259 return {};
260 }
261 } else {
262 data_bytes += new_offset - offset;
263 offset = new_offset;
264 }
265 if (offset >= farthest_seek) {
266 break;
267 }
268 new_offset = fd->LSeek(offset, SEEK_DATA);
269 if (new_offset == -1) {
270 // ENXIO is returned when there are no more blocks of this type coming.
271 if (fd->GetErrno() == ENXIO) {
272 break;
273 } else {
274 LOG(ERROR) << "Could not lseek in \"" << path << "\": " << fd->StrError();
275 return {};
276 }
277 } else {
278 offset = new_offset;
279 }
280 }
281 return (FileSizes) { .sparse_size = farthest_seek, .disk_size = data_bytes };
282 }
283
cpp_basename(const std::string & str)284 std::string cpp_basename(const std::string& str) {
285 char* copy = strdup(str.c_str()); // basename may modify its argument
286 std::string ret(basename(copy));
287 free(copy);
288 return ret;
289 }
290
cpp_dirname(const std::string & str)291 std::string cpp_dirname(const std::string& str) {
292 char* copy = strdup(str.c_str()); // dirname may modify its argument
293 std::string ret(dirname(copy));
294 free(copy);
295 return ret;
296 }
297
FileIsSocket(const std::string & path)298 bool FileIsSocket(const std::string& path) {
299 struct stat st;
300 return stat(path.c_str(), &st) == 0 && S_ISSOCK(st.st_mode);
301 }
302
303
304 } // namespace cuttlefish
305