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
16 #include "bundle_scanner.h"
17
18 #include <cstddef>
19 #include <dirent.h>
20 #include <string>
21
22 #include "app_log_wrapper.h"
23 #include "bundle_constants.h"
24 #include "bundle_mgr_service.h"
25 #include "bundle_util.h"
26 #include "hitrace_meter.h"
27 #include "datetime_ex.h"
28 #include "perf_profile.h"
29 #include "string_ex.h"
30
31 namespace OHOS {
32 namespace AppExecFwk {
33
BundleScanner()34 BundleScanner::BundleScanner()
35 {
36 APP_LOGI("BundleScanner instance is created");
37 }
38
~BundleScanner()39 BundleScanner::~BundleScanner()
40 {
41 APP_LOGI("BundleScanner instance is destroyed");
42 }
43
Scan(const std::string & dirPath)44 const std::list<std::string> &BundleScanner::Scan(const std::string &dirPath)
45 {
46 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
47 PerfProfile::GetInstance().SetBundleScanStartTime(GetTickCount());
48
49 APP_LOGD("path:%{private}s", dirPath.c_str());
50 if (!dirPath.empty()) {
51 if (!ScanImpl(dirPath)) {
52 APP_LOGW("BundleScanner::Scan scan error");
53 }
54 }
55
56 APP_LOGD("scan result num:%{public}zu", entries_.size());
57 for (const auto &item : entries_) {
58 APP_LOGD("app item:%{private}s", item.c_str());
59 }
60
61 PerfProfile::GetInstance().SetBundleScanEndTime(GetTickCount());
62 return entries_;
63 }
64
ScanImpl(const std::string & dirPath)65 bool BundleScanner::ScanImpl(const std::string &dirPath)
66 {
67 DIR *dirp = opendir(dirPath.c_str());
68 if (dirp == nullptr) {
69 APP_LOGE("BundleScanner::ScanImpl open dir:%{private}s fail", dirPath.c_str());
70 return false;
71 }
72
73 struct dirent *dirent = nullptr;
74 for (;;) {
75 dirent = readdir(dirp);
76 if (dirent == nullptr) {
77 break;
78 }
79
80 std::string currentName(dirent->d_name);
81 APP_LOGD("folder found:'%{private}s'", dirent->d_name);
82 if (currentName.compare(".") == 0 || currentName.compare("..") == 0) {
83 continue;
84 }
85 entries_.push_back(dirPath + Constants::PATH_SEPARATOR + currentName);
86 }
87
88 if (closedir(dirp) == -1) {
89 APP_LOGW("close dir fail");
90 }
91 return true;
92 }
93
94 } // namespace AppExecFwk
95 } // namespace OHOS
96