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 #ifndef INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_ 18 #define INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_ 19 20 #include <fcntl.h> // For mode_t & O_RDONLY/RDWR. Exists also on Windows. 21 #include <stddef.h> 22 23 #include <string> 24 #include <vector> 25 26 #include "perfetto/base/build_config.h" 27 #include "perfetto/base/export.h" 28 #include "perfetto/base/status.h" 29 #include "perfetto/ext/base/scoped_file.h" 30 #include "perfetto/ext/base/optional.h" 31 #include "perfetto/ext/base/utils.h" 32 33 namespace perfetto { 34 namespace base { 35 36 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) 37 using FileOpenMode = int; 38 #else 39 using FileOpenMode = mode_t; 40 #endif 41 42 constexpr FileOpenMode kFileModeInvalid = static_cast<FileOpenMode>(-1); 43 44 bool ReadPlatformHandle(PlatformHandle, std::string* out); 45 bool ReadFileDescriptor(int fd, std::string* out); 46 bool ReadFileStream(FILE* f, std::string* out); 47 bool ReadFile(const std::string& path, std::string* out); 48 49 // A wrapper around read(2). It deals with Linux vs Windows includes. It also 50 // deals with handling EINTR. Has the same semantics of UNIX's read(2). 51 ssize_t Read(int fd, void* dst, size_t dst_size); 52 53 // Call write until all data is written or an error is detected. 54 // 55 // man 2 write: 56 // If a write() is interrupted by a signal handler before any bytes are 57 // written, then the call fails with the error EINTR; if it is 58 // interrupted after at least one byte has been written, the call 59 // succeeds, and returns the number of bytes written. 60 ssize_t WriteAll(int fd, const void* buf, size_t count); 61 62 ssize_t WriteAllHandle(PlatformHandle, const void* buf, size_t count); 63 64 ScopedFile OpenFile(const std::string& path, 65 int flags, 66 FileOpenMode = kFileModeInvalid); 67 68 // This is an alias for close(). It's to avoid leaking Windows.h in headers. 69 // Exported because ScopedFile is used in the /include/ext API by Chromium 70 // component builds. 71 int PERFETTO_EXPORT CloseFile(int fd); 72 73 bool FlushFile(int fd); 74 75 // Returns true if mkdir succeeds, false if it fails (see errno in that case). 76 bool Mkdir(const std::string& path); 77 78 // Calls rmdir() on UNIX, _rmdir() on Windows. 79 bool Rmdir(const std::string& path); 80 81 // Wrapper around access(path, F_OK). 82 bool FileExists(const std::string& path); 83 84 // Gets the extension for a filename. If the file has two extensions, returns 85 // only the last one (foo.pb.gz => .gz). Returns empty string if there is no 86 // extension. 87 std::string GetFileExtension(const std::string& filename); 88 89 // Puts the path to all files under |dir_path| in |output|, recursively walking 90 // subdirectories. File paths are relative to |dir_path|. Only files are 91 // included, not directories. Path separator is always '/', even on windows (not 92 // '\'). 93 base::Status ListFilesRecursive(const std::string& dir_path, 94 std::vector<std::string>& output); 95 96 // Returns the size of the file at `path` or nullopt in case of error. 97 Optional<size_t> GetFileSize(const std::string& path); 98 99 } // namespace base 100 } // namespace perfetto 101 102 #endif // INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_ 103