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