OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 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_READER_H_ | 15 #ifndef CRASHPAD_UTIL_FILE_FILE_READER_H_ |
16 #define CRASHPAD_UTIL_FILE_FILE_READER_H_ | 16 #define CRASHPAD_UTIL_FILE_FILE_READER_H_ |
17 | 17 |
18 #include <stdio.h> | |
19 #include <sys/types.h> | 18 #include <sys/types.h> |
20 | 19 |
21 #include "base/files/file_path.h" | 20 #include "base/files/file_path.h" |
22 #include "base/macros.h" | 21 #include "base/macros.h" |
23 #include "util/file/file_io.h" | 22 #include "util/file/file_io.h" |
24 #include "util/file/file_seeker.h" | 23 #include "util/file/file_seeker.h" |
25 | 24 |
26 namespace crashpad { | 25 namespace crashpad { |
27 | 26 |
28 //! \brief An interface to read to files and other file-like objects with | 27 //! \brief An interface to read to files and other file-like objects with |
29 //! semantics matching the underlying platform (POSIX or Windows). | 28 //! semantics matching the underlying platform (POSIX or Windows). |
30 class FileReaderInterface : public virtual FileSeekerInterface { | 29 class FileReaderInterface : public virtual FileSeekerInterface { |
31 public: | 30 public: |
32 virtual ~FileReaderInterface() {} | 31 virtual ~FileReaderInterface() {} |
33 | 32 |
34 //! \brief Wraps ReadFile(), or provides an implementation with identical | 33 //! \brief Wraps ReadFile(), or provides an implementation with identical |
35 //! semantics. | 34 //! semantics. |
36 //! | 35 //! |
37 //! \return The number of bytes actually read if the operation succeeded, | 36 //! \return The number of bytes actually read if the operation succeeded, |
38 //! which may be `0` or any positive value less than or equal to \a size. | 37 //! which may be `0` or any positive value less than or equal to \a size. |
39 //! `-1` if the operation failed, with an error message logged. | 38 //! `-1` if the operation failed, with an error message logged. |
40 virtual FileOperationResult Read(void* data, size_t size) = 0; | 39 virtual FileOperationResult Read(void* data, size_t size) = 0; |
41 | 40 |
42 //! \brief Wraps Read(), ensuring that the read succeeded and exactly \a size | 41 //! \brief Wraps Read(), ensuring that the read succeeded and exactly \a size |
43 //! bytes were read. | 42 //! bytes were read. |
44 //! | 43 //! |
45 //! Semantically, this behaves as LoggingReadFile(). | 44 //! Semantically, this behaves as LoggingReadFileExactly(). |
46 //! | 45 //! |
47 //! \return `true` if the operation succeeded, `false` if it failed, with an | 46 //! \return `true` if the operation succeeded, `false` if it failed, with an |
48 //! error message logged. Short reads are treated as failures. | 47 //! error message logged. Short reads are treated as failures. |
49 bool ReadExactly(void* data, size_t size); | 48 bool ReadExactly(void* data, size_t size); |
50 }; | 49 }; |
51 | 50 |
52 //! \brief A file reader backed by a FileHandle. | 51 //! \brief A file reader backed by a FileHandle. |
53 //! | 52 //! |
54 //! FileReader requires users to provide a FilePath to open, but this class | 53 //! FileReader requires users to provide a FilePath to open, but this class |
55 //! accepts an already-open FileHandle instead. Like FileReader, this class may | 54 //! accepts an already-open FileHandle instead. Like FileReader, this class may |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 //! a Close(). | 134 //! a Close(). |
136 FileOffset Seek(FileOffset offset, int whence) override; | 135 FileOffset Seek(FileOffset offset, int whence) override; |
137 | 136 |
138 private: | 137 private: |
139 ScopedFileHandle file_; | 138 ScopedFileHandle file_; |
140 WeakFileHandleFileReader weak_file_handle_file_reader_; | 139 WeakFileHandleFileReader weak_file_handle_file_reader_; |
141 | 140 |
142 DISALLOW_COPY_AND_ASSIGN(FileReader); | 141 DISALLOW_COPY_AND_ASSIGN(FileReader); |
143 }; | 142 }; |
144 | 143 |
145 //! \brief A file reader backed by a standard input/output `FILE*`. | |
146 //! | |
147 //! This class accepts an already-open `FILE*`. It is not responsible for | |
148 //! opening or closing this `FILE*`. Users of this class must ensure that the | |
149 //! `FILE*` is closed appropriately elsewhere. Objects of this class may be used | |
150 //! to read from `FILE*` objects not associated with filesystem-based files, | |
151 //! although special attention should be paid to the Seek() method, which may | |
152 //! not function on `FILE*` objects that do not refer to disk-based files. | |
153 //! | |
154 //! This class is expected to be used when other code is responsible for | |
155 //! opening `FILE*` objects and already provides `FILE*` objects. A good use | |
156 //! would be a WeakStdioFileReader for `stdin`. | |
157 class WeakStdioFileReader : public FileReaderInterface { | |
158 public: | |
159 explicit WeakStdioFileReader(FILE* file); | |
160 ~WeakStdioFileReader() override; | |
161 | |
162 // FileReaderInterface: | |
163 FileOperationResult Read(void* data, size_t size) override; | |
164 | |
165 // FileSeekerInterface: | |
166 | |
167 //! \copydoc FileReaderInterface::Seek() | |
168 //! | |
169 //! \note This method is only guaranteed to function on `FILE*` objects | |
170 //! referring to disk-based files. | |
171 FileOffset Seek(FileOffset offset, int whence) override; | |
172 | |
173 private: | |
174 FILE* file_; // weak | |
175 | |
176 DISALLOW_COPY_AND_ASSIGN(WeakStdioFileReader); | |
177 }; | |
178 | |
179 } // namespace crashpad | 144 } // namespace crashpad |
180 | 145 |
181 #endif // CRASHPAD_UTIL_FILE_FILE_READER_H_ | 146 #endif // CRASHPAD_UTIL_FILE_FILE_READER_H_ |
OLD | NEW |