blob: 2eb2f55ef1001c309ace74c70b68b70a5262112f [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
Doug Zongker512536a2010-02-17 16:11:44 -080017#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
28#include "applypatch.h"
29
30static int EliminateOpenFiles(char** files, int file_count) {
31 DIR* d;
32 struct dirent* de;
33 d = opendir("/proc");
34 if (d == NULL) {
35 printf("error opening /proc: %s\n", strerror(errno));
36 return -1;
37 }
38 while ((de = readdir(d)) != 0) {
39 int i;
40 for (i = 0; de->d_name[i] != '\0' && isdigit(de->d_name[i]); ++i);
41 if (de->d_name[i]) continue;
42
43 // de->d_name[i] is numeric
44
45 char path[FILENAME_MAX];
46 strcpy(path, "/proc/");
47 strcat(path, de->d_name);
48 strcat(path, "/fd/");
49
50 DIR* fdd;
51 struct dirent* fdde;
52 fdd = opendir(path);
53 if (fdd == NULL) {
54 printf("error opening %s: %s\n", path, strerror(errno));
55 continue;
56 }
57 while ((fdde = readdir(fdd)) != 0) {
58 char fd_path[FILENAME_MAX];
59 char link[FILENAME_MAX];
60 strcpy(fd_path, path);
61 strcat(fd_path, fdde->d_name);
62
63 int count;
64 count = readlink(fd_path, link, sizeof(link)-1);
65 if (count >= 0) {
66 link[count] = '\0';
67
68 // This is inefficient, but it should only matter if there are
69 // lots of files in /cache, and lots of them are open (neither
70 // of which should be true, especially in recovery).
71 if (strncmp(link, "/cache/", 7) == 0) {
72 int j;
73 for (j = 0; j < file_count; ++j) {
74 if (files[j] && strcmp(files[j], link) == 0) {
75 printf("%s is open by %s\n", link, de->d_name);
76 free(files[j]);
77 files[j] = NULL;
78 }
79 }
80 }
81 }
82 }
83 closedir(fdd);
84 }
85 closedir(d);
86
87 return 0;
88}
89
90int FindExpendableFiles(char*** names, int* entries) {
91 DIR* d;
92 struct dirent* de;
93 int size = 32;
94 *entries = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -070095 *names = reinterpret_cast<char**>(malloc(size * sizeof(char*)));
Doug Zongker512536a2010-02-17 16:11:44 -080096
97 char path[FILENAME_MAX];
98
99 // We're allowed to delete unopened regular files in any of these
100 // directories.
101 const char* dirs[2] = {"/cache", "/cache/recovery/otatest"};
102
Tao Baoba9a42a2015-06-23 23:23:33 -0700103 for (size_t i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) {
Doug Zongker512536a2010-02-17 16:11:44 -0800104 d = opendir(dirs[i]);
105 if (d == NULL) {
106 printf("error opening %s: %s\n", dirs[i], strerror(errno));
107 continue;
108 }
109
110 // Look for regular files in the directory (not in any subdirectories).
111 while ((de = readdir(d)) != 0) {
112 strcpy(path, dirs[i]);
113 strcat(path, "/");
114 strcat(path, de->d_name);
115
116 // We can't delete CACHE_TEMP_SOURCE; if it's there we might have
117 // restarted during installation and could be depending on it to
118 // be there.
119 if (strcmp(path, CACHE_TEMP_SOURCE) == 0) continue;
120
121 struct stat st;
122 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
123 if (*entries >= size) {
124 size *= 2;
Tao Baoba9a42a2015-06-23 23:23:33 -0700125 *names = reinterpret_cast<char**>(realloc(*names, size * sizeof(char*)));
Doug Zongker512536a2010-02-17 16:11:44 -0800126 }
127 (*names)[(*entries)++] = strdup(path);
128 }
129 }
130
131 closedir(d);
132 }
133
134 printf("%d regular files in deletable directories\n", *entries);
135
136 if (EliminateOpenFiles(*names, *entries) < 0) {
137 return -1;
138 }
139
140 return 0;
141}
142
143int MakeFreeSpaceOnCache(size_t bytes_needed) {
144 size_t free_now = FreeSpaceForFile("/cache");
Tao Baoba9a42a2015-06-23 23:23:33 -0700145 printf("%zu bytes free on /cache (%zu needed)\n", free_now, bytes_needed);
Doug Zongker512536a2010-02-17 16:11:44 -0800146
147 if (free_now >= bytes_needed) {
148 return 0;
149 }
150
151 char** names;
152 int entries;
153
154 if (FindExpendableFiles(&names, &entries) < 0) {
155 return -1;
156 }
157
158 if (entries == 0) {
159 // nothing we can delete to free up space!
160 printf("no files can be deleted to free space on /cache\n");
161 return -1;
162 }
163
164 // We could try to be smarter about which files to delete: the
165 // biggest ones? the smallest ones that will free up enough space?
166 // the oldest? the newest?
167 //
168 // Instead, we'll be dumb.
169
170 int i;
171 for (i = 0; i < entries && free_now < bytes_needed; ++i) {
172 if (names[i]) {
173 unlink(names[i]);
174 free_now = FreeSpaceForFile("/cache");
Tao Baoba9a42a2015-06-23 23:23:33 -0700175 printf("deleted %s; now %zu bytes free\n", names[i], free_now);
Doug Zongker512536a2010-02-17 16:11:44 -0800176 free(names[i]);
177 }
178 }
179
180 for (; i < entries; ++i) {
181 free(names[i]);
182 }
183 free(names);
184
185 return (free_now >= bytes_needed) ? 0 : -1;
186}