| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 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 #ifndef CRASHPAD_UTIL_FILE_FILE_WRITER_H_ |
| 16 #define CRASHPAD_UTIL_FILE_FILE_WRITER_H_ |
| 17 |
| 18 #include <sys/types.h> |
| 19 |
| 20 #include <vector> |
| 21 |
| 22 #include "base/basictypes.h" |
| 23 #include "base/files/file_path.h" |
| 24 #include "util/file/file_io.h" |
| 25 #include "util/file/file_seeker.h" |
| 26 |
| 27 namespace crashpad { |
| 28 |
| 29 //! \brief A version of `iovec` with a `const` #iov_base field. |
| 30 //! |
| 31 //! This structure is intended to be used for write operations. |
| 32 // |
| 33 // Type compatibility with iovec is tested with static assertions in the |
| 34 // implementation file. |
| 35 struct WritableIoVec { |
| 36 //! \brief The base address of a memory region for output. |
| 37 const void* iov_base; |
| 38 |
| 39 //! \brief The size of the memory pointed to by #iov_base. |
| 40 size_t iov_len; |
| 41 }; |
| 42 |
| 43 //! \brief An interface to write to files and other file-like objects with |
| 44 //! semantics matching the underlying platform (POSIX or Windows). |
| 45 class FileWriterInterface : public virtual FileSeekerInterface { |
| 46 public: |
| 47 virtual ~FileWriterInterface() {} |
| 48 |
| 49 //! \brief Wraps LoggingWriteFile(), or provides an implementation with |
| 50 //! identical semantics. |
| 51 //! |
| 52 //! \return `true` if the operation succeeded, `false` if it failed, with an |
| 53 //! error message logged. |
| 54 virtual bool Write(const void* data, size_t size) = 0; |
| 55 |
| 56 //! \brief Wraps `writev()` on POSIX or provides an alternate implementation |
| 57 //! with identical semantics. This method will write entire buffers, |
| 58 //! continuing after a short write or after being interrupted. On |
| 59 //! non-POSIX this is a simple wrapper around Write(). |
| 60 //! |
| 61 //! \return `true` if the operation succeeded, `false` if it failed, with an |
| 62 //! error message logged. |
| 63 //! |
| 64 //! \note The contents of \a iovecs are undefined when this method returns. |
| 65 virtual bool WriteIoVec(std::vector<WritableIoVec>* iovecs) = 0; |
| 66 }; |
| 67 |
| 68 //! \brief A file writer backed by a FileHandle. |
| 69 //! |
| 70 //! FileWriter requires users to provide a FilePath to open, but this class |
| 71 //! accepts an already-open FileHandle instead. Like FileWriter, this class may |
| 72 //! write to a filesystem-based file, but unlike FileWriter, this class is not |
| 73 //! responsible for creating or closing the file. Users of this class must |
| 74 //! ensure that the file handle is closed appropriately elsewhere. Objects of |
| 75 //! this class may be used to write to file handles not associated with |
| 76 //! filesystem-based files, although special attention should be paid to the |
| 77 //! Seek() method, which may not function on file handles that do not refer to |
| 78 //! disk-based files. |
| 79 //! |
| 80 //! This class is expected to be used when other code is responsible for |
| 81 //! creating files and already provides file handles. |
| 82 class WeakFileHandleFileWriter : public FileWriterInterface { |
| 83 public: |
| 84 explicit WeakFileHandleFileWriter(FileHandle file_handle); |
| 85 ~WeakFileHandleFileWriter() override; |
| 86 |
| 87 // FileWriterInterface: |
| 88 bool Write(const void* data, size_t size) override; |
| 89 bool WriteIoVec(std::vector<WritableIoVec>* iovecs) override; |
| 90 |
| 91 // FileSeekerInterface: |
| 92 |
| 93 //! \copydoc FileWriterInterface::Seek() |
| 94 //! |
| 95 //! \note This method is only guaranteed to function on file handles referring |
| 96 //! to disk-based files. |
| 97 FileOffset Seek(FileOffset offset, int whence) override; |
| 98 |
| 99 private: |
| 100 void set_file_handle(FileHandle file_handle) { file_handle_ = file_handle; } |
| 101 |
| 102 FileHandle file_handle_; // weak |
| 103 |
| 104 // FileWriter uses this class as its internal implementation, and it needs to |
| 105 // be able to call set_file_handle(). FileWriter cannot initialize a |
| 106 // WeakFileHandleFileWriter with a correct file descriptor at the time of |
| 107 // construction because no file descriptor will be available until |
| 108 // FileWriter::Open() is called. |
| 109 friend class FileWriter; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileWriter); |
| 112 }; |
| 113 |
| 114 //! \brief A file writer implementation that wraps traditional system file |
| 115 //! operations on files accessed through the filesystem. |
| 116 class FileWriter : public FileWriterInterface { |
| 117 public: |
| 118 FileWriter(); |
| 119 ~FileWriter() override; |
| 120 |
| 121 // FileWriterInterface: |
| 122 |
| 123 //! \brief Wraps LoggingOpenFileForWrite(). |
| 124 //! |
| 125 //! \return `true` if the operation succeeded, `false` if it failed, with an |
| 126 //! error message logged. |
| 127 //! |
| 128 //! \note After a successful call, this method cannot be called again until |
| 129 //! after Close(). |
| 130 bool Open(const base::FilePath& path, |
| 131 FileWriteMode write_mode, |
| 132 FilePermissions permissions); |
| 133 |
| 134 //! \brief Wraps CheckedCloseHandle(). |
| 135 //! |
| 136 //! \note It is only valid to call this method on an object that has had a |
| 137 //! successful Open() that has not yet been matched by a subsequent call |
| 138 //! to this method. |
| 139 void Close(); |
| 140 |
| 141 // FileWriterInterface: |
| 142 |
| 143 //! \copydoc FileWriterInterface::Write() |
| 144 //! |
| 145 //! \note It is only valid to call this method between a successful Open() and |
| 146 //! a Close(). |
| 147 bool Write(const void* data, size_t size) override; |
| 148 |
| 149 //! \copydoc FileWriterInterface::WriteIoVec() |
| 150 //! |
| 151 //! \note It is only valid to call this method between a successful Open() and |
| 152 //! a Close(). |
| 153 bool WriteIoVec(std::vector<WritableIoVec>* iovecs) override; |
| 154 |
| 155 // FileSeekerInterface: |
| 156 |
| 157 //! \copydoc FileWriterInterface::Seek() |
| 158 //! |
| 159 //! \note It is only valid to call this method between a successful Open() and |
| 160 //! a Close(). |
| 161 FileOffset Seek(FileOffset offset, int whence) override; |
| 162 |
| 163 private: |
| 164 ScopedFileHandle file_; |
| 165 WeakFileHandleFileWriter weak_file_handle_file_writer_; |
| 166 |
| 167 DISALLOW_COPY_AND_ASSIGN(FileWriter); |
| 168 }; |
| 169 |
| 170 } // namespace crashpad |
| 171 |
| 172 #endif // CRASHPAD_UTIL_FILE_FILE_WRITER_H_ |
| OLD | NEW |