| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium OS 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 <base/string_number_conversions.h> |
| 6 #include <base/string_util.h> |
| 7 #include <fcntl.h> |
| 8 #include <libudev.h> |
| 9 #include <sys/statvfs.h> |
| 10 #include <fstream> |
| 11 |
| 12 #include "udev-device.h" |
| 13 |
| 14 namespace cros_disks { |
| 15 |
| 16 UdevDevice::UdevDevice(struct udev_device *dev) |
| 17 : dev_(dev) { |
| 18 |
| 19 CHECK(dev_) << "Invalid udev device"; |
| 20 udev_device_ref(dev_); |
| 21 } |
| 22 |
| 23 UdevDevice::~UdevDevice() { |
| 24 udev_device_unref(dev_); |
| 25 } |
| 26 |
| 27 bool UdevDevice::IsValueBooleanTrue(const char *value) const { |
| 28 return value && strcmp(value, "1") == 0; |
| 29 } |
| 30 |
| 31 std::string UdevDevice::GetAttribute(const char *key) const { |
| 32 const char *value = udev_device_get_sysattr_value(dev_, key); |
| 33 return (value) ? value : ""; |
| 34 } |
| 35 |
| 36 bool UdevDevice::IsAttributeTrue(const char *key) const { |
| 37 const char *value = udev_device_get_sysattr_value(dev_, key); |
| 38 return IsValueBooleanTrue(value); |
| 39 } |
| 40 |
| 41 bool UdevDevice::HasAttribute(const char *key) const { |
| 42 const char *value = udev_device_get_sysattr_value(dev_, key); |
| 43 return value != NULL; |
| 44 } |
| 45 |
| 46 std::string UdevDevice::GetProperty(const char *key) const { |
| 47 const char *value = udev_device_get_property_value(dev_, key); |
| 48 return (value) ? value : ""; |
| 49 } |
| 50 |
| 51 bool UdevDevice::IsPropertyTrue(const char *key) const { |
| 52 const char *value = udev_device_get_property_value(dev_, key); |
| 53 return IsValueBooleanTrue(value); |
| 54 } |
| 55 |
| 56 bool UdevDevice::HasProperty(const char *key) const { |
| 57 const char *value = udev_device_get_property_value(dev_, key); |
| 58 return value != NULL; |
| 59 } |
| 60 |
| 61 void UdevDevice::GetSizeInfo(uint64 *total_size, uint64 *remaining_size) const { |
| 62 const char *dev_file = udev_device_get_devnode(dev_); |
| 63 |
| 64 struct statvfs stat; |
| 65 bool stat_available = (statvfs(dev_file, &stat) == 0); |
| 66 |
| 67 if (total_size) { |
| 68 *total_size = (stat_available) ? (stat.f_blocks * stat.f_frsize) : 0; |
| 69 const char *partition_size = udev_device_get_property_value(dev_, |
| 70 "UDISKS_PARTITION_SIZE"); |
| 71 int64 size = 0; |
| 72 if (partition_size) { |
| 73 base::StringToInt64(partition_size, &size); |
| 74 *total_size = size; |
| 75 } else { |
| 76 const char *size_attr = udev_device_get_sysattr_value(dev_, "size"); |
| 77 if (size_attr) { |
| 78 base::StringToInt64(size_attr, &size); |
| 79 *total_size = size; |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 if (remaining_size) |
| 85 *remaining_size = (stat_available) ? (stat.f_bfree * stat.f_frsize) : 0; |
| 86 } |
| 87 |
| 88 bool UdevDevice::IsMediaAvailable() const { |
| 89 bool is_media_available = true; |
| 90 if (IsAttributeTrue("removable")) { |
| 91 if (IsPropertyTrue("ID_CDROM")) { |
| 92 is_media_available = IsPropertyTrue("ID_CDROM_MEDIA"); |
| 93 } else { |
| 94 const char *dev_file = udev_device_get_devnode(dev_); |
| 95 int fd = open(dev_file, O_RDONLY); |
| 96 if (fd < 0) { |
| 97 is_media_available = true; |
| 98 } else { |
| 99 close(fd); |
| 100 } |
| 101 } |
| 102 } |
| 103 return is_media_available; |
| 104 } |
| 105 |
| 106 std::vector<std::string> UdevDevice::GetMountedPaths() const { |
| 107 const std::string dev_file = udev_device_get_devnode(dev_); |
| 108 std::ifstream fs("/proc/mounts"); |
| 109 if (fs.is_open()) { |
| 110 return ParseMountedPaths(dev_file, fs); |
| 111 } |
| 112 return std::vector<std::string>(); |
| 113 } |
| 114 |
| 115 std::vector<std::string> UdevDevice::ParseMountedPaths( |
| 116 const std::string& device_path, std::istream& stream) { |
| 117 std::vector<std::string> mounted_paths; |
| 118 std::string line; |
| 119 while (std::getline(stream, line)) { |
| 120 std::vector<std::string> tokens; |
| 121 SplitString(line, ' ', &tokens); |
| 122 if (tokens.size() >= 2) { |
| 123 if (tokens[0] == device_path) |
| 124 mounted_paths.push_back(tokens[1]); |
| 125 } |
| 126 } |
| 127 return mounted_paths; |
| 128 } |
| 129 |
| 130 Disk UdevDevice::ToDisk() const { |
| 131 Disk disk; |
| 132 |
| 133 disk.set_is_read_only(IsAttributeTrue("ro")); |
| 134 disk.set_is_drive(HasAttribute("range")); |
| 135 disk.set_is_rotational(HasProperty("ID_ATA_ROTATION_RATE_RPM")); |
| 136 disk.set_is_optical_disk(IsPropertyTrue("ID_CDROM")); |
| 137 disk.set_is_hidden(IsPropertyTrue("UDISKS_PRESENTATION_HIDE")); |
| 138 disk.set_is_media_available(IsMediaAvailable()); |
| 139 disk.set_drive_model(GetProperty("ID_MODEL")); |
| 140 disk.set_label(GetProperty("ID_FS_LABEL")); |
| 141 disk.set_native_path(udev_device_get_syspath(dev_)); |
| 142 |
| 143 const char *dev_file = udev_device_get_devnode(dev_); |
| 144 disk.set_device_file(dev_file); |
| 145 |
| 146 std::vector<std::string> mounted_paths = GetMountedPaths(); |
| 147 disk.set_is_mounted(!mounted_paths.empty()); |
| 148 disk.set_mount_path(mounted_paths[0]); // TODO(benchan): multiple paths |
| 149 |
| 150 uint64 total_size, remaining_size; |
| 151 GetSizeInfo(&total_size, &remaining_size); |
| 152 disk.set_device_capacity(total_size); |
| 153 disk.set_bytes_remaining(remaining_size); |
| 154 |
| 155 return disk; |
| 156 } |
| 157 |
| 158 } // namespace cros_disks |
| OLD | NEW |