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