file_reader.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include "util/file/file_reader.h"
  15. #include "base/logging.h"
  16. #include "base/numerics/safe_conversions.h"
  17. #include "build/build_config.h"
  18. namespace crashpad {
  19. namespace {
  20. class FileReaderReadExactly final : public internal::ReadExactlyInternal {
  21. public:
  22. explicit FileReaderReadExactly(FileReaderInterface* file_reader)
  23. : ReadExactlyInternal(), file_reader_(file_reader) {}
  24. ~FileReaderReadExactly() {}
  25. private:
  26. // ReadExactlyInternal:
  27. FileOperationResult Read(void* buffer, size_t size, bool can_log) override {
  28. DCHECK(can_log);
  29. return file_reader_->Read(buffer, size);
  30. }
  31. FileReaderInterface* file_reader_; // weak
  32. DISALLOW_COPY_AND_ASSIGN(FileReaderReadExactly);
  33. };
  34. } // namespace
  35. bool FileReaderInterface::ReadExactly(void* data, size_t size) {
  36. FileReaderReadExactly read_exactly(this);
  37. return read_exactly.ReadExactly(data, size, true);
  38. }
  39. WeakFileHandleFileReader::WeakFileHandleFileReader(FileHandle file_handle)
  40. : file_handle_(file_handle) {
  41. }
  42. WeakFileHandleFileReader::~WeakFileHandleFileReader() {
  43. }
  44. FileOperationResult WeakFileHandleFileReader::Read(void* data, size_t size) {
  45. DCHECK_NE(file_handle_, kInvalidFileHandle);
  46. base::checked_cast<FileOperationResult>(size);
  47. FileOperationResult rv = ReadFile(file_handle_, data, size);
  48. if (rv < 0) {
  49. PLOG(ERROR) << internal::kNativeReadFunctionName;
  50. return -1;
  51. }
  52. return rv;
  53. }
  54. FileOffset WeakFileHandleFileReader::Seek(FileOffset offset, int whence) {
  55. DCHECK_NE(file_handle_, kInvalidFileHandle);
  56. return LoggingSeekFile(file_handle_, offset, whence);
  57. }
  58. FileReader::FileReader()
  59. : file_(),
  60. weak_file_handle_file_reader_(kInvalidFileHandle) {
  61. }
  62. FileReader::~FileReader() {
  63. }
  64. bool FileReader::Open(const base::FilePath& path) {
  65. CHECK(!file_.is_valid());
  66. file_.reset(LoggingOpenFileForRead(path));
  67. if (!file_.is_valid()) {
  68. return false;
  69. }
  70. weak_file_handle_file_reader_.set_file_handle(file_.get());
  71. return true;
  72. }
  73. void FileReader::Close() {
  74. CHECK(file_.is_valid());
  75. weak_file_handle_file_reader_.set_file_handle(kInvalidFileHandle);
  76. file_.reset();
  77. }
  78. FileOperationResult FileReader::Read(void* data, size_t size) {
  79. DCHECK(file_.is_valid());
  80. return weak_file_handle_file_reader_.Read(data, size);
  81. }
  82. FileOffset FileReader::Seek(FileOffset offset, int whence) {
  83. DCHECK(file_.is_valid());
  84. return weak_file_handle_file_reader_.Seek(offset, whence);
  85. }
  86. } // namespace crashpad