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 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <libgen.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/statfs.h> |
| 24 | #include <unistd.h> |
| 25 | #include <dirent.h> |
| 26 | #include <ctype.h> |
| 27 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 28 | #include <memory> |
| 29 | #include <set> |
| 30 | #include <string> |
| 31 | |
| 32 | #include <android-base/parseint.h> |
| 33 | #include <android-base/stringprintf.h> |
| 34 | |
Tao Bao | d80a998 | 2016-03-03 11:43:47 -0800 | [diff] [blame] | 35 | #include "applypatch/applypatch.h" |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 36 | #include "otautil/cache_location.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 37 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 38 | static int EliminateOpenFiles(std::set<std::string>* files) { |
| 39 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir); |
| 40 | if (!d) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 41 | printf("error opening /proc: %s\n", strerror(errno)); |
| 42 | return -1; |
| 43 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 44 | struct dirent* de; |
| 45 | while ((de = readdir(d.get())) != 0) { |
| 46 | unsigned int pid; |
| 47 | if (!android::base::ParseUint(de->d_name, &pid)) { |
| 48 | continue; |
| 49 | } |
| 50 | std::string path = android::base::StringPrintf("/proc/%s/fd/", de->d_name); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 51 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 52 | struct dirent* fdde; |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 53 | std::unique_ptr<DIR, decltype(&closedir)> fdd(opendir(path.c_str()), closedir); |
| 54 | if (!fdd) { |
| 55 | printf("error opening %s: %s\n", path.c_str(), strerror(errno)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 56 | continue; |
| 57 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 58 | while ((fdde = readdir(fdd.get())) != 0) { |
| 59 | std::string fd_path = path + fdde->d_name; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 60 | char link[FILENAME_MAX]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 61 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 62 | int count = readlink(fd_path.c_str(), link, sizeof(link)-1); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 63 | if (count >= 0) { |
| 64 | link[count] = '\0'; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 65 | if (strncmp(link, "/cache/", 7) == 0) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 66 | if (files->erase(link) > 0) { |
| 67 | printf("%s is open by %s\n", link, de->d_name); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 72 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 76 | static std::set<std::string> FindExpendableFiles() { |
| 77 | std::set<std::string> files; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 78 | // We're allowed to delete unopened regular files in any of these |
| 79 | // directories. |
| 80 | const char* dirs[2] = {"/cache", "/cache/recovery/otatest"}; |
| 81 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 82 | for (size_t i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 83 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirs[i]), closedir); |
| 84 | if (!d) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 85 | printf("error opening %s: %s\n", dirs[i], strerror(errno)); |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | // Look for regular files in the directory (not in any subdirectories). |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 90 | struct dirent* de; |
| 91 | while ((de = readdir(d.get())) != 0) { |
| 92 | std::string path = std::string(dirs[i]) + "/" + de->d_name; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 93 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 94 | // We can't delete cache_temp_source; if it's there we might have restarted during |
| 95 | // installation and could be depending on it to be there. |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 96 | if (path == CacheLocation::location().cache_temp_source()) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 97 | continue; |
| 98 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 99 | |
| 100 | struct stat st; |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 101 | if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { |
| 102 | files.insert(path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 107 | printf("%zu regular files in deletable directories\n", files.size()); |
| 108 | if (EliminateOpenFiles(&files) < 0) { |
| 109 | return std::set<std::string>(); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 110 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 111 | return files; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int MakeFreeSpaceOnCache(size_t bytes_needed) { |
Tianjie Xu | e40c80d | 2018-02-03 17:20:56 -0800 | [diff] [blame] | 115 | #ifndef __ANDROID__ |
| 116 | // TODO (xunchang) implement a heuristic cache size check during host simulation. |
| 117 | printf("Skip making (%zu) bytes free space on cache; program is running on host\n", bytes_needed); |
| 118 | return 0; |
| 119 | #endif |
| 120 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 121 | size_t free_now = FreeSpaceForFile("/cache"); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 122 | printf("%zu bytes free on /cache (%zu needed)\n", free_now, bytes_needed); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 123 | |
| 124 | if (free_now >= bytes_needed) { |
| 125 | return 0; |
| 126 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 127 | std::set<std::string> files = FindExpendableFiles(); |
| 128 | if (files.empty()) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 129 | // nothing we can delete to free up space! |
| 130 | printf("no files can be deleted to free space on /cache\n"); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | // We could try to be smarter about which files to delete: the |
| 135 | // biggest ones? the smallest ones that will free up enough space? |
| 136 | // the oldest? the newest? |
| 137 | // |
| 138 | // Instead, we'll be dumb. |
| 139 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 140 | for (const auto& file : files) { |
| 141 | unlink(file.c_str()); |
| 142 | free_now = FreeSpaceForFile("/cache"); |
| 143 | printf("deleted %s; now %zu bytes free\n", file.c_str(), free_now); |
| 144 | if (free_now < bytes_needed) { |
| 145 | break; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 148 | return (free_now >= bytes_needed) ? 0 : -1; |
| 149 | } |