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