• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "mtp_storage_manager.h"
17 #include <mutex>
18 #include <sys/statvfs.h>
19 
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "media_log.h"
23 #include "medialibrary_errno.h"
24 #include "system_ability_definition.h"
25 
26 using namespace std;
27 
28 namespace OHOS {
29 namespace Media {
30 
31 std::shared_ptr<MtpStorageManager> MtpStorageManager::instance_ = nullptr;
32 std::mutex MtpStorageManager::mutex_;
33 
MtpStorageManager(void)34 MtpStorageManager::MtpStorageManager(void)
35 {
36     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37     auto remote = samgr->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
38     proxy_ = iface_cast<StorageManager::IStorageManager>(remote);
39 }
40 
~MtpStorageManager(void)41 MtpStorageManager::~MtpStorageManager(void)
42 {
43 }
44 
GetInstance()45 std::shared_ptr<MtpStorageManager> MtpStorageManager::GetInstance()
46 {
47     if (instance_ == nullptr) {
48         std::lock_guard<std::mutex> lock(mutex_);
49         if (instance_ == nullptr) {
50             instance_ = std::shared_ptr<MtpStorageManager>(new MtpStorageManager());
51         }
52     }
53     return instance_;
54 }
55 
GetTotalSize()56 int64_t MtpStorageManager::GetTotalSize()
57 {
58     struct statvfs diskInfo;
59     int ret = statvfs("/data", &diskInfo);
60     if (ret != 0) {
61         MEDIA_ERR_LOG("GetTotalSize failed, errno: %{public}d", errno);
62         return MTP_FAIL;
63     }
64     int64_t totalSize =
65         static_cast<long long>(diskInfo.f_bsize) * static_cast<long long>(diskInfo.f_blocks);
66     return totalSize;
67 }
68 
GetFreeSize()69 int64_t MtpStorageManager::GetFreeSize()
70 {
71     struct statvfs diskInfo;
72     int ret = statvfs("/data", &diskInfo);
73     if (ret != 0) {
74         MEDIA_ERR_LOG("GetFreeSize failed, errno: %{public}d", errno);
75         return MTP_FAIL;
76     }
77     int64_t freeSize =
78         static_cast<long long>(diskInfo.f_bsize) * static_cast<long long>(diskInfo.f_bfree);
79     return freeSize;
80 }
81 
AddStorage(shared_ptr<Storage> & storage)82 void MtpStorageManager::AddStorage(shared_ptr<Storage> &storage)
83 {
84     storages.push_back(storage);
85 }
86 
RemoveStorage(std::shared_ptr<Storage> & storage)87 void MtpStorageManager::RemoveStorage(std::shared_ptr<Storage> &storage)
88 {
89     auto iter = std::find(storages.begin(), storages.end(), storage);
90     if (iter != storages.end()) {
91         storages.erase(iter);
92     }
93 }
94 
GetStorage(uint32_t id)95 shared_ptr<Storage> MtpStorageManager::GetStorage(uint32_t id)
96 {
97     for (auto storage : storages) {
98         if (storage->GetStorageID() == id) {
99             return storage;
100         }
101     }
102     return nullptr;
103 }
104 
HasStorage(uint32_t id)105 bool MtpStorageManager::HasStorage(uint32_t id)
106 {
107     bool result = false;
108 
109     if (id == MTP_STORAGE_ID_ALL || id == MTP_STORAGE_ID_ALL2) {
110         result = (storages.size() > 0);
111     } else {
112         result = (GetStorage(id) != nullptr);
113     }
114 
115     return result;
116 }
117 
GetStorages()118 std::vector<std::shared_ptr<Storage>> MtpStorageManager::GetStorages()
119 {
120     return storages;
121 }
122 }  // namespace Media
123 }  // namespace OHOS
124