disk.h 7.3 KB

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