| 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 <limits.h> | 17 #include <limits.h> |
| 18 #include <stddef.h> |
| 18 #include <string.h> | 19 #include <string.h> |
| 19 | 20 |
| 20 #include <algorithm> | 21 #include <algorithm> |
| 21 | 22 |
| 22 #include "base/logging.h" | 23 #include "base/logging.h" |
| 23 #include "base/numerics/safe_conversions.h" | 24 #include "base/numerics/safe_conversions.h" |
| 24 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| 25 #include "util/misc/implicit_cast.h" | 26 #include "util/misc/implicit_cast.h" |
| 26 | 27 |
| 27 #if defined(OS_POSIX) | 28 #if defined(OS_POSIX) |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 bool FileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { | 186 bool FileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { |
| 186 DCHECK(file_.is_valid()); | 187 DCHECK(file_.is_valid()); |
| 187 return weak_file_handle_file_writer_.WriteIoVec(iovecs); | 188 return weak_file_handle_file_writer_.WriteIoVec(iovecs); |
| 188 } | 189 } |
| 189 | 190 |
| 190 FileOffset FileWriter::Seek(FileOffset offset, int whence) { | 191 FileOffset FileWriter::Seek(FileOffset offset, int whence) { |
| 191 DCHECK(file_.is_valid()); | 192 DCHECK(file_.is_valid()); |
| 192 return weak_file_handle_file_writer_.Seek(offset, whence); | 193 return weak_file_handle_file_writer_.Seek(offset, whence); |
| 193 } | 194 } |
| 194 | 195 |
| 195 WeakStdioFileWriter::WeakStdioFileWriter(FILE* file) | |
| 196 : file_(file) { | |
| 197 } | |
| 198 | |
| 199 WeakStdioFileWriter::~WeakStdioFileWriter() { | |
| 200 } | |
| 201 | |
| 202 bool WeakStdioFileWriter::Write(const void* data, size_t size) { | |
| 203 DCHECK(file_); | |
| 204 | |
| 205 size_t rv = fwrite(data, 1, size, file_); | |
| 206 if (rv != size) { | |
| 207 if (ferror(file_)) { | |
| 208 STDIO_PLOG(ERROR) << "fwrite"; | |
| 209 } else { | |
| 210 LOG(ERROR) << "fwrite: expected " << size << ", observed " << rv; | |
| 211 } | |
| 212 return false; | |
| 213 } | |
| 214 | |
| 215 return true; | |
| 216 } | |
| 217 | |
| 218 bool WeakStdioFileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { | |
| 219 DCHECK(file_); | |
| 220 | |
| 221 if (iovecs->empty()) { | |
| 222 LOG(ERROR) << "WriteIoVec(): no iovecs"; | |
| 223 return false; | |
| 224 } | |
| 225 | |
| 226 for (const WritableIoVec& iov : *iovecs) { | |
| 227 if (!Write(iov.iov_base, iov.iov_len)) { | |
| 228 return false; | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 #ifndef NDEBUG | |
| 233 // The interface says that |iovecs| is not sacred, so scramble it to make sure | |
| 234 // that nobody depends on it. | |
| 235 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size()); | |
| 236 #endif | |
| 237 | |
| 238 return true; | |
| 239 } | |
| 240 | |
| 241 FileOffset WeakStdioFileWriter::Seek(FileOffset offset, int whence) { | |
| 242 DCHECK(file_); | |
| 243 if (fseeko(file_, offset, whence) == -1) { | |
| 244 STDIO_PLOG(ERROR) << "fseeko"; | |
| 245 return -1; | |
| 246 } | |
| 247 | |
| 248 FileOffset new_offset = ftello(file_); | |
| 249 if (new_offset == -1) { | |
| 250 STDIO_PLOG(ERROR) << "ftello"; | |
| 251 return -1; | |
| 252 } | |
| 253 | |
| 254 return new_offset; | |
| 255 } | |
| 256 | |
| 257 } // namespace crashpad | 196 } // namespace crashpad |
| OLD | NEW |