1 /*
2 * Copyright (C) 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 #define MLOG_TAG "Scanner"
17
18 #include "media_scanner_manager.h"
19
20 #include "directory_ex.h"
21
22 #include "media_log.h"
23 #include "medialibrary_errno.h"
24 #include "medialibrary_data_manager.h"
25
26 namespace OHOS {
27 namespace Media {
28 std::shared_ptr<MediaScannerManager> MediaScannerManager::instance_ = nullptr;
29 std::mutex MediaScannerManager::instanceMutex_;
30
GetInstance()31 std::shared_ptr<MediaScannerManager> MediaScannerManager::GetInstance()
32 {
33 if (instance_ == nullptr) {
34 std::lock_guard<std::mutex> guard(instanceMutex_);
35
36 if (instance_ != nullptr) {
37 return instance_;
38 }
39
40 instance_ = std::shared_ptr<MediaScannerManager>(new (std::nothrow) MediaScannerManager());
41 }
42
43 return instance_;
44 }
45
ScanCheck(const std::string & path,bool isDir)46 string MediaScannerManager::ScanCheck(const std::string &path, bool isDir)
47 {
48 if (path.empty()) {
49 MEDIA_ERR_LOG("path is empty");
50 return "";
51 }
52
53 string realPath;
54 if (!PathToRealPath(path, realPath)) {
55 MEDIA_ERR_LOG("failed to get real path %{private}s, errno %{public}d", path.c_str(), errno);
56 return "";
57 }
58
59 if (isDir && !ScannerUtils::IsDirectory(realPath)) {
60 MEDIA_ERR_LOG("path %{private}s isn't a dir", realPath.c_str());
61 return "";
62 }
63
64 if (!isDir && ScannerUtils::IsDirectory(realPath)) {
65 MEDIA_ERR_LOG("path %{private}s is a dir", realPath.c_str());
66 return "";
67 }
68
69 return realPath;
70 }
71
ScanFile(const std::string & path,const std::shared_ptr<IMediaScannerCallback> & callback)72 int32_t MediaScannerManager::ScanFile(const std::string &path, const std::shared_ptr<IMediaScannerCallback> &callback)
73 {
74 MEDIA_DEBUG_LOG("scan file %{private}s", path.c_str());
75
76 string realPath = ScanCheck(path, false);
77 if (realPath.empty()) {
78 return E_INVALID_PATH;
79 }
80
81 std::unique_ptr<MediaScannerObj> scanner = std::make_unique<MediaScannerObj>(realPath, callback, false);
82 executor_.Commit(move(scanner));
83
84 return E_OK;
85 }
86
ScanFileSync(const std::string & path,const std::shared_ptr<IMediaScannerCallback> & callback)87 int32_t MediaScannerManager::ScanFileSync(const std::string &path,
88 const std::shared_ptr<IMediaScannerCallback> &callback)
89 {
90 MEDIA_DEBUG_LOG("scan file %{private}s", path.c_str());
91
92 string realPath = ScanCheck(path, false);
93 if (realPath.empty()) {
94 return E_INVALID_PATH;
95 }
96
97 MediaScannerObj scanner = MediaScannerObj(realPath, callback, false);
98 scanner.Scan();
99
100 return E_OK;
101 }
102
ScanDir(const std::string & path,const std::shared_ptr<IMediaScannerCallback> & callback)103 int32_t MediaScannerManager::ScanDir(const std::string &path, const std::shared_ptr<IMediaScannerCallback> &callback)
104 {
105 MEDIA_DEBUG_LOG("scan dir %{private}s", path.c_str());
106
107 string realPath = ScanCheck(path, true);
108 if (realPath.empty()) {
109 return E_INVALID_PATH;
110 }
111
112 std::unique_ptr<MediaScannerObj> scanner = std::make_unique<MediaScannerObj>(realPath, callback, true);
113 executor_.Commit(move(scanner));
114
115 return E_OK;
116 }
117
Start()118 int32_t MediaScannerManager::Start()
119 {
120 executor_.Start();
121 return ScanDir(ROOT_MEDIA_DIR, nullptr);
122 }
123
Stop()124 int32_t MediaScannerManager::Stop()
125 {
126 executor_.Stop();
127 return E_OK;
128 }
129 } // namespace Media
130 } // namespace OHOS