| 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 |
| 23 #if defined(OS_POSIX) |
| 24 #include <sys/uio.h> |
| 25 #include <unistd.h> |
| 22 #include "base/posix/eintr_wrapper.h" | 26 #include "base/posix/eintr_wrapper.h" |
| 23 #include "util/file/file_io.h" | 27 #endif // OS_POSIX |
| 24 | 28 |
| 25 namespace crashpad { | 29 namespace crashpad { |
| 26 | 30 |
| 31 #if defined(OS_POSIX) |
| 27 // Ensure type compatibility between WritableIoVec and iovec. | 32 // Ensure type compatibility between WritableIoVec and iovec. |
| 28 static_assert(sizeof(WritableIoVec) == sizeof(iovec), "WritableIoVec size"); | 33 static_assert(sizeof(WritableIoVec) == sizeof(iovec), "WritableIoVec size"); |
| 29 static_assert(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base), | 34 static_assert(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base), |
| 30 "WritableIoVec base offset"); | 35 "WritableIoVec base offset"); |
| 31 static_assert(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len), | 36 static_assert(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len), |
| 32 "WritableIoVec len offset"); | 37 "WritableIoVec len offset"); |
| 38 #endif // OS_POSIX |
| 33 | 39 |
| 34 FileWriter::FileWriter() : fd_() { | 40 FileWriter::FileWriter() : file_() { |
| 35 } | 41 } |
| 36 | 42 |
| 37 FileWriter::~FileWriter() { | 43 FileWriter::~FileWriter() { |
| 38 } | 44 } |
| 39 | 45 |
| 40 bool FileWriter::Open(const base::FilePath& path, int oflag, mode_t mode) { | 46 bool FileWriter::Open(const base::FilePath& path, |
| 41 CHECK(!fd_.is_valid()); | 47 FileWriteMode write_mode, |
| 42 | 48 bool world_readable) { |
| 43 DCHECK((oflag & O_WRONLY) || (oflag & O_RDWR)); | 49 CHECK(!file_.is_valid()); |
| 44 | 50 file_.reset(LoggingOpenFileForWrite(path, write_mode, world_readable)); |
| 45 fd_.reset(HANDLE_EINTR(open(path.value().c_str(), oflag, mode))); | 51 return file_.is_valid(); |
| 46 if (!fd_.is_valid()) { | |
| 47 PLOG(ERROR) << "open " << path.value(); | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 return true; | |
| 52 } | 52 } |
| 53 | 53 |
| 54 void FileWriter::Close() { | 54 void FileWriter::Close() { |
| 55 CHECK(fd_.is_valid()); | 55 CHECK(file_.is_valid()); |
| 56 | 56 |
| 57 fd_.reset(); | 57 file_.reset(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 bool FileWriter::Write(const void* data, size_t size) { | 60 bool FileWriter::Write(const void* data, size_t size) { |
| 61 DCHECK(fd_.is_valid()); | 61 DCHECK(file_.is_valid()); |
| 62 | 62 |
| 63 // TODO(mark): Write no more than SSIZE_MAX bytes in a single call. | 63 // TODO(mark): Write no more than SSIZE_MAX bytes in a single call. |
| 64 ssize_t written = WriteFile(fd_.get(), data, size); | 64 ssize_t written = WriteFile(file_.get(), data, size); |
| 65 if (written < 0) { | 65 if (written < 0) { |
| 66 PLOG(ERROR) << "write"; | 66 PLOG(ERROR) << "write"; |
| 67 return false; | 67 return false; |
| 68 } else if (written == 0) { | 68 } else if (written == 0) { |
| 69 LOG(ERROR) << "write: returned 0"; | 69 LOG(ERROR) << "write: returned 0"; |
| 70 return false; | 70 return false; |
| 71 } | 71 } |
| 72 | 72 |
| 73 return true; | 73 return true; |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool FileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { | 76 bool FileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { |
| 77 DCHECK(fd_.is_valid()); | 77 DCHECK(file_.is_valid()); |
| 78 |
| 79 #if defined(OS_POSIX) |
| 78 | 80 |
| 79 ssize_t size = 0; | 81 ssize_t size = 0; |
| 80 for (const WritableIoVec& iov : *iovecs) { | 82 for (const WritableIoVec& iov : *iovecs) { |
| 81 // TODO(mark): Check to avoid overflow of ssize_t, and fail with EINVAL. | 83 // TODO(mark): Check to avoid overflow of ssize_t, and fail with EINVAL. |
| 82 size += iov.iov_len; | 84 size += iov.iov_len; |
| 83 } | 85 } |
| 84 | 86 |
| 85 // Get an iovec*, because that’s what writev wants. The only difference | 87 // Get an iovec*, because that’s what writev wants. The only difference |
| 86 // between WritableIoVec and iovec is that WritableIoVec’s iov_base is a | 88 // between WritableIoVec and iovec is that WritableIoVec’s iov_base is a |
| 87 // pointer to a const buffer, where iovec’s iov_base isn’t. writev doesn’t | 89 // pointer to a const buffer, where iovec’s iov_base isn’t. writev doesn’t |
| 88 // actually write to the data, so this cast is safe here. iovec’s iov_base is | 90 // actually write to the data, so this cast is safe here. iovec’s iov_base is |
| 89 // non-const because the same structure is used for readv and writev, and | 91 // non-const because the same structure is used for readv and writev, and |
| 90 // readv needs to write to the buffer that iov_base points to. | 92 // readv needs to write to the buffer that iov_base points to. |
| 91 iovec* iov = reinterpret_cast<iovec*>(&(*iovecs)[0]); | 93 iovec* iov = reinterpret_cast<iovec*>(&(*iovecs)[0]); |
| 92 size_t remaining_iovecs = iovecs->size(); | 94 size_t remaining_iovecs = iovecs->size(); |
| 93 | 95 |
| 94 while (size > 0) { | 96 while (size > 0) { |
| 95 size_t writev_iovec_count = | 97 size_t writev_iovec_count = |
| 96 std::min(remaining_iovecs, implicit_cast<size_t>(IOV_MAX)); | 98 std::min(remaining_iovecs, implicit_cast<size_t>(IOV_MAX)); |
| 97 ssize_t written = HANDLE_EINTR(writev(fd_.get(), iov, writev_iovec_count)); | 99 ssize_t written = |
| 100 HANDLE_EINTR(writev(file_.get(), iov, writev_iovec_count)); |
| 98 if (written < 0) { | 101 if (written < 0) { |
| 99 PLOG(ERROR) << "writev"; | 102 PLOG(ERROR) << "writev"; |
| 100 return false; | 103 return false; |
| 101 } else if (written == 0) { | 104 } else if (written == 0) { |
| 102 LOG(ERROR) << "writev: returned 0"; | 105 LOG(ERROR) << "writev: returned 0"; |
| 103 return false; | 106 return false; |
| 104 } | 107 } |
| 105 | 108 |
| 106 size -= written; | 109 size -= written; |
| 107 DCHECK_GE(size, 0); | 110 DCHECK_GE(size, 0); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 121 iov->iov_len -= wrote_this_iovec; | 124 iov->iov_len -= wrote_this_iovec; |
| 122 } else { | 125 } else { |
| 123 ++iov; | 126 ++iov; |
| 124 --remaining_iovecs; | 127 --remaining_iovecs; |
| 125 } | 128 } |
| 126 } | 129 } |
| 127 } | 130 } |
| 128 | 131 |
| 129 DCHECK_EQ(remaining_iovecs, 0u); | 132 DCHECK_EQ(remaining_iovecs, 0u); |
| 130 | 133 |
| 134 #else // !OS_POSIX |
| 135 |
| 136 for (const WritableIoVec& iov : *iovecs) { |
| 137 if (!Write(iov.iov_base, iov.iov_len)) |
| 138 return false; |
| 139 } |
| 140 |
| 141 #endif // OS_POSIX |
| 142 |
| 131 #ifndef NDEBUG | 143 #ifndef NDEBUG |
| 132 // The interface says that |iovecs| is not sacred, so scramble it to make sure | 144 // The interface says that |iovecs| is not sacred, so scramble it to make sure |
| 133 // that nobody depends on it. | 145 // that nobody depends on it. |
| 134 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size()); | 146 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size()); |
| 135 #endif | 147 #endif |
| 136 | 148 |
| 137 return true; | 149 return true; |
| 138 } | 150 } |
| 139 | 151 |
| 140 off_t FileWriter::Seek(off_t offset, int whence) { | 152 FileOffset FileWriter::Seek(FileOffset offset, int whence) { |
| 141 DCHECK(fd_.is_valid()); | 153 DCHECK(file_.is_valid()); |
| 142 | 154 return LoggingSeekFile(file_.get(), offset, whence); |
| 143 off_t rv = lseek(fd_.get(), offset, whence); | |
| 144 if (rv < 0) { | |
| 145 PLOG(ERROR) << "lseek"; | |
| 146 } | |
| 147 | |
| 148 return rv; | |
| 149 } | 155 } |
| 150 | 156 |
| 151 } // namespace crashpad | 157 } // namespace crashpad |
| OLD | NEW |