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