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

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

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

Powered by Google App Engine
This is Rietveld 408576698