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 CONTEXT_DATA_APP{ "/data/app/" };
34 static const std::vector<std::string> CONTEXT_ELS{ "el1", "el2", "el3", "el4" };
35 static const std::string PATH_SEPARATOR = { "/" };
36 static const char FILE_SEPARATOR_CHAR = '/';
37 static const std::string CONTEXT_BASE{ "/base/" };
38 static const std::string MARK_TEMP_DIR{ "temp_useless" };
39 static const std::string CONTEXT_HAPS{ "/haps" };
40 
41 static const size_t MARK_TEMP_LEN = 12;
42 static const int PATH_MAX_SIZE = 256;
43 
44 const mode_t MODE = 0777;
45 static const int RESULT_OK = 0;
46 static const int RESULT_ERR = -1;
47 
48 static const char TASK_NAME[] = "ApplicationCleaner::ClearTempData";
49 static constexpr uint64_t DELAY = 5000000; //5s
50 constexpr int64_t MAX_FILE_SIZE = 50 * 1024;
51 
52 } // namespace
RenameTempData()53 void ApplicationCleaner::RenameTempData()
54 {
55     if (context_ == nullptr) {
56         TAG_LOGE(AAFwkTag::APPKIT, "Context is null");
57         return;
58     }
59     std::vector<std::string> tempdir{};
60     context_->GetAllTempDir(tempdir);
61     if (tempdir.empty()) {
62         TAG_LOGE(AAFwkTag::APPKIT, "Get app temp path list is empty");
63         return;
64     }
65     int64_t now =
66         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
67             .count();
68     std::ostringstream stream;
69     stream << std::hex << now;
70     for (const auto &path : tempdir) {
71         auto newPath = path + MARK_SYMBOL + stream.str();
72         if (rename(path.c_str(), newPath.c_str()) != 0) {
73             TAG_LOGE(AAFwkTag::APPKIT, "Rename temp dir failed, msg is %{public}s", strerror(errno));
74         }
75     }
76 }
77 
ClearTempData()78 void ApplicationCleaner::ClearTempData()
79 {
80     TAG_LOGD(AAFwkTag::APPKIT, "Called");
81     std::vector<std::string> rootDir;
82     if (GetRootPath(rootDir) != RESULT_OK) {
83         TAG_LOGE(AAFwkTag::APPKIT, "Get root dir error");
84         return;
85     }
86     auto cleanTemp = [self = shared_from_this(), rootDir]() {
87         if (self == nullptr || self->context_ == nullptr) {
88             TAG_LOGE(AAFwkTag::APPKIT, "Invalid shared pointer");
89             return;
90         }
91         std::vector<std::string> temps;
92         if (self->GetObsoleteBundleTempPath(rootDir, temps) != RESULT_OK) {
93             TAG_LOGE(AAFwkTag::APPKIT, "Get bundle temp file list is false");
94             return;
95         }
96 
97         for (const auto &temp : temps) {
98             if (self->RemoveDir(temp) == false) {
99                 TAG_LOGE(AAFwkTag::APPKIT, "Clean bundle data dir failed, path: %{private}s", temp.c_str());
100             }
101         }
102     };
103 
104     if (CheckFileSize(rootDir)) {
105         ffrt::submit(cleanTemp);
106     } else {
107         ffrt::task_attr attr;
108         attr.name(TASK_NAME);
109         attr.delay(DELAY);
110         ffrt::submit(std::move(cleanTemp), attr);
111     }
112 }
113 
CheckFileSize(const std::vector<std::string> & bundlePath)114 bool ApplicationCleaner::CheckFileSize(const std::vector<std::string> &bundlePath)
115 {
116     int64_t fileSize = 0;
117 
118     for (const auto& dir : bundlePath) {
119         struct stat fileInfo = { 0 };
120         if (stat(dir.c_str(), &fileInfo) != 0) {
121             continue;
122         }
123         fileSize += fileInfo.st_size;
124     }
125     return (fileSize <= MAX_FILE_SIZE);
126 }
127 
GetRootPath(std::vector<std::string> & rootPath)128 int ApplicationCleaner::GetRootPath(std::vector<std::string> &rootPath)
129 {
130     if (context_ == nullptr) {
131         TAG_LOGE(AAFwkTag::APPKIT, "Invalid context pointer");
132         return RESULT_ERR;
133     }
134 
135     auto instance = DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance();
136     if (instance == nullptr) {
137         TAG_LOGE(AAFwkTag::APPKIT, "Failed to get OsAccountManager instance");
138         return RESULT_ERR;
139     }
140 
141     int userId = -1;
142     if (instance->GetOsAccountLocalIdFromProcess(userId) != RESULT_OK) {
143         return RESULT_ERR;
144     }
145 
146     rootPath.clear();
147     auto baseDir = context_->GetBaseDir();
148     auto infos = context_->GetApplicationInfo();
149     if (infos == nullptr) {
150         TAG_LOGE(AAFwkTag::APPKIT, "Input param is invalid");
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, "Input param is invalid");
170         return RESULT_ERR;
171     }
172 
173     for (const auto &dir : rootPath) {
174         if (dir.empty()) {
175             TAG_LOGE(AAFwkTag::APPKIT, "Input param is invalid");
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 invaild");
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, "Open dir error. %{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, "Failed to obtain file properties");
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