• 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 "volume/notification.h"
19 #include "safe_map.h"
20 #include "storage_daemon_communication/storage_daemon_communication.h"
21 
22 #include "storage_service_errno.h"
23 #include "storage_service_log.h"
24 #include "disk.h"
25 #include "disk/disk_manager_service.h"
26 
27 using namespace std;
28 
29 namespace OHOS {
30 namespace StorageManager {
VolumeManagerService()31     VolumeManagerService::VolumeManagerService() {}
~VolumeManagerService()32     VolumeManagerService::~VolumeManagerService() {}
33 
VolumeStateNotify(int32_t state,string volumeId,string diskId,string fsUuid,string path)34     void VolumeManagerService::VolumeStateNotify(int32_t state, string volumeId, string diskId,
35         string fsUuid, string path)
36     {
37         DelayedSingleton<Notification>::GetInstance()->NotifyVolumeChange(state, volumeId, diskId, fsUuid, path);
38     }
39 
OnVolumeCreated(VolumeCore vc)40     void VolumeManagerService::OnVolumeCreated(VolumeCore vc)
41     {
42         auto volumePtr = make_shared<VolumeExternal>(vc);
43         volumeMap_.Insert(volumePtr->GetId(), volumePtr);
44         Mount(volumePtr->GetId());
45     }
46 
OnVolumeDestroyed(string volumeId)47     void VolumeManagerService::OnVolumeDestroyed(string volumeId)
48     {
49         if (!volumeMap_.Contains(volumeId)) {
50             LOGE("VolumeManagerService::OnVolumeDestroyed volumeId %{public}s not exists", volumeId.c_str());
51             return;
52         }
53         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
54         int32_t state = VOLUME_REMOVED;
55         if (volumePtr->GetState() != VolumeState::UNMOUNTED) {
56             state =  VOLUME_BAD_REMOVAL;
57         }
58         VolumeStateNotify(state, volumeId, volumePtr->GetDiskId(), "", "");
59         volumeMap_.Erase(volumeId);
60     }
61 
OnVolumeMounted(std::string volumeId,int fsType,std::string fsUuid,std::string path,std::string description)62     void VolumeManagerService::OnVolumeMounted(std::string volumeId, int fsType, std::string fsUuid,
63         std::string path, std::string description)
64     {
65         if (!volumeMap_.Contains(volumeId)) {
66             LOGE("VolumeManagerService::OnVolumeMounted volumeId %{public}s not exists", volumeId.c_str());
67             return;
68         }
69         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
70         volumePtr->SetFsType(fsType);
71         volumePtr->SetFsUuid(fsUuid);
72         volumePtr->SetPath(path);
73         std::string des = description;
74         if (des == "") {
75             auto disk = DelayedSingleton<DiskManagerService>::GetInstance()->GetDiskById(volumePtr->GetDiskId());
76             if (disk != nullptr) {
77                 if (disk->GetFlag() == SD_FLAG) {
78                     des = "MySDCard";
79                 } else if (disk->GetFlag() == USB_FLAG) {
80                     des = "MyUSB";
81                 } else {
82                     des = "Default";
83                 }
84             }
85         }
86         volumePtr->SetDescription(des);
87         volumePtr->SetState(VolumeState::MOUNTED);
88         VolumeStateNotify(VOLUME_MOUNTED, volumeId, volumePtr->GetDiskId(), fsUuid, path);
89     }
90 
Mount(std::string volumeId)91     int32_t VolumeManagerService::Mount(std::string volumeId)
92     {
93         if (!volumeMap_.Contains(volumeId)) {
94             LOGE("VolumeManagerService::Mount volumeId %{public}s not exists", volumeId.c_str());
95             return E_NON_EXIST;
96         }
97         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
98         if (volumePtr->GetState() != VolumeState::UNMOUNTED) {
99             LOGE("VolumeManagerService::The type of volume(Id %{public}s) is not unmounted", volumeId.c_str());
100             return E_MOUNT;
101         }
102         std::shared_ptr<StorageDaemonCommunication> sdCommunication;
103         sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
104         int32_t result = Check(volumePtr->GetId());
105         if (result == E_OK) {
106             result = sdCommunication->Mount(volumeId, 0);
107             if (result != E_OK) {
108                 volumePtr->SetState(VolumeState::UNMOUNTED);
109             }
110         } else {
111             volumePtr->SetState(VolumeState::UNMOUNTED);
112         }
113         return result;
114     }
115 
Unmount(std::string volumeId)116     int32_t VolumeManagerService::Unmount(std::string volumeId)
117     {
118         if (!volumeMap_.Contains(volumeId)) {
119             LOGE("VolumeManagerService::Unmount volumeId %{public}s not exists", volumeId.c_str());
120             return E_NON_EXIST;
121         }
122         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
123         if (volumePtr->GetState() != VolumeState::MOUNTED) {
124             LOGE("VolumeManagerService::The type of volume(Id %{public}s) is not mounted", volumeId.c_str());
125             return E_UMOUNT;
126         }
127         std::shared_ptr<StorageDaemonCommunication> sdCommunication;
128         sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
129         volumePtr->SetState(VolumeState::EJECTING);
130         VolumeStateNotify(VOLUME_EJECT, volumeId, volumePtr->GetDiskId(), "", "");
131         int32_t result = sdCommunication->Unmount(volumeId);
132         if (result == E_OK) {
133             volumePtr->SetState(VolumeState::UNMOUNTED);
134             volumePtr->Reset();
135             VolumeStateNotify(VOLUME_UNMOUNTED, volumeId, volumePtr->GetDiskId(), "", "");
136         } else {
137             volumePtr->SetState(VolumeState::MOUNTED);
138         }
139         return result;
140     }
141 
Check(std::string volumeId)142     int32_t VolumeManagerService::Check(std::string volumeId)
143     {
144         std::shared_ptr<VolumeExternal> volumePtr = volumeMap_[volumeId];
145         std::shared_ptr<StorageDaemonCommunication> sdCommunication;
146         sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
147         volumePtr->SetState(VolumeState::CHECKING);
148         int32_t result = sdCommunication->Check(volumeId);
149         return result;
150     }
151 
GetAllVolumes()152     vector<VolumeExternal> VolumeManagerService::GetAllVolumes()
153     {
154         vector<VolumeExternal> result;
155         for (auto it = volumeMap_.Begin(); it != volumeMap_.End(); ++it) {
156             VolumeExternal vc = *(it->second);
157             result.push_back(vc);
158         }
159         return result;
160     }
161 
GetVolumeByUuid(std::string volumeUuid)162     std::shared_ptr<VolumeExternal> VolumeManagerService::GetVolumeByUuid(std::string volumeUuid)
163     {
164         for (auto it = volumeMap_.Begin(); it != volumeMap_.End(); ++it) {
165             auto vc = it->second;
166             if (vc->GetUuid() == volumeUuid) {
167                 LOGE("VolumeManagerService::GetVolumeByUuid volumeUuid %{public}s exists", volumeUuid.c_str());
168                 return vc;
169             }
170         }
171         return nullptr;
172     }
173 } // StorageManager
174 } // OHOS