blob: dbd4b72b189df0320cf8543a176fa0e289090a72 [file] [log] [blame]
Tao Baoba9a42a2015-06-23 23:23:33 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tianjie Xud5fbcc12018-04-11 14:38:09 -070017#include <ctype.h>
18#include <dirent.h>
Doug Zongker512536a2010-02-17 16:11:44 -080019#include <errno.h>
Tianjie Xud5fbcc12018-04-11 14:38:09 -070020#include <error.h>
Doug Zongker512536a2010-02-17 16:11:44 -080021#include <libgen.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/statfs.h>
27#include <unistd.h>
Doug Zongker512536a2010-02-17 16:11:44 -080028
Tianjie Xud5fbcc12018-04-11 14:38:09 -070029#include <algorithm>
30#include <limits>
Yabin Cuid483c202016-02-03 17:08:52 -080031#include <memory>
32#include <set>
33#include <string>
34
Tianjie Xud5fbcc12018-04-11 14:38:09 -070035#include <android-base/file.h>
Yabin Cuid483c202016-02-03 17:08:52 -080036#include <android-base/parseint.h>
37#include <android-base/stringprintf.h>
Tianjie Xud5fbcc12018-04-11 14:38:09 -070038#include <android-base/strings.h>
Yabin Cuid483c202016-02-03 17:08:52 -080039
Tao Baod80a9982016-03-03 11:43:47 -080040#include "applypatch/applypatch.h"
Tao Bao641fa972018-04-25 18:59:40 -070041#include "otautil/paths.h"
Doug Zongker512536a2010-02-17 16:11:44 -080042
Tianjie Xud5fbcc12018-04-11 14:38:09 -070043static int EliminateOpenFiles(const std::string& dirname, std::set<std::string>* files) {
Yabin Cuid483c202016-02-03 17:08:52 -080044 std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir);
45 if (!d) {
Doug Zongker512536a2010-02-17 16:11:44 -080046 printf("error opening /proc: %s\n", strerror(errno));
47 return -1;
48 }
Yabin Cuid483c202016-02-03 17:08:52 -080049 struct dirent* de;
50 while ((de = readdir(d.get())) != 0) {
51 unsigned int pid;
52 if (!android::base::ParseUint(de->d_name, &pid)) {
53 continue;
54 }
55 std::string path = android::base::StringPrintf("/proc/%s/fd/", de->d_name);
Doug Zongker512536a2010-02-17 16:11:44 -080056
Doug Zongker512536a2010-02-17 16:11:44 -080057 struct dirent* fdde;
Yabin Cuid483c202016-02-03 17:08:52 -080058 std::unique_ptr<DIR, decltype(&closedir)> fdd(opendir(path.c_str()), closedir);
59 if (!fdd) {
60 printf("error opening %s: %s\n", path.c_str(), strerror(errno));
Doug Zongker512536a2010-02-17 16:11:44 -080061 continue;
62 }
Yabin Cuid483c202016-02-03 17:08:52 -080063 while ((fdde = readdir(fdd.get())) != 0) {
64 std::string fd_path = path + fdde->d_name;
Doug Zongker512536a2010-02-17 16:11:44 -080065 char link[FILENAME_MAX];
Doug Zongker512536a2010-02-17 16:11:44 -080066
Yabin Cuid483c202016-02-03 17:08:52 -080067 int count = readlink(fd_path.c_str(), link, sizeof(link)-1);
Doug Zongker512536a2010-02-17 16:11:44 -080068 if (count >= 0) {
69 link[count] = '\0';
Tianjie Xud5fbcc12018-04-11 14:38:09 -070070 if (android::base::StartsWith(link, dirname)) {
Yabin Cuid483c202016-02-03 17:08:52 -080071 if (files->erase(link) > 0) {
72 printf("%s is open by %s\n", link, de->d_name);
Doug Zongker512536a2010-02-17 16:11:44 -080073 }
74 }
75 }
76 }
Doug Zongker512536a2010-02-17 16:11:44 -080077 }
Doug Zongker512536a2010-02-17 16:11:44 -080078 return 0;
79}
80
Tianjie Xud5fbcc12018-04-11 14:38:09 -070081static std::vector<std::string> FindExpendableFiles(
82 const std::string& dirname, const std::function<bool(const std::string&)>& name_filter) {
Yabin Cuid483c202016-02-03 17:08:52 -080083 std::set<std::string> files;
Doug Zongker512536a2010-02-17 16:11:44 -080084
Tianjie Xud5fbcc12018-04-11 14:38:09 -070085 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
86 if (!d) {
87 printf("error opening %s: %s\n", dirname.c_str(), strerror(errno));
88 return {};
89 }
90
91 // Look for regular files in the directory (not in any subdirectories).
92 struct dirent* de;
93 while ((de = readdir(d.get())) != 0) {
94 std::string path = dirname + "/" + de->d_name;
95
96 // We can't delete cache_temp_source; if it's there we might have restarted during
97 // installation and could be depending on it to be there.
Tao Bao641fa972018-04-25 18:59:40 -070098 if (path == Paths::Get().cache_temp_source()) {
Doug Zongker512536a2010-02-17 16:11:44 -080099 continue;
100 }
101
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700102 // Do not delete the file if it doesn't have the expected format.
103 if (name_filter != nullptr && !name_filter(de->d_name)) {
104 continue;
105 }
Doug Zongker512536a2010-02-17 16:11:44 -0800106
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700107 struct stat st;
108 if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
109 files.insert(path);
Doug Zongker512536a2010-02-17 16:11:44 -0800110 }
Doug Zongker512536a2010-02-17 16:11:44 -0800111 }
112
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700113 printf("%zu regular files in deletable directory\n", files.size());
114 if (EliminateOpenFiles(dirname, &files) < 0) {
115 return {};
Doug Zongker512536a2010-02-17 16:11:44 -0800116 }
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700117
118 return std::vector<std::string>(files.begin(), files.end());
119}
120
121// Parses the index of given log file, e.g. 3 for last_log.3; returns max number if the log name
122// doesn't have the expected format so that we'll delete these ones first.
123static unsigned int GetLogIndex(const std::string& log_name) {
124 if (log_name == "last_log" || log_name == "last_kmsg") {
125 return 0;
126 }
127
128 unsigned int index;
129 if (sscanf(log_name.c_str(), "last_log.%u", &index) == 1 ||
130 sscanf(log_name.c_str(), "last_kmsg.%u", &index) == 1) {
131 return index;
132 }
133
134 return std::numeric_limits<unsigned int>::max();
Doug Zongker512536a2010-02-17 16:11:44 -0800135}
136
137int MakeFreeSpaceOnCache(size_t bytes_needed) {
Tianjie Xue40c80d2018-02-03 17:20:56 -0800138#ifndef __ANDROID__
139 // TODO (xunchang) implement a heuristic cache size check during host simulation.
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700140 printf("Skip making (%zu) bytes free space on /cache; program is running on host\n",
141 bytes_needed);
Tianjie Xue40c80d2018-02-03 17:20:56 -0800142 return 0;
143#endif
144
Tao Bao641fa972018-04-25 18:59:40 -0700145 std::vector<std::string> dirs = { "/cache", Paths::Get().cache_log_directory() };
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700146 for (const auto& dirname : dirs) {
147 if (RemoveFilesInDirectory(bytes_needed, dirname, FreeSpaceForFile)) {
148 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800149 }
150 }
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700151
152 return -1;
153}
154
155bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
156 const std::function<size_t(const std::string&)>& space_checker) {
157 struct stat st;
158 if (stat(dirname.c_str(), &st) != 0) {
159 error(0, errno, "Unable to free space on %s", dirname.c_str());
160 return false;
161 }
162 if (!S_ISDIR(st.st_mode)) {
163 printf("%s is not a directory\n", dirname.c_str());
164 return false;
165 }
166
167 size_t free_now = space_checker(dirname);
168 printf("%zu bytes free on %s (%zu needed)\n", free_now, dirname.c_str(), bytes_needed);
169
170 if (free_now >= bytes_needed) {
171 return true;
172 }
173
174 std::vector<std::string> files;
Tao Bao641fa972018-04-25 18:59:40 -0700175 if (dirname == Paths::Get().cache_log_directory()) {
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700176 // Deletes the log files only.
177 auto log_filter = [](const std::string& file_name) {
178 return android::base::StartsWith(file_name, "last_log") ||
179 android::base::StartsWith(file_name, "last_kmsg");
180 };
181
182 files = FindExpendableFiles(dirname, log_filter);
183
184 // Older logs will come to the top of the queue.
185 auto comparator = [](const std::string& name1, const std::string& name2) -> bool {
186 unsigned int index1 = GetLogIndex(android::base::Basename(name1));
187 unsigned int index2 = GetLogIndex(android::base::Basename(name2));
188 if (index1 == index2) {
189 return name1 < name2;
190 }
191
192 return index1 > index2;
193 };
194
195 std::sort(files.begin(), files.end(), comparator);
196 } else {
197 // We're allowed to delete unopened regular files in the directory.
198 files = FindExpendableFiles(dirname, nullptr);
199 }
200
201 for (const auto& file : files) {
202 if (unlink(file.c_str()) == -1) {
203 error(0, errno, "Failed to delete %s", file.c_str());
204 continue;
205 }
206
207 free_now = space_checker(dirname);
208 printf("deleted %s; now %zu bytes free\n", file.c_str(), free_now);
209 if (free_now >= bytes_needed) {
210 return true;
211 }
212 }
213
214 return false;
Doug Zongker512536a2010-02-17 16:11:44 -0800215}