Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: third_party/crashpad/crashpad/util/file/file_io.h

Issue 2773813002: Update Crashpad to 8e37886d418dd042c3c7bfadac99214739ee4d98 (Closed)
Patch Set: Update Crashpad to 8e37886d418dd042c3c7bfadac99214739ee4d98 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_POSIX) 22 #if defined(OS_POSIX)
23 #include "base/files/scoped_file.h" 23 #include "base/files/scoped_file.h"
24 #elif defined(OS_WIN) 24 #elif defined(OS_WIN)
25 #include <windows.h> 25 #include <windows.h>
26 #include "util/win/scoped_handle.h" 26 #include "util/win/scoped_handle.h"
27 #endif 27 #endif
28 28
29 //! \file
30
31 #if defined(OS_POSIX) || DOXYGEN
32
33 //! \brief A `PLOG()` macro usable for standard input/output error conditions.
34 //!
35 //! The `PLOG()` macro uses `errno` on POSIX and is appropriate to report
36 //! errors from standard input/output functions. On Windows, `PLOG()` uses
37 //! `GetLastError()`, and cannot be used to report errors from standard
38 //! input/output functions. This macro uses `PLOG()` when appropriate for
39 //! standard I/O functions, and `LOG()` otherwise.
40 #define STDIO_PLOG(x) PLOG(x)
41
42 #else
43
44 #define STDIO_PLOG(x) LOG(x)
45 #define fseeko(file, offset, whence) _fseeki64(file, offset, whence)
46 #define ftello(file) _ftelli64(file)
47
48 #endif
49
50 namespace base { 29 namespace base {
51 class FilePath; 30 class FilePath;
52 } // namespace base 31 } // namespace base
53 32
54 namespace crashpad { 33 namespace crashpad {
55 34
56 #if defined(OS_POSIX) || DOXYGEN 35 #if defined(OS_POSIX) || DOXYGEN
57 36
58 //! \brief Platform-specific alias for a low-level file handle. 37 //! \brief Platform-specific alias for a low-level file handle.
59 using FileHandle = int; 38 using FileHandle = int;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 86
108 //! \brief Determines the locking mode that LoggingLockFile() uses. 87 //! \brief Determines the locking mode that LoggingLockFile() uses.
109 enum class FileLocking : bool { 88 enum class FileLocking : bool {
110 //! \brief Equivalent to `flock()` with `LOCK_SH`. 89 //! \brief Equivalent to `flock()` with `LOCK_SH`.
111 kShared, 90 kShared,
112 91
113 //! \brief Equivalent to `flock()` with `LOCK_EX`. 92 //! \brief Equivalent to `flock()` with `LOCK_EX`.
114 kExclusive, 93 kExclusive,
115 }; 94 };
116 95
117 //! \brief Reads from a file, retrying when interrupted on POSIX or following a 96 //! \brief Determines the FileHandle that StdioFileHandle() returns.
118 //! short read. 97 enum class StdioStream {
119 //! 98 //! \brief Standard input, or `stdin`.
120 //! This function reads into \a buffer, stopping only when \a size bytes have 99 kStandardInput,
121 //! been read or when end-of-file has been reached. On Windows, reading from 100
122 //! sockets is not currently supported. 101 //! \brief Standard output, or `stdout`.
102 kStandardOutput,
103
104 //! \brief Standard error, or `stderr`.
105 kStandardError,
106 };
107
108 namespace internal {
109
110 //! \brief The name of the native read function used by ReadFile().
111 //!
112 //! This value may be useful for logging.
113 //!
114 //! \sa kNativeWriteFunctionName
115 extern const char kNativeReadFunctionName[];
116
117 //! \brief The name of the native write function used by WriteFile().
118 //!
119 //! This value may be useful for logging.
120 //!
121 //! \sa kNativeReadFunctionName
122 extern const char kNativeWriteFunctionName[];
123
124 //! \brief The internal implementation of ReadFileExactly() and its wrappers.
125 //!
126 //! The logic is exposed so that it may be reused by FileReaderInterface, and
127 //! so that it may be tested without requiring large files to be read. It is not
128 //! intended to be used more generally. Use ReadFileExactly(),
129 //! LoggingReadFileExactly(), CheckedReadFileExactly(), or
130 //! FileReaderInterface::ReadExactly() instead.
131 class ReadExactlyInternal {
132 public:
133 //! \brief Calls Read(), retrying following a short read, ensuring that
134 //! exactly \a size bytes are read.
135 //!
136 //! \return `true` on success. `false` if the underlying Read() fails or if
137 //! fewer than \a size bytes were read. When returning `false`, if \a
138 //! can_log is `true`, logs a message.
139 bool ReadExactly(void* buffer, size_t size, bool can_log);
140
141 protected:
142 ReadExactlyInternal() {}
143 ~ReadExactlyInternal() {}
144
145 private:
146 //! \brief Wraps a read operation, such as ReadFile().
147 //!
148 //! \return The number of bytes read and placed into \a buffer, or `-1` on
149 //! error. When returning `-1`, if \a can_log is `true`, logs a message.
150 virtual FileOperationResult Read(void* buffer, size_t size, bool can_log) = 0;
151
152 DISALLOW_COPY_AND_ASSIGN(ReadExactlyInternal);
153 };
154
155 //! \brief The internal implementation of WriteFile() and its wrappers.
156 //!
157 //! The logic is exposed so that it may be tested without requiring large files
158 //! to be written. It is not intended to be used more generally. Use
159 //! WriteFile(), LoggingWriteFile(), CheckedWriteFile(), or
160 //! FileWriterInterface::Write() instead.
161 class WriteAllInternal {
162 public:
163 //! \brief Calls Write(), retrying following a short write, ensuring that
164 //! exactly \a size bytes are written.
165 //!
166 //! \return `true` on success. `false` if the underlying Write() fails or if
167 //! fewer than \a size bytes were written.
168 bool WriteAll(const void* buffer, size_t size);
169
170 protected:
171 WriteAllInternal() {}
172 ~WriteAllInternal() {}
173
174 private:
175 //! \brief Wraps a write operation, such as NativeWriteFile().
176 //!
177 //! \return The number of bytes written from \a buffer, or `-1` on error.
178 virtual FileOperationResult Write(const void* buffer, size_t size) = 0;
179
180 DISALLOW_COPY_AND_ASSIGN(WriteAllInternal);
181 };
182
183 //! \brief Writes to a file, retrying when interrupted on POSIX.
184 //!
185 //! Fewer than \a size bytes may be written to \a file. This can happen if the
186 //! underlying write operation returns before writing the entire buffer, or if
187 //! the buffer is too large to write in a single operation, possibly due to a
188 //! limitation of a data type used to express the number of bytes written.
189 //!
190 //! This function adapts native write operations for uniform use by WriteFile().
191 //! This function should only be called by WriteFile(). Other code should call
192 //! WriteFile() or another function that wraps WriteFile().
193 //!
194 //! \param[in] file The file to write to.
195 //! \param[in] buffer A buffer containing data to be written.
196 //! \param[in] size The number of bytes from \a buffer to write.
197 //!
198 //! \return The number of bytes actually written from \a buffer to \a file on
199 //! success. `-1` on error, with `errno` or `GetLastError()` set
200 //! appropriately.
201 FileOperationResult NativeWriteFile(FileHandle file,
202 const void* buffer,
203 size_t size);
204
205 } // namespace internal
206
207 //! \brief Reads from a file, retrying when interrupted before reading any data
208 //! on POSIX.
209 //!
210 //! This function reads into \a buffer. Fewer than \a size bytes may be read.
211 //! On Windows, reading from sockets is not currently supported.
123 //! 212 //!
124 //! \return The number of bytes read and placed into \a buffer, or `-1` on 213 //! \return The number of bytes read and placed into \a buffer, or `-1` on
125 //! error, with `errno` or `GetLastError()` set appropriately. On error, a 214 //! error, with `errno` or `GetLastError()` set appropriately. On error, a
126 //! portion of \a file may have been read into \a buffer. 215 //! portion of \a file may have been read into \a buffer.
127 //! 216 //!
128 //! \sa WriteFile 217 //! \sa WriteFile
129 //! \sa LoggingReadFile 218 //! \sa ReadFileExactly
130 //! \sa CheckedReadFile 219 //! \sa LoggingReadFileExactly
220 //! \sa CheckedReadFileExactly
131 //! \sa CheckedReadFileAtEOF 221 //! \sa CheckedReadFileAtEOF
132 FileOperationResult ReadFile(FileHandle file, void* buffer, size_t size); 222 FileOperationResult ReadFile(FileHandle file, void* buffer, size_t size);
133 223
134 //! \brief Writes to a file, retrying when interrupted or following a short 224 //! \brief Writes to a file, retrying when interrupted on POSIX or following a
135 //! write on POSIX. 225 //! short write.
136 //! 226 //!
137 //! This function writes to \a file, stopping only when \a size bytes have been 227 //! This function writes to \a file, stopping only when \a size bytes have been
138 //! written. 228 //! written.
139 //! 229 //!
140 //! \return The number of bytes written from \a buffer, or `-1` on error, with 230 //! \return `true` on success. `false` on error, with `errno` or
141 //! `errno` or `GetLastError()` set appropriately. On error, a portion of 231 //! `GetLastError()` set appropriately. On error, a portion of \a buffer may
142 //! \a buffer may have been written to \a file. 232 //! have been written to \a file.
143 //! 233 //!
144 //! \sa ReadFile 234 //! \sa ReadFile
145 //! \sa LoggingWriteFile 235 //! \sa LoggingWriteFile
146 //! \sa CheckedWriteFile 236 //! \sa CheckedWriteFile
147 FileOperationResult WriteFile(FileHandle file, const void* buffer, size_t size); 237 bool WriteFile(FileHandle file, const void* buffer, size_t size);
148 238
149 //! \brief Wraps ReadFile(), ensuring that exactly \a size bytes are read. 239 //! \brief Wraps ReadFile(), retrying following a short read, ensuring that
150 //! 240 //! exactly \a size bytes are read.
151 //! \return `true` on success. If \a size is out of the range of possible 241 //!
152 //! ReadFile() return values, if the underlying ReadFile() fails, or if 242 //! \return `true` on success. If the underlying ReadFile() fails, or if fewer
153 //! other than \a size bytes were read, this function logs a message and 243 //! than \a size bytes were read, this function logs a message and
154 //! returns `false`. 244 //! returns `false`.
155 //! 245 //!
156 //! \sa LoggingWriteFile 246 //! \sa LoggingWriteFile
157 //! \sa ReadFile 247 //! \sa ReadFile
158 //! \sa CheckedReadFile 248 //! \sa LoggingReadFileExactly
159 //! \sa CheckedReadFileAtEOF 249 //! \sa CheckedReadFileExactly
160 bool LoggingReadFile(FileHandle file, void* buffer, size_t size); 250 //! \sa CheckedReadFileAtEOF
251 bool ReadFileExactly(FileHandle file, void* buffer, size_t size);
252
253 //! \brief Wraps ReadFile(), retrying following a short read, ensuring that
254 //! exactly \a size bytes are read.
255 //!
256 //! \return `true` on success. If the underlying ReadFile() fails, or if fewer
257 //! than \a size bytes were read, this function logs a message and
258 //! returns `false`.
259 //!
260 //! \sa LoggingWriteFile
261 //! \sa ReadFile
262 //! \sa ReadFileExactly
263 //! \sa CheckedReadFileExactly
264 //! \sa CheckedReadFileAtEOF
265 bool LoggingReadFileExactly(FileHandle file, void* buffer, size_t size);
161 266
162 //! \brief Wraps WriteFile(), ensuring that exactly \a size bytes are written. 267 //! \brief Wraps WriteFile(), ensuring that exactly \a size bytes are written.
163 //! 268 //!
164 //! \return `true` on success. If \a size is out of the range of possible 269 //! \return `true` on success. If the underlying WriteFile() fails, or if fewer
165 //! WriteFile() return values, if the underlying WriteFile() fails, or if 270 //! than \a size bytes were written, this function logs a message and
166 //! other than \a size bytes were written, this function logs a message and
167 //! returns `false`. 271 //! returns `false`.
168 //! 272 //!
169 //! \sa LoggingReadFile 273 //! \sa LoggingReadFileExactly
170 //! \sa WriteFile 274 //! \sa WriteFile
171 //! \sa CheckedWriteFile 275 //! \sa CheckedWriteFile
172 bool LoggingWriteFile(FileHandle file, const void* buffer, size_t size); 276 bool LoggingWriteFile(FileHandle file, const void* buffer, size_t size);
173 277
174 //! \brief Wraps ReadFile(), ensuring that exactly \a size bytes are read. 278 //! \brief Wraps ReadFile(), ensuring that exactly \a size bytes are read.
175 //! 279 //!
176 //! If \a size is out of the range of possible ReadFile() return values, if the 280 //! If the underlying ReadFile() fails, or if fewer than \a size bytes were
177 //! underlying ReadFile() fails, or if other than \a size bytes were read, this 281 //! read, this function causes execution to terminate without returning.
178 //! function causes execution to terminate without returning.
179 //! 282 //!
180 //! \sa CheckedWriteFile 283 //! \sa CheckedWriteFile
181 //! \sa ReadFile 284 //! \sa ReadFile
182 //! \sa LoggingReadFile 285 //! \sa LoggingReadFileExactly
183 //! \sa CheckedReadFileAtEOF 286 //! \sa CheckedReadFileAtEOF
184 void CheckedReadFile(FileHandle file, void* buffer, size_t size); 287 void CheckedReadFileExactly(FileHandle file, void* buffer, size_t size);
185 288
186 //! \brief Wraps WriteFile(), ensuring that exactly \a size bytes are written. 289 //! \brief Wraps WriteFile(), ensuring that exactly \a size bytes are written.
187 //! 290 //!
188 //! If \a size is out of the range of possible WriteFile() return values, if the 291 //! if the underlying WriteFile() fails, or if fewer than \a size bytes were
189 //! underlying WriteFile() fails, or if other than \a size bytes were written, 292 //! written, this function causes execution to terminate without returning.
190 //! this function causes execution to terminate without returning. 293 //!
191 //! 294 //! \sa CheckedReadFileExactly
192 //! \sa CheckedReadFile
193 //! \sa WriteFile 295 //! \sa WriteFile
194 //! \sa LoggingWriteFile 296 //! \sa LoggingWriteFile
195 void CheckedWriteFile(FileHandle file, const void* buffer, size_t size); 297 void CheckedWriteFile(FileHandle file, const void* buffer, size_t size);
196 298
197 //! \brief Wraps ReadFile(), ensuring that it indicates end-of-file. 299 //! \brief Wraps ReadFile(), ensuring that it indicates end-of-file.
198 //! 300 //!
199 //! Attempts to read a single byte from \a file, expecting no data to be read. 301 //! Attempts to read a single byte from \a file, expecting no data to be read.
200 //! If the underlying ReadFile() fails, or if a byte actually is read, this 302 //! If the underlying ReadFile() fails, or if a byte actually is read, this
201 //! function causes execution to terminate without returning. 303 //! function causes execution to terminate without returning.
202 //! 304 //!
203 //! \sa CheckedReadFile 305 //! \sa CheckedReadFileExactly
204 //! \sa ReadFile 306 //! \sa ReadFile
205 void CheckedReadFileAtEOF(FileHandle file); 307 void CheckedReadFileAtEOF(FileHandle file);
206 308
207 //! \brief Wraps `open()` or `CreateFile()`, opening an existing file for 309 //! \brief Wraps `open()` or `CreateFile()`, opening an existing file for
208 //! reading. 310 //! reading.
209 //! 311 //!
210 //! \return The newly opened FileHandle, or an invalid FileHandle on failure. 312 //! \return The newly opened FileHandle, or an invalid FileHandle on failure.
211 //! 313 //!
212 //! \sa ScopedFileHandle 314 //! \sa ScopedFileHandle
213 //! \sa OpenFileForWrite 315 //! \sa OpenFileForWrite
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 440
339 //! \brief Wraps `close()` or `CloseHandle()`, ensuring that it succeeds. 441 //! \brief Wraps `close()` or `CloseHandle()`, ensuring that it succeeds.
340 //! 442 //!
341 //! If the underlying function fails, this function causes execution to 443 //! If the underlying function fails, this function causes execution to
342 //! terminate without returning. 444 //! terminate without returning.
343 void CheckedCloseFile(FileHandle file); 445 void CheckedCloseFile(FileHandle file);
344 446
345 //! \brief Determines the size of a file. 447 //! \brief Determines the size of a file.
346 //! 448 //!
347 //! \param[in] file The handle to the file for which the size should be 449 //! \param[in] file The handle to the file for which the size should be
348 //! retrived. 450 //! retrieved.
349 //! 451 //!
350 //! \return The size of the file. If an error occurs when attempting to 452 //! \return The size of the file. If an error occurs when attempting to
351 //! determine its size, returns `-1` with an error logged. 453 //! determine its size, returns `-1` with an error logged.
352 FileOffset LoggingFileSizeByHandle(FileHandle file); 454 FileOffset LoggingFileSizeByHandle(FileHandle file);
353 455
456 //! \brief Returns a FileHandle corresponding to the requested standard I/O
457 //! stream.
458 //!
459 //! The returned FileHandle should not be closed on POSIX, where it is
460 //! important to maintain valid file descriptors occupying the slots reserved
461 //! for these streams. If a need to close such a stream arises on POSIX,
462 //! `dup2()` should instead be used to replace the existing file descriptor with
463 //! one opened to `/dev/null`. See CloseStdinAndStdout().
464 //!
465 //! \param[in] stdio_stream The requested standard I/O stream.
466 //!
467 //! \return A corresponding FileHandle on success. kInvalidFileHandle on error,
468 //! with a message logged.
469 FileHandle StdioFileHandle(StdioStream stdio_stream);
470
354 } // namespace crashpad 471 } // namespace crashpad
355 472
356 #endif // CRASHPAD_UTIL_FILE_FILE_IO_H_ 473 #endif // CRASHPAD_UTIL_FILE_FILE_IO_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698