• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_service.h"
17 
18 #include "disk.h"
19 #include "disk/disk_manager_service.h"
20 #include "safe_map.h"
21 #include "storage_daemon_communication/storage_daemon_communication.h"
22 #include "storage_service_errno.h"
23 #include "storage_service_log.h"
24 #include "volume/notification.h"
25 
26 using namespace std;
27 
28 namespace OHOS {
29 namespace StorageManager {
VolumeManagerService()30     VolumeManagerService::VolumeManagerService() {}
~VolumeManagerService()31     VolumeManagerService::~VolumeManagerService() {}
32 
VolumeStateNotify(VolumeState state,std::shared_ptr<VolumeExternal> volume)33     void VolumeManagerService::VolumeStateNotify(VolumeState state, std::shared_ptr<VolumeExternal> volume)
34     {
35         DelayedSingleton<Notification>::GetInstance()->NotifyVolumeChange(state, volume);
36     }
37 
OnVolumeCreated(VolumeCore vc)38     void VolumeManagerService::OnVolumeCreated(VolumeCore vc)
39     {
40         auto volumePtr = make_shared<VolumeExternal>(vc);
41         volumeMap_.Insert(volumePtr->GetId(), volumePtr);
42         Mount(volumePtr->GetId());
43     }
44 
OnVolumeStateChanged(string volumeId,VolumeState state)45     void VolumeManagerService::OnVolumeStateChanged(string volumeId, VolumeState state)
46     {
47         if (!volumeMap_.Contains(volumeId)) {
48             LOGE("VolumeManagerService::OnVolumeDestroyed volumeId %{public}s not exists", volumeId.c_str());
49             return;
50         }
51         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
52         VolumeStateNotify(state, volumePtr);
53         if (state == VolumeState::REMOVED || state == VolumeState::BAD_REMOVAL) {
54             volumeMap_.Erase(volumeId);
55         }
56     }
57 
OnVolumeMounted(std::string volumeId,int fsType,std::string fsUuid,std::string path,std::string description)58     void VolumeManagerService::OnVolumeMounted(std::string volumeId, int fsType, std::string fsUuid,
59         std::string path, std::string description)
60     {
61         if (!volumeMap_.Contains(volumeId)) {
62             LOGE("VolumeManagerService::OnVolumeMounted volumeId %{public}s not exists", volumeId.c_str());
63             return;
64         }
65         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
66         volumePtr->SetFsType(fsType);
67         volumePtr->SetFsUuid(fsUuid);
68         volumePtr->SetPath(path);
69         std::string des = description;
70         if (des == "") {
71             auto disk = DelayedSingleton<DiskManagerService>::GetInstance()->GetDiskById(volumePtr->GetDiskId());
72             if (disk != nullptr) {
73                 if (disk->GetFlag() == SD_FLAG) {
74                     des = "MySDCard";
75                 } else if (disk->GetFlag() == USB_FLAG) {
76                     des = "MyUSB";
77                 } else {
78                     des = "Default";
79                 }
80             }
81         }
82         volumePtr->SetDescription(des);
83         volumePtr->SetState(VolumeState::MOUNTED);
84         VolumeStateNotify(VolumeState::MOUNTED, volumePtr);
85     }
86 
Mount(std::string volumeId)87     int32_t VolumeManagerService::Mount(std::string volumeId)
88     {
89         if (!volumeMap_.Contains(volumeId)) {
90             LOGE("VolumeManagerService::Mount volumeId %{public}s not exists", volumeId.c_str());
91             return E_NON_EXIST;
92         }
93         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
94         if (volumePtr->GetState() != VolumeState::UNMOUNTED) {
95             LOGE("VolumeManagerService::The type of volume(Id %{public}s) is not unmounted", volumeId.c_str());
96             return E_MOUNT;
97         }
98         std::shared_ptr<StorageDaemonCommunication> sdCommunication;
99         sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
100         int32_t result = Check(volumePtr->GetId());
101         if (result == E_OK) {
102             result = sdCommunication->Mount(volumeId, 0);
103             if (result != E_OK) {
104                 volumePtr->SetState(VolumeState::UNMOUNTED);
105             }
106         } else {
107             volumePtr->SetState(VolumeState::UNMOUNTED);
108         }
109         return result;
110     }
111 
Unmount(std::string volumeId)112     int32_t VolumeManagerService::Unmount(std::string volumeId)
113     {
114         if (!volumeMap_.Contains(volumeId)) {
115             LOGE("VolumeManagerService::Unmount volumeId %{public}s not exists", volumeId.c_str());
116             return E_NON_EXIST;
117         }
118         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
119         if (volumePtr->GetState() != VolumeState::MOUNTED) {
120             LOGE("VolumeManagerService::The type of volume(Id %{public}s) is not mounted", volumeId.c_str());
121             return E_UMOUNT;
122         }
123         std::shared_ptr<StorageDaemonCommunication> sdCommunication;
124         sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
125         volumePtr->SetState(VolumeState::EJECTING);
126         int32_t result = sdCommunication->Unmount(volumeId);
127         if (result == E_OK) {
128             volumePtr->SetState(VolumeState::UNMOUNTED);
129             volumePtr->Reset();
130         } else {
131             volumePtr->SetState(VolumeState::MOUNTED);
132         }
133         return result;
134     }
135 
Check(std::string volumeId)136     int32_t VolumeManagerService::Check(std::string volumeId)
137     {
138         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
139         std::shared_ptr<StorageDaemonCommunication> sdCommunication;
140         sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
141         volumePtr->SetState(VolumeState::CHECKING);
142         int32_t result = sdCommunication->Check(volumeId);
143         return result;
144     }
145 
GetAllVolumes()146     vector<VolumeExternal> VolumeManagerService::GetAllVolumes()
147     {
148         vector<VolumeExternal> result;
149         for (auto it = volumeMap_.Begin(); it != volumeMap_.End(); ++it) {
150             VolumeExternal vc = *(it->second);
151             result.push_back(vc);
152         }
153         return result;
154     }
155 
GetVolumeByUuid(std::string volumeUuid)156     std::shared_ptr<VolumeExternal> VolumeManagerService::GetVolumeByUuid(std::string volumeUuid)
157     {
158         for (auto it = volumeMap_.Begin(); it != volumeMap_.End(); ++it) {
159             auto vc = it->second;
160             if (vc->GetUuid() == volumeUuid) {
161                 LOGE("VolumeManagerService::GetVolumeByUuid volumeUuid %{public}s exists", volumeUuid.c_str());
162                 return vc;
163             }
164         }
165         return nullptr;
166     }
167 
GetVolumeByUuid(std::string fsUuid,VolumeExternal & vc)168     int32_t VolumeManagerService::GetVolumeByUuid(std::string fsUuid, VolumeExternal &vc)
169     {
170         for (auto it = volumeMap_.Begin(); it != volumeMap_.End(); ++it) {
171             auto volume = it->second;
172             if (volume->GetUuid() == fsUuid) {
173                 LOGI("VolumeManagerService::GetVolumeByUuid volumeUuid %{public}s exists", fsUuid.c_str());
174                 vc = *volume;
175                 return E_OK;
176             }
177         }
178         return E_NON_EXIST;
179     }
180 
GetVolumeById(std::string volumeId,VolumeExternal & vc)181     int32_t VolumeManagerService::GetVolumeById(std::string volumeId, VolumeExternal &vc)
182     {
183         if (volumeMap_.Contains(volumeId)) {
184             vc = *volumeMap_[volumeId];
185             return E_OK;
186         }
187         return E_NON_EXIST;
188     }
189 
SetVolumeDescription(std::string fsUuid,std::string description)190     int32_t VolumeManagerService::SetVolumeDescription(std::string fsUuid, std::string description)
191     {
192         for (auto it = volumeMap_.Begin(); it != volumeMap_.End(); ++it) {
193             auto volume = it->second;
194             if (volume->GetUuid() == fsUuid) {
195                 LOGI("VolumeManagerService::SetVolumeDescription volumeUuid %{public}s exists", fsUuid.c_str());
196                 if (volume->GetState() != VolumeState::UNMOUNTED) {
197                     LOGE("VolumeManagerService::SetVolumeDescription volume state is not unmounted!");
198                     return E_VOL_STATE;
199                 }
200                 std::shared_ptr<StorageDaemonCommunication> sdCommunication;
201                 sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
202                 return sdCommunication->SetVolumeDescription(volume->GetId(), description);
203             }
204         }
205         return E_NON_EXIST;
206     }
207 
Format(std::string volumeId,std::string fsType)208     int32_t VolumeManagerService::Format(std::string volumeId, std::string fsType)
209     {
210         if (volumeMap_.Find(volumeId) == volumeMap_.End()) {
211             return E_NON_EXIST;
212         }
213         if (volumeMap_[volumeId]->GetState() != VolumeState::UNMOUNTED) {
214             LOGE("VolumeManagerService::SetVolumeDescription volume state is not unmounted!");
215             return E_VOL_STATE;
216         }
217         // check fstype!!!!
218         std::shared_ptr<StorageDaemonCommunication> sdCommunication;
219         sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
220         return sdCommunication->Format(volumeId, fsType);
221     }
222 } // StorageManager
223 } // OHOS
224