Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | |
|
Robert Sesek
2015/02/18 18:44:28
2015
| |
| 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 | |
| 24 namespace crashpad { | |
| 25 | |
| 26 //! \brief An interface to read to files and other file-like objects with | |
| 27 //! semantics matching the underlying platform (POSIX or Windows). | |
| 28 // | |
| 29 // TODO(mark): Templatize and share more with FileWriterInterface? | |
|
Robert Sesek
2015/02/18 18:44:28
Wouldn't this just be a FileSeekerInterface ?
| |
| 30 class FileReaderInterface { | |
| 31 public: | |
| 32 //! \brief Wraps ReadFile(), or provides an implementation with identical | |
| 33 //! semantics. | |
| 34 //! | |
| 35 //! \return The number of bytes actually read if the operation succeeded, | |
| 36 //! which may be `0`. `-1` if the operation failed, with an error message | |
| 37 //! logged. | |
| 38 virtual ssize_t Read(void* data, size_t size) = 0; | |
| 39 | |
| 40 //! \brief Wraps LoggingFileSeek() or provides an alternate implementation | |
| 41 //! with identical semantics. | |
| 42 //! | |
| 43 //! \return The return value of LoggingFileSeek(). `-1` on failure, | |
| 44 //! with an error message logged. | |
| 45 // | |
| 46 // TODO(mark): This may be better with a common base of FileWriterInterface | |
| 47 // and FileReaderInterface to declaring Seek(). | |
| 48 virtual FileOffset Seek(FileOffset offset, int whence) = 0; | |
| 49 | |
| 50 protected: | |
| 51 ~FileReaderInterface() {} | |
| 52 }; | |
| 53 | |
| 54 //! \brief A file reader backed by a FileHandle. | |
| 55 //! | |
| 56 //! FileReader requires users to provide a FilePath to open, but this class | |
| 57 //! accepts an already-open FileHandle instead. Like FileReader, this class may | |
| 58 //! read from a filesystem-based file, but unlike FileReader, this class is not | |
| 59 //! responsible for opening or closing the file. Users of this class must ensure | |
| 60 //! that the file handle is closed appropriately elsewhere. Objects of this | |
| 61 //! class may be used to read from file handles not associated with | |
| 62 //! filesystem-based files, although special attention should be paid to the | |
| 63 //! Seek() method, which may not function on file handles that do not refer to | |
| 64 //! disk-based files. | |
| 65 //! | |
| 66 //! This class is expected to be used when other code is responsible for | |
| 67 //! opening files and already provides file handles. | |
| 68 class WeakFileHandleFileReader : public FileReaderInterface { | |
| 69 public: | |
| 70 explicit WeakFileHandleFileReader(FileHandle file_handle); | |
| 71 ~WeakFileHandleFileReader(); | |
| 72 | |
| 73 // FileReaderInterface: | |
| 74 ssize_t Read(void* data, size_t size) override; | |
| 75 | |
| 76 //! \copydoc FileReaderInterface::Seek() | |
| 77 //! | |
| 78 //! \note This method is only guaranteed to function on file handles referring | |
| 79 //! to disk-based files. | |
| 80 FileOffset Seek(FileOffset offset, int whence) override; | |
| 81 | |
| 82 private: | |
| 83 void set_file_handle(FileHandle file_handle) { file_handle_ = file_handle; } | |
| 84 | |
| 85 FileHandle file_handle_; // weak | |
| 86 | |
| 87 // FileReader uses this class as its internal implementation, and it needs to | |
| 88 // be able to call set_file_handle(). FileReader cannot initialize a | |
| 89 // WeakFileHandleFileReader with a correct file descriptor at the time of | |
| 90 // construction because no file descriptor will be available until | |
| 91 // FileReader::Open() is called. | |
| 92 friend class FileReader; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileReader); | |
| 95 }; | |
| 96 | |
| 97 //! \brief A file reader implementation that wraps traditional system file | |
| 98 //! operations on files accessed through the filesystem. | |
| 99 class FileReader : public FileReaderInterface { | |
| 100 public: | |
| 101 FileReader(); | |
| 102 ~FileReader(); | |
| 103 | |
| 104 //! \brief Wraps LoggingOpenFileForRead(). | |
| 105 //! | |
| 106 //! \return `true` if the operation succeeded, `false` if it failed, with an | |
| 107 //! error message logged. | |
| 108 //! | |
| 109 //! \note After a successful call, this method cannot be called again until | |
| 110 //! after Close(). | |
| 111 bool Open(const base::FilePath& path, | |
| 112 FileWriteMode write_mode, | |
| 113 FilePermissions permissions); | |
| 114 | |
| 115 //! \brief Wraps CheckedCloseHandle(). | |
| 116 //! | |
| 117 //! \note It is only valid to call this method on an object that has had a | |
| 118 //! successful Open() that has not yet been matched by a subsequent call | |
| 119 //! to this method. | |
| 120 void Close(); | |
| 121 | |
| 122 // FileReaderInterface: | |
| 123 | |
| 124 //! \copydoc FileReaderInterface::Read() | |
| 125 //! | |
| 126 //! \note It is only valid to call this method between a successful Open() and | |
| 127 //! a Close(). | |
| 128 ssize_t Read(void* data, size_t size) override; | |
| 129 | |
| 130 //! \copydoc FileReaderInterface::Seek() | |
| 131 //! | |
| 132 //! \note It is only valid to call this method between a successful Open() and | |
| 133 //! a Close(). | |
| 134 FileOffset Seek(FileOffset offset, int whence) override; | |
| 135 | |
| 136 private: | |
| 137 ScopedFileHandle file_; | |
| 138 WeakFileHandleFileReader weak_file_handle_file_reader_; | |
| 139 | |
| 140 DISALLOW_COPY_AND_ASSIGN(FileReader); | |
| 141 }; | |
| 142 | |
| 143 } // namespace crashpad | |
| 144 | |
| 145 #endif // CRASHPAD_UTIL_FILE_FILE_READER_H_ | |
| OLD | NEW |