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 <dirent.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 18 | #include <errno.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 23 | #include <unistd.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 24 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | #include <limits> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <set> |
| 29 | #include <string> |
| 30 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 31 | #include <android-base/file.h> |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 32 | #include <android-base/logging.h> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 33 | #include <android-base/parseint.h> |
| 34 | #include <android-base/stringprintf.h> |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 35 | #include <android-base/strings.h> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 36 | |
Tao Bao | d80a998 | 2016-03-03 11:43:47 -0800 | [diff] [blame] | 37 | #include "applypatch/applypatch.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 38 | #include "otautil/paths.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 39 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 40 | static int EliminateOpenFiles(const std::string& dirname, std::set<std::string>* files) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 41 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir); |
| 42 | if (!d) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 43 | PLOG(ERROR) << "Failed to open /proc"; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 44 | return -1; |
| 45 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 46 | struct dirent* de; |
| 47 | while ((de = readdir(d.get())) != 0) { |
| 48 | unsigned int pid; |
| 49 | if (!android::base::ParseUint(de->d_name, &pid)) { |
| 50 | continue; |
| 51 | } |
| 52 | std::string path = android::base::StringPrintf("/proc/%s/fd/", de->d_name); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 53 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 54 | struct dirent* fdde; |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 55 | std::unique_ptr<DIR, decltype(&closedir)> fdd(opendir(path.c_str()), closedir); |
| 56 | if (!fdd) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 57 | PLOG(ERROR) << "Failed to open " << path; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 58 | continue; |
| 59 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 60 | while ((fdde = readdir(fdd.get())) != 0) { |
| 61 | std::string fd_path = path + fdde->d_name; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 62 | char link[FILENAME_MAX]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 63 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 64 | int count = readlink(fd_path.c_str(), link, sizeof(link)-1); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 65 | if (count >= 0) { |
| 66 | link[count] = '\0'; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 67 | if (android::base::StartsWith(link, dirname)) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 68 | if (files->erase(link) > 0) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 69 | LOG(INFO) << link << " is open by " << de->d_name; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 74 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 78 | static std::vector<std::string> FindExpendableFiles( |
| 79 | const std::string& dirname, const std::function<bool(const std::string&)>& name_filter) { |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 80 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir); |
| 81 | if (!d) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 82 | PLOG(ERROR) << "Failed to open " << dirname; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 83 | return {}; |
| 84 | } |
| 85 | |
| 86 | // Look for regular files in the directory (not in any subdirectories). |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 87 | std::set<std::string> files; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 88 | struct dirent* de; |
| 89 | while ((de = readdir(d.get())) != 0) { |
| 90 | std::string path = dirname + "/" + de->d_name; |
| 91 | |
| 92 | // We can't delete cache_temp_source; if it's there we might have restarted during |
| 93 | // installation and could be depending on it to be there. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 94 | if (path == Paths::Get().cache_temp_source()) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 95 | continue; |
| 96 | } |
| 97 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 98 | // Do not delete the file if it doesn't have the expected format. |
| 99 | if (name_filter != nullptr && !name_filter(de->d_name)) { |
| 100 | continue; |
| 101 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 102 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 103 | struct stat st; |
| 104 | if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { |
| 105 | files.insert(path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 106 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 109 | LOG(INFO) << files.size() << " regular files in deletable directory"; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 110 | if (EliminateOpenFiles(dirname, &files) < 0) { |
| 111 | return {}; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 112 | } |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 113 | |
| 114 | return std::vector<std::string>(files.begin(), files.end()); |
| 115 | } |
| 116 | |
| 117 | // Parses the index of given log file, e.g. 3 for last_log.3; returns max number if the log name |
| 118 | // doesn't have the expected format so that we'll delete these ones first. |
| 119 | static unsigned int GetLogIndex(const std::string& log_name) { |
| 120 | if (log_name == "last_log" || log_name == "last_kmsg") { |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | unsigned int index; |
| 125 | if (sscanf(log_name.c_str(), "last_log.%u", &index) == 1 || |
| 126 | sscanf(log_name.c_str(), "last_kmsg.%u", &index) == 1) { |
| 127 | return index; |
| 128 | } |
| 129 | |
| 130 | return std::numeric_limits<unsigned int>::max(); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | int MakeFreeSpaceOnCache(size_t bytes_needed) { |
Tianjie Xu | e40c80d | 2018-02-03 17:20:56 -0800 | [diff] [blame] | 134 | #ifndef __ANDROID__ |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 135 | // TODO(xunchang): Implement a heuristic cache size check during host simulation. |
| 136 | LOG(WARNING) << "Skipped making (" << bytes_needed |
| 137 | << ") bytes free space on /cache; program is running on host"; |
Tianjie Xu | e40c80d | 2018-02-03 17:20:56 -0800 | [diff] [blame] | 138 | return 0; |
| 139 | #endif |
| 140 | |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 141 | std::vector<std::string> dirs{ "/cache", Paths::Get().cache_log_directory() }; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 142 | for (const auto& dirname : dirs) { |
| 143 | if (RemoveFilesInDirectory(bytes_needed, dirname, FreeSpaceForFile)) { |
| 144 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 147 | |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname, |
| 152 | const std::function<size_t(const std::string&)>& space_checker) { |
| 153 | struct stat st; |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 154 | if (stat(dirname.c_str(), &st) == -1) { |
| 155 | PLOG(ERROR) << "Failed to stat " << dirname; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | if (!S_ISDIR(st.st_mode)) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 159 | LOG(ERROR) << dirname << " is not a directory"; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
| 163 | size_t free_now = space_checker(dirname); |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 164 | LOG(INFO) << free_now << " bytes free on " << dirname << " (" << bytes_needed << " needed)"; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 165 | |
| 166 | if (free_now >= bytes_needed) { |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | std::vector<std::string> files; |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 171 | if (dirname == Paths::Get().cache_log_directory()) { |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 172 | // Deletes the log files only. |
| 173 | auto log_filter = [](const std::string& file_name) { |
| 174 | return android::base::StartsWith(file_name, "last_log") || |
| 175 | android::base::StartsWith(file_name, "last_kmsg"); |
| 176 | }; |
| 177 | |
| 178 | files = FindExpendableFiles(dirname, log_filter); |
| 179 | |
| 180 | // Older logs will come to the top of the queue. |
| 181 | auto comparator = [](const std::string& name1, const std::string& name2) -> bool { |
| 182 | unsigned int index1 = GetLogIndex(android::base::Basename(name1)); |
| 183 | unsigned int index2 = GetLogIndex(android::base::Basename(name2)); |
| 184 | if (index1 == index2) { |
| 185 | return name1 < name2; |
| 186 | } |
| 187 | |
| 188 | return index1 > index2; |
| 189 | }; |
| 190 | |
| 191 | std::sort(files.begin(), files.end(), comparator); |
| 192 | } else { |
| 193 | // We're allowed to delete unopened regular files in the directory. |
| 194 | files = FindExpendableFiles(dirname, nullptr); |
| 195 | } |
| 196 | |
| 197 | for (const auto& file : files) { |
| 198 | if (unlink(file.c_str()) == -1) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 199 | PLOG(ERROR) << "Failed to delete " << file; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 200 | continue; |
| 201 | } |
| 202 | |
| 203 | free_now = space_checker(dirname); |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 204 | LOG(INFO) << "Deleted " << file << "; now " << free_now << " bytes free"; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 205 | if (free_now >= bytes_needed) { |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return false; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 211 | } |