• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_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 } // namespace
RenameTempData()48 void ApplicationCleaner::RenameTempData()
49 {
50     if (context_ == nullptr) {
51         HILOG_ERROR("Context is null.");
52         return;
53     }
54     std::vector<std::string> tempdir{};
55     context_->GetAllTempDir(tempdir);
56     if (tempdir.empty()) {
57         HILOG_ERROR("Get app temp path list is empty.");
58         return;
59     }
60     int64_t now =
61         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
62             .count();
63     std::ostringstream stream;
64     stream << std::hex << now;
65     for (const auto &path : tempdir) {
66         auto newPath = path + MARK_SYMBOL + stream.str();
67         if (rename(path.c_str(), newPath.c_str()) != 0) {
68             HILOG_ERROR("Rename temp dir failed, msg is %{public}s", strerror(errno));
69         }
70     }
71 }
72 
ClearTempData()73 void ApplicationCleaner::ClearTempData()
74 {
75     HILOG_DEBUG("Called");
76     auto cleanTemp = [self = shared_from_this()]() {
77         if (self == nullptr || self->context_ == nullptr) {
78             HILOG_ERROR("Invalid shared pointer");
79             return;
80         }
81 
82         std::vector<std::string> rootDir;
83         std::vector<std::string> temps;
84 
85         if (self->GetRootPath(rootDir) != RESULT_OK) {
86             HILOG_ERROR("Get root dir error.");
87             return;
88         }
89 
90         if (self->GetObsoleteBundleTempPath(rootDir, temps) != RESULT_OK) {
91             HILOG_ERROR("Get bundle temp file list is false.");
92             return;
93         }
94 
95         for (const auto &temp : temps) {
96             if (self->RemoveDir(temp) == false) {
97                 HILOG_ERROR("Clean bundle data dir failed, path: %{private}s", temp.c_str());
98             }
99         }
100     };
101     ffrt::submit(cleanTemp);
102 }
103 
GetRootPath(std::vector<std::string> & rootPath)104 int ApplicationCleaner::GetRootPath(std::vector<std::string> &rootPath)
105 {
106     if (context_ == nullptr) {
107         HILOG_ERROR("Invalid context pointer");
108         return RESULT_ERR;
109     }
110 
111     auto instance = DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance();
112     if (instance == nullptr) {
113         HILOG_ERROR("Failed to get OsAccountManager instance.");
114         return RESULT_ERR;
115     }
116 
117     int userId = -1;
118     if (instance->GetOsAccountLocalIdFromProcess(userId) != RESULT_OK) {
119         return RESULT_ERR;
120     }
121 
122     rootPath.clear();
123     auto baseDir = context_->GetBaseDir();
124     auto infos = context_->GetApplicationInfo();
125     if (infos == nullptr) {
126         HILOG_ERROR("Input param is invalid");
127         return RESULT_ERR;
128     }
129 
130     rootPath.emplace_back(baseDir);
131     for (const auto &moudle : infos->moduleInfos) {
132         auto moudleDir = baseDir + CONTEXT_HAPS + PATH_SEPARATOR + moudle.moduleName;
133         if (access(moudleDir.c_str(), F_OK) != 0) {
134             continue;
135         }
136         rootPath.emplace_back(moudleDir);
137     }
138     return RESULT_OK;
139 }
140 
GetObsoleteBundleTempPath(const std::vector<std::string> & rootPath,std::vector<std::string> & tempPath)141 ErrCode ApplicationCleaner::GetObsoleteBundleTempPath(
142     const std::vector<std::string> &rootPath, std::vector<std::string> &tempPath)
143 {
144     if (rootPath.empty()) {
145         HILOG_ERROR("Input param is invalid");
146         return RESULT_ERR;
147     }
148 
149     for (const auto &dir : rootPath) {
150         if (dir.empty()) {
151             HILOG_ERROR("Input param is invalid");
152             continue;
153         }
154         std::vector<std::string> temp;
155         TraverseObsoleteTempDirectory(dir, temp);
156         std::copy(temp.begin(), temp.end(), std::back_inserter(tempPath));
157     }
158     return RESULT_OK;
159 }
160 
TraverseObsoleteTempDirectory(const std::string & currentPath,std::vector<std::string> & tempDirs)161 void ApplicationCleaner::TraverseObsoleteTempDirectory(
162     const std::string &currentPath, std::vector<std::string> &tempDirs)
163 {
164     if (currentPath.empty() || (currentPath.size() > PATH_MAX_SIZE)) {
165         HILOG_ERROR("Traverse temp directory current path invaild");
166         return;
167     }
168 
169     std::string filePath = currentPath;
170     DIR *dir = opendir(filePath.c_str());
171     if (dir == nullptr) {
172         HILOG_ERROR("Open dir error. %{public}s", currentPath.c_str());
173         return;
174     }
175     if (filePath.back() != FILE_SEPARATOR_CHAR) {
176         filePath.push_back(FILE_SEPARATOR_CHAR);
177     }
178     struct dirent *ptr = nullptr;
179     while ((ptr = readdir(dir)) != nullptr) {
180         if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
181             continue;
182         }
183         if (ptr->d_type == DT_DIR && strncmp(ptr->d_name, MARK_TEMP_DIR.c_str(), MARK_TEMP_LEN) == 0) {
184             std::string tempDir = filePath + std::string(ptr->d_name);
185             tempDirs.emplace_back(tempDir);
186             continue;
187         }
188         if (ptr->d_type == DT_DIR) {
189             std::string currentDir = filePath + std::string(ptr->d_name);
190             TraverseObsoleteTempDirectory(currentDir, tempDirs);
191         }
192     }
193     closedir(dir);
194 }
195 
RemoveDir(const std::string & tempPath)196 bool ApplicationCleaner::RemoveDir(const std::string &tempPath)
197 {
198     HILOG_DEBUG("Called");
199     if (tempPath.empty()) {
200         return false;
201     }
202     struct stat buf = {};
203     if (stat(tempPath.c_str(), &buf) != 0) {
204         HILOG_ERROR("Failed to obtain file properties");
205         return false;
206     }
207 
208     if (S_ISREG(buf.st_mode)) {
209         return OHOS::RemoveFile(tempPath);
210     }
211 
212     if (S_ISDIR(buf.st_mode)) {
213         return OHOS::ForceRemoveDirectory(tempPath);
214     }
215 
216     return false;
217 }
218 
219 } // namespace AppExecFwk
220 } // namespace OHOS
221