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 #define MLOG_TAG "MtpStorageManager"
16 #include "mtp_storage_manager.h"
17 #include <filesystem>
18 #include <mutex>
19 #include "media_log.h"
20 #include "medialibrary_errno.h"
21 #include "parameter.h"
22
23 using namespace std;
24
25 namespace OHOS {
26 namespace Media {
27 namespace {
28 enum SizeType {
29 TOTAL,
30 FREE,
31 USED
32 };
33 const std::string PUBLIC_PATH_DATA = "/storage/media/local/files/Docs";
34 const std::string CHINESE_ABBREVIATION = "zh-Hans";
35 const std::string ENGLISH_ABBREVIATION = "en-Latn-US";
36 const std::string INNER_STORAGE_DESC_ZH = "内部存储";
37 const std::string INNER_STORAGE_DESC_EN = "Internal storage";
38 const std::string EXTER_STORAGE_DESC_ZH = "存储卡";
39 const std::string EXTER_STORAGE_DESC_EN = "Memory Card";
40 const std::string UNSPECIFIED = "Unspecified";
41 const std::string LANGUAGE_KEY = "persist.global.language";
42 const std::string DEFAULT_LANGUAGE_KEY = "const.global.language";
43 constexpr int32_t SYSPARA_SIZE = 64;
44 } // namespace
45
46 std::shared_ptr<MtpStorageManager> MtpStorageManager::instance_ = nullptr;
47 std::mutex MtpStorageManager::mutex_;
48
GetInstance()49 std::shared_ptr<MtpStorageManager> MtpStorageManager::GetInstance()
50 {
51 if (instance_ == nullptr) {
52 std::lock_guard<std::mutex> lock(mutex_);
53 if (instance_ == nullptr) {
54 instance_ = std::make_shared<MtpStorageManager>();
55 }
56 }
57 return instance_;
58 }
59
GetTotalSize(const std::string & path)60 int64_t MtpStorageManager::GetTotalSize(const std::string &path)
61 {
62 std::string p = path.empty() ? PUBLIC_PATH_DATA : path;
63 std::error_code ec;
64 auto info = std::filesystem::space(p, ec);
65 CHECK_AND_RETURN_RET_LOG(ec.value() == E_OK, 0, "GetTotalSize failed, errno: %{public}d", errno);
66
67 return info.capacity;
68 }
69
GetFreeSize(const std::string & path)70 int64_t MtpStorageManager::GetFreeSize(const std::string &path)
71 {
72 std::string p = path.empty() ? PUBLIC_PATH_DATA : path;
73 std::error_code ec;
74 auto info = std::filesystem::space(p, ec);
75 CHECK_AND_RETURN_RET_LOG(ec.value() == E_OK, 0, "GetFreeSize failed, errno: %{public}d", errno);
76
77 return info.available;
78 }
79
AddStorage(shared_ptr<Storage> & storage)80 void MtpStorageManager::AddStorage(shared_ptr<Storage> &storage)
81 {
82 if (!storages.empty()) {
83 for (auto stor : storages) {
84 if (stor->GetStorageID() == storage->GetStorageID()) {
85 return;
86 }
87 }
88 }
89 storages.push_back(storage);
90 }
91
RemoveStorage(std::shared_ptr<Storage> & storage)92 void MtpStorageManager::RemoveStorage(std::shared_ptr<Storage> &storage)
93 {
94 auto iter = std::find(storages.begin(), storages.end(), storage);
95 if (iter != storages.end()) {
96 storages.erase(iter);
97 }
98 }
99
GetStorage(uint32_t id)100 shared_ptr<Storage> MtpStorageManager::GetStorage(uint32_t id)
101 {
102 for (auto storage : storages) {
103 if (storage->GetStorageID() == id) {
104 return storage;
105 }
106 }
107 return nullptr;
108 }
109
HasStorage(uint32_t id)110 bool MtpStorageManager::HasStorage(uint32_t id)
111 {
112 bool result = false;
113
114 if (id == MTP_STORAGE_ID_ALL || id == MTP_STORAGE_ID_ALL2) {
115 result = (storages.size() > 0);
116 } else {
117 result = (GetStorage(id) != nullptr);
118 }
119
120 return result;
121 }
122
GetStorages()123 std::vector<std::shared_ptr<Storage>> MtpStorageManager::GetStorages()
124 {
125 return storages;
126 }
127
ClearStorages()128 void MtpStorageManager::ClearStorages()
129 {
130 std::vector<std::shared_ptr<Storage>>().swap(storages);
131 }
132
GetSystemLanguage()133 std::string MtpStorageManager::GetSystemLanguage()
134 {
135 char param[SYSPARA_SIZE] = {0};
136 int status = GetParameter(LANGUAGE_KEY.c_str(), "", param, SYSPARA_SIZE);
137 CHECK_AND_RETURN_RET(status <= 0, param);
138 status = GetParameter(DEFAULT_LANGUAGE_KEY.c_str(), "", param, SYSPARA_SIZE);
139 CHECK_AND_RETURN_RET(status <= 0, param);
140 MEDIA_ERR_LOG("Failed to get system language");
141 return "";
142 }
143
GetStorageDescription(const uint16_t type)144 std::string MtpStorageManager::GetStorageDescription(const uint16_t type)
145 {
146 std::string language = GetSystemLanguage();
147 if (language.empty()) {
148 language = CHINESE_ABBREVIATION;
149 }
150 switch (type) {
151 case MTP_STORAGE_FIXEDRAM:
152 return language == CHINESE_ABBREVIATION ? INNER_STORAGE_DESC_ZH : INNER_STORAGE_DESC_EN;
153 case MTP_STORAGE_REMOVABLERAM:
154 return language == CHINESE_ABBREVIATION ? EXTER_STORAGE_DESC_ZH : EXTER_STORAGE_DESC_EN;
155 default:
156 break;
157 }
158 return UNSPECIFIED;
159 }
160
161 } // namespace Media
162 } // namespace OHOS
163