Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: util/file/file_writer.cc

Issue 432003005: Introduce MinidumpWritable, its dependencies, and their tests (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address review comments Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/file/file_writer.h ('k') | util/file/string_file_writer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/file/file_writer.h"
16
17 #include <algorithm>
18
19 #include "base/logging.h"
20 #include "base/posix/eintr_wrapper.h"
21 #include "util/file/fd_io.h"
22
23 namespace crashpad {
24
25 // Ensure type compatibility between WritableIoVec and iovec.
26 COMPILE_ASSERT(sizeof(WritableIoVec) == sizeof(iovec), WritableIoVec_size);
27 COMPILE_ASSERT(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base),
28 WritableIoVec_base_offset);
29 COMPILE_ASSERT(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len),
30 WritableIoVec_len_offset);
31
32 FileWriter::FileWriter() : fd_() {
33 }
34
35 FileWriter::~FileWriter() {
36 }
37
38 bool FileWriter::Open(const base::FilePath& path, int oflag, mode_t mode) {
39 CHECK(!fd_.is_valid());
40
41 DCHECK((oflag & O_WRONLY) || (oflag & O_RDWR));
42
43 fd_.reset(HANDLE_EINTR(open(path.value().c_str(), oflag, mode)));
44 if (!fd_.is_valid()) {
45 PLOG(ERROR) << "open " << path.value();
46 return false;
47 }
48
49 return true;
50 }
51
52 void FileWriter::Close() {
53 CHECK(fd_.is_valid());
54
55 fd_.reset();
56 }
57
58 bool FileWriter::Write(const void* data, size_t size) {
59 DCHECK(fd_.is_valid());
60
61 // TODO(mark): Write no more than SSIZE_MAX bytes in a single call.
62 ssize_t written = WriteFD(fd_.get(), data, size);
63 if (written < 0) {
64 PLOG(ERROR) << "write";
65 return false;
66 } else if (written == 0) {
67 LOG(ERROR) << "write: returned 0";
68 return false;
69 }
70
71 return true;
72 }
73
74 bool FileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) {
75 DCHECK(fd_.is_valid());
76
77 ssize_t size = 0;
78 for (const WritableIoVec& iov : *iovecs) {
79 // TODO(mark): Check to avoid overflow of ssize_t, and fail with EINVAL.
80 size += iov.iov_len;
81 }
82
83 // Get an iovec*, because that’s what writev wants. The only difference
84 // between WritableIoVec and iovec is that WritableIoVec’s iov_base is a
85 // pointer to a const buffer, where iovec’s iov_base isn’t. writev doesn’t
86 // actually write to the data, so this cast is safe here. iovec’s iov_base is
87 // non-const because the same structure is used for readv and writev, and
88 // readv needs to write to the buffer that iov_base points to.
89 iovec* iov = reinterpret_cast<iovec*>(&(*iovecs)[0]);
90 size_t remaining_iovecs = iovecs->size();
91
92 while (size > 0) {
93 size_t writev_iovec_count =
94 std::min(remaining_iovecs, static_cast<size_t>(IOV_MAX));
95 ssize_t written = HANDLE_EINTR(writev(fd_.get(), iov, writev_iovec_count));
96 if (written < 0) {
97 PLOG(ERROR) << "writev";
98 return false;
99 } else if (written == 0) {
100 LOG(ERROR) << "writev: returned 0";
101 return false;
102 }
103
104 size -= written;
105 DCHECK_GE(size, 0);
106
107 if (size == 0) {
108 remaining_iovecs = 0;
109 break;
110 }
111
112 while (written > 0) {
113 size_t wrote_this_iovec =
114 std::min(static_cast<size_t>(written), iov->iov_len);
115 written -= wrote_this_iovec;
116 if (wrote_this_iovec < iov->iov_len) {
117 iov->iov_base =
118 reinterpret_cast<char*>(iov->iov_base) + wrote_this_iovec;
119 iov->iov_len -= wrote_this_iovec;
120 } else {
121 ++iov;
122 --remaining_iovecs;
123 }
124 }
125 }
126
127 DCHECK_EQ(remaining_iovecs, 0u);
128
129 #ifndef NDEBUG
130 // The interface says that |iovecs| is not sacred, so scramble it to make sure
131 // that nobody depends on it.
132 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size());
133 #endif
134
135 return true;
136 }
137
138 off_t FileWriter::Seek(off_t offset, int whence) {
139 DCHECK(fd_.is_valid());
140
141 off_t rv = lseek(fd_.get(), offset, whence);
142 if (rv < 0) {
143 PLOG(ERROR) << "lseek";
144 }
145
146 return rv;
147 }
148
149 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/file/file_writer.h ('k') | util/file/string_file_writer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698