• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 "perfetto/ext/base/file_utils.h"
18 
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 
22 #include <algorithm>
23 #include <deque>
24 #include <optional>
25 #include <string>
26 #include <vector>
27 
28 #include "perfetto/base/build_config.h"
29 #include "perfetto/base/logging.h"
30 #include "perfetto/base/platform_handle.h"
31 #include "perfetto/base/status.h"
32 #include "perfetto/ext/base/platform.h"
33 #include "perfetto/ext/base/scoped_file.h"
34 #include "perfetto/ext/base/utils.h"
35 
36 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
37 #include <Windows.h>
38 #include <direct.h>
39 #include <io.h>
40 #include <stringapiset.h>
41 #else
42 #include <dirent.h>
43 #include <unistd.h>
44 #endif
45 
46 namespace perfetto {
47 namespace base {
48 namespace {
49 constexpr size_t kBufSize = 2048;
50 
51 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
52 // Wrap FindClose to: (1) make the return unix-style; (2) deal with stdcall.
CloseFindHandle(HANDLE h)53 int CloseFindHandle(HANDLE h) {
54   return FindClose(h) ? 0 : -1;
55 }
56 
ToUtf16(const std::string str)57 std::optional<std::wstring> ToUtf16(const std::string str) {
58   int len = MultiByteToWideChar(CP_UTF8, 0, str.data(),
59                                 static_cast<int>(str.size()), nullptr, 0);
60   if (len < 0) {
61     return std::nullopt;
62   }
63   std::vector<wchar_t> tmp;
64   tmp.resize(static_cast<std::vector<wchar_t>::size_type>(len));
65   len =
66       MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.size()),
67                           tmp.data(), static_cast<int>(tmp.size()));
68   if (len < 0) {
69     return std::nullopt;
70   }
71   PERFETTO_CHECK(static_cast<std::vector<wchar_t>::size_type>(len) ==
72                  tmp.size());
73   return std::wstring(tmp.data(), tmp.size());
74 }
75 
76 #endif
77 
78 }  // namespace
79 
Read(int fd,void * dst,size_t dst_size)80 ssize_t Read(int fd, void* dst, size_t dst_size) {
81   ssize_t ret;
82   platform::BeforeMaybeBlockingSyscall();
83 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
84   ret = _read(fd, dst, static_cast<unsigned>(dst_size));
85 #else
86   ret = PERFETTO_EINTR(read(fd, dst, dst_size));
87 #endif
88   platform::AfterMaybeBlockingSyscall();
89   return ret;
90 }
91 
ReadFileDescriptor(int fd,std::string * out)92 bool ReadFileDescriptor(int fd, std::string* out) {
93   // Do not override existing data in string.
94   size_t i = out->size();
95 
96   struct stat buf {};
97   if (fstat(fd, &buf) != -1) {
98     if (buf.st_size > 0)
99       out->resize(i + static_cast<size_t>(buf.st_size));
100   }
101 
102   ssize_t bytes_read;
103   for (;;) {
104     if (out->size() < i + kBufSize)
105       out->resize(out->size() + kBufSize);
106 
107     bytes_read = Read(fd, &((*out)[i]), kBufSize);
108     if (bytes_read > 0) {
109       i += static_cast<size_t>(bytes_read);
110     } else {
111       out->resize(i);
112       return bytes_read == 0;
113     }
114   }
115 }
116 
ReadPlatformHandle(PlatformHandle h,std::string * out)117 bool ReadPlatformHandle(PlatformHandle h, std::string* out) {
118 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
119   // Do not override existing data in string.
120   size_t i = out->size();
121 
122   for (;;) {
123     if (out->size() < i + kBufSize)
124       out->resize(out->size() + kBufSize);
125     DWORD bytes_read = 0;
126     auto res = ::ReadFile(h, &((*out)[i]), kBufSize, &bytes_read, nullptr);
127     if (res && bytes_read > 0) {
128       i += static_cast<size_t>(bytes_read);
129     } else {
130       out->resize(i);
131       const bool is_eof = res && bytes_read == 0;
132       auto err = res ? 0 : GetLastError();
133       // The "Broken pipe" error on Windows is slighly different than Unix:
134       // On Unix: a "broken pipe" error can happen only on the writer side. On
135       // the reader there is no broken pipe, just a EOF.
136       // On windows: the reader also sees a broken pipe error.
137       // Here we normalize on the Unix behavior, treating broken pipe as EOF.
138       return is_eof || err == ERROR_BROKEN_PIPE;
139     }
140   }
141 #else
142   return ReadFileDescriptor(h, out);
143 #endif
144 }
145 
ReadFileStream(FILE * f,std::string * out)146 bool ReadFileStream(FILE* f, std::string* out) {
147   return ReadFileDescriptor(fileno(f), out);
148 }
149 
ReadFile(const std::string & path,std::string * out)150 bool ReadFile(const std::string& path, std::string* out) {
151   base::ScopedFile fd = base::OpenFile(path, O_RDONLY);
152   if (!fd)
153     return false;
154 
155   return ReadFileDescriptor(*fd, out);
156 }
157 
WriteAll(int fd,const void * buf,size_t count)158 ssize_t WriteAll(int fd, const void* buf, size_t count) {
159   size_t written = 0;
160   while (written < count) {
161     // write() on windows takes an unsigned int size.
162     uint32_t bytes_left = static_cast<uint32_t>(
163         std::min(count - written, static_cast<size_t>(UINT32_MAX)));
164     platform::BeforeMaybeBlockingSyscall();
165     ssize_t wr = PERFETTO_EINTR(
166         write(fd, static_cast<const char*>(buf) + written, bytes_left));
167     platform::AfterMaybeBlockingSyscall();
168     if (wr == 0)
169       break;
170     if (wr < 0)
171       return wr;
172     written += static_cast<size_t>(wr);
173   }
174   return static_cast<ssize_t>(written);
175 }
176 
WriteAllHandle(PlatformHandle h,const void * buf,size_t count)177 ssize_t WriteAllHandle(PlatformHandle h, const void* buf, size_t count) {
178 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
179   DWORD wsize = 0;
180   if (::WriteFile(h, buf, static_cast<DWORD>(count), &wsize, nullptr)) {
181     return wsize;
182   } else {
183     return -1;
184   }
185 #else
186   return WriteAll(h, buf, count);
187 #endif
188 }
189 
FlushFile(int fd)190 bool FlushFile(int fd) {
191   PERFETTO_DCHECK(fd != 0);
192 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
193     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
194   return !PERFETTO_EINTR(fdatasync(fd));
195 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
196   return !PERFETTO_EINTR(_commit(fd));
197 #else
198   return !PERFETTO_EINTR(fsync(fd));
199 #endif
200 }
201 
Mkdir(const std::string & path)202 bool Mkdir(const std::string& path) {
203 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
204   return _mkdir(path.c_str()) == 0;
205 #else
206   return mkdir(path.c_str(), 0755) == 0;
207 #endif
208 }
209 
Rmdir(const std::string & path)210 bool Rmdir(const std::string& path) {
211 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
212   return _rmdir(path.c_str()) == 0;
213 #else
214   return rmdir(path.c_str()) == 0;
215 #endif
216 }
217 
CloseFile(int fd)218 int CloseFile(int fd) {
219   return close(fd);
220 }
221 
OpenFile(const std::string & path,int flags,FileOpenMode mode)222 ScopedFile OpenFile(const std::string& path, int flags, FileOpenMode mode) {
223   // If a new file might be created, ensure that the permissions for the new
224   // file are explicitly specified.
225   PERFETTO_CHECK((flags & O_CREAT) == 0 || mode != kFileModeInvalid);
226 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
227   // Always use O_BINARY on Windows, to avoid silly EOL translations.
228   ScopedFile fd(_open(path.c_str(), flags | O_BINARY, mode));
229 #else
230   // Always open a ScopedFile with O_CLOEXEC so we can safely fork and exec.
231   ScopedFile fd(open(path.c_str(), flags | O_CLOEXEC, mode));
232 #endif
233   return fd;
234 }
235 
OpenFstream(const char * path,const char * mode)236 ScopedFstream OpenFstream(const char* path, const char* mode) {
237   ScopedFstream file;
238 // On Windows fopen interprets filename using the ANSI or OEM codepage but
239 // sqlite3_value_text returns a UTF-8 string. To make sure we interpret the
240 // filename correctly we use _wfopen and a UTF-16 string on windows.
241 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
242   auto w_path = ToUtf16(path);
243   auto w_mode = ToUtf16(mode);
244   if (w_path && w_mode) {
245     file.reset(_wfopen(w_path->c_str(), w_mode->c_str()));
246   }
247 #else
248   file.reset(fopen(path, mode));
249 #endif
250   return file;
251 }
252 
FileExists(const std::string & path)253 bool FileExists(const std::string& path) {
254 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
255   return _access(path.c_str(), 0) == 0;
256 #else
257   return access(path.c_str(), F_OK) == 0;
258 #endif
259 }
260 
261 // Declared in base/platform_handle.h.
ClosePlatformHandle(PlatformHandle handle)262 int ClosePlatformHandle(PlatformHandle handle) {
263 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
264   // Make the return value UNIX-style.
265   return CloseHandle(handle) ? 0 : -1;
266 #else
267   return close(handle);
268 #endif
269 }
270 
ListFilesRecursive(const std::string & dir_path,std::vector<std::string> & output)271 base::Status ListFilesRecursive(const std::string& dir_path,
272                                 std::vector<std::string>& output) {
273   std::string root_dir_path = dir_path;
274   if (root_dir_path.back() == '\\') {
275     root_dir_path.back() = '/';
276   } else if (root_dir_path.back() != '/') {
277     root_dir_path.push_back('/');
278   }
279 
280   // dir_queue contains full paths to the directories. The paths include the
281   // root_dir_path at the beginning and the trailing slash at the end.
282   std::deque<std::string> dir_queue;
283   dir_queue.push_back(root_dir_path);
284 
285   while (!dir_queue.empty()) {
286     const std::string cur_dir = std::move(dir_queue.front());
287     dir_queue.pop_front();
288 #if PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
289     return base::ErrStatus("ListFilesRecursive not supported yet");
290 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
291     std::string glob_path = cur_dir + "*";
292     // + 1 because we also have to count the NULL terminator.
293     if (glob_path.length() + 1 > MAX_PATH)
294       return base::ErrStatus("Directory path %s is too long", dir_path.c_str());
295     WIN32_FIND_DATAA ffd;
296 
297     base::ScopedResource<HANDLE, CloseFindHandle, nullptr, false,
298                          base::PlatformHandleChecker>
299         hFind(FindFirstFileA(glob_path.c_str(), &ffd));
300     if (!hFind) {
301       // For empty directories, there should be at least one entry '.'.
302       // If FindFirstFileA returns INVALID_HANDLE_VALUE, this means directory
303       // couldn't be accessed.
304       return base::ErrStatus("Failed to open directory %s", cur_dir.c_str());
305     }
306     do {
307       if (strcmp(ffd.cFileName, ".") == 0 || strcmp(ffd.cFileName, "..") == 0)
308         continue;
309       if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
310         std::string subdir_path = cur_dir + ffd.cFileName + '/';
311         dir_queue.push_back(subdir_path);
312       } else {
313         const std::string full_path = cur_dir + ffd.cFileName;
314         PERFETTO_CHECK(full_path.length() > root_dir_path.length());
315         output.push_back(full_path.substr(root_dir_path.length()));
316       }
317     } while (FindNextFileA(*hFind, &ffd));
318 #else
319     ScopedDir dir = ScopedDir(opendir(cur_dir.c_str()));
320     if (!dir) {
321       return base::ErrStatus("Failed to open directory %s", cur_dir.c_str());
322     }
323     for (auto* dirent = readdir(dir.get()); dirent != nullptr;
324          dirent = readdir(dir.get())) {
325       if (strcmp(dirent->d_name, ".") == 0 ||
326           strcmp(dirent->d_name, "..") == 0) {
327         continue;
328       }
329       if (dirent->d_type == DT_DIR) {
330         dir_queue.push_back(cur_dir + dirent->d_name + '/');
331       } else if (dirent->d_type == DT_REG) {
332         const std::string full_path = cur_dir + dirent->d_name;
333         PERFETTO_CHECK(full_path.length() > root_dir_path.length());
334         output.push_back(full_path.substr(root_dir_path.length()));
335       }
336     }
337 #endif
338   }
339   return base::OkStatus();
340 }
341 
GetFileExtension(const std::string & filename)342 std::string GetFileExtension(const std::string& filename) {
343   auto ext_idx = filename.rfind('.');
344   if (ext_idx == std::string::npos)
345     return std::string();
346   return filename.substr(ext_idx);
347 }
348 
349 }  // namespace base
350 }  // namespace perfetto
351