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 kReuseOrCreate, |
| 43 |
| 44 //! \brief Creates a new file. If the file already exists, it will be |
| 45 //! overwritten. |
| 46 kTruncateOrCreate, |
| 47 |
| 48 //! \brief Creates a new file. If the file already exists, the open will fail. |
| 49 kCreateOrFail, |
| 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. |
44 //! | 62 //! |
45 //! \sa WriteFile | 63 //! \sa WriteFile |
46 //! \sa LoggingReadFile | 64 //! \sa LoggingReadFile |
47 //! \sa CheckedReadFile | 65 //! \sa CheckedReadFile |
48 //! \sa CheckedReadFileAtEOF | 66 //! \sa CheckedReadFileAtEOF |
49 ssize_t ReadFile(FileHandle file, void* buffer, size_t size); | 67 ssize_t ReadFile(FileHandle file, void* buffer, size_t size); |
50 | 68 |
51 //! \brief Writes to a file, retrying when interrupted or following a short | 69 //! \brief Writes to a file, retrying when interrupted or following a short |
52 //! write on POSIX. | 70 //! write on POSIX. |
53 //! | 71 //! |
54 //! This function writes to \a file, stopping only when \a size bytes have been | 72 //! This function writes to \a file, stopping only when \a size bytes have been |
55 //! written. | 73 //! written. |
56 //! | 74 //! |
57 //! \return The number of bytes written from \a buffer, or `-1` on error, with | 75 //! \return The number of bytes written from \a buffer, or `-1` on error, with |
58 //! `errno` or `GetLastError()` set appropriately. On error, a portion of | 76 //! `errno` or `GetLastError()` set appropriately. On error, a portion of |
59 //! \a buffer may have been written to \a file. | 77 //! \a buffer may have been written to \a file. |
60 //! | 78 //! |
61 //! \sa ReadFile | 79 //! \sa ReadFile |
62 //! \sa LoggingWriteFile | 80 //! \sa LoggingWriteFile |
(...skipping 51 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()`, opening an existing file for |
| 143 //! reading. Logs an error if the operation fails. |
| 144 //! |
| 145 //! \return The newly opened FileHandle, or an invalid FileHandle on failure. |
| 146 //! |
| 147 //! \sa ScopedFD |
| 148 //! \sa ScopedFileHANDLE |
| 149 FileHandle LoggingOpenFileForRead(const base::FilePath& path); |
| 150 |
| 151 //! \brief Wraps `open()` or `CreateFile()`, creating a file for output. Logs |
| 152 //! an error if the operation fails. |
| 153 //! |
| 154 //! \a write_mode determines the style (truncate, reuse, etc.) that is used to |
| 155 //! open the file. On POSIX, if \a world_readable, `0644` will be used as |
| 156 //! `mode` permissions bits for `open()`, otherwise `0600` will be used. On |
| 157 //! Windows, the file is always opened in binary mode (that is, no CRLF |
| 158 //! translation). |
| 159 //! |
| 160 //! \return The newly opened FileHandle, or an invalid FileHandle on failure. |
| 161 //! |
| 162 //! \sa FileWriteMode |
| 163 //! \sa ScopedFD |
| 164 //! \sa ScopedFileHANDLE |
| 165 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, |
| 166 FileWriteMode write_mode, |
| 167 bool world_readable); |
| 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 |