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