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_IO_H_ | 15 #ifndef CRASHPAD_UTIL_FILE_FILE_IO_H_ |
16 #define CRASHPAD_UTIL_FILE_FILE_IO_H_ | 16 #define CRASHPAD_UTIL_FILE_FILE_IO_H_ |
17 | 17 |
18 #include <sys/types.h> | 18 #include <sys/types.h> |
19 | 19 |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 | 21 |
22 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
23 #include <windows.h> | 23 #include <windows.h> |
24 #endif | 24 #endif |
25 | 25 |
26 namespace base { | |
27 class FilePath; | |
28 } // namespace base | |
29 | |
26 namespace crashpad { | 30 namespace crashpad { |
27 | 31 |
28 #if defined(OS_POSIX) | 32 #if defined(OS_POSIX) || DOXYGEN |
33 //! \brief Platform-specific alias for a low level file handle. | |
29 using FileHandle = int; | 34 using FileHandle = int; |
30 #elif defined(OS_WIN) | 35 #elif defined(OS_WIN) |
31 using FileHandle = HANDLE; | 36 using FileHandle = HANDLE; |
32 #endif | 37 #endif |
33 | 38 |
39 //! \brief Determines the mode that LoggingOpenFileForWrite() uses. | |
40 enum class FileWriteMode { | |
41 //! \brief Opens the file if it exists, or creates a new file. | |
42 kAppend, | |
Mark Mentovai
2014/12/18 21:52:32
kAppend still sounds like it seeks to the end…
Ho
scottmg
2014/12/18 22:12:44
"Reuse" sounds a bit funny to me, but I can't thin
| |
43 | |
44 //! \brief Creates a new file. If the file already exists, it will be | |
45 //! overwritten. | |
46 kCreate, | |
47 | |
48 //! \brief Creates a new file. If the file already exists, the open will fail. | |
49 kCreateNew, | |
50 }; | |
51 | |
34 //! \brief Reads from a file, retrying when interrupted on POSIX or following a | 52 //! \brief Reads from a file, retrying when interrupted on POSIX or following a |
35 //! short read. | 53 //! short read. |
36 //! | 54 //! |
37 //! This function reads into \a buffer, stopping only when \a size bytes have | 55 //! This function reads into \a buffer, stopping only when \a size bytes have |
38 //! been read or when end-of-file has been reached. On Windows, reading from | 56 //! been read or when end-of-file has been reached. On Windows, reading from |
39 //! sockets is not currently supported. | 57 //! sockets is not currently supported. |
40 //! | 58 //! |
41 //! \return The number of bytes read and placed into \a buffer, or `-1` on | 59 //! \return The number of bytes read and placed into \a buffer, or `-1` on |
42 //! error, with `errno` or `GetLastError()` set appropriately. On error, a | 60 //! error, with `errno` or `GetLastError()` set appropriately. On error, a |
43 //! portion of \a file may have been read into \a buffer. | 61 //! portion of \a file may have been read into \a buffer. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 //! \brief Wraps ReadFile(), ensuring that it indicates end-of-file. | 132 //! \brief Wraps ReadFile(), ensuring that it indicates end-of-file. |
115 //! | 133 //! |
116 //! Attempts to read a single byte from \a file, expecting no data to be read. | 134 //! Attempts to read a single byte from \a file, expecting no data to be read. |
117 //! If the underlying ReadFile() fails, or if a byte actually is read, this | 135 //! If the underlying ReadFile() fails, or if a byte actually is read, this |
118 //! function causes execution to terminate without returning. | 136 //! function causes execution to terminate without returning. |
119 //! | 137 //! |
120 //! \sa CheckedReadFile | 138 //! \sa CheckedReadFile |
121 //! \sa ReadFile | 139 //! \sa ReadFile |
122 void CheckedReadFileAtEOF(FileHandle file); | 140 void CheckedReadFileAtEOF(FileHandle file); |
123 | 141 |
142 //! \brief Wraps `open()` or `CreateFile()`, creating a file for output. Logs | |
143 //! an error if the operation fails. | |
144 //! | |
145 //! \a write_mode determines the style (clobber, truncate, etc.) that is used | |
Mark Mentovai
2014/12/18 21:52:32
clobber == truncate, right?
scottmg
2014/12/18 22:12:44
Done.
| |
146 //! to open the file. On POSIX, if \a world_readable, `0644` will be used as | |
147 //! `mode` permissions bits for `open()`, otherwise `0600` will be used. On | |
Mark Mentovai
2014/12/18 21:52:32
Yup, score!
| |
148 //! Windows, the file is always opened in binary mode (that is, no CRLF | |
149 //! translation). | |
150 //! | |
151 //! \return The newly opened FileHandle, or an invalid FileHandle on failure. | |
152 //! | |
153 //! \sa FileWriteMode | |
154 //! \sa ScopedFD | |
155 //! \sa ScopedFileHANDLE | |
156 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, | |
157 FileWriteMode write_mode, | |
158 bool world_readable); | |
159 | |
160 //! \brief Wraps `open()` or `CreateFile()`, opening an existing file for | |
161 //! reading. Logs an error if the operation fails. | |
162 //! | |
163 //! \return The newly opened FileHandle, or an invalid FileHandle on failure. | |
164 //! | |
165 //! \sa ScopedFD | |
166 //! \sa ScopedFileHANDLE | |
167 FileHandle LoggingOpenFileForRead(const base::FilePath& path); | |
168 | |
124 //! \brief Wraps `close()` or `CloseHandle()`, logging an error if the operation | 169 //! \brief Wraps `close()` or `CloseHandle()`, logging an error if the operation |
125 //! fails. | 170 //! fails. |
126 //! | 171 //! |
127 //! \return On success, `true` is returned. On failure, an error is logged and | 172 //! \return On success, `true` is returned. On failure, an error is logged and |
128 //! `false` is returned. | 173 //! `false` is returned. |
129 bool LoggingCloseFile(FileHandle file); | 174 bool LoggingCloseFile(FileHandle file); |
130 | 175 |
131 //! \brief Wraps `close()` or `CloseHandle()`, ensuring that it succeeds. | 176 //! \brief Wraps `close()` or `CloseHandle()`, ensuring that it succeeds. |
132 //! | 177 //! |
133 //! If the underlying function fails, this function causes execution to | 178 //! If the underlying function fails, this function causes execution to |
134 //! terminate without returning. | 179 //! terminate without returning. |
135 void CheckedCloseFile(FileHandle file); | 180 void CheckedCloseFile(FileHandle file); |
136 | 181 |
137 } // namespace crashpad | 182 } // namespace crashpad |
138 | 183 |
139 #endif // CRASHPAD_UTIL_FILE_FILE_IO_H_ | 184 #endif // CRASHPAD_UTIL_FILE_FILE_IO_H_ |
OLD | NEW |