Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1 | /* |
| 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 Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
| 18 | #include <dirent.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 20 | #include <error.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 21 | #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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 28 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <limits> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 31 | #include <memory> |
| 32 | #include <set> |
| 33 | #include <string> |
| 34 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 35 | #include <android-base/file.h> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
| 37 | #include <android-base/stringprintf.h> |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 38 | #include <android-base/strings.h> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 39 | |
Tao Bao | d80a998 | 2016-03-03 11:43:47 -0800 | [diff] [blame] | 40 | #include "applypatch/applypatch.h" |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 41 | #include "otautil/cache_location.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 42 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 43 | static int EliminateOpenFiles(const std::string& dirname, std::set<std::string>* files) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 44 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir); |
| 45 | if (!d) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 46 | printf("error opening /proc: %s\n", strerror(errno)); |
| 47 | return -1; |
| 48 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 49 | 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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 56 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 57 | struct dirent* fdde; |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 58 | 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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 61 | continue; |
| 62 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 63 | while ((fdde = readdir(fdd.get())) != 0) { |
| 64 | std::string fd_path = path + fdde->d_name; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 65 | char link[FILENAME_MAX]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 66 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 67 | int count = readlink(fd_path.c_str(), link, sizeof(link)-1); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 68 | if (count >= 0) { |
| 69 | link[count] = '\0'; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 70 | if (android::base::StartsWith(link, dirname)) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 71 | if (files->erase(link) > 0) { |
| 72 | printf("%s is open by %s\n", link, de->d_name); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 77 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 81 | static std::vector<std::string> FindExpendableFiles( |
| 82 | const std::string& dirname, const std::function<bool(const std::string&)>& name_filter) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 83 | std::set<std::string> files; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 84 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 85 | 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. |
| 98 | if (path == CacheLocation::location().cache_temp_source()) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 99 | continue; |
| 100 | } |
| 101 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 102 | // 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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 106 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 107 | struct stat st; |
| 108 | if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { |
| 109 | files.insert(path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 110 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 113 | printf("%zu regular files in deletable directory\n", files.size()); |
| 114 | if (EliminateOpenFiles(dirname, &files) < 0) { |
| 115 | return {}; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 116 | } |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 117 | |
| 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. |
| 123 | static 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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int MakeFreeSpaceOnCache(size_t bytes_needed) { |
Tianjie Xu | e40c80d | 2018-02-03 17:20:56 -0800 | [diff] [blame] | 138 | #ifndef __ANDROID__ |
| 139 | // TODO (xunchang) implement a heuristic cache size check during host simulation. |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 140 | printf("Skip making (%zu) bytes free space on /cache; program is running on host\n", |
| 141 | bytes_needed); |
Tianjie Xu | e40c80d | 2018-02-03 17:20:56 -0800 | [diff] [blame] | 142 | return 0; |
| 143 | #endif |
| 144 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 145 | std::vector<std::string> dirs = { "/cache", CacheLocation::location().cache_log_directory() }; |
| 146 | for (const auto& dirname : dirs) { |
| 147 | if (RemoveFilesInDirectory(bytes_needed, dirname, FreeSpaceForFile)) { |
| 148 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 151 | |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | bool 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; |
| 175 | if (dirname == CacheLocation::location().cache_log_directory()) { |
| 176 | // 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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 215 | } |