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

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

Issue 812403002: win: Add open equivalent (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@scoped-handle
Patch Set: Created 6 years 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/file_writer_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
23 #include "util/file/file_io.h" 22 #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 FileWriter::FileWriter() : file_() {
28 static_assert(sizeof(WritableIoVec) == sizeof(iovec), "WritableIoVec size");
29 static_assert(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base),
30 "WritableIoVec base offset");
31 static_assert(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len),
32 "WritableIoVec len offset");
33
34 FileWriter::FileWriter() : fd_() {
35 } 27 }
36 28
37 FileWriter::~FileWriter() { 29 FileWriter::~FileWriter() {
38 } 30 }
39 31
40 bool FileWriter::Open(const base::FilePath& path, int oflag, mode_t mode) { 32 void FileWriter::Close() {
41 CHECK(!fd_.is_valid()); 33 CHECK(file_.is_valid());
42 34
43 DCHECK((oflag & O_WRONLY) || (oflag & O_RDWR)); 35 file_.reset();
44
45 fd_.reset(HANDLE_EINTR(open(path.value().c_str(), oflag, mode)));
46 if (!fd_.is_valid()) {
47 PLOG(ERROR) << "open " << path.value();
48 return false;
49 }
50
51 return true;
52 }
53
54 void FileWriter::Close() {
55 CHECK(fd_.is_valid());
56
57 fd_.reset();
58 } 36 }
59 37
60 bool FileWriter::Write(const void* data, size_t size) { 38 bool FileWriter::Write(const void* data, size_t size) {
61 DCHECK(fd_.is_valid()); 39 DCHECK(file_.is_valid());
62 40
63 // TODO(mark): Write no more than SSIZE_MAX bytes in a single call. 41 // TODO(mark): Write no more than SSIZE_MAX bytes in a single call.
64 ssize_t written = WriteFile(fd_.get(), data, size); 42 ssize_t written = WriteFile(file_.get(), data, size);
65 if (written < 0) { 43 if (written < 0) {
66 PLOG(ERROR) << "write"; 44 PLOG(ERROR) << "write";
67 return false; 45 return false;
68 } else if (written == 0) { 46 } else if (written == 0) {
69 LOG(ERROR) << "write: returned 0"; 47 LOG(ERROR) << "write: returned 0";
70 return false; 48 return false;
71 } 49 }
72 50
73 return true; 51 return true;
74 } 52 }
75 53
76 bool FileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) {
77 DCHECK(fd_.is_valid());
78
79 ssize_t size = 0;
80 for (const WritableIoVec& iov : *iovecs) {
81 // TODO(mark): Check to avoid overflow of ssize_t, and fail with EINVAL.
82 size += iov.iov_len;
83 }
84
85 // 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
87 // 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
89 // 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.
91 iovec* iov = reinterpret_cast<iovec*>(&(*iovecs)[0]);
92 size_t remaining_iovecs = iovecs->size();
93
94 while (size > 0) {
95 size_t writev_iovec_count =
96 std::min(remaining_iovecs, implicit_cast<size_t>(IOV_MAX));
97 ssize_t written = HANDLE_EINTR(writev(fd_.get(), iov, writev_iovec_count));
98 if (written < 0) {
99 PLOG(ERROR) << "writev";
100 return false;
101 } else if (written == 0) {
102 LOG(ERROR) << "writev: returned 0";
103 return false;
104 }
105
106 size -= written;
107 DCHECK_GE(size, 0);
108
109 if (size == 0) {
110 remaining_iovecs = 0;
111 break;
112 }
113
114 while (written > 0) {
115 size_t wrote_this_iovec =
116 std::min(implicit_cast<size_t>(written), iov->iov_len);
117 written -= wrote_this_iovec;
118 if (wrote_this_iovec < iov->iov_len) {
119 iov->iov_base =
120 reinterpret_cast<char*>(iov->iov_base) + wrote_this_iovec;
121 iov->iov_len -= wrote_this_iovec;
122 } else {
123 ++iov;
124 --remaining_iovecs;
125 }
126 }
127 }
128
129 DCHECK_EQ(remaining_iovecs, 0u);
130
131 #ifndef NDEBUG
132 // The interface says that |iovecs| is not sacred, so scramble it to make sure
133 // that nobody depends on it.
134 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size());
135 #endif
136
137 return true;
138 }
139
140 off_t FileWriter::Seek(off_t offset, int whence) {
141 DCHECK(fd_.is_valid());
142
143 off_t rv = lseek(fd_.get(), offset, whence);
144 if (rv < 0) {
145 PLOG(ERROR) << "lseek";
146 }
147
148 return rv;
149 }
150
151 } // namespace crashpad 54 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/file/file_writer.h ('k') | util/file/file_writer_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698