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