Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: third_party/crashpad/crashpad/util/file/file_reader.cc

Issue 2773813002: Update Crashpad to 8e37886d418dd042c3c7bfadac99214739ee4d98 (Closed)
Patch Set: Update Crashpad to 8e37886d418dd042c3c7bfadac99214739ee4d98 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "util/file/file_reader.h" 15 #include "util/file/file_reader.h"
16 16
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/numerics/safe_conversions.h" 18 #include "base/numerics/safe_conversions.h"
19 #include "build/build_config.h" 19 #include "build/build_config.h"
20 20
21 namespace crashpad { 21 namespace crashpad {
22 22
23 bool FileReaderInterface::ReadExactly(void* data, size_t size) { 23 namespace {
24 FileOperationResult expect = base::checked_cast<FileOperationResult>(size); 24
25 FileOperationResult rv = Read(data, size); 25 class FileReaderReadExactly final : public internal::ReadExactlyInternal {
26 if (rv < 0) { 26 public:
27 // Read() will have logged its own error. 27 explicit FileReaderReadExactly(FileReaderInterface* file_reader)
28 return false; 28 : ReadExactlyInternal(), file_reader_(file_reader) {}
29 } else if (rv != expect) { 29 ~FileReaderReadExactly() {}
30 LOG(ERROR) << "ReadExactly(): expected " << expect << ", observed " << rv; 30
31 return false; 31 private:
32 // ReadExactlyInternal:
33 FileOperationResult Read(void* buffer, size_t size, bool can_log) override {
34 DCHECK(can_log);
35 return file_reader_->Read(buffer, size);
32 } 36 }
33 37
34 return true; 38 FileReaderInterface* file_reader_; // weak
39
40 DISALLOW_COPY_AND_ASSIGN(FileReaderReadExactly);
41 };
42
43 } // namespace
44
45 bool FileReaderInterface::ReadExactly(void* data, size_t size) {
46 FileReaderReadExactly read_exactly(this);
47 return read_exactly.ReadExactly(data, size, true);
35 } 48 }
36 49
37 WeakFileHandleFileReader::WeakFileHandleFileReader(FileHandle file_handle) 50 WeakFileHandleFileReader::WeakFileHandleFileReader(FileHandle file_handle)
38 : file_handle_(file_handle) { 51 : file_handle_(file_handle) {
39 } 52 }
40 53
41 WeakFileHandleFileReader::~WeakFileHandleFileReader() { 54 WeakFileHandleFileReader::~WeakFileHandleFileReader() {
42 } 55 }
43 56
44 FileOperationResult WeakFileHandleFileReader::Read(void* data, size_t size) { 57 FileOperationResult WeakFileHandleFileReader::Read(void* data, size_t size) {
45 DCHECK_NE(file_handle_, kInvalidFileHandle); 58 DCHECK_NE(file_handle_, kInvalidFileHandle);
46 59
47 // Don’t use LoggingReadFile(), which insists on a full read and only returns
48 // a bool. This method permits short reads and returns the number of bytes
49 // read.
50 base::checked_cast<FileOperationResult>(size); 60 base::checked_cast<FileOperationResult>(size);
51 FileOperationResult rv = ReadFile(file_handle_, data, size); 61 FileOperationResult rv = ReadFile(file_handle_, data, size);
52 if (rv < 0) { 62 if (rv < 0) {
53 PLOG(ERROR) << "read"; 63 PLOG(ERROR) << internal::kNativeReadFunctionName;
54 return -1; 64 return -1;
55 } 65 }
56 66
57 return rv; 67 return rv;
58 } 68 }
59 69
60 FileOffset WeakFileHandleFileReader::Seek(FileOffset offset, int whence) { 70 FileOffset WeakFileHandleFileReader::Seek(FileOffset offset, int whence) {
61 DCHECK_NE(file_handle_, kInvalidFileHandle); 71 DCHECK_NE(file_handle_, kInvalidFileHandle);
62 return LoggingSeekFile(file_handle_, offset, whence); 72 return LoggingSeekFile(file_handle_, offset, whence);
63 } 73 }
(...skipping 27 matching lines...) Expand all
91 FileOperationResult FileReader::Read(void* data, size_t size) { 101 FileOperationResult FileReader::Read(void* data, size_t size) {
92 DCHECK(file_.is_valid()); 102 DCHECK(file_.is_valid());
93 return weak_file_handle_file_reader_.Read(data, size); 103 return weak_file_handle_file_reader_.Read(data, size);
94 } 104 }
95 105
96 FileOffset FileReader::Seek(FileOffset offset, int whence) { 106 FileOffset FileReader::Seek(FileOffset offset, int whence) {
97 DCHECK(file_.is_valid()); 107 DCHECK(file_.is_valid());
98 return weak_file_handle_file_reader_.Seek(offset, whence); 108 return weak_file_handle_file_reader_.Seek(offset, whence);
99 } 109 }
100 110
101 WeakStdioFileReader::WeakStdioFileReader(FILE* file)
102 : file_(file) {
103 }
104
105 WeakStdioFileReader::~WeakStdioFileReader() {
106 }
107
108 FileOperationResult WeakStdioFileReader::Read(void* data, size_t size) {
109 DCHECK(file_);
110
111 size_t rv = fread(data, 1, size, file_);
112 if (rv != size && ferror(file_)) {
113 STDIO_PLOG(ERROR) << "fread";
114 return -1;
115 }
116 if (rv > size) {
117 LOG(ERROR) << "fread: expected " << size << ", observed " << rv;
118 return -1;
119 }
120
121 return rv;
122 }
123
124 FileOffset WeakStdioFileReader::Seek(FileOffset offset, int whence) {
125 DCHECK(file_);
126 if (fseeko(file_, offset, whence) == -1) {
127 STDIO_PLOG(ERROR) << "fseeko";
128 return -1;
129 }
130
131 FileOffset new_offset = ftello(file_);
132 if (new_offset == -1) {
133 STDIO_PLOG(ERROR) << "ftello";
134 return -1;
135 }
136
137 return new_offset;
138 }
139
140 } // namespace crashpad 111 } // namespace crashpad
OLDNEW
« no previous file with comments | « third_party/crashpad/crashpad/util/file/file_reader.h ('k') | third_party/crashpad/crashpad/util/file/file_reader_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698