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