disk.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include "ash/components/disks/disk.h"
  5. #include <utility>
  6. #include "base/files/file_path.h"
  7. #include "base/memory/ptr_util.h"
  8. namespace ash {
  9. namespace disks {
  10. namespace {
  11. constexpr char kStatefulPartition[] = "/mnt/stateful_partition";
  12. } // namespace
  13. Disk::Disk(const DiskInfo& disk_info,
  14. bool write_disabled_by_policy,
  15. const std::string& base_mount_path)
  16. : device_path_(disk_info.device_path()),
  17. mount_path_(disk_info.mount_path()),
  18. write_disabled_by_policy_(write_disabled_by_policy),
  19. file_path_(disk_info.file_path()),
  20. device_label_(disk_info.label()),
  21. drive_label_(disk_info.drive_label()),
  22. vendor_id_(disk_info.vendor_id()),
  23. vendor_name_(disk_info.vendor_name()),
  24. product_id_(disk_info.product_id()),
  25. product_name_(disk_info.product_name()),
  26. fs_uuid_(disk_info.uuid()),
  27. storage_device_path_(disk_info.storage_device_path()),
  28. device_type_(disk_info.device_type()),
  29. bus_number_(disk_info.bus_number()),
  30. device_number_(disk_info.device_number()),
  31. total_size_in_bytes_(disk_info.total_size_in_bytes()),
  32. is_parent_(disk_info.is_drive()),
  33. is_read_only_hardware_(disk_info.is_read_only()),
  34. has_media_(disk_info.has_media()),
  35. on_boot_device_(disk_info.on_boot_device()),
  36. on_removable_device_(disk_info.on_removable_device()),
  37. is_hidden_(disk_info.is_hidden()),
  38. is_auto_mountable_(disk_info.is_auto_mountable()),
  39. // cros-disks only provides mount paths if the disk is actually mounted.
  40. is_mounted_(!disk_info.mount_path().empty()),
  41. file_system_type_(disk_info.file_system_type()),
  42. base_mount_path_(base_mount_path) {}
  43. Disk::Disk() = default;
  44. Disk::Disk(const Disk&) = default;
  45. Disk::~Disk() = default;
  46. void Disk::SetMountPath(const std::string& mount_path) {
  47. mount_path_ = mount_path;
  48. if (base_mount_path_.empty())
  49. base_mount_path_ = mount_path;
  50. }
  51. bool Disk::IsStatefulPartition() const {
  52. return mount_path_ == kStatefulPartition;
  53. }
  54. Disk::Builder::Builder() : disk_(base::WrapUnique(new Disk())) {}
  55. Disk::Builder::~Builder() = default;
  56. Disk::Builder& Disk::Builder::SetDevicePath(const std::string& device_path) {
  57. disk_->device_path_ = device_path;
  58. return *this;
  59. }
  60. Disk::Builder& Disk::Builder::SetMountPath(const std::string& mount_path) {
  61. disk_->mount_path_ = mount_path;
  62. return *this;
  63. }
  64. Disk::Builder& Disk::Builder::SetWriteDisabledByPolicy(
  65. bool write_disabled_by_policy) {
  66. disk_->write_disabled_by_policy_ = write_disabled_by_policy;
  67. return *this;
  68. }
  69. Disk::Builder& Disk::Builder::SetFilePath(const std::string& file_path) {
  70. disk_->file_path_ = file_path;
  71. return *this;
  72. }
  73. Disk::Builder& Disk::Builder::SetDeviceLabel(const std::string& device_label) {
  74. disk_->device_label_ = device_label;
  75. return *this;
  76. }
  77. Disk::Builder& Disk::Builder::SetDriveLabel(const std::string& drive_label) {
  78. disk_->drive_label_ = drive_label;
  79. return *this;
  80. }
  81. Disk::Builder& Disk::Builder::SetVendorId(const std::string& vendor_id) {
  82. disk_->vendor_id_ = vendor_id;
  83. return *this;
  84. }
  85. Disk::Builder& Disk::Builder::SetVendorName(const std::string& vendor_name) {
  86. disk_->vendor_name_ = vendor_name;
  87. return *this;
  88. }
  89. Disk::Builder& Disk::Builder::SetProductId(const std::string& product_id) {
  90. disk_->product_id_ = product_id;
  91. return *this;
  92. }
  93. Disk::Builder& Disk::Builder::SetProductName(const std::string& product_name) {
  94. disk_->product_name_ = product_name;
  95. return *this;
  96. }
  97. Disk::Builder& Disk::Builder::SetFileSystemUUID(const std::string& fs_uuid) {
  98. disk_->fs_uuid_ = fs_uuid;
  99. return *this;
  100. }
  101. Disk::Builder& Disk::Builder::SetStorageDevicePath(
  102. const std::string& storage_device_path) {
  103. disk_->storage_device_path_ = storage_device_path;
  104. return *this;
  105. }
  106. Disk::Builder& Disk::Builder::SetDeviceType(DeviceType device_type) {
  107. disk_->device_type_ = device_type;
  108. return *this;
  109. }
  110. Disk::Builder& Disk::Builder::SetBusNumber(int bus_number) {
  111. disk_->bus_number_ = bus_number;
  112. return *this;
  113. }
  114. Disk::Builder& Disk::Builder::SetDeviceNumber(int device_number) {
  115. disk_->device_number_ = device_number;
  116. return *this;
  117. }
  118. Disk::Builder& Disk::Builder::SetSizeInBytes(uint64_t total_size_in_bytes) {
  119. disk_->total_size_in_bytes_ = total_size_in_bytes;
  120. return *this;
  121. }
  122. Disk::Builder& Disk::Builder::SetIsParent(bool is_parent) {
  123. disk_->is_parent_ = is_parent;
  124. return *this;
  125. }
  126. Disk::Builder& Disk::Builder::SetIsReadOnlyHardware(
  127. bool is_read_only_hardware) {
  128. disk_->is_read_only_hardware_ = is_read_only_hardware;
  129. return *this;
  130. }
  131. Disk::Builder& Disk::Builder::SetHasMedia(bool has_media) {
  132. disk_->has_media_ = has_media;
  133. return *this;
  134. }
  135. Disk::Builder& Disk::Builder::SetOnBootDevice(bool on_boot_device) {
  136. disk_->on_boot_device_ = on_boot_device;
  137. return *this;
  138. }
  139. Disk::Builder& Disk::Builder::SetOnRemovableDevice(bool on_removable_device) {
  140. disk_->on_removable_device_ = on_removable_device;
  141. return *this;
  142. }
  143. Disk::Builder& Disk::Builder::SetIsHidden(bool is_hidden) {
  144. disk_->is_hidden_ = is_hidden;
  145. return *this;
  146. }
  147. Disk::Builder& Disk::Builder::SetFileSystemType(
  148. const std::string& file_system_type) {
  149. disk_->file_system_type_ = file_system_type;
  150. return *this;
  151. }
  152. Disk::Builder& Disk::Builder::SetBaseMountPath(
  153. const std::string& base_mount_path) {
  154. disk_->base_mount_path_ = base_mount_path;
  155. return *this;
  156. }
  157. std::unique_ptr<Disk> Disk::Builder::Build() {
  158. return std::move(disk_);
  159. }
  160. Disk::Builder& Disk::Builder::SetIsMounted(bool is_mounted) {
  161. disk_->is_mounted_ = is_mounted;
  162. return *this;
  163. }
  164. base::FilePath GetStatefulPartitionPath() {
  165. return base::FilePath(kStatefulPartition);
  166. }
  167. } // namespace disks
  168. } // namespace ash