Chromium Code Reviews| 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_FD_IO_H_ | 15 #ifndef CRASHPAD_UTIL_FILE_FILE_IO_H_ |
| 16 #define CRASHPAD_UTIL_FILE_FD_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" | |
| 21 | |
| 22 #if defined(OS_WIN) | |
| 23 #include <windows.h> | |
| 24 #endif | |
| 25 | |
| 20 namespace crashpad { | 26 namespace crashpad { |
| 21 | 27 |
| 22 //! \brief Wraps `read()`, retrying when interrupted or following a short read. | 28 #if defined(OS_POSIX) |
| 29 using FileHandle = int; | |
| 30 #elif defined(OS_WIN) | |
| 31 using FileHandle = HANDLE; | |
| 32 #endif | |
| 33 | |
| 34 //! \brief Reads from a file, retrying when interrupted on POSIX or following a | |
| 35 //! short read. | |
| 23 //! | 36 //! |
| 24 //! This function reads into \a buffer, stopping only when \a size bytes have | 37 //! This function reads into \a buffer, stopping only when \a size bytes have |
| 25 //! been read or when `read()` returns 0, indicating that end-of-file has been | 38 //! been read or when end-of-file has been reached. |
| 26 //! reached. | |
| 27 //! | 39 //! |
| 28 //! \return The number of bytes read and placed into \a buffer, or `-1` on | 40 //! \return The number of bytes read and placed into \a buffer, or `-1` on |
| 29 //! error, with `errno` set appropriately. On error, a portion of \a fd may | 41 //! error, with `errno` or `GetLastError()` set appropriately. On error, a |
| 30 //! have been read into \a buffer. | 42 //! portion of \a file may have been read into \a buffer. |
| 31 //! | 43 //! |
| 32 //! \sa WriteFD | 44 //! \sa WriteFile |
| 33 //! \sa LoggingReadFD | 45 //! \sa LoggingReadFile |
| 34 //! \sa CheckedReadFD | 46 //! \sa CheckedReadFile |
| 35 //! \sa CheckedReadFDAtEOF | 47 //! \sa CheckedReadFileAtEOF |
| 36 ssize_t ReadFD(int fd, void* buffer, size_t size); | 48 ssize_t ReadFile(FileHandle file, void* buffer, size_t size); |
| 37 | 49 |
| 38 //! \brief Wraps `write()`, retrying when interrupted or following a short | 50 //! \brief Writes to a file, retrying when interrupted or following a short |
| 39 //! write. | 51 //! write on POSIX. |
| 40 //! | 52 //! |
| 41 //! This function writes to \a fd, stopping only when \a size bytes have been | 53 //! This function writes to \a file, stopping only when \a size bytes have been |
| 42 //! written. | 54 //! written. |
| 43 //! | 55 //! |
| 44 //! \return The number of bytes written from \a buffer, or `-1` on error, with | 56 //! \return The number of bytes written from \a buffer, or `-1` on error, with |
| 45 //! `errno` set appropriately. On error, a portion of \a buffer may have | 57 //! `errno` or `GetLastError()` set appropriately. On error, a portion of |
| 46 //! been written to \a fd. | 58 //! \a buffer may have been written to \a file. |
| 47 //! | 59 //! |
| 48 //! \sa ReadFD | 60 //! \sa ReadFile |
| 49 //! \sa LoggingWriteFD | 61 //! \sa LoggingWriteFile |
| 50 //! \sa CheckedWriteFD | 62 //! \sa CheckedWriteFile |
| 51 ssize_t WriteFD(int fd, const void* buffer, size_t size); | 63 ssize_t WriteFile(FileHandle file, const void* buffer, size_t size); |
| 52 | 64 |
| 53 //! \brief Wraps ReadFD(), ensuring that exactly \a size bytes are read. | 65 //! \brief Wraps ReadFile(), ensuring that exactly \a size bytes are read. |
| 54 //! | 66 //! |
| 55 //! \return `true` on success. If \a size is out of the range of possible | 67 //! \return `true` on success. If \a size is out of the range of possible |
| 56 //! `read()` return values, if the underlying ReadFD() fails, or if other | 68 //! ReadFile() return values, if the underlying ReadFile() fails, or if |
| 57 //! than \a size bytes were read, this function logs a message and returns | 69 //! other than \a size bytes were read, this function logs a message and |
| 58 //! `false`. | 70 //! returns `false`. |
| 59 //! | 71 //! |
| 60 //! \sa LoggingWriteFD | 72 //! \sa LoggingWriteFile |
| 61 //! \sa ReadFD | 73 //! \sa ReadFile |
| 62 //! \sa CheckedReadFD | 74 //! \sa CheckedReadFile |
| 63 //! \sa CheckedReadFDAtEOF | 75 //! \sa CheckedReadFileAtEOF |
| 64 bool LoggingReadFD(int fd, void* buffer, size_t size); | 76 bool LoggingReadFile(FileHandle file, void* buffer, size_t size); |
| 65 | 77 |
| 66 //! \brief Wraps WriteFD(), ensuring that exactly \a size bytes are written. | 78 //! \brief Wraps WriteFile(), ensuring that exactly \a size bytes are written. |
| 67 //! | 79 //! |
| 68 //! \return `true` on success. If \a size is out of the range of possible | 80 //! \return `true` on success. If \a size is out of the range of possible |
| 69 //! `write()` return values, if the underlying WriteFD() fails, or if other | 81 //! WriteFile() return values, if the underlying WriteFile() fails, or if |
| 70 //! than \a size bytes were written, this function logs a message and | 82 //! other than \a size bytes were written, this function logs a message and |
| 71 //! returns `false`. | 83 //! returns `false`. |
| 72 //! | 84 //! |
| 73 //! \sa LoggingReadFD | 85 //! \sa LoggingReadFile |
| 74 //! \sa WriteFD | 86 //! \sa WriteFile |
| 75 //! \sa CheckedWriteFD | 87 //! \sa CheckedWriteFile |
| 76 bool LoggingWriteFD(int fd, const void* buffer, size_t size); | 88 bool LoggingWriteFile(FileHandle file, const void* buffer, size_t size); |
| 77 | 89 |
| 78 //! \brief Wraps ReadFD(), ensuring that exactly \a size bytes are read. | 90 //! \brief Wraps ReadFile(), ensuring that exactly \a size bytes are read. |
| 79 //! | 91 //! |
| 80 //! If \a size is out of the range of possible `read()` return values, if the | 92 //! If \a size is out of the range of possible ReadFile() return values, if the |
| 81 //! underlying ReadFD() fails, or if other than \a size bytes were read, this | 93 //! underlying ReadFile() fails, or if other than \a size bytes were read, this |
| 82 //! function causes execution to terminate without returning. | 94 //! function causes execution to terminate without returning. |
| 83 //! | 95 //! |
| 84 //! \sa CheckedWriteFD | 96 //! \sa CheckedWriteFile |
| 85 //! \sa ReadFD | 97 //! \sa ReadFile |
| 86 //! \sa LoggingReadFD | 98 //! \sa LoggingReadFile |
| 87 //! \sa CheckedReadFDAtEOF | 99 //! \sa CheckedReadFileAtEOF |
| 88 void CheckedReadFD(int fd, void* buffer, size_t size); | 100 void CheckedReadFile(FileHandle file, void* buffer, size_t size); |
| 89 | 101 |
| 90 //! \brief Wraps WriteFD(), ensuring that exactly \a size bytes are written. | 102 //! \brief Wraps WriteFile(), ensuring that exactly \a size bytes are written. |
| 91 //! | 103 //! |
| 92 //! If \a size is out of the range of possible `write()` return values, if the | 104 //! If \a size is out of the range of possible WriteFile() return values, if the |
| 93 //! underlying WriteFD() fails, or if other than \a size bytes were written, | 105 //! underlying WriteFile() fails, or if other than \a size bytes were written, |
| 94 //! this function causes execution to terminate without returning. | 106 //! this function causes execution to terminate without returning. |
| 95 //! | 107 //! |
| 96 //! \sa CheckedReadFD | 108 //! \sa CheckedReadFile |
| 97 //! \sa WriteFD | 109 //! \sa WriteFile |
| 98 //! \sa LoggingWriteFD | 110 //! \sa LoggingWriteFile |
| 99 void CheckedWriteFD(int fd, const void* buffer, size_t size); | 111 void CheckedWriteFile(FileHandle file, const void* buffer, size_t size); |
| 100 | 112 |
| 101 //! \brief Wraps ReadFD(), ensuring that it indicates end-of-file. | 113 //! \brief Wraps ReadFile(), ensuring that it indicates end-of-file. |
| 102 //! | 114 //! |
| 103 //! Attempts to read a single byte from \a fd, expecting no data to be read. If | 115 //! Attempts to read a single byte from \a file, expecting no data to be read. |
| 104 //! the underlying ReadFD() fails, or if a byte actually is read, this function | 116 //! If the underlying ReadFile() fails, or if a byte actually is read, this |
| 105 //! causes execution to terminate without returning. | 117 //! function causes execution to terminate without returning. |
| 106 //! | 118 //! |
| 107 //! \sa CheckedReadFD | 119 //! \sa CheckedReadFile |
| 108 //! \sa ReadFD | 120 //! \sa ReadFile |
| 109 void CheckedReadFDAtEOF(int fd); | 121 void CheckedReadFileAtEOF(FileHandle file); |
| 110 | 122 |
| 111 //! \brief Wraps `close()`, logging an error if the operation fails. | 123 //! \brief Wraps `close()` or `CloseHandle()`, logging an error if the operation |
| 124 //! fails. | |
| 112 //! | 125 //! |
| 113 //! \return On success, `true` is returned. On failure, an error is logged and | 126 //! \return On success, `true` is returned. On failure, an error is logged and |
| 114 //! `false` is returned. | 127 //! `false` is returned. |
| 115 bool LoggingCloseFD(int fd); | 128 bool LoggingCloseFile(FileHandle file); |
| 116 | 129 |
| 117 //! \brief Wraps `close()`, ensuring that it succeeds. | 130 //! \brief Wraps `close()` or `CloseHandle()`, ensuring that it succeeds. |
| 118 //! | 131 //! |
| 119 //! If `close()` fails, this function causes execution to terminate without | 132 //! If the underlying functions fails, this function causes execution to |
|
Mark Mentovai
2014/12/17 05:39:16
“function fails” or “functions fail”
scottmg
2014/12/17 20:18:32
Done.
| |
| 120 //! returning. | 133 //! terminate without returning. |
| 121 void CheckedCloseFD(int fd); | 134 void CheckedCloseFile(FileHandle file); |
| 122 | 135 |
| 123 } // namespace crashpad | 136 } // namespace crashpad |
| 124 | 137 |
| 125 #endif // CRASHPAD_UTIL_FILE_FD_IO_H_ | 138 #endif // CRASHPAD_UTIL_FILE_FILE_IO_H_ |
| OLD | NEW |