| 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 // StorageMonitorLinux implementation. | 5 // StorageMonitorLinux implementation. |
| 6 | 6 |
| 7 #include "components/storage_monitor/storage_monitor_linux.h" | 7 #include "components/storage_monitor/storage_monitor_linux.h" |
| 8 | 8 |
| 9 #include <mntent.h> | 9 #include <mntent.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 bool result_; | 92 bool result_; |
| 93 | 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(ScopedGetDeviceInfoResultRecorder); | 94 DISALLOW_COPY_AND_ASSIGN(ScopedGetDeviceInfoResultRecorder); |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 // Returns the storage partition size of the device specified by |device_path|. | 97 // Returns the storage partition size of the device specified by |device_path|. |
| 98 // If the requested information is unavailable, returns 0. | 98 // If the requested information is unavailable, returns 0. |
| 99 uint64 GetDeviceStorageSize(const base::FilePath& device_path, | 99 uint64 GetDeviceStorageSize(const base::FilePath& device_path, |
| 100 struct udev_device* device) { | 100 struct udev_device* device) { |
| 101 // sysfs provides the device size in units of 512-byte blocks. | 101 // sysfs provides the device size in units of 512-byte blocks. |
| 102 const std::string partition_size = udev_device_get_sysattr_value( | 102 const std::string partition_size = device::udev_device_get_sysattr_value( |
| 103 device, kSizeSysAttr); | 103 device, kSizeSysAttr); |
| 104 | 104 |
| 105 // Keep track of device size, to see how often this information is | 105 // Keep track of device size, to see how often this information is |
| 106 // unavailable. | 106 // unavailable. |
| 107 UMA_HISTOGRAM_BOOLEAN( | 107 UMA_HISTOGRAM_BOOLEAN( |
| 108 "RemovableDeviceNotificationsLinux.device_partition_size_available", | 108 "RemovableDeviceNotificationsLinux.device_partition_size_available", |
| 109 !partition_size.empty()); | 109 !partition_size.empty()); |
| 110 | 110 |
| 111 uint64 total_size_in_bytes = 0; | 111 uint64 total_size_in_bytes = 0; |
| 112 if (!base::StringToUint64(partition_size, &total_size_in_bytes)) | 112 if (!base::StringToUint64(partition_size, &total_size_in_bytes)) |
| 113 return 0; | 113 return 0; |
| 114 return (total_size_in_bytes <= kuint64max / 512) ? | 114 return (total_size_in_bytes <= kuint64max / 512) ? |
| 115 total_size_in_bytes * 512 : 0; | 115 total_size_in_bytes * 512 : 0; |
| 116 } | 116 } |
| 117 | 117 |
| 118 // Gets the device information using udev library. | 118 // Gets the device information using udev library. |
| 119 scoped_ptr<StorageInfo> GetDeviceInfo(const base::FilePath& device_path, | 119 scoped_ptr<StorageInfo> GetDeviceInfo(const base::FilePath& device_path, |
| 120 const base::FilePath& mount_point) { | 120 const base::FilePath& mount_point) { |
| 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 122 DCHECK(!device_path.empty()); | 122 DCHECK(!device_path.empty()); |
| 123 | 123 |
| 124 scoped_ptr<StorageInfo> storage_info; | 124 scoped_ptr<StorageInfo> storage_info; |
| 125 | 125 |
| 126 ScopedGetDeviceInfoResultRecorder results_recorder; | 126 ScopedGetDeviceInfoResultRecorder results_recorder; |
| 127 | 127 |
| 128 device::ScopedUdevPtr udev_obj(udev_new()); | 128 device::ScopedUdevPtr udev_obj(device::udev_new()); |
| 129 if (!udev_obj.get()) | 129 if (!udev_obj.get()) |
| 130 return storage_info.Pass(); | 130 return storage_info.Pass(); |
| 131 | 131 |
| 132 struct stat device_stat; | 132 struct stat device_stat; |
| 133 if (stat(device_path.value().c_str(), &device_stat) < 0) | 133 if (stat(device_path.value().c_str(), &device_stat) < 0) |
| 134 return storage_info.Pass(); | 134 return storage_info.Pass(); |
| 135 | 135 |
| 136 char device_type; | 136 char device_type; |
| 137 if (S_ISCHR(device_stat.st_mode)) | 137 if (S_ISCHR(device_stat.st_mode)) |
| 138 device_type = 'c'; | 138 device_type = 'c'; |
| 139 else if (S_ISBLK(device_stat.st_mode)) | 139 else if (S_ISBLK(device_stat.st_mode)) |
| 140 device_type = 'b'; | 140 device_type = 'b'; |
| 141 else | 141 else |
| 142 return storage_info.Pass(); // Not a supported type. | 142 return storage_info.Pass(); // Not a supported type. |
| 143 | 143 |
| 144 device::ScopedUdevDevicePtr device( | 144 device::ScopedUdevDevicePtr device( |
| 145 udev_device_new_from_devnum(udev_obj.get(), device_type, | 145 device::udev_device_new_from_devnum(udev_obj.get(), device_type, |
| 146 device_stat.st_rdev)); | 146 device_stat.st_rdev)); |
| 147 if (!device.get()) | 147 if (!device.get()) |
| 148 return storage_info.Pass(); | 148 return storage_info.Pass(); |
| 149 | 149 |
| 150 base::string16 volume_label = | 150 base::string16 volume_label = |
| 151 base::UTF8ToUTF16(GetUdevDevicePropertyValue(device.get(), kLabel)); | 151 base::UTF8ToUTF16(GetUdevDevicePropertyValue(device.get(), kLabel)); |
| 152 base::string16 vendor_name = | 152 base::string16 vendor_name = |
| 153 base::UTF8ToUTF16(GetUdevDevicePropertyValue(device.get(), kVendor)); | 153 base::UTF8ToUTF16(GetUdevDevicePropertyValue(device.get(), kVendor)); |
| 154 base::string16 model_name = | 154 base::string16 model_name = |
| 155 base::UTF8ToUTF16(GetUdevDevicePropertyValue(device.get(), kModel)); | 155 base::UTF8ToUTF16(GetUdevDevicePropertyValue(device.get(), kModel)); |
| 156 | 156 |
| 157 std::string unique_id = MakeDeviceUniqueId(device.get()); | 157 std::string unique_id = MakeDeviceUniqueId(device.get()); |
| 158 | 158 |
| 159 // Keep track of device info details to see how often we get invalid values. | 159 // Keep track of device info details to see how often we get invalid values. |
| 160 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, volume_label); | 160 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, volume_label); |
| 161 | 161 |
| 162 const char* value = | 162 const char* value = |
| 163 udev_device_get_sysattr_value(device.get(), kRemovableSysAttr); | 163 device::udev_device_get_sysattr_value(device.get(), kRemovableSysAttr); |
| 164 if (!value) { | 164 if (!value) { |
| 165 // |parent_device| is owned by |device| and does not need to be cleaned | 165 // |parent_device| is owned by |device| and does not need to be cleaned |
| 166 // up. | 166 // up. |
| 167 struct udev_device* parent_device = | 167 struct udev_device* parent_device = |
| 168 udev_device_get_parent_with_subsystem_devtype(device.get(), | 168 device::udev_device_get_parent_with_subsystem_devtype( |
| 169 kBlockSubsystemKey, | 169 device.get(), |
| 170 kDiskDeviceTypeKey); | 170 kBlockSubsystemKey, |
| 171 value = udev_device_get_sysattr_value(parent_device, kRemovableSysAttr); | 171 kDiskDeviceTypeKey); |
| 172 value = device::udev_device_get_sysattr_value(parent_device, |
| 173 kRemovableSysAttr); |
| 172 } | 174 } |
| 173 const bool is_removable = (value && atoi(value) == 1); | 175 const bool is_removable = (value && atoi(value) == 1); |
| 174 | 176 |
| 175 StorageInfo::Type type = StorageInfo::FIXED_MASS_STORAGE; | 177 StorageInfo::Type type = StorageInfo::FIXED_MASS_STORAGE; |
| 176 if (is_removable) { | 178 if (is_removable) { |
| 177 if (MediaStorageUtil::HasDcim(mount_point)) | 179 if (MediaStorageUtil::HasDcim(mount_point)) |
| 178 type = StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM; | 180 type = StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM; |
| 179 else | 181 else |
| 180 type = StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM; | 182 type = StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM; |
| 181 } | 183 } |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 mount_priority_map_[mount_device][mount_point] = removable; | 500 mount_priority_map_[mount_device][mount_point] = removable; |
| 499 receiver()->ProcessAttach(*storage_info); | 501 receiver()->ProcessAttach(*storage_info); |
| 500 } | 502 } |
| 501 | 503 |
| 502 StorageMonitor* StorageMonitor::CreateInternal() { | 504 StorageMonitor* StorageMonitor::CreateInternal() { |
| 503 const base::FilePath kDefaultMtabPath("/etc/mtab"); | 505 const base::FilePath kDefaultMtabPath("/etc/mtab"); |
| 504 return new StorageMonitorLinux(kDefaultMtabPath); | 506 return new StorageMonitorLinux(kDefaultMtabPath); |
| 505 } | 507 } |
| 506 | 508 |
| 507 } // namespace storage_monitor | 509 } // namespace storage_monitor |
| OLD | NEW |