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_manager.h"
17
18 #include <sys/sysmacros.h>
19
20 #include "ipc/storage_manager_client.h"
21 #include "storage_service_errno.h"
22 #include "storage_service_log.h"
23 #include "utils/storage_radar.h"
24 #include "utils/string_utils.h"
25 #include "volume/external_volume_info.h"
26
27 using namespace std;
28 using namespace OHOS::StorageService;
29 namespace OHOS {
30 namespace StorageDaemon {
31 VolumeManager* VolumeManager::instance_ = nullptr;
32
Instance()33 VolumeManager* VolumeManager::Instance()
34 {
35 if (instance_ == nullptr) {
36 instance_ = new VolumeManager();
37 }
38
39 return instance_;
40 }
41
GetVolume(const std::string volId)42 std::shared_ptr<VolumeInfo> VolumeManager::GetVolume(const std::string volId)
43 {
44 auto it = volumes_.Find(volId);
45 if (it == volumes_.End()) {
46 return nullptr;
47 }
48 return it->second;
49 }
50
CreateVolume(const std::string diskId,dev_t device)51 std::string VolumeManager::CreateVolume(const std::string diskId, dev_t device)
52 {
53 std::string volId = StringPrintf("vol-%u-%u", major(device), minor(device));
54
55 LOGI("create volume %{public}s.", volId.c_str());
56
57 std::shared_ptr<VolumeInfo> tmp = GetVolume(volId);
58 if (tmp != nullptr) {
59 LOGE("volume %{public}s exist.", volId.c_str());
60 return "";
61 }
62
63 auto info = std::make_shared<ExternalVolumeInfo>();
64 int32_t ret = info->Create(volId, diskId, device);
65 if (ret) {
66 return "";
67 }
68
69 volumes_.Insert(volId, info);
70
71 StorageManagerClient client;
72 ret = client.NotifyVolumeCreated(info);
73 if (ret != E_OK) {
74 LOGE("Volume Notify Created failed");
75 }
76
77 return volId;
78 }
79
DestroyVolume(const std::string volId)80 int32_t VolumeManager::DestroyVolume(const std::string volId)
81 {
82 LOGI("destroy volume %{public}s.", volId.c_str());
83
84 std::shared_ptr<VolumeInfo> destroyNode = GetVolume(volId);
85
86 if (destroyNode == nullptr) {
87 LOGE("the volume %{public}s does not exist", volId.c_str());
88 return E_NON_EXIST;
89 }
90
91 int32_t ret = destroyNode->Destroy();
92 if (ret)
93 return ret;
94 volumes_.Erase(volId);
95 destroyNode.reset();
96
97 return E_OK;
98 }
99
Check(const std::string volId)100 int32_t VolumeManager::Check(const std::string volId)
101 {
102 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
103 if (info == nullptr) {
104 LOGE("the volume %{public}s does not exist.", volId.c_str());
105 return E_NON_EXIST;
106 }
107
108 int32_t err = info->Check();
109 if (err != E_OK) {
110 LOGE("the volume %{public}s check failed.", volId.c_str());
111 return err;
112 }
113 return E_OK;
114 }
115
Mount(const std::string volId,uint32_t flags)116 int32_t VolumeManager::Mount(const std::string volId, uint32_t flags)
117 {
118 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
119 if (info == nullptr) {
120 #ifdef SUPPORT_OPEN_SOURCE_MTP_DEVICE
121 return DelayedSingleton<OHOS::StorageDaemon::MtpDeviceMonitor>::GetInstance()->Mount(volId);
122 #else
123 LOGE("the volume %{public}s does not exist.", volId.c_str());
124 return E_NON_EXIST;
125 #endif
126 }
127
128 int32_t err = info->Mount(flags);
129 if (err != E_OK) {
130 LOGE("the volume %{public}s mount failed.", volId.c_str());
131 return err;
132 }
133
134 StorageManagerClient client;
135 err = client.NotifyVolumeMounted(info);
136 if (err) {
137 LOGE("Volume Notify Mount Destroyed failed");
138 }
139 return E_OK;
140 }
141
UMount(const std::string volId)142 int32_t VolumeManager::UMount(const std::string volId)
143 {
144 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
145 if (info == nullptr) {
146 #ifdef SUPPORT_OPEN_SOURCE_MTP_DEVICE
147 return DelayedSingleton<OHOS::StorageDaemon::MtpDeviceMonitor>::GetInstance()->Umount(volId);
148 #else
149 LOGE("the volume %{public}s does not exist.", volId.c_str());
150 return E_NON_EXIST;
151 #endif
152 }
153
154 int32_t err = info->UMount();
155 if (err != E_OK) {
156 LOGE("the volume %{public}s mount failed.", volId.c_str());
157 return err;
158 }
159 return E_OK;
160 }
161
Format(const std::string volId,const std::string fsType)162 int32_t VolumeManager::Format(const std::string volId, const std::string fsType)
163 {
164 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
165 if (info == nullptr) {
166 LOGE("the volume %{public}s does not exist.", volId.c_str());
167 return E_NON_EXIST;
168 }
169
170 int32_t err = info->Format(fsType);
171 if (err != E_OK) {
172 LOGE("the volume %{public}s format failed.", volId.c_str());
173 StorageRadar::ReportVolumeOperation("VolumeInfo::Format", err);
174 return err;
175 }
176
177 return E_OK;
178 }
179
SetVolumeDescription(const std::string volId,const std::string description)180 int32_t VolumeManager::SetVolumeDescription(const std::string volId, const std::string description)
181 {
182 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
183 if (info == nullptr) {
184 LOGE("the volume %{public}s does not exist.", volId.c_str());
185 return E_NON_EXIST;
186 }
187
188 int32_t err = info->SetVolumeDescription(description);
189 if (err != E_OK) {
190 LOGE("the volume %{public}s setVolumeDescription failed.", volId.c_str());
191 StorageRadar::ReportVolumeOperation("VolumeInfo::SetVolumeDescription", err);
192 return err;
193 }
194
195 return E_OK;
196 }
197 } // StorageDaemon
198 } // OHOS
199