1 // Copyright 2018 The Chromium 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 #ifndef CHROMEOS_DISKS_DISK_H_
6 #define CHROMEOS_DISKS_DISK_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/component_export.h"
12 #include "base/macros.h"
13 #include "chromeos/dbus/cros_disks_client.h"
14 
15 namespace chromeos {
16 namespace disks {
17 
COMPONENT_EXPORT(CHROMEOS_DISKS)18 class COMPONENT_EXPORT(CHROMEOS_DISKS) Disk {
19  public:
20   class Builder;
21 
22   Disk(const DiskInfo& disk_info,
23        // Whether the device is mounted in read-only mode by the policy.
24        // Valid only when the device mounted and mount_path_ is non-empty.
25        bool write_disabled_by_policy,
26        const std::string& base_mount_path);
27 
28   // For tests.
29   // TODO(amistry): Eliminate this copy constructor. It is only used in tests.
30   Disk(const Disk&);
31 
32   ~Disk();
33 
34   // The path of the device, used by devicekit-disks.
35   // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
36   const std::string& device_path() const { return device_path_; }
37 
38   // The path to the mount point of this device. Will be empty if not mounted.
39   // (e.g. /media/removable/VOLUME)
40   // TODO(amistry): mount_path() being set DOES NOT means the disk is mounted.
41   // See crrev.com/f8692888d11a10b5b5f8ad6fbfdeae21aed8cbf6 for the reason.
42   const std::string& mount_path() const { return mount_path_; }
43 
44   // The path of the device according to filesystem.
45   // (e.g. /dev/sdb)
46   const std::string& file_path() const { return file_path_; }
47 
48   // Device's label.
49   const std::string& device_label() const { return device_label_; }
50 
51   void set_device_label(const std::string& device_label) {
52     device_label_ = device_label;
53   }
54 
55   // If disk is a parent, then its label, else parents label.
56   // (e.g. "TransMemory")
57   const std::string& drive_label() const { return drive_label_; }
58 
59   // Vendor ID of the device (e.g. "18d1").
60   const std::string& vendor_id() const { return vendor_id_; }
61 
62   // Vendor name of the device (e.g. "Google Inc.").
63   const std::string& vendor_name() const { return vendor_name_; }
64 
65   // Product ID of the device (e.g. "4e11").
66   const std::string& product_id() const { return product_id_; }
67 
68   // Product name of the device (e.g. "Nexus One").
69   const std::string& product_name() const { return product_name_; }
70 
71   // Returns the file system uuid string.
72   const std::string& fs_uuid() const { return fs_uuid_; }
73 
74   // Path of the storage device this device's block is a part of.
75   // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
76   const std::string& storage_device_path() const {
77     return storage_device_path_;
78   }
79 
80   // Device type.
81   DeviceType device_type() const { return device_type_; }
82 
83   // Total size of the device in bytes.
84   uint64_t total_size_in_bytes() const { return total_size_in_bytes_; }
85 
86   // Is the device is a parent device (i.e. sdb rather than sdb1).
87   bool is_parent() const { return is_parent_; }
88 
89   // Whether the user can write to the device. True if read-only.
90   bool is_read_only() const {
91     return is_read_only_hardware_ || write_disabled_by_policy_;
92   }
93 
94   // Is the device read only.
95   bool is_read_only_hardware() const { return is_read_only_hardware_; }
96 
97   // Does the device contains media.
98   bool has_media() const { return has_media_; }
99 
100   // Is the device on the boot device.
101   bool on_boot_device() const { return on_boot_device_; }
102 
103   // Is the device on the removable device.
104   bool on_removable_device() const { return on_removable_device_; }
105 
106   // Shoud the device be shown in the UI, or automounted.
107   bool is_hidden() const { return is_hidden_; }
108 
109   // Is the disk auto-mountable.
110   bool is_auto_mountable() const { return is_auto_mountable_; }
111 
112   void set_write_disabled_by_policy(bool disable) {
113     write_disabled_by_policy_ = disable;
114   }
115 
116   void clear_mount_path() { mount_path_.clear(); }
117 
118   bool is_mounted() const { return is_mounted_; }
119 
120   void set_mounted(bool mounted) { is_mounted_ = mounted; }
121 
122   const std::string& file_system_type() const { return file_system_type_; }
123 
124   void set_file_system_type(const std::string& file_system_type) {
125     file_system_type_ = file_system_type;
126   }
127   // Name of the first mount path of the disk.
128   const std::string& base_mount_path() const { return base_mount_path_; }
129 
130   void SetMountPath(const std::string& mount_path);
131 
132   bool IsStatefulPartition() const;
133 
134   // Is the disk being mounted for the first time since being plugged in.
135   bool is_first_mount() const { return is_first_mount_; }
136 
137   void set_is_first_mount(bool first_mount) { is_first_mount_ = first_mount; }
138 
139  private:
140   friend class Builder;
141 
142   Disk();
143 
144   std::string device_path_;
145   std::string mount_path_;
146   bool write_disabled_by_policy_ = false;
147   std::string file_path_;
148   std::string device_label_;
149   std::string drive_label_;
150   std::string vendor_id_;
151   std::string vendor_name_;
152   std::string product_id_;
153   std::string product_name_;
154   std::string fs_uuid_;
155   std::string storage_device_path_;
156   DeviceType device_type_ = DEVICE_TYPE_UNKNOWN;
157   uint64_t total_size_in_bytes_ = 0;
158   bool is_parent_ = false;
159   bool is_read_only_hardware_ = false;
160   bool has_media_ = false;
161   bool on_boot_device_ = false;
162   bool on_removable_device_ = false;
163   bool is_hidden_ = false;
164   bool is_auto_mountable_ = false;
165   bool is_mounted_ = false;
166   bool is_first_mount_ = true;
167   std::string file_system_type_;
168   std::string base_mount_path_;
169 };
170 
COMPONENT_EXPORT(CHROMEOS_DISKS)171 class COMPONENT_EXPORT(CHROMEOS_DISKS) Disk::Builder {
172  public:
173   Builder();
174   ~Builder();
175 
176   Builder& SetDevicePath(const std::string& device_path);
177   Builder& SetMountPath(const std::string& mount_path);
178   Builder& SetWriteDisabledByPolicy(bool write_disabled_by_policy);
179   Builder& SetFilePath(const std::string& file_path);
180   Builder& SetDeviceLabel(const std::string& device_label);
181   Builder& SetDriveLabel(const std::string& drive_label);
182   Builder& SetVendorId(const std::string& vendor_id);
183   Builder& SetVendorName(const std::string& vendor_name);
184   Builder& SetProductId(const std::string& product_id);
185   Builder& SetProductName(const std::string& product_name);
186   Builder& SetFileSystemUUID(const std::string& fs_uuid);
187   Builder& SetStorageDevicePath(const std::string& storage_device_path_);
188   Builder& SetDeviceType(DeviceType device_type);
189   Builder& SetSizeInBytes(uint64_t total_size_in_bytes);
190   Builder& SetIsParent(bool is_parent);
191   Builder& SetIsReadOnlyHardware(bool is_read_only_hardware);
192   Builder& SetHasMedia(bool has_media);
193   Builder& SetOnBootDevice(bool on_boot_device);
194   Builder& SetOnRemovableDevice(bool on_removable_device);
195   Builder& SetIsHidden(bool is_hidden);
196   Builder& SetFileSystemType(const std::string& file_system_type);
197   Builder& SetBaseMountPath(const std::string& base_mount_path);
198   Builder& SetIsMounted(bool is_mounted);
199 
200   std::unique_ptr<Disk> Build();
201 
202  private:
203   std::unique_ptr<Disk> disk_;
204 
205   DISALLOW_COPY_AND_ASSIGN(Builder);
206 };
207 
208 }  // namespace disks
209 }  // namespace chromeos
210 
211 #endif  // CHROMEOS_DISKS_DISK_H_
212