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