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, "Context is null");
53         return;
54     }
55     std::vector<std::string> tempdir{};
56     context_->GetAllTempDir(tempdir);
57     if (tempdir.empty()) {
58         TAG_LOGE(AAFwkTag::APPKIT, "Get app temp path list is empty");
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, "Rename temp dir failed, msg is %{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 cleanTemp = [self = shared_from_this(), rootDir]() {
83         if (self == nullptr || self->context_ == nullptr) {
84             TAG_LOGE(AAFwkTag::APPKIT, "Invalid shared pointer");
85             return;
86         }
87         std::vector<std::string> temps;
88         if (self->GetObsoleteBundleTempPath(rootDir, temps) != RESULT_OK) {
89             TAG_LOGE(AAFwkTag::APPKIT, "Get bundle temp file list is false");
90             return;
91         }
92 
93         for (const auto &temp : temps) {
94             if (self->RemoveDir(temp) == false) {
95                 TAG_LOGE(AAFwkTag::APPKIT, "Clean bundle data dir failed, path: %{private}s", temp.c_str());
96             }
97         }
98     };
99 
100     if (CheckFileSize(rootDir)) {
101         ffrt::submit(cleanTemp);
102     } else {
103         ffrt::task_attr attr;
104         attr.name(TASK_NAME);
105         attr.delay(DELAY);
106         ffrt::submit(std::move(cleanTemp), attr);
107     }
108 }
109 
CheckFileSize(const std::vector<std::string> &bundlePath)110 bool ApplicationCleaner::CheckFileSize(const std::vector<std::string> &bundlePath)
111 {
112     int64_t fileSize = 0;
113 
114     for (const auto& dir : bundlePath) {
115         struct stat fileInfo = { 0 };
116         if (stat(dir.c_str(), &fileInfo) != 0) {
117             continue;
118         }
119         fileSize += fileInfo.st_size;
120     }
121     return (fileSize <= MAX_FILE_SIZE);
122 }
123 
GetRootPath(std::vector<std::string> &rootPath)124 int ApplicationCleaner::GetRootPath(std::vector<std::string> &rootPath)
125 {
126     if (context_ == nullptr) {
127         TAG_LOGE(AAFwkTag::APPKIT, "Invalid context pointer");
128         return RESULT_ERR;
129     }
130 
131     auto instance = DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance();
132     if (instance == nullptr) {
133         TAG_LOGE(AAFwkTag::APPKIT, "Failed to get OsAccountManager instance");
134         return RESULT_ERR;
135     }
136 
137     int userId = -1;
138     if (instance->GetOsAccountLocalIdFromProcess(userId) != RESULT_OK) {
139         TAG_LOGE(AAFwkTag::APPKIT, "Get account failed");
140         return RESULT_ERR;
141     }
142 
143     rootPath.clear();
144     auto baseDir = context_->GetBaseDir();
145     auto infos = context_->GetApplicationInfo();
146     if (infos == nullptr) {
147         TAG_LOGE(AAFwkTag::APPKIT, "Input param is invalid");
148         return RESULT_ERR;
149     }
150 
151     rootPath.emplace_back(baseDir);
152     for (const auto &moudle : infos->moduleInfos) {
153         auto moudleDir = baseDir + CONTEXT_HAPS + PATH_SEPARATOR + moudle.moduleName;
154         if (access(moudleDir.c_str(), F_OK) != 0) {
155             continue;
156         }
157         rootPath.emplace_back(moudleDir);
158     }
159     return RESULT_OK;
160 }
161 
GetObsoleteBundleTempPath( const std::vector<std::string> &rootPath, std::vector<std::string> &tempPath)162 ErrCode ApplicationCleaner::GetObsoleteBundleTempPath(
163     const std::vector<std::string> &rootPath, std::vector<std::string> &tempPath)
164 {
165     if (rootPath.empty()) {
166         TAG_LOGE(AAFwkTag::APPKIT, "Input param is invalid");
167         return RESULT_ERR;
168     }
169 
170     for (const auto &dir : rootPath) {
171         if (dir.empty()) {
172             TAG_LOGE(AAFwkTag::APPKIT, "Input param is invalid");
173             continue;
174         }
175         std::vector<std::string> temp;
176         TraverseObsoleteTempDirectory(dir, temp);
177         std::copy(temp.begin(), temp.end(), std::back_inserter(tempPath));
178     }
179     return RESULT_OK;
180 }
181 
TraverseObsoleteTempDirectory( const std::string &currentPath, std::vector<std::string> &tempDirs)182 void ApplicationCleaner::TraverseObsoleteTempDirectory(
183     const std::string &currentPath, std::vector<std::string> &tempDirs)
184 {
185     if (currentPath.empty() || (currentPath.size() > PATH_MAX_SIZE)) {
186         TAG_LOGE(AAFwkTag::APPKIT, "Traverse temp directory current path invaild");
187         return;
188     }
189 
190     std::string filePath = currentPath;
191     DIR *dir = opendir(filePath.c_str());
192     if (dir == nullptr) {
193         TAG_LOGE(AAFwkTag::APPKIT, "Open dir error. %{public}s", currentPath.c_str());
194         return;
195     }
196     if (filePath.back() != FILE_SEPARATOR_CHAR) {
197         filePath.push_back(FILE_SEPARATOR_CHAR);
198     }
199     struct dirent *ptr = nullptr;
200     while ((ptr = readdir(dir)) != nullptr) {
201         if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
202             continue;
203         }
204         if (ptr->d_type == DT_DIR && strncmp(ptr->d_name, MARK_TEMP_DIR.c_str(), MARK_TEMP_LEN) == 0) {
205             std::string tempDir = filePath + std::string(ptr->d_name);
206             tempDirs.emplace_back(tempDir);
207             continue;
208         }
209         if (ptr->d_type == DT_DIR) {
210             std::string currentDir = filePath + std::string(ptr->d_name);
211             TraverseObsoleteTempDirectory(currentDir, tempDirs);
212         }
213     }
214     closedir(dir);
215 }
216 
RemoveDir(const std::string &tempPath)217 bool ApplicationCleaner::RemoveDir(const std::string &tempPath)
218 {
219     TAG_LOGD(AAFwkTag::APPKIT, "Called");
220     if (tempPath.empty()) {
221         return false;
222     }
223     struct stat buf = {};
224     if (stat(tempPath.c_str(), &buf) != 0) {
225         TAG_LOGE(AAFwkTag::APPKIT, "Failed to obtain file properties");
226         return false;
227     }
228 
229     if (S_ISREG(buf.st_mode)) {
230         return OHOS::RemoveFile(tempPath);
231     }
232 
233     if (S_ISDIR(buf.st_mode)) {
234         return OHOS::ForceRemoveDirectory(tempPath);
235     }
236 
237     return false;
238 }
239 
240 } // namespace AppExecFwk
241 } // namespace OHOS
242