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> |
18 #include <sys/types.h> | 19 #include <sys/types.h> |
19 | 20 |
20 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
21 #include "base/files/file_path.h" | 22 #include "base/files/file_path.h" |
22 #include "util/file/file_io.h" | 23 #include "util/file/file_io.h" |
23 #include "util/file/file_seeker.h" | 24 #include "util/file/file_seeker.h" |
24 | 25 |
25 namespace crashpad { | 26 namespace crashpad { |
26 | 27 |
27 //! \brief An interface to read to files and other file-like objects with | 28 //! \brief An interface to read to files and other file-like objects with |
28 //! semantics matching the underlying platform (POSIX or Windows). | 29 //! semantics matching the underlying platform (POSIX or Windows). |
29 class FileReaderInterface : public virtual FileSeekerInterface { | 30 class FileReaderInterface : public virtual FileSeekerInterface { |
30 public: | 31 public: |
| 32 virtual ~FileReaderInterface() {} |
| 33 |
31 //! \brief Wraps ReadFile(), or provides an implementation with identical | 34 //! \brief Wraps ReadFile(), or provides an implementation with identical |
32 //! semantics. | 35 //! semantics. |
33 //! | 36 //! |
34 //! \return The number of bytes actually read if the operation succeeded, | 37 //! \return The number of bytes actually read if the operation succeeded, |
35 //! which may be `0` or any positive value less than or equal to \a size. | 38 //! which may be `0` or any positive value less than or equal to \a size. |
36 //! `-1` if the operation failed, with an error message logged. | 39 //! `-1` if the operation failed, with an error message logged. |
37 virtual ssize_t Read(void* data, size_t size) = 0; | 40 virtual ssize_t Read(void* data, size_t size) = 0; |
38 | 41 |
39 //! \brief Wraps Read(), ensuring that the read succeeded and exactly \a size | 42 //! \brief Wraps Read(), ensuring that the read succeeded and exactly \a size |
40 //! bytes were read. | 43 //! bytes were read. |
41 //! | 44 //! |
42 //! Semantically, this behaves as LoggingReadFile(). | 45 //! Semantically, this behaves as LoggingReadFile(). |
43 //! | 46 //! |
44 //! \return `true` if the operation succeeded, `false` if it failed, with an | 47 //! \return `true` if the operation succeeded, `false` if it failed, with an |
45 //! error message logged. Short reads are treated as failures. | 48 //! error message logged. Short reads are treated as failures. |
46 bool ReadExactly(void* data, size_t size); | 49 bool ReadExactly(void* data, size_t size); |
47 | |
48 protected: | |
49 ~FileReaderInterface() {} | |
50 }; | 50 }; |
51 | 51 |
52 //! \brief A file reader backed by a FileHandle. | 52 //! \brief A file reader backed by a FileHandle. |
53 //! | 53 //! |
54 //! FileReader requires users to provide a FilePath to open, but this class | 54 //! FileReader requires users to provide a FilePath to open, but this class |
55 //! accepts an already-open FileHandle instead. Like FileReader, this class may | 55 //! accepts an already-open FileHandle instead. Like FileReader, this class may |
56 //! read from a filesystem-based file, but unlike FileReader, this class is not | 56 //! read from a filesystem-based file, but unlike FileReader, this class is not |
57 //! responsible for opening or closing the file. Users of this class must ensure | 57 //! responsible for opening or closing the file. Users of this class must ensure |
58 //! that the file handle is closed appropriately elsewhere. Objects of this | 58 //! that the file handle is closed appropriately elsewhere. Objects of this |
59 //! class may be used to read from file handles not associated with | 59 //! class may be used to read from file handles not associated with |
60 //! filesystem-based files, although special attention should be paid to the | 60 //! filesystem-based files, although special attention should be paid to the |
61 //! Seek() method, which may not function on file handles that do not refer to | 61 //! Seek() method, which may not function on file handles that do not refer to |
62 //! disk-based files. | 62 //! disk-based files. |
63 //! | 63 //! |
64 //! This class is expected to be used when other code is responsible for | 64 //! This class is expected to be used when other code is responsible for |
65 //! opening files and already provides file handles. | 65 //! opening files and already provides file handles. |
66 class WeakFileHandleFileReader : public FileReaderInterface { | 66 class WeakFileHandleFileReader : public FileReaderInterface { |
67 public: | 67 public: |
68 explicit WeakFileHandleFileReader(FileHandle file_handle); | 68 explicit WeakFileHandleFileReader(FileHandle file_handle); |
69 ~WeakFileHandleFileReader(); | 69 ~WeakFileHandleFileReader() override; |
70 | 70 |
71 // FileReaderInterface: | 71 // FileReaderInterface: |
72 ssize_t Read(void* data, size_t size) override; | 72 ssize_t Read(void* data, size_t size) override; |
73 | 73 |
74 // FileSeekerInterface: | 74 // FileSeekerInterface: |
75 | 75 |
76 //! \copydoc FileReaderInterface::Seek() | 76 //! \copydoc FileReaderInterface::Seek() |
77 //! | 77 //! |
78 //! \note This method is only guaranteed to function on file handles referring | 78 //! \note This method is only guaranteed to function on file handles referring |
79 //! to disk-based files. | 79 //! to disk-based files. |
(...skipping 12 matching lines...) Expand all Loading... |
92 friend class FileReader; | 92 friend class FileReader; |
93 | 93 |
94 DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileReader); | 94 DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileReader); |
95 }; | 95 }; |
96 | 96 |
97 //! \brief A file reader implementation that wraps traditional system file | 97 //! \brief A file reader implementation that wraps traditional system file |
98 //! operations on files accessed through the filesystem. | 98 //! operations on files accessed through the filesystem. |
99 class FileReader : public FileReaderInterface { | 99 class FileReader : public FileReaderInterface { |
100 public: | 100 public: |
101 FileReader(); | 101 FileReader(); |
102 ~FileReader(); | 102 ~FileReader() override; |
103 | 103 |
104 // FileReaderInterface: | 104 // FileReaderInterface: |
105 | 105 |
106 //! \brief Wraps LoggingOpenFileForRead(). | 106 //! \brief Wraps LoggingOpenFileForRead(). |
107 //! | 107 //! |
108 //! \return `true` if the operation succeeded, `false` if it failed, with an | 108 //! \return `true` if the operation succeeded, `false` if it failed, with an |
109 //! error message logged. | 109 //! error message logged. |
110 //! | 110 //! |
111 //! \note After a successful call, this method cannot be called again until | 111 //! \note After a successful call, this method cannot be called again until |
112 //! after Close(). | 112 //! after Close(). |
(...skipping 22 matching lines...) Expand all Loading... |
135 //! a Close(). | 135 //! a Close(). |
136 FileOffset Seek(FileOffset offset, int whence) override; | 136 FileOffset Seek(FileOffset offset, int whence) override; |
137 | 137 |
138 private: | 138 private: |
139 ScopedFileHandle file_; | 139 ScopedFileHandle file_; |
140 WeakFileHandleFileReader weak_file_handle_file_reader_; | 140 WeakFileHandleFileReader weak_file_handle_file_reader_; |
141 | 141 |
142 DISALLOW_COPY_AND_ASSIGN(FileReader); | 142 DISALLOW_COPY_AND_ASSIGN(FileReader); |
143 }; | 143 }; |
144 | 144 |
| 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 ssize_t 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 |
145 } // namespace crashpad | 179 } // namespace crashpad |
146 | 180 |
147 #endif // CRASHPAD_UTIL_FILE_FILE_READER_H_ | 181 #endif // CRASHPAD_UTIL_FILE_FILE_READER_H_ |
OLD | NEW |