| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #ifndef CRASHPAD_UTIL_FILE_FILE_WRITER_H_ | 15 #ifndef CRASHPAD_UTIL_FILE_FILE_WRITER_H_ |
| 16 #define CRASHPAD_UTIL_FILE_FILE_WRITER_H_ | 16 #define CRASHPAD_UTIL_FILE_FILE_WRITER_H_ |
| 17 | 17 |
| 18 #include <fcntl.h> | 18 #include <sys/types.h> |
| 19 #include <stddef.h> | |
| 20 | 19 |
| 21 #include <string> | |
| 22 #include <vector> | 20 #include <vector> |
| 23 | 21 |
| 24 #include "base/basictypes.h" | 22 #include "base/basictypes.h" |
| 25 #include "base/files/file_path.h" | 23 #include "base/files/file_path.h" |
| 26 #include "build/build_config.h" | |
| 27 #include "util/file/file_io.h" | 24 #include "util/file/file_io.h" |
| 28 | 25 |
| 29 namespace crashpad { | 26 namespace crashpad { |
| 30 | 27 |
| 31 //! \brief A version of `iovec` with a `const` #iov_base field. | 28 //! \brief A version of `iovec` with a `const` #iov_base field. |
| 32 //! | 29 //! |
| 33 //! This structure is intended to be used for write operations. | 30 //! This structure is intended to be used for write operations. |
| 34 // | 31 // |
| 35 // Type compatibility with iovec is tested with static assertions in the | 32 // Type compatibility with iovec is tested with static assertions in the |
| 36 // implementation file. | 33 // implementation file. |
| 37 struct WritableIoVec { | 34 struct WritableIoVec { |
| 38 //! \brief The base address of a memory region for output. | 35 //! \brief The base address of a memory region for output. |
| 39 const void* iov_base; | 36 const void* iov_base; |
| 40 | 37 |
| 41 //! \brief The size of the memory pointed to by #iov_base. | 38 //! \brief The size of the memory pointed to by #iov_base. |
| 42 size_t iov_len; | 39 size_t iov_len; |
| 43 }; | 40 }; |
| 44 | 41 |
| 45 //! \brief An interface to write to files and other file-like objects with | 42 //! \brief An interface to write to files and other file-like objects with |
| 46 //! semantics matching the underlying platform (POSIX or Windows). | 43 //! semantics matching the underlying platform (POSIX or Windows). |
| 44 // |
| 45 // TODO(mark): Templatize and share more with FileReaderInterface? |
| 47 class FileWriterInterface { | 46 class FileWriterInterface { |
| 48 public: | 47 public: |
| 49 //! \brief Wraps WriteFile(), or provides an implementation with identical | 48 //! \brief Wraps WriteFile(), or provides an implementation with identical |
| 50 //! semantics. | 49 //! semantics. |
| 51 //! | 50 //! |
| 52 //! \return `true` if the operation succeeded, `false` if it failed, with an | 51 //! \return `true` if the operation succeeded, `false` if it failed, with an |
| 53 //! error message logged. | 52 //! error message logged. |
| 54 virtual bool Write(const void* data, size_t size) = 0; | 53 virtual bool Write(const void* data, size_t size) = 0; |
| 55 | 54 |
| 56 //! \brief Wraps `writev()` on POSIX or provides an alternate implementation | 55 //! \brief Wraps `writev()` on POSIX or provides an alternate implementation |
| 57 //! with identical semantics. This method will write entire buffers, | 56 //! with identical semantics. This method will write entire buffers, |
| 58 //! continuing after a short write or after being interrupted. On | 57 //! continuing after a short write or after being interrupted. On |
| 59 //! non-POSIX this is a simple wrapper around Write(). | 58 //! non-POSIX this is a simple wrapper around Write(). |
| 60 //! | 59 //! |
| 61 //! \return `true` if the operation succeeded, `false` if it failed, with an | 60 //! \return `true` if the operation succeeded, `false` if it failed, with an |
| 62 //! error message logged. | 61 //! error message logged. |
| 63 //! | 62 //! |
| 64 //! \note The contents of \a iovecs are undefined when this method returns. | 63 //! \note The contents of \a iovecs are undefined when this method returns. |
| 65 virtual bool WriteIoVec(std::vector<WritableIoVec>* iovecs) = 0; | 64 virtual bool WriteIoVec(std::vector<WritableIoVec>* iovecs) = 0; |
| 66 | 65 |
| 67 //! \brief Wraps LoggingFileSeek() or provides an alternate implementation | 66 //! \brief Wraps LoggingFileSeek() or provides an alternate implementation |
| 68 //! with identical semantics. | 67 //! with identical semantics. |
| 69 //! | 68 //! |
| 70 //! \return The return value of LoggingFileSeek(). `-1` on failure, | 69 //! \return The return value of LoggingFileSeek(). `-1` on failure, |
| 71 //! with an error message logged. | 70 //! with an error message logged. |
| 71 // |
| 72 // TODO(mark): This may be better with a common base of FileWriterInterface |
| 73 // and FileReaderInterface to declaring Seek(). |
| 72 virtual FileOffset Seek(FileOffset offset, int whence) = 0; | 74 virtual FileOffset Seek(FileOffset offset, int whence) = 0; |
| 73 | 75 |
| 74 protected: | 76 protected: |
| 75 ~FileWriterInterface() {} | 77 ~FileWriterInterface() {} |
| 76 }; | 78 }; |
| 77 | 79 |
| 78 //! \brief A file writer backed by a FileHandle. | 80 //! \brief A file writer backed by a FileHandle. |
| 79 //! | 81 //! |
| 80 //! FileWriter requires users to provide a FilePath to open, but this class | 82 //! FileWriter requires users to provide a FilePath to open, but this class |
| 81 //! accepts an already-open FileHandle instead. Like FileWriter, this class may | 83 //! accepts an already-open FileHandle instead. Like FileWriter, this class may |
| (...skipping 21 matching lines...) Expand all Loading... |
| 103 //! \note This method is only guaranteed to function on file handles referring | 105 //! \note This method is only guaranteed to function on file handles referring |
| 104 //! to disk-based files. | 106 //! to disk-based files. |
| 105 FileOffset Seek(FileOffset offset, int whence) override; | 107 FileOffset Seek(FileOffset offset, int whence) override; |
| 106 | 108 |
| 107 private: | 109 private: |
| 108 void set_file_handle(FileHandle file_handle) { file_handle_ = file_handle; } | 110 void set_file_handle(FileHandle file_handle) { file_handle_ = file_handle; } |
| 109 | 111 |
| 110 FileHandle file_handle_; // weak | 112 FileHandle file_handle_; // weak |
| 111 | 113 |
| 112 // FileWriter uses this class as its internal implementation, and it needs to | 114 // FileWriter uses this class as its internal implementation, and it needs to |
| 113 // be able to call set_file_handle(). FileWriter cannot initialize an | 115 // be able to call set_file_handle(). FileWriter cannot initialize a |
| 114 // WeakFileHandleFileWriter with a correct file descriptor at the time of | 116 // WeakFileHandleFileWriter with a correct file descriptor at the time of |
| 115 // construction because no file descriptor will be available until | 117 // construction because no file descriptor will be available until |
| 116 // FileWriter::Open() is called. | 118 // FileWriter::Open() is called. |
| 117 friend class FileWriter; | 119 friend class FileWriter; |
| 118 | 120 |
| 119 DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileWriter); | 121 DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileWriter); |
| 120 }; | 122 }; |
| 121 | 123 |
| 122 //! \brief A file writer implementation that wraps traditional system file | 124 //! \brief A file writer implementation that wraps traditional system file |
| 123 //! operations on files accessed through the filesystem. | 125 //! operations on files accessed through the filesystem. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 private: | 169 private: |
| 168 ScopedFileHandle file_; | 170 ScopedFileHandle file_; |
| 169 WeakFileHandleFileWriter weak_file_handle_file_writer_; | 171 WeakFileHandleFileWriter weak_file_handle_file_writer_; |
| 170 | 172 |
| 171 DISALLOW_COPY_AND_ASSIGN(FileWriter); | 173 DISALLOW_COPY_AND_ASSIGN(FileWriter); |
| 172 }; | 174 }; |
| 173 | 175 |
| 174 } // namespace crashpad | 176 } // namespace crashpad |
| 175 | 177 |
| 176 #endif // CRASHPAD_UTIL_FILE_FILE_WRITER_H_ | 178 #endif // CRASHPAD_UTIL_FILE_FILE_WRITER_H_ |
| OLD | NEW |