2015-06-24 08:23:33 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-02-18 01:11:44 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <android-base/parseint.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
|
2016-03-03 20:43:47 +01:00
|
|
|
#include "applypatch/applypatch.h"
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
static int EliminateOpenFiles(std::set<std::string>* files) {
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir);
|
|
|
|
if (!d) {
|
2010-02-18 01:11:44 +01:00
|
|
|
printf("error opening /proc: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2016-02-04 02:08:52 +01:00
|
|
|
struct dirent* de;
|
|
|
|
while ((de = readdir(d.get())) != 0) {
|
|
|
|
unsigned int pid;
|
|
|
|
if (!android::base::ParseUint(de->d_name, &pid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string path = android::base::StringPrintf("/proc/%s/fd/", de->d_name);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
|
|
|
struct dirent* fdde;
|
2016-02-04 02:08:52 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> fdd(opendir(path.c_str()), closedir);
|
|
|
|
if (!fdd) {
|
|
|
|
printf("error opening %s: %s\n", path.c_str(), strerror(errno));
|
2010-02-18 01:11:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-02-04 02:08:52 +01:00
|
|
|
while ((fdde = readdir(fdd.get())) != 0) {
|
|
|
|
std::string fd_path = path + fdde->d_name;
|
2010-02-18 01:11:44 +01:00
|
|
|
char link[FILENAME_MAX];
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
int count = readlink(fd_path.c_str(), link, sizeof(link)-1);
|
2010-02-18 01:11:44 +01:00
|
|
|
if (count >= 0) {
|
|
|
|
link[count] = '\0';
|
|
|
|
if (strncmp(link, "/cache/", 7) == 0) {
|
2016-02-04 02:08:52 +01:00
|
|
|
if (files->erase(link) > 0) {
|
|
|
|
printf("%s is open by %s\n", link, de->d_name);
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
static std::set<std::string> FindExpendableFiles() {
|
|
|
|
std::set<std::string> files;
|
2010-02-18 01:11:44 +01:00
|
|
|
// We're allowed to delete unopened regular files in any of these
|
|
|
|
// directories.
|
|
|
|
const char* dirs[2] = {"/cache", "/cache/recovery/otatest"};
|
|
|
|
|
2015-06-24 08:23:33 +02:00
|
|
|
for (size_t i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) {
|
2016-02-04 02:08:52 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirs[i]), closedir);
|
|
|
|
if (!d) {
|
2010-02-18 01:11:44 +01:00
|
|
|
printf("error opening %s: %s\n", dirs[i], strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for regular files in the directory (not in any subdirectories).
|
2016-02-04 02:08:52 +01:00
|
|
|
struct dirent* de;
|
|
|
|
while ((de = readdir(d.get())) != 0) {
|
|
|
|
std::string path = std::string(dirs[i]) + "/" + de->d_name;
|
2010-02-18 01:11:44 +01:00
|
|
|
|
|
|
|
// We can't delete CACHE_TEMP_SOURCE; if it's there we might have
|
|
|
|
// restarted during installation and could be depending on it to
|
|
|
|
// be there.
|
2016-02-04 02:08:52 +01:00
|
|
|
if (path == CACHE_TEMP_SOURCE) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
|
|
|
struct stat st;
|
2016-02-04 02:08:52 +01:00
|
|
|
if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
|
|
|
|
files.insert(path);
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
printf("%zu regular files in deletable directories\n", files.size());
|
|
|
|
if (EliminateOpenFiles(&files) < 0) {
|
|
|
|
return std::set<std::string>();
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2016-02-04 02:08:52 +01:00
|
|
|
return files;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int MakeFreeSpaceOnCache(size_t bytes_needed) {
|
|
|
|
size_t free_now = FreeSpaceForFile("/cache");
|
2015-06-24 08:23:33 +02:00
|
|
|
printf("%zu bytes free on /cache (%zu needed)\n", free_now, bytes_needed);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
|
|
|
if (free_now >= bytes_needed) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-04 02:08:52 +01:00
|
|
|
std::set<std::string> files = FindExpendableFiles();
|
|
|
|
if (files.empty()) {
|
2010-02-18 01:11:44 +01:00
|
|
|
// nothing we can delete to free up space!
|
|
|
|
printf("no files can be deleted to free space on /cache\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We could try to be smarter about which files to delete: the
|
|
|
|
// biggest ones? the smallest ones that will free up enough space?
|
|
|
|
// the oldest? the newest?
|
|
|
|
//
|
|
|
|
// Instead, we'll be dumb.
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
for (const auto& file : files) {
|
|
|
|
unlink(file.c_str());
|
|
|
|
free_now = FreeSpaceForFile("/cache");
|
|
|
|
printf("deleted %s; now %zu bytes free\n", file.c_str(), free_now);
|
|
|
|
if (free_now < bytes_needed) {
|
|
|
|
break;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (free_now >= bytes_needed) ? 0 : -1;
|
|
|
|
}
|