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 #include "util/file/file_reader.h" |
| 16 |
| 17 #include "base/logging.h" |
| 18 #include "base/numerics/safe_conversions.h" |
| 19 |
| 20 namespace crashpad { |
| 21 |
| 22 WeakFileHandleFileReader::WeakFileHandleFileReader(FileHandle file_handle) |
| 23 : file_handle_(file_handle) { |
| 24 } |
| 25 |
| 26 WeakFileHandleFileReader::~WeakFileHandleFileReader() { |
| 27 } |
| 28 |
| 29 ssize_t WeakFileHandleFileReader::Read(void* data, size_t size) { |
| 30 DCHECK_NE(file_handle_, kInvalidFileHandle); |
| 31 |
| 32 // Don’t use LoggingReadFile(), which insists on a full read and only returns |
| 33 // a bool. This method permits short reads and returns the number of bytes |
| 34 // read. |
| 35 base::checked_cast<ssize_t>(size); |
| 36 ssize_t rv = ReadFile(file_handle_, data, size); |
| 37 if (rv < 0) { |
| 38 PLOG(ERROR) << "read"; |
| 39 return -1; |
| 40 } |
| 41 |
| 42 return rv; |
| 43 } |
| 44 |
| 45 FileOffset WeakFileHandleFileReader::Seek(FileOffset offset, int whence) { |
| 46 DCHECK_NE(file_handle_, kInvalidFileHandle); |
| 47 return LoggingSeekFile(file_handle_, offset, whence); |
| 48 } |
| 49 |
| 50 FileReader::FileReader() |
| 51 : file_(), |
| 52 weak_file_handle_file_reader_(kInvalidFileHandle) { |
| 53 } |
| 54 |
| 55 FileReader::~FileReader() { |
| 56 } |
| 57 |
| 58 bool FileReader::Open(const base::FilePath& path, |
| 59 FileWriteMode write_mode, |
| 60 FilePermissions permissions) { |
| 61 CHECK(!file_.is_valid()); |
| 62 file_.reset(LoggingOpenFileForRead(path)); |
| 63 if (!file_.is_valid()) { |
| 64 return false; |
| 65 } |
| 66 |
| 67 weak_file_handle_file_reader_.set_file_handle(file_.get()); |
| 68 return true; |
| 69 } |
| 70 |
| 71 void FileReader::Close() { |
| 72 CHECK(file_.is_valid()); |
| 73 |
| 74 weak_file_handle_file_reader_.set_file_handle(kInvalidFileHandle); |
| 75 file_.reset(); |
| 76 } |
| 77 |
| 78 ssize_t FileReader::Read(void* data, size_t size) { |
| 79 DCHECK(file_.is_valid()); |
| 80 return weak_file_handle_file_reader_.Read(data, size); |
| 81 } |
| 82 |
| 83 FileOffset FileReader::Seek(FileOffset offset, int whence) { |
| 84 DCHECK(file_.is_valid()); |
| 85 return weak_file_handle_file_reader_.Seek(offset, whence); |
| 86 } |
| 87 |
| 88 } // namespace crashpad |
OLD | NEW |