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

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: Minor updates. 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 <sys/socket.h>
Robert Sesek 2014/06/16 19:32:54 nit: s > I (assuming capitalization doesn't matter
Drew Haven 2014/06/16 21:51:39 Done. I don't think the 'git cl upload' presubmit
6 #include <IOKit/storage/IOStorageProtocolCharacteristics.h>
7
8 #include "base/command_line.h"
9 #include "base/files/scoped_file.h"
10 #include "base/posix/eintr_wrapper.h"
11 #include "base/process/kill.h"
12 #include "base/process/launch.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/utility/image_writer/disk_unmounter_mac.h"
15 #include "chrome/utility/image_writer/error_messages.h"
16 #include "chrome/utility/image_writer/image_writer.h"
17
18 namespace image_writer {
19
20 static const char kAuthOpenPath[] = "/usr/libexec/authopen";
21 static const size_t kDataBufferSize = 1024;
22
23 bool ImageWriter::IsValidDevice() {
24 base::ScopedCFTypeRef<DASessionRef> session(DASessionCreate(NULL));
25 base::ScopedCFTypeRef<DADiskRef> disk(DADiskCreateFromBSDName(
26 kCFAllocatorDefault, session, device_path_.value().c_str()));
27
28 if (!disk)
29 return false;
30
31 base::ScopedCFTypeRef<CFDictionaryRef> disk_description(
32 DADiskCopyDescription(disk));
33
34 CFBooleanRef ejectable = base::mac::GetValueFromDictionary<CFBooleanRef>(
35 disk_description, kDADiskDescriptionMediaEjectableKey);
36 CFBooleanRef removable = base::mac::GetValueFromDictionary<CFBooleanRef>(
37 disk_description, kDADiskDescriptionMediaRemovableKey);
38 CFBooleanRef writable = base::mac::GetValueFromDictionary<CFBooleanRef>(
39 disk_description, kDADiskDescriptionMediaWritableKey);
40 CFBooleanRef whole = base::mac::GetValueFromDictionary<CFBooleanRef>(
41 disk_description, kDADiskDescriptionMediaWholeKey);
42 CFStringRef kind = base::mac::GetValueFromDictionary<CFStringRef>(
43 disk_description, kDADiskDescriptionMediaKindKey);
44
45 // A drive is valid if it is
46 // - ejectable
47 // - removable
48 // - writable
49 // - a whole drive
50 // - it is of type IOMedia (external DVD drives and the like are IOCDMedia or
51 // IODVDMedia)
52 return CFBooleanGetValue(ejectable) && CFBooleanGetValue(removable) &&
53 CFBooleanGetValue(writable) && CFBooleanGetValue(whole) &&
54 CFStringCompare(kind, CFSTR("IOMedia"), 0) == kCFCompareEqualTo;
55 }
56
57 void ImageWriter::UnmountVolumes(const base::Closure& continuation) {
58 if (unmounter_ == NULL) {
59 unmounter_.reset(new DiskUnmounterMac());
60 }
61
62 unmounter_->Unmount(
63 device_path_.value(),
64 continuation,
65 base::Bind(
66 &ImageWriter::Error, base::Unretained(this), error::kUnmountVolumes));
67 }
68
69 bool ImageWriter::OpenDevice() {
70 base::LaunchOptions options = base::LaunchOptions();
71 options.wait = false;
72
73 // Create a socket pair for communication.
74 int sockets[2];
75 int result = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
76 if (result == -1) {
77 LOG(ERROR) << "Unable to allocate socket pair.";
Robert Sesek 2014/06/16 19:32:53 Use PLOG to capture errno in the output.
Drew Haven 2014/06/16 21:51:39 Done.
78 return false;
79 }
80 base::ScopedFD parent_socket(sockets[0]);
81 base::ScopedFD child_socket(sockets[1]);
82
83 // Map the client socket to the client's STDOUT.
84 base::FileHandleMappingVector fd_map;
85 fd_map.push_back(std::pair<int, int>(child_socket.get(), STDOUT_FILENO));
86 options.fds_to_remap = &fd_map;
87
88 // Find the file path to open.
89 base::FilePath real_device_path;
90 if (device_path_.IsAbsolute()) {
91 real_device_path = device_path_;
92 } else {
93 real_device_path = base::FilePath("/dev").Append(device_path_);
94 }
95
96 // Build the command line.
97 std::string rdwr = base::StringPrintf("%d", O_RDWR);
98
99 base::CommandLine cmd_line((base::FilePath(kAuthOpenPath)));
100 cmd_line.AppendSwitch("-stdoutpipe");
101 // Using AppendSwitchNative will use an equal-symbol which we don't want.
102 cmd_line.AppendArg("-o");
103 cmd_line.AppendArg(rdwr);
104 cmd_line.AppendArgPath(real_device_path);
105
106 // Launch the process.
107 base::ProcessHandle process_handle;
108 if (!base::LaunchProcess(cmd_line, options, &process_handle)) {
109 LOG(ERROR) << "Failed to launch authopen process.";
110 return false;
111 }
112
113 // Receive a file descriptor from authopen which sends a single FD via
114 // sendmsg and the SCM_RIGHTS extension.
115 int fd = -1;
116 char data_buffer[kDataBufferSize];
Robert Sesek 2014/06/16 19:32:53 What is data_buffer used for and where did the val
Drew Haven 2014/06/16 21:51:39 So, I looked into it. What we want is the size of
117
118 iovec io_vec[1];
Robert Sesek 2014/06/16 19:32:54 I know it's not necessary in C++, but since this i
Drew Haven 2014/06/16 21:51:39 Done.
119 io_vec[0].iov_base = data_buffer;
120 io_vec[0].iov_len = kDataBufferSize;
121
122 const socklen_t kCmsgSocketSize =
123 static_cast<socklen_t>(CMSG_SPACE(sizeof(int)));
124 char cmsgSocket[kCmsgSocketSize];
Robert Sesek 2014/06/16 19:32:54 naming: cmsg_socket
Drew Haven 2014/06/16 21:51:39 Done.
125
126 msghdr message = {0};
Robert Sesek 2014/06/16 19:32:54 Same, |struct msghdr|.
Drew Haven 2014/06/16 21:51:39 Done.
127 message.msg_iov = io_vec;
128 message.msg_iovlen = sizeof(io_vec);
129 message.msg_control = cmsgSocket;
130 message.msg_controllen = kCmsgSocketSize;
131
132 ssize_t size = HANDLE_EINTR(recvmsg(parent_socket.get(), &message, 0));
133 if (size > 0) {
134 cmsghdr* cmsgSocketHeader = CMSG_FIRSTHDR(&message);
Robert Sesek 2014/06/16 19:32:53 Same, |struct smsghdr|.
Drew Haven 2014/06/16 21:51:39 Done. Variable name as well.
135
136 if (cmsgSocketHeader && cmsgSocketHeader->cmsg_level == SOL_SOCKET &&
137 cmsgSocketHeader->cmsg_type == SCM_RIGHTS)
138 fd = *reinterpret_cast<int*>(CMSG_DATA(cmsgSocketHeader));
139 }
140
141 device_file_ = base::File(fd);
142
143 // Wait for the child.
144 int child_exit_status;
145 if (!base::WaitForExitCode(process_handle, &child_exit_status)) {
146 LOG(ERROR) << "Unable to wait for child.";
147 return false;
148 }
149
150 if (child_exit_status) {
151 LOG(ERROR) << "Child process returned failure.";
152 return false;
153 }
154
155 return device_file_.IsValid();
156 }
157
158 } // namespace image_writer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698