1 /*
2 * Copyright (c) 2025-2025 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 #include "b_utils/scan_file_singleton.h"
16 #include <filemgmt_libhilog.h>
17
18 namespace OHOS::FileManagement::Backup {
19
GetInstance()20 ScanFileSingleton& ScanFileSingleton::GetInstance()
21 {
22 static ScanFileSingleton instance;
23 return instance;
24 }
25
~ScanFileSingleton()26 ScanFileSingleton::~ScanFileSingleton()
27 {
28 isCalculateCompleted_ = false;
29 }
30
AddBigFile(const std::string & key,const struct stat & value)31 void ScanFileSingleton::AddBigFile(const std::string& key, const struct stat& value)
32 {
33 std::lock_guard<std::mutex> lock(mutexLock_);
34 bigFileQueue_.push({key, value});
35 waitForFilesAddCv_.notify_all();
36 }
37
AddSmallFile(const std::string & key,size_t value)38 void ScanFileSingleton::AddSmallFile(const std::string& key, size_t value)
39 {
40 std::lock_guard<std::mutex> lock(mutexLock_);
41 smallFiles_[key] = value;
42 }
43
GetAllBigFiles()44 std::map<std::string, struct stat> ScanFileSingleton::GetAllBigFiles()
45 {
46 std::lock_guard<std::mutex> lock(mutexLock_);
47 std::map<std::string, struct stat> fileMap;
48 while (!bigFileQueue_.empty()) {
49 fileMap[bigFileQueue_.front().first] = bigFileQueue_.front().second;
50 bigFileQueue_.pop();
51 }
52 return fileMap;
53 }
54
GetCompletedFlag()55 bool ScanFileSingleton::GetCompletedFlag()
56 {
57 std::lock_guard<std::mutex> lock(mutexLock_);
58 return isCalculateCompleted_;
59 }
60
SetCompletedFlag(bool value)61 void ScanFileSingleton::SetCompletedFlag(bool value)
62 {
63 std::lock_guard<std::mutex> lock(mutexLock_);
64 isCalculateCompleted_ = value;
65 if (value) {
66 waitForFilesAddCv_.notify_all();
67 }
68 }
69
GetAllSmallFiles()70 std::map<std::string, size_t> ScanFileSingleton::GetAllSmallFiles()
71 {
72 std::lock_guard<std::mutex> lock(mutexLock_);
73 return smallFiles_;
74 }
75
SetIncludeSize(uint32_t includeSize)76 void ScanFileSingleton::SetIncludeSize(uint32_t includeSize)
77 {
78 std::lock_guard<std::mutex> lock(mutexLock_);
79 includeSize_ = includeSize;
80 }
81
SetExcludeSize(uint32_t excludeSize)82 void ScanFileSingleton::SetExcludeSize(uint32_t excludeSize)
83 {
84 std::lock_guard<std::mutex> lock(mutexLock_);
85 excludeSize_ = excludeSize;
86 }
87
GetIncludeSize()88 uint32_t ScanFileSingleton::GetIncludeSize()
89 {
90 std::lock_guard<std::mutex> lock(mutexLock_);
91 return includeSize_;
92 }
93
GetExcludeSize()94 uint32_t ScanFileSingleton::GetExcludeSize()
95 {
96 std::lock_guard<std::mutex> lock(mutexLock_);
97 return excludeSize_;
98 }
99
WaitForFiles()100 void ScanFileSingleton::WaitForFiles()
101 {
102 HILOGI("calculate is uncompleted, need to wait");
103 std::unique_lock<std::mutex> lock(mutexLock_);
104 waitForFilesAddCv_.wait(lock, [this] {return !bigFileQueue_.empty() || isCalculateCompleted_; });
105 }
106
107 } // namespace OHOS::FileManagement::Backup