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 "disk/disk_manager_service.h"
17 #include "storage_daemon_communication/storage_daemon_communication.h"
18 #include "storage_service_log.h"
19 #include "storage_service_errno.h"
20
21 namespace OHOS {
22 namespace StorageManager {
DiskManagerService()23 DiskManagerService::DiskManagerService() {}
~DiskManagerService()24 DiskManagerService::~DiskManagerService() {}
25
GetDiskById(std::string diskId)26 std::shared_ptr<Disk> DiskManagerService::GetDiskById(std::string diskId)
27 {
28 if (!diskMap_.Contains(diskId)) {
29 return nullptr;
30 }
31 return diskMap_[diskId];
32 }
33
OnDiskCreated(Disk disk)34 void DiskManagerService::OnDiskCreated(Disk disk)
35 {
36 if (diskMap_.Contains(disk.GetDiskId())) {
37 LOGE("DiskManagerService::OnDiskCreated the disk %{public}s already exists", disk.GetDiskId().c_str());
38 return;
39 }
40 auto diskPtr = std::make_shared<Disk>(disk);
41 diskMap_.Insert(diskPtr->GetDiskId(), diskPtr);
42 }
43
OnDiskDestroyed(std::string diskId)44 void DiskManagerService::OnDiskDestroyed(std::string diskId)
45 {
46 if (!diskMap_.Contains(diskId)) {
47 LOGE("DiskManagerService::OnDiskDestroyed the disk %{public}s doesn't exist", diskId.c_str());
48 return;
49 }
50 diskMap_.Erase(diskId);
51 }
52
Partition(std::string diskId,int32_t type)53 int32_t DiskManagerService::Partition(std::string diskId, int32_t type)
54 {
55 if (!diskMap_.Contains(diskId)) {
56 LOGE("DiskManagerService::Partition the disk %{public}s doesn't exist", diskId.c_str());
57 return E_NON_EXIST;
58 }
59 std::shared_ptr<StorageDaemonCommunication> sdCommunication;
60 sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
61 int32_t err = sdCommunication->Partition(diskId, type);
62 return err;
63 }
64
GetAllDisks()65 std::vector<Disk> DiskManagerService::GetAllDisks()
66 {
67 std::vector<Disk> result;
68 for (auto it = diskMap_.Begin(); it != diskMap_.End(); ++it) {
69 Disk disk = *(it->second);
70 result.push_back(disk);
71 }
72 return result;
73 }
74 }
75 }