blob: a7aeafc08c6cfcb8e1bd2c5acf20b2f2dbe2eaa4 [file] [log] [blame]
Ben Chance7ee542011-04-12 17:02:49 -07001// 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 Chan490319f2011-05-06 14:00:42 -07005#ifndef CROS_DISKS_UDEV_DEVICE_H_
Ben Chance7ee542011-04-12 17:02:49 -07006#define CROS_DISKS_UDEV_DEVICE_H_
7
Ben Chanda410a02011-08-24 23:37:53 -07008#include <blkid/blkid.h>
Ben Chana169b0a2014-08-06 17:22:40 -07009#include <stdint.h>
Ben Chanda410a02011-08-24 23:37:53 -070010
Anand K Mistry5757f632019-09-11 13:29:41 +100011#include <memory>
Ben Chance7ee542011-04-12 17:02:49 -070012#include <string>
13#include <vector>
14
Anand K Mistry5757f632019-09-11 13:29:41 +100015#include <base/callback.h>
Ben Chan6d0b2722011-11-18 08:24:14 -080016#include <chromeos/dbus/service_constants.h>
Ben Chan54342622011-08-18 00:57:11 -070017#include <gtest/gtest_prod.h>
Ben Chanbdc39742011-05-11 17:51:26 -070018
19#include "cros-disks/disk.h"
Ben Chance7ee542011-04-12 17:02:49 -070020
Anand K Mistry5757f632019-09-11 13:29:41 +100021namespace brillo {
22class UdevDevice;
23} // namespace brillo
Ben Chance7ee542011-04-12 17:02:49 -070024
25namespace cros_disks {
26
27// A utility class that helps query information about a udev device.
28class UdevDevice {
29 public:
Anand K Mistry5757f632019-09-11 13:29:41 +100030 explicit UdevDevice(std::unique_ptr<brillo::UdevDevice> dev);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090031 UdevDevice(const UdevDevice&) = delete;
32 UdevDevice& operator=(const UdevDevice&) = delete;
33
Ben Chance7ee542011-04-12 17:02:49 -070034 ~UdevDevice();
35
36 // Gets the string value of a device attribute.
Ben Chande0e3f62017-09-26 06:28:39 -070037 std::string GetAttribute(const char* key) const;
Ben Chance7ee542011-04-12 17:02:49 -070038
39 // Checks if the value of a device attribute represents a Boolean true.
Ben Chande0e3f62017-09-26 06:28:39 -070040 bool IsAttributeTrue(const char* key) const;
Ben Chance7ee542011-04-12 17:02:49 -070041
42 // Checks if a device attribute exists.
Ben Chande0e3f62017-09-26 06:28:39 -070043 bool HasAttribute(const char* key) const;
Ben Chance7ee542011-04-12 17:02:49 -070044
45 // Gets the string value of a device property.
Ben Chande0e3f62017-09-26 06:28:39 -070046 std::string GetProperty(const char* key) const;
Ben Chance7ee542011-04-12 17:02:49 -070047
48 // Checks if the value of a device property represents a Boolean true.
Ben Chande0e3f62017-09-26 06:28:39 -070049 bool IsPropertyTrue(const char* key) const;
Ben Chance7ee542011-04-12 17:02:49 -070050
51 // Checks if a device property exists.
Ben Chande0e3f62017-09-26 06:28:39 -070052 bool HasProperty(const char* key) const;
Ben Chance7ee542011-04-12 17:02:49 -070053
Ben Chanda410a02011-08-24 23:37:53 -070054 // Gets the string value of a device property from blkid.
Ben Chande0e3f62017-09-26 06:28:39 -070055 std::string GetPropertyFromBlkId(const char* key);
Ben Chanda410a02011-08-24 23:37:53 -070056
Ben Chance7ee542011-04-12 17:02:49 -070057 // Gets the total and remaining capacity of the device.
Ben Chande0e3f62017-09-26 06:28:39 -070058 void GetSizeInfo(uint64_t* total_size, uint64_t* remaining_size) const;
Ben Chance7ee542011-04-12 17:02:49 -070059
Ben Chan53e12a22014-02-13 23:34:09 -080060 // Gets the number of partitions on the device.
61 size_t GetPartitionCount() const;
Ben Chanca369942011-09-10 17:03:05 -070062
Ben Chan0a389a62011-10-03 15:02:34 -070063 // Gets the device media type used on the device.
64 DeviceMediaType GetDeviceMediaType() const;
65
Ben Chan120c11c2012-09-10 23:10:18 -070066 // 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 Lohd8133442020-11-03 16:21:07 +110071 // Gets the bus and device numbers for the device.
72 void GetBusAndDeviceNumber(int* bus_number, int* device_number) const;
73
Ben Chan1c2d4252011-06-03 13:33:49 -070074 // Checks if a device should be auto-mounted. Currently, all external
Ben Chan0255db62011-09-19 12:14:28 -070075 // 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 Chan1c2d4252011-06-03 13:33:49 -070081
Ben Chan2923ed72013-07-29 15:52:29 -070082 // 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 Chance7ee542011-04-12 17:02:49 -070090 // Checks if any media is available in the device.
91 bool IsMediaAvailable() const;
92
Ben Chan1f418382013-06-17 23:33:28 -070093 // Checks if the device is a mobile broadband device.
94 bool IsMobileBroadbandDevice() const;
95
Ben Chan42888362011-06-09 18:16:24 -070096 // Checks if the device is on the boot device.
Ben Chan490319f2011-05-06 14:00:42 -070097 bool IsOnBootDevice() const;
98
Ben Chan54342622011-08-18 00:57:11 -070099 // Checks if the device is on the removable device.
100 bool IsOnRemovableDevice() const;
101
Ben Chan42888362011-06-09 18:16:24 -0700102 // Checks if the device is a virtual device.
Ben Chan9cda93a2011-05-24 13:31:04 -0700103 bool IsVirtual() const;
104
Ben Chan2923ed72013-07-29 15:52:29 -0700105 // Checks if the device is a loop device.
106 bool IsLoopDevice() const;
107
Ben Chan42888362011-06-09 18:16:24 -0700108 // Gets the native sysfs path of the device.
109 std::string NativePath() const;
110
Austin Tankiang36f5bb52019-03-25 16:49:30 +1100111 // Gets the path of the storage device this device is a part of, if any.
112 std::string StorageDevicePath() const;
113
Ben Chanf51ff002011-04-25 12:41:57 -0700114 // Gets the mount paths for the device.
115 std::vector<std::string> GetMountPaths() const;
Ben Chance7ee542011-04-12 17:02:49 -0700116
Ben Chanf51ff002011-04-25 12:41:57 -0700117 // Gets the mount paths for a given device path.
Ben Chande0e3f62017-09-26 06:28:39 -0700118 static std::vector<std::string> GetMountPaths(const std::string& device_path);
Ben Chanf51ff002011-04-25 12:41:57 -0700119
Ben Chance7ee542011-04-12 17:02:49 -0700120 // Returns a Disk object based on the device information.
Ben Chanda410a02011-08-24 23:37:53 -0700121 Disk ToDisk();
Ben Chance7ee542011-04-12 17:02:49 -0700122
123 private:
Anand K Mistry5757f632019-09-11 13:29:41 +1000124 using EnumerateCallback =
125 base::RepeatingCallback<bool(const brillo::UdevDevice& device)>;
126
Ben Chan22c0e602012-02-13 12:37:34 -0800127 // 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 Chan5540d902018-07-25 14:52:58 -0700131 // Checks if the device is on a SD card device.
132 bool IsOnSdDevice() const;
Ben Chan58aad1d2013-02-26 16:09:34 -0800133
Anand K Mistry5757f632019-09-11 13:29:41 +1000134 // 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 Mistryc0ece3d2020-01-28 15:55:29 +1100137 bool EnumerateParentDevices(EnumerateCallback callback) const;
Anand K Mistry5757f632019-09-11 13:29:41 +1000138
Ben Chanbdc39742011-05-11 17:51:26 -0700139 // Checks if a string contains a "1" (as Boolean true).
Ben Chande0e3f62017-09-26 06:28:39 -0700140 static bool IsValueBooleanTrue(const char* value);
Ben Chance7ee542011-04-12 17:02:49 -0700141
Anand K Mistry5757f632019-09-11 13:29:41 +1000142 const std::unique_ptr<brillo::UdevDevice> dev_;
Ben Chanda410a02011-08-24 23:37:53 -0700143 blkid_cache blkid_cache_;
144
Ben Chan22c0e602012-02-13 12:37:34 -0800145 FRIEND_TEST(UdevDeviceTest, EnsureUTF8String);
Ben Chan54342622011-08-18 00:57:11 -0700146 FRIEND_TEST(UdevDeviceTest, IsValueBooleanTrue);
Ben Chance7ee542011-04-12 17:02:49 -0700147};
148
149} // namespace cros_disks
150
151#endif // CROS_DISKS_UDEV_DEVICE_H_