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/posix/eintr_wrapper.h" | 22 #include "base/posix/eintr_wrapper.h" |
23 #include "util/file/file_io.h" | |
24 | 23 |
25 namespace crashpad { | 24 namespace crashpad { |
26 | 25 |
27 // Ensure type compatibility between WritableIoVec and iovec. | 26 // Ensure type compatibility between WritableIoVec and iovec. |
28 static_assert(sizeof(WritableIoVec) == sizeof(iovec), "WritableIoVec size"); | 27 static_assert(sizeof(WritableIoVec) == sizeof(iovec), "WritableIoVec size"); |
29 static_assert(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base), | 28 static_assert(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base), |
30 "WritableIoVec base offset"); | 29 "WritableIoVec base offset"); |
31 static_assert(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len), | 30 static_assert(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len), |
32 "WritableIoVec len offset"); | 31 "WritableIoVec len offset"); |
33 | 32 |
34 FileWriter::FileWriter() : fd_() { | 33 bool FileWriter::Open(const base::FilePath& path, int oflag, uint32_t mode) { |
35 } | 34 CHECK(!file.is_valid()); |
36 | |
37 FileWriter::~FileWriter() { | |
38 } | |
39 | |
40 bool FileWriter::Open(const base::FilePath& path, int oflag, mode_t mode) { | |
41 CHECK(!fd_.is_valid()); | |
42 | 35 |
43 DCHECK((oflag & O_WRONLY) || (oflag & O_RDWR)); | 36 DCHECK((oflag & O_WRONLY) || (oflag & O_RDWR)); |
44 | 37 |
45 fd_.reset(HANDLE_EINTR(open(path.value().c_str(), oflag, mode))); | 38 file.reset(HANDLE_EINTR(open(path.value().c_str(), oflag, mode))); |
46 if (!fd_.is_valid()) { | 39 if (!file.is_valid()) { |
47 PLOG(ERROR) << "open " << path.value(); | 40 PLOG(ERROR) << "open " << path.value(); |
48 return false; | 41 return false; |
49 } | 42 } |
50 | 43 |
51 return true; | 44 return true; |
52 } | 45 } |
53 | 46 |
54 void FileWriter::Close() { | |
55 CHECK(fd_.is_valid()); | |
56 | |
57 fd_.reset(); | |
58 } | |
59 | |
60 bool FileWriter::Write(const void* data, size_t size) { | |
61 DCHECK(fd_.is_valid()); | |
62 | |
63 // TODO(mark): Write no more than SSIZE_MAX bytes in a single call. | |
64 ssize_t written = WriteFile(fd_.get(), data, size); | |
65 if (written < 0) { | |
66 PLOG(ERROR) << "write"; | |
67 return false; | |
68 } else if (written == 0) { | |
69 LOG(ERROR) << "write: returned 0"; | |
70 return false; | |
71 } | |
72 | |
73 return true; | |
74 } | |
75 | |
76 bool FileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { | 47 bool FileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { |
77 DCHECK(fd_.is_valid()); | 48 DCHECK(fd_.is_valid()); |
78 | 49 |
79 ssize_t size = 0; | 50 ssize_t size = 0; |
80 for (const WritableIoVec& iov : *iovecs) { | 51 for (const WritableIoVec& iov : *iovecs) { |
81 // TODO(mark): Check to avoid overflow of ssize_t, and fail with EINVAL. | 52 // TODO(mark): Check to avoid overflow of ssize_t, and fail with EINVAL. |
82 size += iov.iov_len; | 53 size += iov.iov_len; |
83 } | 54 } |
84 | 55 |
85 // Get an iovec*, because that’s what writev wants. The only difference | 56 // Get an iovec*, because that’s what writev wants. The only difference |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 101 |
131 #ifndef NDEBUG | 102 #ifndef NDEBUG |
132 // The interface says that |iovecs| is not sacred, so scramble it to make sure | 103 // The interface says that |iovecs| is not sacred, so scramble it to make sure |
133 // that nobody depends on it. | 104 // that nobody depends on it. |
134 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size()); | 105 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size()); |
135 #endif | 106 #endif |
136 | 107 |
137 return true; | 108 return true; |
138 } | 109 } |
139 | 110 |
140 off_t FileWriter::Seek(off_t offset, int whence) { | 111 FileOffset FileWriter::Seek(FileOffset offset, int whence) { |
141 DCHECK(fd_.is_valid()); | 112 DCHECK(file_.is_valid()); |
142 | 113 |
143 off_t rv = lseek(fd_.get(), offset, whence); | 114 off_t rv = lseek(file_.get(), offset, whence); |
144 if (rv < 0) { | 115 if (rv < 0) { |
145 PLOG(ERROR) << "lseek"; | 116 PLOG(ERROR) << "lseek"; |
146 } | 117 } |
147 | 118 |
148 return rv; | 119 return rv; |
149 } | 120 } |
150 | 121 |
151 } // namespace crashpad | 122 } // namespace crashpad |
OLD | NEW |