1 /*
2 * Copyright (c) 2023-2024 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 "application_cleaner.h"
17
18 #include <cstring>
19 #include <dirent.h>
20 #include <pthread.h>
21 #include <sstream>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include "directory_ex.h"
27 #include "ffrt.h"
28 #include "hilog_tag_wrapper.h"
29 #include "os_account_manager_wrapper.h"
30 namespace OHOS {
31 namespace AppExecFwk {
32 namespace {
33 static const std::string MARK_SYMBOL{ "_useless" };
34 static const std::string PATH_SEPARATOR = { "/" };
35 static const char FILE_SEPARATOR_CHAR = '/';
36 static const std::string MARK_TEMP_DIR{ "temp_useless" };
37 static const std::string CONTEXT_HAPS{ "/haps" };
38
39 static const size_t MARK_TEMP_LEN = 12;
40 static const int PATH_MAX_SIZE = 256;
41
42 static const int RESULT_OK = 0;
43 static const int RESULT_ERR = -1;
44
45 static const char TASK_NAME[] = "ApplicationCleaner::ClearTempData";
46 static constexpr uint64_t DELAY = 5000000; //5s
47 constexpr int64_t MAX_FILE_SIZE = 50 * 1024;
48 constexpr int32_t MAX_CPU_INDEX = 4;
49
SetCurrentThreadAffinity()50 void SetCurrentThreadAffinity()
51 {
52 cpu_set_t cpuSet;
53 CPU_ZERO(&cpuSet);
54 for (int32_t i = 0; i < MAX_CPU_INDEX; i++) {
55 CPU_SET(i, &cpuSet);
56 }
57
58 if (pthread_setaffinity_np(pthread_self(), sizeof(cpuSet), &cpuSet) != 0) {
59 TAG_LOGW(AAFwkTag::APPKIT, "set ThreadAffinity failed errno %{public}d", errno);
60 }
61 }
62 } // namespace
RenameTempData()63 void ApplicationCleaner::RenameTempData()
64 {
65 if (context_ == nullptr) {
66 TAG_LOGE(AAFwkTag::APPKIT, "null context");
67 return;
68 }
69 std::vector<std::string> tempdir{};
70 context_->GetAllTempDir(tempdir);
71 if (tempdir.empty()) {
72 TAG_LOGE(AAFwkTag::APPKIT, "empty tempdir");
73 return;
74 }
75 int64_t now =
76 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
77 .count();
78 std::ostringstream stream;
79 stream << std::hex << now;
80 for (const auto &path : tempdir) {
81 auto newPath = path + MARK_SYMBOL + stream.str();
82 if (rename(path.c_str(), newPath.c_str()) != 0) {
83 TAG_LOGE(AAFwkTag::APPKIT, "msg: %{public}s", strerror(errno));
84 }
85 }
86 }
87
ClearTempData()88 void ApplicationCleaner::ClearTempData()
89 {
90 if (hasCleaned_) {
91 return;
92 }
93 hasCleaned_ = true;
94 TAG_LOGI(AAFwkTag::APPKIT, "ClearTempData");
95 std::vector<std::string> rootDir;
96 if (GetRootPath(rootDir) != RESULT_OK) {
97 TAG_LOGE(AAFwkTag::APPKIT, "Get root dir error");
98 return;
99 }
100 auto weakThis = weak_from_this();
101 auto cleanTemp = [weakThis, rootDir]() {
102 auto sharedThis = weakThis.lock();
103 if (sharedThis == nullptr || sharedThis->context_ == nullptr) {
104 TAG_LOGE(AAFwkTag::APPKIT, "Invalid shared pointer");
105 return;
106 }
107 SetCurrentThreadAffinity();
108 std::vector<std::string> temps;
109 if (sharedThis->GetObsoleteBundleTempPath(rootDir, temps) != RESULT_OK) {
110 TAG_LOGE(AAFwkTag::APPKIT, "get bundle temp file list false");
111 return;
112 }
113
114 for (const auto &temp : temps) {
115 if (sharedThis->RemoveDir(temp) == false) {
116 TAG_LOGW(AAFwkTag::APPKIT, "path: %{private}s", temp.c_str());
117 }
118 }
119 };
120
121 ffrt::task_attr attr;
122 attr.name(TASK_NAME);
123 attr.qos(ffrt_qos_background);
124 if (!CheckFileSize(rootDir)) {
125 attr.delay(DELAY);
126 }
127 ffrt::submit(std::move(cleanTemp), attr);
128 }
129
CheckFileSize(const std::vector<std::string> & bundlePath)130 bool ApplicationCleaner::CheckFileSize(const std::vector<std::string> &bundlePath)
131 {
132 int64_t fileSize = 0;
133
134 for (const auto& dir : bundlePath) {
135 struct stat fileInfo = { 0 };
136 if (stat(dir.c_str(), &fileInfo) != 0) {
137 continue;
138 }
139 fileSize += fileInfo.st_size;
140 }
141 return (fileSize <= MAX_FILE_SIZE);
142 }
143
GetRootPath(std::vector<std::string> & rootPath)144 int ApplicationCleaner::GetRootPath(std::vector<std::string> &rootPath)
145 {
146 if (context_ == nullptr) {
147 TAG_LOGE(AAFwkTag::APPKIT, "null context");
148 return RESULT_ERR;
149 }
150
151 auto instance = AppExecFwk::OsAccountManagerWrapper::GetInstance();
152 if (instance == nullptr) {
153 TAG_LOGE(AAFwkTag::APPKIT, "null instance");
154 return RESULT_ERR;
155 }
156
157 int userId = -1;
158 if (instance->GetOsAccountLocalIdFromProcess(userId) != RESULT_OK) {
159 TAG_LOGE(AAFwkTag::APPKIT, "Get account failed");
160 return RESULT_ERR;
161 }
162 TAG_LOGD(AAFwkTag::APPKIT, "userId: %{public}d", userId);
163
164 rootPath.clear();
165 auto baseDir = context_->GetBaseDir();
166 auto infos = context_->GetApplicationInfo();
167 if (infos == nullptr) {
168 TAG_LOGE(AAFwkTag::APPKIT, "null infos");
169 return RESULT_ERR;
170 }
171
172 rootPath.emplace_back(baseDir);
173 for (const auto &moudle : infos->moduleInfos) {
174 auto moudleDir = baseDir + CONTEXT_HAPS + PATH_SEPARATOR + moudle.moduleName;
175 if (access(moudleDir.c_str(), F_OK) != 0) {
176 continue;
177 }
178 rootPath.emplace_back(moudleDir);
179 }
180 return RESULT_OK;
181 }
182
GetObsoleteBundleTempPath(const std::vector<std::string> & rootPath,std::vector<std::string> & tempPath)183 ErrCode ApplicationCleaner::GetObsoleteBundleTempPath(
184 const std::vector<std::string> &rootPath, std::vector<std::string> &tempPath)
185 {
186 if (rootPath.empty()) {
187 TAG_LOGE(AAFwkTag::APPKIT, "empty rootPath");
188 return RESULT_ERR;
189 }
190
191 for (const auto &dir : rootPath) {
192 if (dir.empty()) {
193 TAG_LOGE(AAFwkTag::APPKIT, "empty dir");
194 continue;
195 }
196 std::vector<std::string> temp;
197 TraverseObsoleteTempDirectory(dir, temp);
198 std::copy(temp.begin(), temp.end(), std::back_inserter(tempPath));
199 }
200 return RESULT_OK;
201 }
202
TraverseObsoleteTempDirectory(const std::string & currentPath,std::vector<std::string> & tempDirs)203 void ApplicationCleaner::TraverseObsoleteTempDirectory(
204 const std::string ¤tPath, std::vector<std::string> &tempDirs)
205 {
206 if (currentPath.empty() || (currentPath.size() > PATH_MAX_SIZE)) {
207 TAG_LOGE(AAFwkTag::APPKIT, "traverse temp directory current path invalid");
208 return;
209 }
210
211 std::string filePath = currentPath;
212 DIR *dir = opendir(filePath.c_str());
213 if (dir == nullptr) {
214 TAG_LOGE(AAFwkTag::APPKIT, "null dir %{public}s", currentPath.c_str());
215 return;
216 }
217 if (filePath.back() != FILE_SEPARATOR_CHAR) {
218 filePath.push_back(FILE_SEPARATOR_CHAR);
219 }
220 struct dirent *ptr = nullptr;
221 while ((ptr = readdir(dir)) != nullptr) {
222 if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
223 continue;
224 }
225 if (ptr->d_type == DT_DIR && strncmp(ptr->d_name, MARK_TEMP_DIR.c_str(), MARK_TEMP_LEN) == 0) {
226 std::string tempDir = filePath + std::string(ptr->d_name);
227 tempDirs.emplace_back(tempDir);
228 continue;
229 }
230 if (ptr->d_type == DT_DIR) {
231 std::string currentDir = filePath + std::string(ptr->d_name);
232 TraverseObsoleteTempDirectory(currentDir, tempDirs);
233 }
234 }
235 closedir(dir);
236 }
237
RemoveDir(const std::string & tempPath)238 bool ApplicationCleaner::RemoveDir(const std::string &tempPath)
239 {
240 TAG_LOGD(AAFwkTag::APPKIT, "Called");
241 if (tempPath.empty()) {
242 return false;
243 }
244 struct stat buf = {};
245 if (stat(tempPath.c_str(), &buf) != 0) {
246 TAG_LOGW(AAFwkTag::APPKIT, "obtain file properties failed");
247 return false;
248 }
249
250 if (S_ISREG(buf.st_mode)) {
251 return OHOS::RemoveFile(tempPath);
252 }
253
254 if (S_ISDIR(buf.st_mode)) {
255 return OHOS::ForceRemoveDirectory(tempPath);
256 }
257
258 return false;
259 }
260
261 } // namespace AppExecFwk
262 } // namespace OHOS
263