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