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