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