diff options
| author | 2017-03-28 05:41:04 +0000 | |
|---|---|---|
| committer | 2017-03-28 05:41:05 +0000 | |
| commit | 5f8e45a196836c1db2cc8f3a748c63f0e26b2f99 (patch) | |
| tree | 7b6ca5a12d064293e9439ca7bed8090a72a92526 | |
| parent | e90eac6a5884ee7212ae03b753113e438cf01e41 (diff) | |
| parent | 1cb4aafc17daae520a5c3a991f59605ea3ee4ee5 (diff) | |
Merge "Remove legacy cache clearing logic."
| -rw-r--r-- | cmds/installd/InstalldNativeService.cpp | 25 | ||||
| -rw-r--r-- | cmds/installd/utils.cpp | 429 | ||||
| -rw-r--r-- | cmds/installd/utils.h | 37 |
3 files changed, 1 insertions, 490 deletions
diff --git a/cmds/installd/InstalldNativeService.cpp b/cmds/installd/InstalldNativeService.cpp index ca4be0a785..381e90c117 100644 --- a/cmds/installd/InstalldNativeService.cpp +++ b/cmds/installd/InstalldNativeService.cpp @@ -899,30 +899,7 @@ binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::strin ATRACE_END(); } else { - ATRACE_BEGIN("start"); - cache_t* cache = start_cache_collection(); - ATRACE_END(); - - ATRACE_BEGIN("add"); - for (auto user : get_known_users(uuid_)) { - add_cache_files(cache, create_data_user_ce_path(uuid_, user)); - add_cache_files(cache, create_data_user_de_path(uuid_, user)); - add_cache_files(cache, - StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); - } - // Add files from /data/preloads/file_cache - if (uuid == nullptr) { - add_preloads_file_cache(cache, uuid_); - } - ATRACE_END(); - - ATRACE_BEGIN("clear"); - clear_cache_files(data_path, cache, freeStorageSize); - ATRACE_END(); - - ATRACE_BEGIN("finish"); - finish_cache_collection(cache); - ATRACE_END(); + return error("Legacy cache logic no longer supported"); } free = data_disk_free(data_path); diff --git a/cmds/installd/utils.cpp b/cmds/installd/utils.cpp index 03ee23f1a8..fbe6edf712 100644 --- a/cmds/installd/utils.cpp +++ b/cmds/installd/utils.cpp @@ -38,7 +38,6 @@ #define LOG_TAG "installd" #endif -#define CACHE_NOISY(x) //x #define DEBUG_XATTRS 0 using android::base::StringPrintf; @@ -641,253 +640,6 @@ int64_t data_disk_free(const std::string& data_path) } } -cache_t* start_cache_collection() -{ - cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t)); - return cache; -} - -#define CACHE_BLOCK_SIZE (512*1024) - -static void* _cache_malloc(cache_t* cache, size_t len) -{ - len = (len+3)&~3; - if (len > (CACHE_BLOCK_SIZE/2)) { - // It doesn't make sense to try to put this allocation into one - // of our blocks, because it is so big. Instead, make a new dedicated - // block for it. - int8_t* res = (int8_t*)malloc(len+sizeof(void*)); - if (res == NULL) { - return NULL; - } - CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %zu", res, len)); - // Link it into our list of blocks, not disrupting the current one. - if (cache->memBlocks == NULL) { - *(void**)res = NULL; - cache->memBlocks = res; - } else { - *(void**)res = *(void**)cache->memBlocks; - *(void**)cache->memBlocks = res; - } - return res + sizeof(void*); - } - int8_t* res = cache->curMemBlockAvail; - int8_t* nextPos = res + len; - if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) { - int8_t* newBlock = (int8_t*) malloc(CACHE_BLOCK_SIZE); - if (newBlock == NULL) { - return NULL; - } - CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock)); - *(void**)newBlock = cache->memBlocks; - cache->memBlocks = newBlock; - res = cache->curMemBlockAvail = newBlock + sizeof(void*); - cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE; - nextPos = res + len; - } - CACHE_NOISY(ALOGI("cache_malloc: ret %p size %zu, block=%p, nextPos=%p", - res, len, cache->memBlocks, nextPos)); - cache->curMemBlockAvail = nextPos; - return res; -} - -static void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len) -{ - // This isn't really a realloc, but it is good enough for our purposes here. - void* alloc = _cache_malloc(cache, len); - if (alloc != NULL && cur != NULL) { - memcpy(alloc, cur, origLen < len ? origLen : len); - } - return alloc; -} - -static void _inc_num_cache_collected(cache_t* cache) -{ - cache->numCollected++; - if ((cache->numCollected%20000) == 0) { - ALOGI("Collected cache so far: %zd directories, %zd files", - cache->numDirs, cache->numFiles); - } -} - -static cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name) -{ - size_t nameLen = strlen(name); - cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1); - if (dir != NULL) { - dir->parent = parent; - dir->childCount = 0; - dir->hiddenCount = 0; - dir->deleted = 0; - strcpy(dir->name, name); - if (cache->numDirs >= cache->availDirs) { - size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2; - cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs, - cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*)); - if (newDirs == NULL) { - ALOGE("Failure growing cache dirs array for %s\n", name); - return NULL; - } - cache->availDirs = newAvail; - cache->dirs = newDirs; - } - cache->dirs[cache->numDirs] = dir; - cache->numDirs++; - if (parent != NULL) { - parent->childCount++; - } - _inc_num_cache_collected(cache); - } else { - ALOGE("Failure allocating cache_dir_t for %s\n", name); - } - return dir; -} - -static cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime, - const char *name) -{ - size_t nameLen = strlen(name); - cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1); - if (file != NULL) { - file->dir = dir; - file->modTime = modTime; - strcpy(file->name, name); - if (cache->numFiles >= cache->availFiles) { - size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2; - cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files, - cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*)); - if (newFiles == NULL) { - ALOGE("Failure growing cache file array for %s\n", name); - return NULL; - } - cache->availFiles = newAvail; - cache->files = newFiles; - } - CACHE_NOISY(ALOGI("Setting file %p at position %zd in array %p", file, - cache->numFiles, cache->files)); - cache->files[cache->numFiles] = file; - cache->numFiles++; - dir->childCount++; - _inc_num_cache_collected(cache); - } else { - ALOGE("Failure allocating cache_file_t for %s\n", name); - } - return file; -} - -static int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName, - DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen) -{ - struct dirent *de; - cache_dir_t* cacheDir = NULL; - int dfd; - - CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s", - parentDir, dirName, dir, pathBase)); - - dfd = dirfd(dir); - - if (dfd < 0) return 0; - - // Sub-directories always get added to the data structure, so if they - // are empty we will know about them to delete them later. - cacheDir = _add_cache_dir_t(cache, parentDir, dirName); - - while ((de = readdir(dir))) { - const char *name = de->d_name; - - if (de->d_type == DT_DIR) { - int subfd; - DIR *subdir; - - /* always skip "." and ".." */ - if (name[0] == '.') { - if (name[1] == 0) continue; - if ((name[1] == '.') && (name[2] == 0)) continue; - } - - subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); - if (subfd < 0) { - ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); - continue; - } - subdir = fdopendir(subfd); - if (subdir == NULL) { - ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); - close(subfd); - continue; - } - if (cacheDir == NULL) { - cacheDir = _add_cache_dir_t(cache, parentDir, dirName); - } - if (cacheDir != NULL) { - // Update pathBase for the new path... this may change dirName - // if that is also pointing to the path, but we are done with it - // now. - size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name); - CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase)); - if (finallen < pathAvailLen) { - _add_cache_files(cache, cacheDir, name, subdir, pathBase, - pathPos+finallen, pathAvailLen-finallen); - } else { - // Whoops, the final path is too long! We'll just delete - // this directory. - ALOGW("Cache dir %s truncated in path %s; deleting dir\n", - name, pathBase); - _delete_dir_contents(subdir, NULL); - if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { - ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); - } - } - } - closedir(subdir); - } else if (de->d_type == DT_REG) { - // Skip files that start with '.'; they will be deleted if - // their entire directory is deleted. This allows for metadata - // like ".nomedia" to remain in the directory until the entire - // directory is deleted. - if (cacheDir == NULL) { - cacheDir = _add_cache_dir_t(cache, parentDir, dirName); - } - if (name[0] == '.') { - cacheDir->hiddenCount++; - continue; - } - if (cacheDir != NULL) { - // Build final full path for file... this may change dirName - // if that is also pointing to the path, but we are done with it - // now. - size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name); - CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase)); - if (finallen < pathAvailLen) { - struct stat s; - if (stat(pathBase, &s) >= 0) { - _add_cache_file_t(cache, cacheDir, s.st_mtime, name); - } else { - ALOGW("Unable to stat cache file %s; deleting\n", pathBase); - if (unlink(pathBase) < 0) { - ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno)); - } - } - } else { - // Whoops, the final path is too long! We'll just delete - // this file. - ALOGW("Cache file %s truncated in path %s; deleting\n", - name, pathBase); - if (unlinkat(dfd, name, 0) < 0) { - *pathPos = 0; - ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase, - strerror(errno)); - } - } - } - } else { - cacheDir->hiddenCount++; - } - } - return 0; -} - int get_path_inode(const std::string& path, ino_t *inode) { struct stat buf; memset(&buf, 0, sizeof(buf)); @@ -981,187 +733,6 @@ std::string read_path_inode(const std::string& parent, const char* name, const c } } -void add_cache_files(cache_t* cache, const std::string& data_path) { - DIR *d; - struct dirent *de; - char dirname[PATH_MAX]; - - const char* basepath = data_path.c_str(); - CACHE_NOISY(ALOGI("add_cache_files: basepath=%s\n", basepath)); - - d = opendir(basepath); - if (d == NULL) { - return; - } - - while ((de = readdir(d))) { - if (de->d_type == DT_DIR) { - DIR* subdir; - const char *name = de->d_name; - - /* always skip "." and ".." */ - if (name[0] == '.') { - if (name[1] == 0) continue; - if ((name[1] == '.') && (name[2] == 0)) continue; - } - - auto parent = StringPrintf("%s/%s", basepath, name); - auto resolved = read_path_inode(parent, "cache", kXattrInodeCache); - strcpy(dirname, resolved.c_str()); - CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname)); - - subdir = opendir(dirname); - if (subdir != NULL) { - size_t dirnameLen = strlen(dirname); - _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen, - PATH_MAX - dirnameLen); - closedir(subdir); - } - } - } - - closedir(d); -} - -void add_preloads_file_cache(cache_t* cache, const char* volume_uuid) { - char dirname[PATH_MAX]; - DIR* subdir; - auto cache_path = StringPrintf("%s/preloads/file_cache", create_data_path(volume_uuid).c_str()); - strcpy(dirname, cache_path.c_str()); - CACHE_NOISY(ALOGI("add_preloads_file_cache: dirname=%s\n", dirname)); - subdir = opendir(dirname); - if (subdir != NULL) { - size_t dirnameLen = strlen(dirname); - _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname + dirnameLen, - PATH_MAX - dirnameLen); - closedir(subdir); - } -} - -static char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir) -{ - char *pos = path; - if (dir->parent != NULL) { - pos = create_dir_path(path, dir->parent); - } - // Note that we don't need to worry about going beyond the buffer, - // since when we were constructing the cache entries our maximum - // buffer size for full paths was PATH_MAX. - strcpy(pos, dir->name); - pos += strlen(pos); - *pos = '/'; - pos++; - *pos = 0; - return pos; -} - -static void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir) -{ - if (dir->parent != NULL) { - create_dir_path(path, dir); - ALOGI("DEL DIR %s\n", path); - if (dir->hiddenCount <= 0) { - if (rmdir(path)) { - ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno)); - return; - } - } else { - // The directory contains hidden files so we need to delete - // them along with the directory itself. - if (delete_dir_contents(path, 1, NULL)) { - return; - } - } - dir->parent->childCount--; - dir->deleted = 1; - if (dir->parent->childCount <= 0) { - delete_cache_dir(path, dir->parent); - } - } else if (dir->hiddenCount > 0) { - // This is a root directory, but it has hidden files. Get rid of - // all of those files, but not the directory itself. - create_dir_path(path, dir); - ALOGI("DEL CONTENTS %s\n", path); - delete_dir_contents(path, 0, NULL); - } -} - -static int cache_modtime_sort(const void *lhsP, const void *rhsP) -{ - const cache_file_t *lhs = *(const cache_file_t**)lhsP; - const cache_file_t *rhs = *(const cache_file_t**)rhsP; - return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0); -} - -void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size) -{ - size_t i; - int skip = 0; - char path[PATH_MAX]; - - ALOGI("Collected cache files: %zd directories, %zd files", - cache->numDirs, cache->numFiles); - - CACHE_NOISY(ALOGI("Sorting files...")); - qsort(cache->files, cache->numFiles, sizeof(cache_file_t*), - cache_modtime_sort); - - CACHE_NOISY(ALOGI("Cleaning empty directories...")); - for (i=cache->numDirs; i>0; i--) { - cache_dir_t* dir = cache->dirs[i-1]; - if (dir->childCount <= 0 && !dir->deleted) { - delete_cache_dir(path, dir); - } - } - - CACHE_NOISY(ALOGI("Trimming files...")); - for (i=0; i<cache->numFiles; i++) { - skip++; - if (skip > 10) { - if (data_disk_free(data_path) > free_size) { - return; - } - skip = 0; - } - cache_file_t* file = cache->files[i]; - strcpy(create_dir_path(path, file->dir), file->name); - ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path); - if (unlink(path) < 0) { - ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno)); - } - file->dir->childCount--; - if (file->dir->childCount <= 0) { - delete_cache_dir(path, file->dir); - } - } -} - -void finish_cache_collection(cache_t* cache) -{ - CACHE_NOISY(size_t i;) - - CACHE_NOISY(ALOGI("clear_cache_files: %zu dirs, %zu files\n", cache->numDirs, cache->numFiles)); - CACHE_NOISY( - for (i=0; i<cache->numDirs; i++) { - cache_dir_t* dir = cache->dirs[i]; - ALOGI("dir #%zu: %p %s parent=%p\n", i, dir, dir->name, dir->parent); - }) - CACHE_NOISY( - for (i=0; i<cache->numFiles; i++) { - cache_file_t* file = cache->files[i]; - ALOGI("file #%zu: %p %s time=%d dir=%p\n", i, file, file->name, - (int)file->modTime, file->dir); - }) - void* block = cache->memBlocks; - while (block != NULL) { - void* nextBlock = *(void**)block; - CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block)); - free(block); - block = nextBlock; - } - free(cache); -} - /** * Validate that the path is valid in the context of the provided directory. * The path is allowed to have at most one subdirectory and no indirections diff --git a/cmds/installd/utils.h b/cmds/installd/utils.h index a4eb1f66b9..7ebfea213c 100644 --- a/cmds/installd/utils.h +++ b/cmds/installd/utils.h @@ -37,33 +37,6 @@ namespace installd { struct dir_rec_t; -typedef struct cache_dir_struct { - struct cache_dir_struct* parent; - int32_t childCount; - int32_t hiddenCount; - int32_t deleted; - char name[]; -} cache_dir_t; - -typedef struct { - cache_dir_t* dir; - time_t modTime; - char name[]; -} cache_file_t; - -typedef struct { - size_t numDirs; - size_t availDirs; - cache_dir_t** dirs; - size_t numFiles; - size_t availFiles; - cache_file_t** files; - size_t numCollected; - void* memBlocks; - int8_t* curMemBlockAvail; - int8_t* curMemBlockEnd; -} cache_t; - constexpr const char* kXattrInodeCache = "user.inode_cache"; constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache"; constexpr const char* kXattrCacheGroup = "user.cache_group"; @@ -139,21 +112,11 @@ int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t int64_t data_disk_free(const std::string& data_path); -cache_t* start_cache_collection(); - int get_path_inode(const std::string& path, ino_t *inode); int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr); std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr); -void add_cache_files(cache_t* cache, const std::string& data_path); - -void add_preloads_file_cache(cache_t* cache, const char* volume_uuid); - -void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size); - -void finish_cache_collection(cache_t* cache); - int validate_system_app_path(const char* path); bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path, const char* volume_uuid, int uid, int storage_flag); |