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 #define MLOG_TAG "scan_demo"
16
17 #ifndef MEDIALIBRARY_SCANNER_START
main()18 int main()
19 {
20 return 0;
21 }
22 #else
23 #include <unistd.h>
24 #include "accesstoken_kit.h"
25 #include "datashare_helper.h"
26 #include "get_self_permissions.h"
27 #include "iservice_registry.h"
28 #include "media_log.h"
29 #include "medialibrary_db_const.h"
30 #include "medialibrary_type_const.h"
31 #include "nativetoken_kit.h"
32 #include "token_setproc.h"
33 #include "userfilemgr_uri.h"
34
35 using namespace std;
36 using namespace OHOS;
37 using namespace OHOS::Media;
38 using namespace OHOS::DataShare;
39
40 namespace OHOS {
41 namespace Media {
42 namespace {
43 constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
44 } // namespace
45 } // namespace Media
46 } // namespace OHOS
47
CreateDataShareHelper(int32_t systemAbilityId)48 static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(int32_t systemAbilityId)
49 {
50 MEDIA_INFO_LOG("CreateDataShareHelper::CreateFileExtHelper ");
51 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
52 if (saManager == nullptr) {
53 MEDIA_INFO_LOG("CreateFileExtHelper Get system ability mgr failed.");
54 return nullptr;
55 }
56 auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
57 if (remoteObj == nullptr) {
58 MEDIA_INFO_LOG("CreateDataShareHelper GetSystemAbility Service Failed.");
59 return nullptr;
60 }
61 return DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI);
62 }
63
64 /*
65 * Feature: MediaScanner
66 * Function: Strat scanner
67 * SubFunction: NA
68 * FunctionPoints: NA
69 * EnvConditions: NA
70 */
main()71 int32_t main()
72 {
73 vector<string> perms;
74 perms.push_back("ohos.permission.READ_MEDIA");
75 perms.push_back("ohos.permission.WRITE_MEDIA");
76 perms.push_back("ohos.permission.READ_IMAGEVIDEO");
77 perms.push_back("ohos.permission.WRITE_IMAGEVIDEO");
78 perms.push_back("ohos.permission.FILE_ACCESS_MANAGER");
79 perms.push_back("ohos.permission.GET_BUNDLE_INFO_PRIVILEGED");
80 uint64_t tokenId = 0;
81 PermissionUtilsUnitTest::SetAccessTokenPermission("MediaLibraryScan", perms, tokenId);
82 if (tokenId == 0) {
83 MEDIA_ERR_LOG("Set Access Token Permisson Failed.");
84 return 0;
85 }
86
87 auto mediaDataShareHelper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID);
88 if (mediaDataShareHelper == nullptr) {
89 MEDIA_ERR_LOG("mediaDataShareHelper fail");
90 return 0;
91 }
92 Uri scanUri(URI_SCANNER);
93 DataShareValuesBucket valuesBucket;
94 valuesBucket.Put(MEDIA_DATA_DB_FILE_PATH, ROOT_MEDIA_DIR);
95 mediaDataShareHelper->Insert(scanUri, valuesBucket);
96 // Waiting for scanner to complete.
97 constexpr int32_t estimatedScanTime = 10;
98 sleep(estimatedScanTime);
99 return 0;
100 }
101
102 #endif // MEDIALIBRARY_SCANNER_START