OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (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 |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #ifndef CRASHPAD_UTIL_FILE_FILE_READER_H_ |
| 16 #define CRASHPAD_UTIL_FILE_FILE_READER_H_ |
| 17 |
| 18 #include <sys/types.h> |
| 19 |
| 20 #include "base/basictypes.h" |
| 21 #include "base/files/file_path.h" |
| 22 #include "util/file/file_io.h" |
| 23 #include "util/file/file_seeker.h" |
| 24 |
| 25 namespace crashpad { |
| 26 |
| 27 //! \brief An interface to read to files and other file-like objects with |
| 28 //! semantics matching the underlying platform (POSIX or Windows). |
| 29 class FileReaderInterface : public FileSeekerInterface { |
| 30 public: |
| 31 //! \brief Wraps ReadFile(), or provides an implementation with identical |
| 32 //! semantics. |
| 33 //! |
| 34 //! \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. |
| 36 //! `-1` if the operation failed, with an error message logged. |
| 37 virtual ssize_t Read(void* data, size_t size) = 0; |
| 38 |
| 39 protected: |
| 40 ~FileReaderInterface() {} |
| 41 }; |
| 42 |
| 43 //! \brief A file reader backed by a FileHandle. |
| 44 //! |
| 45 //! FileReader requires users to provide a FilePath to open, but this class |
| 46 //! accepts an already-open FileHandle instead. Like FileReader, this class may |
| 47 //! read from a filesystem-based file, but unlike FileReader, this class is not |
| 48 //! responsible for opening or closing the file. Users of this class must ensure |
| 49 //! that the file handle is closed appropriately elsewhere. Objects of this |
| 50 //! class may be used to read from file handles not associated with |
| 51 //! filesystem-based files, although special attention should be paid to the |
| 52 //! Seek() method, which may not function on file handles that do not refer to |
| 53 //! disk-based files. |
| 54 //! |
| 55 //! This class is expected to be used when other code is responsible for |
| 56 //! opening files and already provides file handles. |
| 57 class WeakFileHandleFileReader : public FileReaderInterface { |
| 58 public: |
| 59 explicit WeakFileHandleFileReader(FileHandle file_handle); |
| 60 ~WeakFileHandleFileReader(); |
| 61 |
| 62 // FileReaderInterface: |
| 63 ssize_t Read(void* data, size_t size) override; |
| 64 |
| 65 // FileSeekerInterface: |
| 66 |
| 67 //! \copydoc FileReaderInterface::Seek() |
| 68 //! |
| 69 //! \note This method is only guaranteed to function on file handles referring |
| 70 //! to disk-based files. |
| 71 FileOffset Seek(FileOffset offset, int whence) override; |
| 72 |
| 73 private: |
| 74 void set_file_handle(FileHandle file_handle) { file_handle_ = file_handle; } |
| 75 |
| 76 FileHandle file_handle_; // weak |
| 77 |
| 78 // FileReader uses this class as its internal implementation, and it needs to |
| 79 // be able to call set_file_handle(). FileReader cannot initialize a |
| 80 // WeakFileHandleFileReader with a correct file descriptor at the time of |
| 81 // construction because no file descriptor will be available until |
| 82 // FileReader::Open() is called. |
| 83 friend class FileReader; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileReader); |
| 86 }; |
| 87 |
| 88 //! \brief A file reader implementation that wraps traditional system file |
| 89 //! operations on files accessed through the filesystem. |
| 90 class FileReader : public FileReaderInterface { |
| 91 public: |
| 92 FileReader(); |
| 93 ~FileReader(); |
| 94 |
| 95 // FileReaderInterface: |
| 96 |
| 97 //! \brief Wraps LoggingOpenFileForRead(). |
| 98 //! |
| 99 //! \return `true` if the operation succeeded, `false` if it failed, with an |
| 100 //! error message logged. |
| 101 //! |
| 102 //! \note After a successful call, this method cannot be called again until |
| 103 //! after Close(). |
| 104 bool Open(const base::FilePath& path, |
| 105 FileWriteMode write_mode, |
| 106 FilePermissions permissions); |
| 107 |
| 108 //! \brief Wraps CheckedCloseHandle(). |
| 109 //! |
| 110 //! \note It is only valid to call this method on an object that has had a |
| 111 //! successful Open() that has not yet been matched by a subsequent call |
| 112 //! to this method. |
| 113 void Close(); |
| 114 |
| 115 // FileReaderInterface: |
| 116 |
| 117 //! \copydoc FileReaderInterface::Read() |
| 118 //! |
| 119 //! \note It is only valid to call this method between a successful Open() and |
| 120 //! a Close(). |
| 121 ssize_t Read(void* data, size_t size) override; |
| 122 |
| 123 // FileSeekerInterface: |
| 124 |
| 125 //! \copydoc FileReaderInterface::Seek() |
| 126 //! |
| 127 //! \note It is only valid to call this method between a successful Open() and |
| 128 //! a Close(). |
| 129 FileOffset Seek(FileOffset offset, int whence) override; |
| 130 |
| 131 private: |
| 132 ScopedFileHandle file_; |
| 133 WeakFileHandleFileReader weak_file_handle_file_reader_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(FileReader); |
| 136 }; |
| 137 |
| 138 } // namespace crashpad |
| 139 |
| 140 #endif // CRASHPAD_UTIL_FILE_FILE_READER_H_ |
OLD | NEW |