1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "volume/volume_info.h"
17
18 #include <sys/mount.h>
19
20 #include "ipc/storage_manager_client.h"
21 #include "storage_service_errno.h"
22 #include "storage_service_log.h"
23 #include "utils/string_utils.h"
24 #include "parameter.h"
25
26 using namespace std;
27 namespace OHOS {
28 namespace StorageDaemon {
29 const int32_t TRUE_LEN = 5;
Create(const std::string volId,const std::string diskId,dev_t device)30 int32_t VolumeInfo::Create(const std::string volId, const std::string diskId, dev_t device)
31 {
32 id_ = volId;
33 diskId_ = diskId;
34 type_ = EXTERNAL;
35 mountState_ = UNMOUNTED;
36 mountFlags_ = 0;
37 userIdOwner_ = 0;
38
39 std::string key = "persist.filemanagement.usb.readonly";
40 int handle = static_cast<int>(FindParameter(key.c_str()));
41 if (handle != -1) {
42 char rdOnlyEnable[255] = {"false"};
43 auto res = GetParameterValue(handle, rdOnlyEnable, 255);
44 if (res >= 0 && strncmp(rdOnlyEnable, "true", TRUE_LEN) == 0) {
45 mountFlags_ |= MS_RDONLY;
46 }
47 }
48
49 int32_t err = DoCreate(device);
50 if (err) {
51 return err;
52 }
53 return E_OK;
54 }
55
GetVolumeId()56 std::string VolumeInfo::GetVolumeId()
57 {
58 return id_;
59 }
60
GetVolumeType()61 int32_t VolumeInfo::GetVolumeType()
62 {
63 return type_;
64 }
65
GetDiskId()66 std::string VolumeInfo::GetDiskId()
67 {
68 return diskId_;
69 }
70
GetState()71 int32_t VolumeInfo::GetState()
72 {
73 return mountState_;
74 }
75
Destroy()76 int32_t VolumeInfo::Destroy()
77 {
78 VolumeState state = REMOVED;
79 if (mountState_ == REMOVED || mountState_ == BADREMOVABLE) {
80 return E_OK;
81 }
82 StorageManagerClient client;
83 if (mountState_ != UNMOUNTED) {
84 // force umount
85 UMount(true);
86 state = BADREMOVABLE;
87 if (client.NotifyVolumeStateChanged(id_, StorageManager::VolumeState::BAD_REMOVAL) != E_OK) {
88 LOGE("Volume Notify Bad Removal failed");
89 }
90 } else {
91 if (client.NotifyVolumeStateChanged(id_, StorageManager::VolumeState::REMOVED) != E_OK) {
92 LOGE("Volume Notify Removed failed");
93 }
94 }
95
96 int32_t err = DoDestroy();
97 if (err) {
98 return err;
99 }
100 mountState_ = state;
101 return E_OK;
102 }
103
Mount(uint32_t flags)104 int32_t VolumeInfo::Mount(uint32_t flags)
105 {
106 int32_t err = 0;
107
108 if (mountState_ == MOUNTED) {
109 return E_OK;
110 }
111 if (mountState_ != CHECKING) {
112 LOGE("please check volume %{public}s first", GetVolumeId().c_str());
113 return E_VOL_STATE;
114 }
115
116 mountFlags_ |= flags;
117 err = DoMount(mountFlags_);
118 if (err) {
119 mountState_ = UNMOUNTED;
120 return err;
121 }
122
123 mountState_ = MOUNTED;
124 return E_OK;
125 }
126
UMount(bool force)127 int32_t VolumeInfo::UMount(bool force)
128 {
129 int32_t err = 0;
130
131 if (mountState_ == REMOVED || mountState_ == BADREMOVABLE) {
132 LOGE("the volume %{public}s is in REMOVED state", GetVolumeId().c_str());
133 return E_VOL_STATE;
134 }
135
136 if (mountState_ == UNMOUNTED) {
137 return E_OK;
138 }
139
140 if (mountState_ == CHECKING) {
141 mountState_ = UNMOUNTED;
142 return E_OK;
143 }
144
145 if (mountState_ == EJECTING && !force) {
146 return E_WAIT;
147 }
148
149 mountState_ = EJECTING;
150 StorageManagerClient client;
151 if (client.NotifyVolumeStateChanged(id_, StorageManager::VolumeState::EJECTING) != E_OK) {
152 LOGE("Volume Notify Ejecting failed");
153 }
154
155 err = DoUMount(force);
156 if (!force && err) {
157 mountState_ = MOUNTED;
158 return err;
159 }
160
161 mountState_ = UNMOUNTED;
162 if (client.NotifyVolumeStateChanged(id_, StorageManager::VolumeState::UNMOUNTED) != E_OK) {
163 LOGE("Volume Notify Unmounted failed");
164 }
165 return E_OK;
166 }
167
Check()168 int32_t VolumeInfo::Check()
169 {
170 if (mountState_ != UNMOUNTED) {
171 LOGE("the volume %{public}s is not in UNMOUNT state", GetVolumeId().c_str());
172 return E_VOL_STATE;
173 }
174
175 if (mountState_ == CHECKING) {
176 mountState_ = UNMOUNTED;
177 }
178
179 int32_t err = DoCheck();
180 if (err) {
181 return err;
182 }
183 mountState_ = CHECKING;
184 return E_OK;
185 }
186
Format(std::string type)187 int32_t VolumeInfo::Format(std::string type)
188 {
189 if (mountState_ != UNMOUNTED) {
190 LOGE("Please unmount the volume %{public}s first", GetVolumeId().c_str());
191 return E_VOL_STATE;
192 }
193
194 int32_t err = DoFormat(type);
195 return err;
196 }
197
SetVolumeDescription(const std::string description)198 int32_t VolumeInfo::SetVolumeDescription(const std::string description)
199 {
200 if (mountState_ != UNMOUNTED) {
201 LOGE("Please unmount the volume %{public}s first", GetVolumeId().c_str());
202 return E_VOL_STATE;
203 }
204
205 int32_t err = DoSetVolDesc(description);
206 return err;
207 }
208 } // StorageDaemon
209 } // OHOS
210