OLD | NEW |
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 bool FileReaderInterface::ReadExactly(void* data, size_t size) { |
24 ssize_t expect = base::checked_cast<ssize_t>(size); | 24 FileOperationResult expect = base::checked_cast<FileOperationResult>(size); |
25 ssize_t rv = Read(data, size); | 25 FileOperationResult rv = Read(data, size); |
26 if (rv < 0) { | 26 if (rv < 0) { |
27 // Read() will have logged its own error. | 27 // Read() will have logged its own error. |
28 return false; | 28 return false; |
29 } else if (rv != expect) { | 29 } else if (rv != expect) { |
30 LOG(ERROR) << "ReadExactly(): expected " << expect << ", observed " << rv; | 30 LOG(ERROR) << "ReadExactly(): expected " << expect << ", observed " << rv; |
31 return false; | 31 return false; |
32 } | 32 } |
33 | 33 |
34 return true; | 34 return true; |
35 } | 35 } |
36 | 36 |
37 WeakFileHandleFileReader::WeakFileHandleFileReader(FileHandle file_handle) | 37 WeakFileHandleFileReader::WeakFileHandleFileReader(FileHandle file_handle) |
38 : file_handle_(file_handle) { | 38 : file_handle_(file_handle) { |
39 } | 39 } |
40 | 40 |
41 WeakFileHandleFileReader::~WeakFileHandleFileReader() { | 41 WeakFileHandleFileReader::~WeakFileHandleFileReader() { |
42 } | 42 } |
43 | 43 |
44 ssize_t WeakFileHandleFileReader::Read(void* data, size_t size) { | 44 FileOperationResult WeakFileHandleFileReader::Read(void* data, size_t size) { |
45 DCHECK_NE(file_handle_, kInvalidFileHandle); | 45 DCHECK_NE(file_handle_, kInvalidFileHandle); |
46 | 46 |
47 // Don’t use LoggingReadFile(), which insists on a full read and only returns | 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 | 48 // a bool. This method permits short reads and returns the number of bytes |
49 // read. | 49 // read. |
50 base::checked_cast<ssize_t>(size); | 50 base::checked_cast<FileOperationResult>(size); |
51 ssize_t rv = ReadFile(file_handle_, data, size); | 51 FileOperationResult rv = ReadFile(file_handle_, data, size); |
52 if (rv < 0) { | 52 if (rv < 0) { |
53 PLOG(ERROR) << "read"; | 53 PLOG(ERROR) << "read"; |
54 return -1; | 54 return -1; |
55 } | 55 } |
56 | 56 |
57 return rv; | 57 return rv; |
58 } | 58 } |
59 | 59 |
60 FileOffset WeakFileHandleFileReader::Seek(FileOffset offset, int whence) { | 60 FileOffset WeakFileHandleFileReader::Seek(FileOffset offset, int whence) { |
61 DCHECK_NE(file_handle_, kInvalidFileHandle); | 61 DCHECK_NE(file_handle_, kInvalidFileHandle); |
(...skipping 19 matching lines...) Expand all Loading... |
81 return true; | 81 return true; |
82 } | 82 } |
83 | 83 |
84 void FileReader::Close() { | 84 void FileReader::Close() { |
85 CHECK(file_.is_valid()); | 85 CHECK(file_.is_valid()); |
86 | 86 |
87 weak_file_handle_file_reader_.set_file_handle(kInvalidFileHandle); | 87 weak_file_handle_file_reader_.set_file_handle(kInvalidFileHandle); |
88 file_.reset(); | 88 file_.reset(); |
89 } | 89 } |
90 | 90 |
91 ssize_t FileReader::Read(void* data, size_t size) { | 91 FileOperationResult FileReader::Read(void* data, size_t size) { |
92 DCHECK(file_.is_valid()); | 92 DCHECK(file_.is_valid()); |
93 return weak_file_handle_file_reader_.Read(data, size); | 93 return weak_file_handle_file_reader_.Read(data, size); |
94 } | 94 } |
95 | 95 |
96 FileOffset FileReader::Seek(FileOffset offset, int whence) { | 96 FileOffset FileReader::Seek(FileOffset offset, int whence) { |
97 DCHECK(file_.is_valid()); | 97 DCHECK(file_.is_valid()); |
98 return weak_file_handle_file_reader_.Seek(offset, whence); | 98 return weak_file_handle_file_reader_.Seek(offset, whence); |
99 } | 99 } |
100 | 100 |
101 WeakStdioFileReader::WeakStdioFileReader(FILE* file) | 101 WeakStdioFileReader::WeakStdioFileReader(FILE* file) |
102 : file_(file) { | 102 : file_(file) { |
103 } | 103 } |
104 | 104 |
105 WeakStdioFileReader::~WeakStdioFileReader() { | 105 WeakStdioFileReader::~WeakStdioFileReader() { |
106 } | 106 } |
107 | 107 |
108 ssize_t WeakStdioFileReader::Read(void* data, size_t size) { | 108 FileOperationResult WeakStdioFileReader::Read(void* data, size_t size) { |
109 DCHECK(file_); | 109 DCHECK(file_); |
110 | 110 |
111 size_t rv = fread(data, 1, size, file_); | 111 size_t rv = fread(data, 1, size, file_); |
112 if (rv < size && ferror(file_)) { | 112 if (rv < size && ferror(file_)) { |
113 STDIO_PLOG(ERROR) << "fread"; | 113 STDIO_PLOG(ERROR) << "fread"; |
114 return -1; | 114 return -1; |
115 } | 115 } |
116 | 116 |
117 return rv; | 117 return rv; |
118 } | 118 } |
119 | 119 |
120 FileOffset WeakStdioFileReader::Seek(FileOffset offset, int whence) { | 120 FileOffset WeakStdioFileReader::Seek(FileOffset offset, int whence) { |
121 DCHECK(file_); | 121 DCHECK(file_); |
122 if (fseeko(file_, offset, whence) == -1) { | 122 if (fseeko(file_, offset, whence) == -1) { |
123 STDIO_PLOG(ERROR) << "fseeko"; | 123 STDIO_PLOG(ERROR) << "fseeko"; |
124 return -1; | 124 return -1; |
125 } | 125 } |
126 | 126 |
127 FileOffset new_offset = ftello(file_); | 127 FileOffset new_offset = ftello(file_); |
128 if (new_offset == -1) { | 128 if (new_offset == -1) { |
129 STDIO_PLOG(ERROR) << "ftello"; | 129 STDIO_PLOG(ERROR) << "ftello"; |
130 return -1; | 130 return -1; |
131 } | 131 } |
132 | 132 |
133 return new_offset; | 133 return new_offset; |
134 } | 134 } |
135 | 135 |
136 } // namespace crashpad | 136 } // namespace crashpad |
OLD | NEW |