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

Side by Side Diff: chrome/utility/image_writer/image_writer_mac.cc

Issue 294163008: Adds USB writing for OS X. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@list-devices
Patch Set: Fixes windows impl to new function signatures. Created 6 years, 6 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
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/utility/image_writer/image_writer_mac.h"
6
7 #include <sys/socket.h>
8 #include <IOKit/storage/IOStorageProtocolCharacteristics.h>
9
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/message_loop/message_pump_mac.h"
12 #include "base/posix/eintr_wrapper.h"
13 #include "chrome/utility/image_writer/error_messages.h"
14 #include "chrome/utility/image_writer/image_writer.h"
15
16 namespace image_writer {
17
18 DiskUnmounter::DiskUnmounter(base::WeakPtr<ImageWriter> image_writer)
19 : image_writer_(image_writer),
20 original_thread_(base::MessageLoopProxy::current()),
21 cf_thread_("ImageWriterDiskArb") {
22 }
23
24 DiskUnmounter::~DiskUnmounter() {
25 if (disk_)
26 DADiskUnclaim(disk_);
27 }
28
29 void DiskUnmounter::Unmount(const std::string& device_path,
30 const base::Closure& continuation) {
31 unmount_continuation_closure_ = continuation;
32
33 base::Thread::Options options;
34 options.message_pump_factory = base::Bind(&CreateMessagePump);
35
36 cf_thread_.StartWithOptions(options);
37
38 cf_thread_.message_loop()->PostTask(
39 FROM_HERE,
40 base::Bind(&DiskUnmounter::UnmountOnWorker, AsWeakPtr(), device_path));
41 }
42
43 // static
44 void DiskUnmounter::DiskClaimed(DADiskRef disk,
45 DADissenterRef dissenter,
46 void* context) {
47 DiskUnmounter* disk_unmounter = static_cast<DiskUnmounter*>(context);
48
49 if (dissenter) {
50 LOG(ERROR) << "Unable to claim disk.";
51 disk_unmounter->Error(error::kUnmountVolumes);
52 return;
53 }
54
55 DADiskUnmount(disk,
56 kDADiskUnmountOptionForce | kDADiskUnmountOptionWhole,
57 DiskUnmounted,
58 disk_unmounter);
59 }
60
61 // static
62 DADissenterRef DiskUnmounter::DiskClaimRevoked(DADiskRef disk, void* context) {
63 CFStringRef reason = CFSTR(
Robert Sesek 2014/06/05 19:49:48 Will this message ever be shown to the user? If so
Drew Haven 2014/06/05 22:28:28 I don't think so. I actually just copied out what
64 "Hi. Sorry to bother you, but I'm busy overwriting the entire disk "
65 "here. There's nothing to claim but the smoldering ruins of bytes "
66 "that were in flash memory. Trust me, it's nothing that you want. "
67 "All the best. Toodles!");
68 return DADissenterCreate(kCFAllocatorDefault, kDAReturnBusy, reason);
69 }
70
71 // static
72 void DiskUnmounter::DiskUnmounted(DADiskRef disk,
73 DADissenterRef dissenter,
74 void* context) {
75 DiskUnmounter* disk_unmounter = static_cast<DiskUnmounter*>(context);
76
77 if (dissenter) {
78 LOG(ERROR) << "Unable to unmount disk.";
79 disk_unmounter->Error(error::kUnmountVolumes);
80 return;
81 }
82
83 disk_unmounter->original_thread_->PostTask(
84 FROM_HERE, disk_unmounter->unmount_continuation_closure_);
85 }
86
87 void DiskUnmounter::UnmountOnWorker(const std::string& device_path) {
Robert Sesek 2014/06/05 19:49:48 DCHECK(cf_thread_.message_loop() == base::MessageL
Drew Haven 2014/06/05 22:28:27 That's reasonable. It's a private method with a p
88 session_.reset(DASessionCreate(NULL));
89
90 DASessionScheduleWithRunLoop(
91 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
92
93 disk_.reset(DADiskCreateFromBSDName(
94 kCFAllocatorDefault, session_, device_path.c_str()));
95
96 if (!disk_) {
97 LOG(ERROR) << "Unable to get disk reference.";
98 Error(error::kUnmountVolumes);
99 return;
100 }
101
102 DADiskClaim(disk_,
103 kDADiskClaimOptionDefault,
104 DiskClaimRevoked,
105 this,
106 DiskClaimed,
107 this);
108 }
109
110 // static
111 scoped_ptr<base::MessagePump> DiskUnmounter::CreateMessagePump() {
112 return scoped_ptr<base::MessagePump>(new base::MessagePumpCFRunLoop);
113 }
114
115 void DiskUnmounter::Error(const std::string& message) {
116 original_thread_->PostTask(
117 FROM_HERE, base::Bind(&ImageWriter::Error, image_writer_, message));
118 }
119
120 bool ImageWriter::IsValidDevice() {
Robert Sesek 2014/06/05 19:49:48 I've seen this function before?
Drew Haven 2014/06/05 22:28:27 Yes! In fact I wanted to ask you about that. I'm
Robert Sesek 2014/06/06 18:34:06 //{chrome,content}/common is where files shared by
Drew Haven 2014/06/10 01:13:25 I ended up adding a bunch of files over there so I
121 base::ScopedCFTypeRef<DASessionRef> session(DASessionCreate(NULL));
122 DADiskRef disk = DADiskCreateFromBSDName(
123 kCFAllocatorDefault, session, device_path_.value().c_str());
124
125 if (!disk)
126 return false;
127
128 base::ScopedCFTypeRef<CFDictionaryRef> dict(DADiskCopyDescription(disk));
129
130 CFBooleanRef internal = base::mac::GetValueFromDictionary<CFBooleanRef>(
131 dict, kDADiskDescriptionDeviceInternalKey);
132 CFStringRef protocol = base::mac::GetValueFromDictionary<CFStringRef>(
133 dict, kDADiskDescriptionDeviceProtocolKey);
134 CFStringRef io_reg_path = base::mac::GetValueFromDictionary<CFStringRef>(
135 dict, kDADiskDescriptionDevicePathKey);
136 CFBooleanRef ejectable = base::mac::GetValueFromDictionary<CFBooleanRef>(
137 dict, kDADiskDescriptionMediaEjectableKey);
138 CFBooleanRef removable = base::mac::GetValueFromDictionary<CFBooleanRef>(
139 dict, kDADiskDescriptionMediaRemovableKey);
140 CFBooleanRef whole = base::mac::GetValueFromDictionary<CFBooleanRef>(
141 dict, kDADiskDescriptionMediaWholeKey);
142 CFStringRef kind = base::mac::GetValueFromDictionary<CFStringRef>(
143 dict, kDADiskDescriptionMediaKindKey);
144
145 // A drive is a USB stick iff:
146 // - it is not internal
147 // - it is attached to the USB bus
148 // - it is ejectable (because it will be ejected after written to)
149 // - it is removable
150 // - it is the whole drive (although the use of
151 // kDADiskDescriptionMatchMediaWhole should have ensured this)
152 // - it is of type IOMedia (external DVD drives and the like are IOCDMedia
153 // or
154 // IODVDMedia)
155 bool is_usb_stick =
156 !CFBooleanGetValue(internal) &&
157 CFEqual(protocol, CFSTR(kIOPropertyPhysicalInterconnectTypeUSB)) &&
158 CFBooleanGetValue(ejectable) && CFBooleanGetValue(removable) &&
159 CFBooleanGetValue(whole) &&
160 CFStringCompare(kind, CFSTR("IOMedia"), 0) == kCFCompareEqualTo;
161
162 // A drive is an SD card iff:
163 // - it is attached to the USB bus
164 // - it is ejectable (because it will be ejected after written to)
165 // - it is removable
166 // - it is the whole drive (although the use of
167 // kDADiskDescriptionMatchMediaWhole should have ensured this)
168 // - it is of type IOMedia (external DVD drives and the like are IOCDMedia
169 // or
170 // IODVDMedia)
171 // - the IORegistry device path contains "AppleUSBCardReader"
172 bool is_sd_card =
173 CFEqual(protocol, CFSTR(kIOPropertyPhysicalInterconnectTypeUSB)) &&
174 CFBooleanGetValue(ejectable) && CFBooleanGetValue(removable) &&
175 CFBooleanGetValue(whole) &&
176 CFStringCompare(kind, CFSTR("IOMedia"), 0) == kCFCompareEqualTo &&
177 CFStringFind(io_reg_path, CFSTR("AppleUSBCardReader"), 0).location !=
178 kCFNotFound;
179
180 return is_usb_stick || is_sd_card;
181 }
182
183 void ImageWriter::UnmountVolumes(const base::Closure& continuation) {
Robert Sesek 2014/06/05 19:49:48 These are for a different class…?
Drew Haven 2014/06/05 22:28:27 It's like what I did for the RemovableStorageProvi
Robert Sesek 2014/06/06 18:34:06 Ah, I see. Yes, I'd not have an image_writer_mac.h
Drew Haven 2014/06/10 01:13:25 Done.
184 if (unmounter_ == NULL) {
185 unmounter_.reset(new DiskUnmounter(AsWeakPtr()));
186 }
187
188 unmounter_->Unmount(device_path_.value(), continuation);
189 }
190
191 bool ImageWriter::OpenDevice() {
192 int sockets[2]; // [parent's end, child's end]
193 int result = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
194 if (result == -1) {
195 LOG(ERROR) << "Unable to allocate socket pair.";
196 return false;
197 }
198
199 char rdwr[10];
200 snprintf(rdwr, sizeof(rdwr), "%d", O_RDWR);
201
202 pid_t childPid = fork();
203 if (childPid == -1) {
204 LOG(ERROR) << "Fork failed.";
205 return false;
206 }
207
208 if (childPid == 0) { // child
209 HANDLE_EINTR(dup2(sockets[1], STDOUT_FILENO));
210 close(sockets[0]);
211 close(sockets[1]);
212
213 base::FilePath real_device_path;
214 if (device_path_.IsAbsolute()) {
215 real_device_path = device_path_;
216 } else {
217 real_device_path = base::FilePath("/dev").Append(device_path_);
218 }
219
220 const char authopenPath[] = "/usr/libexec/authopen";
221 execl(authopenPath,
222 authopenPath,
223 "-stdoutpipe",
224 "-o",
225 rdwr,
226 real_device_path.value().c_str(),
227 NULL);
228 _exit(errno);
229 } else { // parent
230 close(sockets[1]);
231 int fd = -1;
232
233 msghdr message = {0};
234 const size_t kDataBufferSize = 1024;
235 char dataBuffer[kDataBufferSize];
236 iovec ioVec[1];
237 ioVec[0].iov_base = dataBuffer;
238 ioVec[0].iov_len = kDataBufferSize;
239 message.msg_iov = ioVec;
240 message.msg_iovlen = 1;
241 const socklen_t kCmsgSocketSize = (socklen_t)CMSG_SPACE(sizeof(int));
242 char cmsgSocket[kCmsgSocketSize];
243 message.msg_control = cmsgSocket;
244 message.msg_controllen = kCmsgSocketSize;
245 ssize_t size = HANDLE_EINTR(recvmsg(sockets[0], &message, 0));
246 if (size > 0) {
247 cmsghdr* cmsgSocketHeader = CMSG_FIRSTHDR(&message);
248 // Paranoia.
249 if (cmsgSocketHeader && cmsgSocketHeader->cmsg_level == SOL_SOCKET &&
250 cmsgSocketHeader->cmsg_type == SCM_RIGHTS)
251 fd = *((int*)CMSG_DATA(cmsgSocketHeader));
252 }
253
254 int childStat;
255 result = HANDLE_EINTR(waitpid(childPid, &childStat, 0));
256 close(sockets[0]);
257
258 if (result != -1 && WIFEXITED(childStat)) {
259 int exitStatus = WEXITSTATUS(childStat);
260 if (exitStatus) {
261 LOG(ERROR) << "Child process returned failure.";
262 return false;
263 }
264 }
265
266 device_file_ = base::File(fd);
267
268 return device_file_.IsValid();
269 }
270 }
271
272 } // namespace image_writer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698