2 * Copyright (C) 2002-2006 Andrew Tridgell
3 * Copyright (C) 2009-2014 Joel Rosdahl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 3 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * When "max files" or "max cache size" is reached, one of the 16 cache
24 * subdirectories is cleaned up. When doing so, files are deleted (in LRU
25 * order) until the levels are below LIMIT_MULTIPLE.
27 #define LIMIT_MULTIPLE 0.8
34 static unsigned allocated; /* Size of the files array. */
35 static unsigned num_files; /* Number of used entries in the files array. */
37 static uint64_t cache_size;
38 static size_t files_in_cache;
39 static uint64_t cache_size_threshold;
40 static size_t files_in_cache_threshold;
42 /* File comparison function that orders files in mtime order, oldest first. */
44 files_compare(struct files **f1, struct files **f2)
46 if ((*f2)->mtime == (*f1)->mtime) {
47 return strcmp((*f1)->fname, (*f2)->fname);
49 if ((*f2)->mtime > (*f1)->mtime) {
55 /* this builds the list of files in the cache */
57 traverse_fn(const char *fname, struct stat *st)
61 if (!S_ISREG(st->st_mode)) {
66 if (str_eq(p, "stats")) {
70 if (str_startswith(p, ".nfs")) {
71 /* Ignore temporary NFS files that may be left for open but deleted files. */
75 if (strstr(p, ".tmp.")) {
76 /* delete any tmp files older than 1 hour */
77 if (st->st_mtime + 3600 < time(NULL)) {
83 if (num_files == allocated) {
84 allocated = 10000 + num_files*2;
85 files = (struct files **)x_realloc(files, sizeof(struct files *)*allocated);
88 files[num_files] = (struct files *)x_malloc(sizeof(struct files));
89 files[num_files]->fname = x_strdup(fname);
90 files[num_files]->mtime = st->st_mtime;
91 files[num_files]->size = file_size(st);
92 cache_size += files[num_files]->size;
101 delete_file(const char *path, size_t size)
103 if (x_unlink(path) == 0) {
106 } else if (errno != ENOENT) {
107 cc_log("Failed to unlink %s (%s)", path, strerror(errno));
112 delete_sibling_file(const char *base, const char *extension)
117 path = format("%s%s", base, extension);
118 if (lstat(path, &st) == 0) {
119 delete_file(path, file_size(&st));
120 } else if (errno != ENOENT) {
121 cc_log("Failed to stat %s (%s)", path, strerror(errno));
126 /* sort the files we've found and delete the oldest ones until we are
127 below the thresholds */
133 char *last_base = x_strdup("");
136 /* Sort in ascending mtime order. */
137 qsort(files, num_files, sizeof(struct files *), (COMPAR_FN_T)files_compare);
140 /* delete enough files to bring us below the threshold */
141 for (i = 0; i < num_files; i++) {
142 if ((cache_size_threshold == 0
143 || cache_size <= cache_size_threshold)
144 && (files_in_cache_threshold == 0
145 || files_in_cache <= files_in_cache_threshold)) {
149 ext = get_extension(files[i]->fname);
150 if (str_eq(ext, ".o")
152 || str_eq(ext, ".dia")
153 || str_eq(ext, ".stderr")
154 || str_eq(ext, "")) {
155 char *base = remove_extension(files[i]->fname);
156 if (!str_eq(base, last_base)) { /* Avoid redundant unlinks. */
158 * Make sure that all sibling files are deleted so that a cached result
159 * is removed completely. Note the order of deletions -- the stderr
160 * file must be deleted last because if the ccache process gets killed
161 * after deleting the .stderr but before deleting the .o, the cached
162 * result would be inconsistent.
164 delete_sibling_file(base, ".o");
165 delete_sibling_file(base, ".d");
166 delete_sibling_file(base, ".dia");
167 delete_sibling_file(base, ".stderr");
168 delete_sibling_file(base, ""); /* Object file from ccache 2.4. */
173 /* .manifest or unknown file. */
174 delete_file(files[i]->fname, files[i]->size);
180 /* cleanup in one cache subdir */
182 cleanup_dir(struct conf *conf, const char *dir)
186 cc_log("Cleaning up cache directory %s", dir);
188 cache_size_threshold = conf->max_size * LIMIT_MULTIPLE / 16;
189 files_in_cache_threshold = conf->max_files * LIMIT_MULTIPLE / 16;
195 /* build a list of files */
196 traverse(dir, traverse_fn);
198 /* clean the cache */
201 stats_set_sizes(dir, files_in_cache, cache_size);
204 for (i = 0; i < num_files; i++) {
205 free(files[i]->fname);
220 /* cleanup in all cache subdirs */
221 void cleanup_all(struct conf *conf)
226 for (i = 0; i <= 0xF; i++) {
227 dname = format("%s/%1x", conf->cache_dir, i);
228 cleanup_dir(conf, dname);
233 /* traverse function for wiping files */
234 static void wipe_fn(const char *fname, struct stat *st)
238 if (!S_ISREG(st->st_mode)) {
243 if (str_eq(p, "stats")) {
252 /* wipe all cached files in all subdirs */
253 void wipe_all(struct conf *conf)
258 for (i = 0; i <= 0xF; i++) {
259 dname = format("%s/%1x", conf->cache_dir, i);
260 traverse(dname, wipe_fn);
264 /* and fix the counters */