| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 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_writer.h" | 15 #include "util/file/file_writer.h" |
| 16 | 16 |
| 17 #include <algorithm> | 17 #include <algorithm> |
| 18 | 18 |
| 19 #include <limits.h> | 19 #include <limits.h> |
| 20 | 20 |
| 21 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #include "base/numerics/safe_conversions.h" |
| 23 #include "build/build_config.h" |
| 22 | 24 |
| 23 #if defined(OS_POSIX) | 25 #if defined(OS_POSIX) |
| 24 #include <sys/uio.h> | 26 #include <sys/uio.h> |
| 25 #include <unistd.h> | 27 #include <unistd.h> |
| 26 #include "base/posix/eintr_wrapper.h" | 28 #include "base/posix/eintr_wrapper.h" |
| 27 #endif // OS_POSIX | 29 #endif // OS_POSIX |
| 28 | 30 |
| 29 namespace crashpad { | 31 namespace crashpad { |
| 30 | 32 |
| 31 #if defined(OS_POSIX) | 33 #if defined(OS_POSIX) |
| 32 // Ensure type compatibility between WritableIoVec and iovec. | 34 // Ensure type compatibility between WritableIoVec and iovec. |
| 33 static_assert(sizeof(WritableIoVec) == sizeof(iovec), "WritableIoVec size"); | 35 static_assert(sizeof(WritableIoVec) == sizeof(iovec), "WritableIoVec size"); |
| 34 static_assert(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base), | 36 static_assert(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base), |
| 35 "WritableIoVec base offset"); | 37 "WritableIoVec base offset"); |
| 36 static_assert(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len), | 38 static_assert(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len), |
| 37 "WritableIoVec len offset"); | 39 "WritableIoVec len offset"); |
| 38 #endif // OS_POSIX | 40 #endif // OS_POSIX |
| 39 | 41 |
| 40 WeakFileHandleFileWriter::WeakFileHandleFileWriter(FileHandle file_handle) | 42 WeakFileHandleFileWriter::WeakFileHandleFileWriter(FileHandle file_handle) |
| 41 : file_handle_(file_handle) { | 43 : file_handle_(file_handle) { |
| 42 } | 44 } |
| 43 | 45 |
| 44 WeakFileHandleFileWriter::~WeakFileHandleFileWriter() { | 46 WeakFileHandleFileWriter::~WeakFileHandleFileWriter() { |
| 45 } | 47 } |
| 46 | 48 |
| 47 bool WeakFileHandleFileWriter::Write(const void* data, size_t size) { | 49 bool WeakFileHandleFileWriter::Write(const void* data, size_t size) { |
| 48 DCHECK_NE(file_handle_, kInvalidFileHandle); | 50 DCHECK_NE(file_handle_, kInvalidFileHandle); |
| 49 | 51 return LoggingWriteFile(file_handle_, data, size); |
| 50 // TODO(mark): Write no more than SSIZE_MAX bytes in a single call. | |
| 51 ssize_t written = WriteFile(file_handle_, data, size); | |
| 52 if (written < 0) { | |
| 53 PLOG(ERROR) << "write"; | |
| 54 return false; | |
| 55 } else if (written == 0) { | |
| 56 LOG(ERROR) << "write: returned 0"; | |
| 57 return false; | |
| 58 } | |
| 59 | |
| 60 return true; | |
| 61 } | 52 } |
| 62 | 53 |
| 63 bool WeakFileHandleFileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { | 54 bool WeakFileHandleFileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { |
| 64 DCHECK_NE(file_handle_, kInvalidFileHandle); | 55 DCHECK_NE(file_handle_, kInvalidFileHandle); |
| 65 | 56 |
| 66 #if defined(OS_POSIX) | 57 #if defined(OS_POSIX) |
| 67 | 58 |
| 68 ssize_t size = 0; | 59 ssize_t size = 0; |
| 69 for (const WritableIoVec& iov : *iovecs) { | 60 for (const WritableIoVec& iov : *iovecs) { |
| 70 // TODO(mark): Check to avoid overflow of ssize_t, and fail with EINVAL. | 61 // TODO(mark): Check to avoid overflow of ssize_t, and fail with EINVAL. |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 DCHECK(file_.is_valid()); | 169 DCHECK(file_.is_valid()); |
| 179 return weak_file_handle_file_writer_.WriteIoVec(iovecs); | 170 return weak_file_handle_file_writer_.WriteIoVec(iovecs); |
| 180 } | 171 } |
| 181 | 172 |
| 182 FileOffset FileWriter::Seek(FileOffset offset, int whence) { | 173 FileOffset FileWriter::Seek(FileOffset offset, int whence) { |
| 183 DCHECK(file_.is_valid()); | 174 DCHECK(file_.is_valid()); |
| 184 return weak_file_handle_file_writer_.Seek(offset, whence); | 175 return weak_file_handle_file_writer_.Seek(offset, whence); |
| 185 } | 176 } |
| 186 | 177 |
| 187 } // namespace crashpad | 178 } // namespace crashpad |
| OLD | NEW |