Imported Upstream version 3.2
[platform/upstream/ccache.git] / cleanup.c
1 /*
2  * Copyright (C) 2002-2006 Andrew Tridgell
3  * Copyright (C) 2009-2014 Joel Rosdahl
4  *
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)
8  * any later version.
9  *
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
13  * more details.
14  *
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
18  */
19
20 #include "ccache.h"
21
22 /*
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.
26  */
27 #define LIMIT_MULTIPLE 0.8
28
29 static struct files {
30         char *fname;
31         time_t mtime;
32         uint64_t size;
33 } **files;
34 static unsigned allocated; /* Size of the files array. */
35 static unsigned num_files; /* Number of used entries in the files array. */
36
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;
41
42 /* File comparison function that orders files in mtime order, oldest first. */
43 static int
44 files_compare(struct files **f1, struct files **f2)
45 {
46         if ((*f2)->mtime == (*f1)->mtime) {
47                 return strcmp((*f1)->fname, (*f2)->fname);
48         }
49         if ((*f2)->mtime > (*f1)->mtime) {
50                 return -1;
51         }
52         return 1;
53 }
54
55 /* this builds the list of files in the cache */
56 static void
57 traverse_fn(const char *fname, struct stat *st)
58 {
59         char *p;
60
61         if (!S_ISREG(st->st_mode)) {
62                 return;
63         }
64
65         p = basename(fname);
66         if (str_eq(p, "stats")) {
67                 goto out;
68         }
69
70         if (str_startswith(p, ".nfs")) {
71                 /* Ignore temporary NFS files that may be left for open but deleted files. */
72                 goto out;
73         }
74
75         if (strstr(p, ".tmp.")) {
76                 /* delete any tmp files older than 1 hour */
77                 if (st->st_mtime + 3600 < time(NULL)) {
78                         x_unlink(fname);
79                         goto out;
80                 }
81         }
82
83         if (num_files == allocated) {
84                 allocated = 10000 + num_files*2;
85                 files = (struct files **)x_realloc(files, sizeof(struct files *)*allocated);
86         }
87
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;
93         files_in_cache++;
94         num_files++;
95
96 out:
97         free(p);
98 }
99
100 static void
101 delete_file(const char *path, size_t size)
102 {
103         if (x_unlink(path) == 0) {
104                 cache_size -= size;
105                 files_in_cache--;
106         } else if (errno != ENOENT) {
107                 cc_log("Failed to unlink %s (%s)", path, strerror(errno));
108         }
109 }
110
111 static void
112 delete_sibling_file(const char *base, const char *extension)
113 {
114         struct stat st;
115         char *path;
116
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));
122         }
123         free(path);
124 }
125
126 /* sort the files we've found and delete the oldest ones until we are
127    below the thresholds */
128 static void
129 sort_and_clean(void)
130 {
131         unsigned i;
132         const char *ext;
133         char *last_base = x_strdup("");
134
135         if (num_files > 1) {
136                 /* Sort in ascending mtime order. */
137                 qsort(files, num_files, sizeof(struct files *), (COMPAR_FN_T)files_compare);
138         }
139
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)) {
146                         break;
147                 }
148
149                 ext = get_extension(files[i]->fname);
150                 if (str_eq(ext, ".o")
151                     || str_eq(ext, ".d")
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. */
157                                 /*
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.
163                                  */
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. */
169                         }
170                         free(last_base);
171                         last_base = base;
172                 } else {
173                         /* .manifest or unknown file. */
174                         delete_file(files[i]->fname, files[i]->size);
175                 }
176         }
177         free(last_base);
178 }
179
180 /* cleanup in one cache subdir */
181 void
182 cleanup_dir(struct conf *conf, const char *dir)
183 {
184         unsigned i;
185
186         cc_log("Cleaning up cache directory %s", dir);
187
188         cache_size_threshold = conf->max_size * LIMIT_MULTIPLE / 16;
189         files_in_cache_threshold = conf->max_files * LIMIT_MULTIPLE / 16;
190
191         num_files = 0;
192         cache_size = 0;
193         files_in_cache = 0;
194
195         /* build a list of files */
196         traverse(dir, traverse_fn);
197
198         /* clean the cache */
199         sort_and_clean();
200
201         stats_set_sizes(dir, files_in_cache, cache_size);
202
203         /* free it up */
204         for (i = 0; i < num_files; i++) {
205                 free(files[i]->fname);
206                 free(files[i]);
207                 files[i] = NULL;
208         }
209         if (files) {
210                 free(files);
211         }
212         allocated = 0;
213         files = NULL;
214
215         num_files = 0;
216         cache_size = 0;
217         files_in_cache = 0;
218 }
219
220 /* cleanup in all cache subdirs */
221 void cleanup_all(struct conf *conf)
222 {
223         char *dname;
224         int i;
225
226         for (i = 0; i <= 0xF; i++) {
227                 dname = format("%s/%1x", conf->cache_dir, i);
228                 cleanup_dir(conf, dname);
229                 free(dname);
230         }
231 }
232
233 /* traverse function for wiping files */
234 static void wipe_fn(const char *fname, struct stat *st)
235 {
236         char *p;
237
238         if (!S_ISREG(st->st_mode)) {
239                 return;
240         }
241
242         p = basename(fname);
243         if (str_eq(p, "stats")) {
244                 free(p);
245                 return;
246         }
247         free(p);
248
249         x_unlink(fname);
250 }
251
252 /* wipe all cached files in all subdirs */
253 void wipe_all(struct conf *conf)
254 {
255         char *dname;
256         int i;
257
258         for (i = 0; i <= 0xF; i++) {
259                 dname = format("%s/%1x", conf->cache_dir, i);
260                 traverse(dname, wipe_fn);
261                 free(dname);
262         }
263
264         /* and fix the counters */
265         cleanup_all(conf);
266 }