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

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

Issue 811823003: Cross platform low level file IO wrappers (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: comment Created 6 years 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_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 a file, retrying when interrupted on POSIX.
23 //! 35 //!
24 //! This function reads into \a buffer, stopping only when \a size bytes have 36 //! 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 37 //! been read or when end-of-file has been reached.
26 //! reached.
27 //! 38 //!
28 //! \return The number of bytes read and placed into \a buffer, or `-1` on 39 //! \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 40 //! error, with `errno` or `GetLastError` set appropriately. On error, a
30 //! have been read into \a buffer. 41 //! portion of \a file may have been read into \a buffer.
31 //! 42 //!
32 //! \sa WriteFD 43 //! \sa WriteFile
33 //! \sa LoggingReadFD 44 //! \sa LoggingReadFile
34 //! \sa CheckedReadFD 45 //! \sa CheckedReadFile
35 //! \sa CheckedReadFDAtEOF 46 //! \sa CheckedReadFileAtEOF
36 ssize_t ReadFD(int fd, void* buffer, size_t size); 47 ssize_t ReadFile(FileHandle file, void* buffer, size_t size);
37 48
38 //! \brief Wraps `write()`, retrying when interrupted or following a short 49 //! \brief Writes a file, retrying when interrupted on POSIX.
39 //! write.
40 //! 50 //!
41 //! This function writes to \a fd, stopping only when \a size bytes have been 51 //! This function writes to \a file, stopping only when \a size bytes have been
42 //! written. 52 //! written.
43 //! 53 //!
44 //! \return The number of bytes written from \a buffer, or `-1` on error, with 54 //! \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 55 //! `errno` or `GetLastError` set appropriately. On error, a portion of
46 //! been written to \a fd. 56 //! \a buffer may have been written to \a file.
47 //! 57 //!
48 //! \sa ReadFD 58 //! \sa ReadFile
49 //! \sa LoggingWriteFD 59 //! \sa LoggingWriteFile
50 //! \sa CheckedWriteFD 60 //! \sa CheckedWriteFile
51 ssize_t WriteFD(int fd, const void* buffer, size_t size); 61 ssize_t WriteFile(FileHandle file, const void* buffer, size_t size);
52 62
53 //! \brief Wraps ReadFD(), ensuring that exactly \a size bytes are read. 63 //! \brief Wraps ReadFile(), ensuring that exactly \a size bytes are read.
54 //! 64 //!
55 //! \return `true` on success. If \a size is out of the range of possible 65 //! \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 66 //! `ReadFile` return values, if the underlying ReadFile() fails, or if
57 //! than \a size bytes were read, this function logs a message and returns 67 //! other than \a size bytes were read, this function logs a message and
58 //! `false`. 68 //! returns `false`.
59 //! 69 //!
60 //! \sa LoggingWriteFD 70 //! \sa LoggingWriteFile
61 //! \sa ReadFD 71 //! \sa ReadFile
62 //! \sa CheckedReadFD 72 //! \sa CheckedReadFile
63 //! \sa CheckedReadFDAtEOF 73 //! \sa CheckedReadFileAtEOF
64 bool LoggingReadFD(int fd, void* buffer, size_t size); 74 bool LoggingReadFile(FileHandle file, void* buffer, size_t size);
65 75
66 //! \brief Wraps WriteFD(), ensuring that exactly \a size bytes are written. 76 //! \brief Wraps WriteFile(), ensuring that exactly \a size bytes are written.
67 //! 77 //!
68 //! \return `true` on success. If \a size is out of the range of possible 78 //! \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 79 //! `WriteFile` return values, if the underlying WriteFile() fails, or if
70 //! than \a size bytes were written, this function logs a message and 80 //! other than \a size bytes were written, this function logs a message and
71 //! returns `false`. 81 //! returns `false`.
72 //! 82 //!
73 //! \sa LoggingReadFD 83 //! \sa LoggingReadFile
74 //! \sa WriteFD 84 //! \sa WriteFile
75 //! \sa CheckedWriteFD 85 //! \sa CheckedWriteFile
76 bool LoggingWriteFD(int fd, const void* buffer, size_t size); 86 bool LoggingWriteFile(FileHandle file, const void* buffer, size_t size);
77 87
78 //! \brief Wraps ReadFD(), ensuring that exactly \a size bytes are read. 88 //! \brief Wraps ReadFile(), ensuring that exactly \a size bytes are read.
79 //! 89 //!
80 //! If \a size is out of the range of possible `read()` return values, if the 90 //! 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 91 //! underlying ReadFile() fails, or if other than \a size bytes were read, this
82 //! function causes execution to terminate without returning. 92 //! function causes execution to terminate without returning.
83 //! 93 //!
84 //! \sa CheckedWriteFD 94 //! \sa CheckedWriteFile
85 //! \sa ReadFD 95 //! \sa ReadFile
86 //! \sa LoggingReadFD 96 //! \sa LoggingReadFile
87 //! \sa CheckedReadFDAtEOF 97 //! \sa CheckedReadFileAtEOF
88 void CheckedReadFD(int fd, void* buffer, size_t size); 98 void CheckedReadFile(FileHandle file, void* buffer, size_t size);
89 99
90 //! \brief Wraps WriteFD(), ensuring that exactly \a size bytes are written. 100 //! \brief Wraps WriteFile(), ensuring that exactly \a size bytes are written.
91 //! 101 //!
92 //! If \a size is out of the range of possible `write()` return values, if the 102 //! 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, 103 //! underlying WriteFile() fails, or if other than \a size bytes were written,
94 //! this function causes execution to terminate without returning. 104 //! this function causes execution to terminate without returning.
95 //! 105 //!
96 //! \sa CheckedReadFD 106 //! \sa CheckedReadFile
97 //! \sa WriteFD 107 //! \sa WriteFile
98 //! \sa LoggingWriteFD 108 //! \sa LoggingWriteFile
99 void CheckedWriteFD(int fd, const void* buffer, size_t size); 109 void CheckedWriteFile(FileHandle file, const void* buffer, size_t size);
100 110
101 //! \brief Wraps ReadFD(), ensuring that it indicates end-of-file. 111 //! \brief Wraps ReadFile(), ensuring that it indicates end-of-file.
102 //! 112 //!
103 //! Attempts to read a single byte from \a fd, expecting no data to be read. If 113 //! 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 114 //! If the underlying ReadFile() fails, or if a byte actually is read, this
105 //! causes execution to terminate without returning. 115 //! function causes execution to terminate without returning.
106 //! 116 //!
107 //! \sa CheckedReadFD 117 //! \sa CheckedReadFile
108 //! \sa ReadFD 118 //! \sa ReadFile
109 void CheckedReadFDAtEOF(int fd); 119 void CheckedReadFileAtEOF(FileHandle file);
110 120
111 //! \brief Wraps `close()`, logging an error if the operation fails. 121 //! \brief Wraps `close()`, logging an error if the operation fails.
112 //! 122 //!
113 //! \return On success, `true` is returned. On failure, an error is logged and 123 //! \return On success, `true` is returned. On failure, an error is logged and
114 //! `false` is returned. 124 //! `false` is returned.
115 bool LoggingCloseFD(int fd); 125 bool LoggingCloseFile(FileHandle file);
116 126
117 //! \brief Wraps `close()`, ensuring that it succeeds. 127 //! \brief Wraps `close()`, ensuring that it succeeds.
118 //! 128 //!
119 //! If `close()` fails, this function causes execution to terminate without 129 //! If `close()` fails, this function causes execution to terminate without
120 //! returning. 130 //! returning.
121 void CheckedCloseFD(int fd); 131 void CheckedCloseFile(FileHandle file);
122 132
123 } // namespace crashpad 133 } // namespace crashpad
124 134
125 #endif // CRASHPAD_UTIL_FILE_FD_IO_H_ 135 #endif // CRASHPAD_UTIL_FILE_FILE_IO_H_
OLDNEW
« no previous file with comments | « util/file/fd_io.cc ('k') | util/file/file_io.cc » ('j') | util/file/file_io.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698