Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 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 | |
Ben Chan | 490319f | 2011-05-06 14:00:42 -0700 | [diff] [blame] | 5 | #ifndef CROS_DISKS_UDEV_DEVICE_H_ |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 6 | #define CROS_DISKS_UDEV_DEVICE_H_ |
| 7 | |
Ben Chan | da410a0 | 2011-08-24 23:37:53 -0700 | [diff] [blame] | 8 | #include <blkid/blkid.h> |
Ben Chan | a169b0a | 2014-08-06 17:22:40 -0700 | [diff] [blame] | 9 | #include <stdint.h> |
Ben Chan | da410a0 | 2011-08-24 23:37:53 -0700 | [diff] [blame] | 10 | |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 11 | #include <memory> |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 15 | #include <base/callback.h> |
Ben Chan | 6d0b272 | 2011-11-18 08:24:14 -0800 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
Ben Chan | 5434262 | 2011-08-18 00:57:11 -0700 | [diff] [blame] | 17 | #include <gtest/gtest_prod.h> |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 18 | |
| 19 | #include "cros-disks/disk.h" |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 20 | |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 21 | namespace brillo { |
| 22 | class UdevDevice; |
| 23 | } // namespace brillo |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 24 | |
| 25 | namespace cros_disks { |
| 26 | |
| 27 | // A utility class that helps query information about a udev device. |
| 28 | class UdevDevice { |
| 29 | public: |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 30 | explicit UdevDevice(std::unique_ptr<brillo::UdevDevice> dev); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 31 | UdevDevice(const UdevDevice&) = delete; |
| 32 | UdevDevice& operator=(const UdevDevice&) = delete; |
| 33 | |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 34 | ~UdevDevice(); |
| 35 | |
| 36 | // Gets the string value of a device attribute. |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 37 | std::string GetAttribute(const char* key) const; |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 38 | |
| 39 | // Checks if the value of a device attribute represents a Boolean true. |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 40 | bool IsAttributeTrue(const char* key) const; |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 41 | |
| 42 | // Checks if a device attribute exists. |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 43 | bool HasAttribute(const char* key) const; |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 44 | |
| 45 | // Gets the string value of a device property. |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 46 | std::string GetProperty(const char* key) const; |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 47 | |
| 48 | // Checks if the value of a device property represents a Boolean true. |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 49 | bool IsPropertyTrue(const char* key) const; |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 50 | |
| 51 | // Checks if a device property exists. |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 52 | bool HasProperty(const char* key) const; |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 53 | |
Ben Chan | da410a0 | 2011-08-24 23:37:53 -0700 | [diff] [blame] | 54 | // Gets the string value of a device property from blkid. |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 55 | std::string GetPropertyFromBlkId(const char* key); |
Ben Chan | da410a0 | 2011-08-24 23:37:53 -0700 | [diff] [blame] | 56 | |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 57 | // Gets the total and remaining capacity of the device. |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 58 | void GetSizeInfo(uint64_t* total_size, uint64_t* remaining_size) const; |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 59 | |
Ben Chan | 53e12a2 | 2014-02-13 23:34:09 -0800 | [diff] [blame] | 60 | // Gets the number of partitions on the device. |
| 61 | size_t GetPartitionCount() const; |
Ben Chan | ca36994 | 2011-09-10 17:03:05 -0700 | [diff] [blame] | 62 | |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 63 | // Gets the device media type used on the device. |
| 64 | DeviceMediaType GetDeviceMediaType() const; |
| 65 | |
Ben Chan | 120c11c | 2012-09-10 23:10:18 -0700 | [diff] [blame] | 66 | // Gets the USB vendor and product ID of the device. Returns true if the |
| 67 | // IDs are found. |
| 68 | bool GetVendorAndProductId(std::string* vendor_id, |
| 69 | std::string* product_id) const; |
| 70 | |
Timothy Loh | d813344 | 2020-11-03 16:21:07 +1100 | [diff] [blame] | 71 | // Gets the bus and device numbers for the device. |
| 72 | void GetBusAndDeviceNumber(int* bus_number, int* device_number) const; |
| 73 | |
Ben Chan | 1c2d425 | 2011-06-03 13:33:49 -0700 | [diff] [blame] | 74 | // Checks if a device should be auto-mounted. Currently, all external |
Ben Chan | 0255db6 | 2011-09-19 12:14:28 -0700 | [diff] [blame] | 75 | // disk devices, which are neither on the boot device nor virtual, |
| 76 | // are considered auto-mountable. |
| 77 | bool IsAutoMountable() const; |
| 78 | |
| 79 | // Checks if a device should be hidden from the file browser. |
| 80 | bool IsHidden(); |
Ben Chan | 1c2d425 | 2011-06-03 13:33:49 -0700 | [diff] [blame] | 81 | |
Ben Chan | 2923ed7 | 2013-07-29 15:52:29 -0700 | [diff] [blame] | 82 | // Checks if the device is completely ignored by cros-disks. Unlike |
| 83 | // IsAutoMountable() or IsHidden(), IsIgnored() prevents a device from being |
| 84 | // reported by cros-disks during device enumeration and udev events, such that |
| 85 | // the system does not even gather properties of the device. Currently, all |
| 86 | // virtual devices, except loop devices, are ignored. Loop devices are used |
| 87 | // by automated tests to simulate removable devices and thus not ignored. |
| 88 | bool IsIgnored() const; |
| 89 | |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 90 | // Checks if any media is available in the device. |
| 91 | bool IsMediaAvailable() const; |
| 92 | |
Ben Chan | 1f41838 | 2013-06-17 23:33:28 -0700 | [diff] [blame] | 93 | // Checks if the device is a mobile broadband device. |
| 94 | bool IsMobileBroadbandDevice() const; |
| 95 | |
Ben Chan | 4288836 | 2011-06-09 18:16:24 -0700 | [diff] [blame] | 96 | // Checks if the device is on the boot device. |
Ben Chan | 490319f | 2011-05-06 14:00:42 -0700 | [diff] [blame] | 97 | bool IsOnBootDevice() const; |
| 98 | |
Ben Chan | 5434262 | 2011-08-18 00:57:11 -0700 | [diff] [blame] | 99 | // Checks if the device is on the removable device. |
| 100 | bool IsOnRemovableDevice() const; |
| 101 | |
Ben Chan | 4288836 | 2011-06-09 18:16:24 -0700 | [diff] [blame] | 102 | // Checks if the device is a virtual device. |
Ben Chan | 9cda93a | 2011-05-24 13:31:04 -0700 | [diff] [blame] | 103 | bool IsVirtual() const; |
| 104 | |
Ben Chan | 2923ed7 | 2013-07-29 15:52:29 -0700 | [diff] [blame] | 105 | // Checks if the device is a loop device. |
| 106 | bool IsLoopDevice() const; |
| 107 | |
Ben Chan | 4288836 | 2011-06-09 18:16:24 -0700 | [diff] [blame] | 108 | // Gets the native sysfs path of the device. |
| 109 | std::string NativePath() const; |
| 110 | |
Austin Tankiang | 36f5bb5 | 2019-03-25 16:49:30 +1100 | [diff] [blame] | 111 | // Gets the path of the storage device this device is a part of, if any. |
| 112 | std::string StorageDevicePath() const; |
| 113 | |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 114 | // Gets the mount paths for the device. |
| 115 | std::vector<std::string> GetMountPaths() const; |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 116 | |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 117 | // Gets the mount paths for a given device path. |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 118 | static std::vector<std::string> GetMountPaths(const std::string& device_path); |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 119 | |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 120 | // Returns a Disk object based on the device information. |
Ben Chan | da410a0 | 2011-08-24 23:37:53 -0700 | [diff] [blame] | 121 | Disk ToDisk(); |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 122 | |
| 123 | private: |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 124 | using EnumerateCallback = |
| 125 | base::RepeatingCallback<bool(const brillo::UdevDevice& device)>; |
| 126 | |
Ben Chan | 22c0e60 | 2012-02-13 12:37:34 -0800 | [diff] [blame] | 127 | // Returns |str| if |str| is a valid UTF8 string (determined by |
| 128 | // base::IsStringUTF8) or an empty string otherwise. |
| 129 | static std::string EnsureUTF8String(const std::string& str); |
| 130 | |
Ben Chan | 5540d90 | 2018-07-25 14:52:58 -0700 | [diff] [blame] | 131 | // Checks if the device is on a SD card device. |
| 132 | bool IsOnSdDevice() const; |
Ben Chan | 58aad1d | 2013-02-26 16:09:34 -0800 | [diff] [blame] | 133 | |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 134 | // Walks up the device parents, starting at the current device, invoking |
| 135 | // |callback| until |callback| returns true. Returns true if |callback| |
| 136 | // returned true, and false if finished walking up the device tree. |
Anand K Mistry | c0ece3d | 2020-01-28 15:55:29 +1100 | [diff] [blame] | 137 | bool EnumerateParentDevices(EnumerateCallback callback) const; |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 138 | |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 139 | // Checks if a string contains a "1" (as Boolean true). |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 140 | static bool IsValueBooleanTrue(const char* value); |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 141 | |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 142 | const std::unique_ptr<brillo::UdevDevice> dev_; |
Ben Chan | da410a0 | 2011-08-24 23:37:53 -0700 | [diff] [blame] | 143 | blkid_cache blkid_cache_; |
| 144 | |
Ben Chan | 22c0e60 | 2012-02-13 12:37:34 -0800 | [diff] [blame] | 145 | FRIEND_TEST(UdevDeviceTest, EnsureUTF8String); |
Ben Chan | 5434262 | 2011-08-18 00:57:11 -0700 | [diff] [blame] | 146 | FRIEND_TEST(UdevDeviceTest, IsValueBooleanTrue); |
Ben Chan | ce7ee54 | 2011-04-12 17:02:49 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | } // namespace cros_disks |
| 150 | |
| 151 | #endif // CROS_DISKS_UDEV_DEVICE_H_ |