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