OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <CoreFoundation/CoreFoundation.h> |
| 6 #include <IOKit/IOBSD.h> |
| 7 #include <IOKit/IOKitLib.h> |
| 8 #include <IOKit/storage/IOBlockStorageDevice.h> |
| 9 #include <IOKit/storage/IOMedia.h> |
5 #include <IOKit/storage/IOStorageProtocolCharacteristics.h> | 10 #include <IOKit/storage/IOStorageProtocolCharacteristics.h> |
6 #include <sys/socket.h> | 11 #include <sys/socket.h> |
7 | 12 |
8 #include "base/command_line.h" | 13 #include "base/command_line.h" |
9 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
| 15 #include "base/mac/scoped_cftyperef.h" |
| 16 #include "base/mac/scoped_ioobject.h" |
10 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
11 #include "base/process/kill.h" | 18 #include "base/process/kill.h" |
12 #include "base/process/launch.h" | 19 #include "base/process/launch.h" |
13 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/strings/sys_string_conversions.h" |
| 22 #include "chrome/common/extensions/image_writer/image_writer_util_mac.h" |
14 #include "chrome/utility/image_writer/disk_unmounter_mac.h" | 23 #include "chrome/utility/image_writer/disk_unmounter_mac.h" |
15 #include "chrome/utility/image_writer/error_messages.h" | 24 #include "chrome/utility/image_writer/error_messages.h" |
16 #include "chrome/utility/image_writer/image_writer.h" | 25 #include "chrome/utility/image_writer/image_writer.h" |
17 | 26 |
18 namespace image_writer { | 27 namespace image_writer { |
19 | 28 |
20 static const char kAuthOpenPath[] = "/usr/libexec/authopen"; | 29 static const char kAuthOpenPath[] = "/usr/libexec/authopen"; |
21 | 30 |
22 bool ImageWriter::IsValidDevice() { | 31 bool ImageWriter::IsValidDevice() { |
23 base::ScopedCFTypeRef<DASessionRef> session(DASessionCreate(NULL)); | 32 base::ScopedCFTypeRef<CFStringRef> cf_bsd_name( |
24 base::ScopedCFTypeRef<DADiskRef> disk(DADiskCreateFromBSDName( | 33 base::SysUTF8ToCFStringRef(device_path_.value())); |
25 kCFAllocatorDefault, session, device_path_.value().c_str())); | 34 CFMutableDictionaryRef matching = IOServiceMatching(kIOMediaClass); |
| 35 CFDictionaryAddValue(matching, CFSTR(kIOMediaWholeKey), kCFBooleanTrue); |
| 36 CFDictionaryAddValue(matching, CFSTR(kIOMediaWritableKey), kCFBooleanTrue); |
| 37 CFDictionaryAddValue(matching, CFSTR(kIOBSDNameKey), cf_bsd_name); |
26 | 38 |
27 if (!disk) | 39 io_service_t disk_obj = |
28 return false; | 40 IOServiceGetMatchingService(kIOMasterPortDefault, matching); |
| 41 base::mac::ScopedIOObject<io_service_t> iterator_ref(disk_obj); |
29 | 42 |
30 base::ScopedCFTypeRef<CFDictionaryRef> disk_description( | 43 if (disk_obj) { |
31 DADiskCopyDescription(disk)); | 44 CFMutableDictionaryRef dict; |
| 45 if (IORegistryEntryCreateCFProperties( |
| 46 disk_obj, &dict, kCFAllocatorDefault, 0) != KERN_SUCCESS) { |
| 47 LOG(ERROR) << "Unable to get properties of disk object."; |
| 48 return false; |
| 49 } |
| 50 base::ScopedCFTypeRef<CFMutableDictionaryRef> dict_ref(dict); |
32 | 51 |
33 CFBooleanRef ejectable = base::mac::GetValueFromDictionary<CFBooleanRef>( | 52 CFBooleanRef cf_removable = base::mac::GetValueFromDictionary<CFBooleanRef>( |
34 disk_description, kDADiskDescriptionMediaEjectableKey); | 53 dict, CFSTR(kIOMediaRemovableKey)); |
35 CFBooleanRef removable = base::mac::GetValueFromDictionary<CFBooleanRef>( | 54 bool removable = CFBooleanGetValue(cf_removable); |
36 disk_description, kDADiskDescriptionMediaRemovableKey); | |
37 CFBooleanRef writable = base::mac::GetValueFromDictionary<CFBooleanRef>( | |
38 disk_description, kDADiskDescriptionMediaWritableKey); | |
39 CFBooleanRef whole = base::mac::GetValueFromDictionary<CFBooleanRef>( | |
40 disk_description, kDADiskDescriptionMediaWholeKey); | |
41 CFStringRef kind = base::mac::GetValueFromDictionary<CFStringRef>( | |
42 disk_description, kDADiskDescriptionMediaKindKey); | |
43 | 55 |
44 // A drive is valid if it is | 56 bool is_usb = extensions::IsUsbDevice(disk_obj); |
45 // - ejectable | 57 |
46 // - removable | 58 return removable || is_usb; |
47 // - writable | 59 } |
48 // - a whole drive | 60 |
49 // - it is of type IOMedia (external DVD drives and the like are IOCDMedia or | 61 return false; |
50 // IODVDMedia) | |
51 return CFBooleanGetValue(ejectable) && CFBooleanGetValue(removable) && | |
52 CFBooleanGetValue(writable) && CFBooleanGetValue(whole) && | |
53 CFStringCompare(kind, CFSTR("IOMedia"), 0) == kCFCompareEqualTo; | |
54 } | 62 } |
55 | 63 |
56 void ImageWriter::UnmountVolumes(const base::Closure& continuation) { | 64 void ImageWriter::UnmountVolumes(const base::Closure& continuation) { |
57 if (unmounter_ == NULL) { | 65 if (unmounter_ == NULL) { |
58 unmounter_.reset(new DiskUnmounterMac()); | 66 unmounter_.reset(new DiskUnmounterMac()); |
59 } | 67 } |
60 | 68 |
61 unmounter_->Unmount( | 69 unmounter_->Unmount( |
62 device_path_.value(), | 70 device_path_.value(), |
63 continuation, | 71 continuation, |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 158 |
151 if (child_exit_status) { | 159 if (child_exit_status) { |
152 LOG(ERROR) << "Child process returned failure."; | 160 LOG(ERROR) << "Child process returned failure."; |
153 return false; | 161 return false; |
154 } | 162 } |
155 | 163 |
156 return device_file_.IsValid(); | 164 return device_file_.IsValid(); |
157 } | 165 } |
158 | 166 |
159 } // namespace image_writer | 167 } // namespace image_writer |
OLD | NEW |