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     int32_t err = DoCreate(device);
40     if (err) {
41         return err;
42     }
43     return E_OK;
44 }
45 
GetVolumeId()46 std::string VolumeInfo::GetVolumeId()
47 {
48     return id_;
49 }
50 
GetVolumeType()51 int32_t VolumeInfo::GetVolumeType()
52 {
53     return type_;
54 }
55 
GetDiskId()56 std::string VolumeInfo::GetDiskId()
57 {
58     return diskId_;
59 }
60 
GetState()61 int32_t VolumeInfo::GetState()
62 {
63     return mountState_;
64 }
65 
Destroy()66 int32_t VolumeInfo::Destroy()
67 {
68     VolumeState state = REMOVED;
69     if (mountState_ == REMOVED || mountState_ == BADREMOVABLE) {
70         return E_OK;
71     }
72     StorageManagerClient client;
73     if (mountState_ != UNMOUNTED) {
74         // force umount
75         UMount(true);
76         state = BADREMOVABLE;
77         if (client.NotifyVolumeStateChanged(id_, StorageManager::VolumeState::BAD_REMOVAL) != E_OK) {
78             LOGE("Volume Notify Bad Removal failed");
79         }
80     } else {
81         if (client.NotifyVolumeStateChanged(id_, StorageManager::VolumeState::REMOVED) != E_OK) {
82             LOGE("Volume Notify Removed failed");
83         }
84     }
85 
86     int32_t err = DoDestroy();
87     if (err) {
88         return err;
89     }
90     mountState_ = state;
91     return E_OK;
92 }
93 
Mount(uint32_t flags)94 int32_t VolumeInfo::Mount(uint32_t flags)
95 {
96     int32_t err = 0;
97 
98     if (mountState_ == MOUNTED) {
99         return E_OK;
100     }
101     if (mountState_ != CHECKING) {
102         LOGE("please check volume %{public}s first", GetVolumeId().c_str());
103         return E_VOL_STATE;
104     }
105 
106     std::string key = "persist.filemanagement.usb.readonly";
107     int handle = static_cast<int>(FindParameter(key.c_str()));
108     if (handle != -1) {
109         char rdOnlyEnable[255] = {"false"};
110         auto res = GetParameterValue(handle, rdOnlyEnable, 255);
111         if (res >= 0 && strncmp(rdOnlyEnable, "true", TRUE_LEN) == 0) {
112             mountFlags_ |= MS_RDONLY;
113         } else {
114             mountFlags_ &= ~MS_RDONLY;
115         }
116     }
117 
118     mountFlags_ |= flags;
119     LOGI("external volume mount start");
120     err = DoMount(mountFlags_);
121     if (err) {
122         mountState_ = UNMOUNTED;
123         return err;
124     }
125     LOGI("external volume mount success");
126     mountState_ = MOUNTED;
127     return E_OK;
128 }
129 
UMount(bool force)130 int32_t VolumeInfo::UMount(bool force)
131 {
132     int32_t err = 0;
133 
134     if (mountState_ == REMOVED || mountState_ == BADREMOVABLE) {
135         LOGE("the volume %{public}s is in REMOVED state", GetVolumeId().c_str());
136         return E_VOL_STATE;
137     }
138 
139     if (mountState_ == UNMOUNTED) {
140         return E_OK;
141     }
142 
143     if (mountState_ == CHECKING) {
144         mountState_ = UNMOUNTED;
145         return E_OK;
146     }
147 
148     if (mountState_ == EJECTING && !force) {
149         return E_WAIT;
150     }
151 
152     mountState_ = EJECTING;
153     StorageManagerClient client;
154     if (client.NotifyVolumeStateChanged(id_, StorageManager::VolumeState::EJECTING) != E_OK) {
155         LOGE("Volume Notify Ejecting failed");
156     }
157 
158     err = DoUMount(force);
159     if (!force && err) {
160         mountState_ = MOUNTED;
161         return err;
162     }
163 
164     mountState_ = UNMOUNTED;
165     if (client.NotifyVolumeStateChanged(id_, StorageManager::VolumeState::UNMOUNTED) != E_OK) {
166         LOGE("Volume Notify Unmounted failed");
167     }
168     return E_OK;
169 }
170 
Check()171 int32_t VolumeInfo::Check()
172 {
173     if (mountState_ != UNMOUNTED) {
174         LOGE("the volume %{public}s is not in UNMOUNT state", GetVolumeId().c_str());
175         return E_VOL_STATE;
176     }
177 
178     if (mountState_ == CHECKING) {
179         mountState_ = UNMOUNTED;
180     }
181 
182     int32_t err = DoCheck();
183     if (err) {
184         return err;
185     }
186     mountState_ = CHECKING;
187     return E_OK;
188 }
189 
Format(std::string type)190 int32_t VolumeInfo::Format(std::string type)
191 {
192     if (mountState_ != UNMOUNTED) {
193         LOGE("Please unmount the volume %{public}s first", GetVolumeId().c_str());
194         return E_VOL_STATE;
195     }
196 
197     int32_t err = DoFormat(type);
198     return err;
199 }
200 
SetVolumeDescription(const std::string description)201 int32_t VolumeInfo::SetVolumeDescription(const std::string description)
202 {
203     if (mountState_ != UNMOUNTED) {
204         LOGE("Please unmount the volume %{public}s first", GetVolumeId().c_str());
205         return E_VOL_STATE;
206     }
207 
208     int32_t err = DoSetVolDesc(description);
209     return err;
210 }
211 } // StorageDaemon
212 } // OHOS
213