1 /*
2 * Copyright (c) 2023 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 "cloud_download_uri_manager.h"
17 #include "dfs_error.h"
18 #include "utils_log.h"
19
20 namespace OHOS::FileManagement::CloudSync {
21
GetInstance()22 CloudDownloadUriManager& CloudDownloadUriManager::GetInstance()
23 {
24 static CloudDownloadUriManager mgr;
25 return mgr;
26 }
27
AddPathToUri(const std::string & path,const std::string & uri)28 int32_t CloudDownloadUriManager::AddPathToUri(const std::string& path, const std::string& uri)
29 {
30 LOGI("download_file : add path [ %{public}s ] -> uri [ %{public}s ]",
31 GetAnonyString(path).c_str(), GetAnonyString(uri).c_str());
32 std::lock_guard<std::mutex> lock(pathUriMutex_);
33 if (pathUriMap_.find(path) == pathUriMap_.end()) {
34 pathUriMap_[path] = uri;
35 LOGI("download_file : add path [ %{public}s ] success, pathUriMap_ size is %{public}zu",
36 GetAnonyString(path).c_str(), pathUriMap_.size());
37 return E_OK;
38 }
39 LOGE("file is already trigger downloading, pathUriMap_ size is %{public}zu", pathUriMap_.size());
40 return E_STOP;
41 }
42
AddDownloadIdToPath(int64_t & downloadId,std::vector<std::string> & pathVec)43 int32_t CloudDownloadUriManager::AddDownloadIdToPath(int64_t &downloadId, std::vector<std::string> &pathVec)
44 {
45 std::lock_guard<std::mutex> lock(downloadIdPathMutex_);
46 if (downloadIdPathMap_.find(downloadId) == downloadIdPathMap_.end()) {
47 downloadIdPathMap_[downloadId] = pathVec;
48 LOGI("download_file : AddDownloadIdToPath add downloadId %{public}lld, downloadIdPathMap_ size is %{public}zu",
49 static_cast<long long>(downloadId), downloadIdPathMap_.size());
50 }
51 return E_OK;
52 }
53
RemoveUri(const std::string & path)54 void CloudDownloadUriManager::RemoveUri(const std::string& path)
55 {
56 std::lock_guard<std::mutex> lock(pathUriMutex_);
57 if (pathUriMap_.find(path) != pathUriMap_.end()) {
58 LOGI("download_file : remove path [ %{public}s ] success, pathUriMap_ size is %{public}zu",
59 GetAnonyString(path).c_str(), pathUriMap_.size());
60 pathUriMap_.erase(path);
61 }
62 }
CheckDownloadIdPathMap(int64_t & downloadId)63 void CloudDownloadUriManager::CheckDownloadIdPathMap(int64_t &downloadId)
64 {
65 bool existUri = false;
66 std::lock_guard<std::mutex> lock(downloadIdPathMutex_);
67 if (downloadIdPathMap_.find(downloadId) != downloadIdPathMap_.end()) {
68 std::vector<std::string> pathVec = downloadIdPathMap_[downloadId];
69
70 std::lock_guard<std::mutex> lock2(pathUriMutex_);
71 for (unsigned long i = 0; i < pathVec.size(); i++) {
72 if (pathUriMap_.find(pathVec[i]) != pathUriMap_.end()) {
73 existUri = true;
74 break;
75 }
76 }
77 if (!existUri) {
78 downloadIdPathMap_.erase(downloadId);
79 LOGI("download_file : remove downloadId [ %{public}lld ] success, downloadIdPathMap_ size is %{public}zu",
80 static_cast<long long>(downloadId), downloadIdPathMap_.size());
81 }
82 }
83 }
84
RemoveUri(const int64_t & downloadId)85 void CloudDownloadUriManager::RemoveUri(const int64_t &downloadId)
86 {
87 std::lock_guard<std::mutex> lock(downloadIdPathMutex_);
88 if (downloadIdPathMap_.find(downloadId) != downloadIdPathMap_.end()) {
89 std::vector<std::string> pathVec = downloadIdPathMap_[downloadId];
90
91 std::lock_guard<std::mutex> lock2(pathUriMutex_);
92 for (unsigned long i = 0; i < pathVec.size(); i++) {
93 if (pathUriMap_.find(pathVec[i]) != pathUriMap_.end()) {
94 pathUriMap_.erase(pathVec[i]);
95 LOGI("download_file : remove path [ %{public}s ] success, pathUriMap_ size is %{public}zu",
96 GetAnonyString(pathVec[i]).c_str(), pathUriMap_.size());
97 }
98 }
99 downloadIdPathMap_.erase(downloadId);
100 LOGI("download_file : remove downloadId [ %{public}lld ] success, downloadIdPathMap_ size is %{public}zu",
101 static_cast<long long>(downloadId), downloadIdPathMap_.size());
102 }
103 }
104
GetUri(const std::string & path)105 std::string CloudDownloadUriManager::GetUri(const std::string& path)
106 {
107 std::lock_guard<std::mutex> lock(pathUriMutex_);
108 if (pathUriMap_.find(path) != pathUriMap_.end()) {
109 LOGI("download_file : get path [ %{public}s ] success, pathUriMap_ size is %{public}zu",
110 GetAnonyString(path).c_str(), pathUriMap_.size());
111 return pathUriMap_[path];
112 }
113
114 LOGE("download_file : get path [ %{public}s ] fail, pathUriMap_ size is %{public}zu",
115 GetAnonyString(path).c_str(), pathUriMap_.size());
116 return "";
117 }
118
Reset()119 void CloudDownloadUriManager::Reset()
120 {
121 std::lock_guard<std::mutex> lock(pathUriMutex_);
122 pathUriMap_.clear();
123 }
124 } // namespace OHOS::FileManagement::CloudSync
125