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 <fcntl.h> |
| 19 #include <stddef.h> |
| 20 #include <sys/uio.h> |
| 21 #include <unistd.h> |
| 22 |
| 23 #include <string> |
| 24 #include <vector> |
| 25 |
| 26 #include "base/basictypes.h" |
| 27 #include "base/files/file_path.h" |
| 28 #include "base/files/scoped_file.h" |
| 29 |
| 30 namespace crashpad { |
| 31 |
| 32 //! \brief A version of `iovec` with a `const` #iov_base field. |
| 33 //! |
| 34 //! This structure is intended to be used for write operations. |
| 35 // |
| 36 // Type compatibility with iovec is tested with static assertions in the |
| 37 // implementation file. |
| 38 struct WritableIoVec { |
| 39 //! \brief The base address of a memory region for output. |
| 40 const void* iov_base; |
| 41 |
| 42 //! \brief The size of the memory pointed to by #iov_base. |
| 43 size_t iov_len; |
| 44 }; |
| 45 |
| 46 //! \brief An interface to write to files and other file-like objects with POSIX |
| 47 //! semantics. |
| 48 class FileWriterInterface { |
| 49 public: |
| 50 //! \brief Wraps `write()` or provides an alternate implementation with |
| 51 //! identical semantics. This method will write the entire buffer, |
| 52 //! continuing after a short write or after being interrupted. |
| 53 //! |
| 54 //! \return `true` if the operation succeeded, `false` if it failed, with an |
| 55 //! error message logged. |
| 56 virtual bool Write(const void* data, size_t size) = 0; |
| 57 |
| 58 //! \brief Wraps `writev()` or provides an alternate implementation with |
| 59 //! identical semantics. This method will write the entire buffer, |
| 60 //! continuing after a short write or after being interrupted. |
| 61 //! |
| 62 //! \return `true` if the operation succeeded, `false` if it failed, with an |
| 63 //! error message logged. |
| 64 //! |
| 65 //! \note The contents of \a iovecs are undefined when this method returns. |
| 66 virtual bool WriteIoVec(std::vector<WritableIoVec>* iovecs) = 0; |
| 67 |
| 68 //! \brief Wraps `lseek()` or provides an alternate implementation with |
| 69 //! identical semantics. |
| 70 //! |
| 71 //! \return The return value of `lseek()`. `-1` on failure, with an error |
| 72 //! message logged. |
| 73 virtual off_t Seek(off_t offset, int whence) = 0; |
| 74 |
| 75 protected: |
| 76 ~FileWriterInterface() {} |
| 77 }; |
| 78 |
| 79 //! \brief A file writer implementation that wraps traditional POSIX file |
| 80 //! operations on files accessed through the filesystem. |
| 81 class FileWriter : public FileWriterInterface { |
| 82 public: |
| 83 FileWriter(); |
| 84 ~FileWriter(); |
| 85 |
| 86 //! \brief Wraps `open()`. |
| 87 //! |
| 88 //! \return `true` if the operation succeeded, `false` if it failed, with an |
| 89 //! error message logged. |
| 90 //! |
| 91 //! \note After a successful call, this method cannot be called again until |
| 92 //! after Close(). |
| 93 bool Open(const base::FilePath& path, int oflag, mode_t mode); |
| 94 |
| 95 //! \brief Wraps `close().` |
| 96 //! |
| 97 //! \note It is only valid to call this method on an object that has had a |
| 98 //! successful Open() that has not yet been matched by a subsequent call |
| 99 //! to this method. |
| 100 void Close(); |
| 101 |
| 102 // FileWriterInterface: |
| 103 |
| 104 //! \copydoc FileWriterInterface::Write() |
| 105 //! |
| 106 //! \note It is only valid to call this method between a successful Open() and |
| 107 //! a Close(). |
| 108 virtual bool Write(const void* data, size_t size) override; |
| 109 |
| 110 //! \copydoc FileWriterInterface::WriteIoVec() |
| 111 //! |
| 112 //! \note It is only valid to call this method between a successful Open() and |
| 113 //! a Close(). |
| 114 virtual bool WriteIoVec(std::vector<WritableIoVec>* iovecs) override; |
| 115 |
| 116 //! \copydoc FileWriterInterface::Seek() |
| 117 //! |
| 118 //! \note It is only valid to call this method between a successful Open() and |
| 119 //! a Close(). |
| 120 virtual off_t Seek(off_t offset, int whence) override; |
| 121 |
| 122 private: |
| 123 base::ScopedFD fd_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(FileWriter); |
| 126 }; |
| 127 |
| 128 } // namespace crashpad |
| 129 |
| 130 #endif // CRASHPAD_UTIL_FILE_FILE_WRITER_H_ |
OLD | NEW |