1prepare_cleanup_test_dir() {
2    local dir=$1
3
4    rm -rf $dir
5    mkdir -p $dir
6    for i in $(seq 0 9); do
7        printf '%4017s' '' | tr ' ' 'A' >$dir/result$i-4017.o
8        backdate $((3 * i + 1)) $dir/result$i-4017.o
9        backdate $((3 * i + 2)) $dir/result$i-4017.d
10        backdate $((3 * i + 3)) $dir/result$i-4017.stderr
11    done
12    # NUMFILES: 30, TOTALSIZE: 40 KiB, MAXFILES: 0, MAXSIZE: 0
13    echo "0 0 0 0 0 0 0 0 0 0 0 30 40 0 0" >$dir/stats
14}
15
16SUITE_cleanup() {
17    # -------------------------------------------------------------------------
18    TEST "Clear cache"
19
20    prepare_cleanup_test_dir $CCACHE_DIR/a
21
22    $CCACHE -C >/dev/null
23    expect_file_count 0 '*.o' $CCACHE_DIR
24    expect_file_count 0 '*.d' $CCACHE_DIR
25    expect_file_count 0 '*.stderr' $CCACHE_DIR
26    expect_stat 'files in cache' 0
27    expect_stat 'cleanups performed' 1
28
29    # -------------------------------------------------------------------------
30    TEST "Forced cache cleanup, no limits"
31
32    prepare_cleanup_test_dir $CCACHE_DIR/a
33
34    $CCACHE -F 0 -M 0 >/dev/null
35    $CCACHE -c >/dev/null
36    expect_file_count 10 '*.o' $CCACHE_DIR
37    expect_file_count 10 '*.d' $CCACHE_DIR
38    expect_file_count 10 '*.stderr' $CCACHE_DIR
39    expect_stat 'files in cache' 30
40    expect_stat 'cleanups performed' 0
41
42    # -------------------------------------------------------------------------
43    TEST "Forced cache cleanup, file limit"
44
45    prepare_cleanup_test_dir $CCACHE_DIR/a
46
47    # No cleanup needed.
48    #
49    # 30 * 16 = 480
50    $CCACHE -F 480 -M 0 >/dev/null
51    $CCACHE -c >/dev/null
52    expect_file_count 10 '*.o' $CCACHE_DIR
53    expect_file_count 10 '*.d' $CCACHE_DIR
54    expect_file_count 10 '*.stderr' $CCACHE_DIR
55    expect_stat 'files in cache' 30
56    expect_stat 'cleanups performed' 0
57
58    # Reduce file limit
59    #
60    # 22 * 16 = 352
61    $CCACHE -F 352 -M 0 >/dev/null
62    $CCACHE -c >/dev/null
63    expect_file_count 7 '*.o' $CCACHE_DIR
64    expect_file_count 7 '*.d' $CCACHE_DIR
65    expect_file_count 8 '*.stderr' $CCACHE_DIR
66    expect_stat 'files in cache' 22
67    expect_stat 'cleanups performed' 1
68    for i in 0 1 2; do
69        file=$CCACHE_DIR/a/result$i-4017.o
70        expect_file_missing $CCACHE_DIR/a/result$i-4017.o
71    done
72    for i in 3 4 5 6 7 8 9; do
73        file=$CCACHE_DIR/a/result$i-4017.o
74        expect_file_exists $file
75    done
76
77    # -------------------------------------------------------------------------
78    TEST "Forced cache cleanup, size limit"
79
80    # NOTE: This test is known to fail on filesystems that have unusual block
81    # sizes, including ecryptfs. The workaround is to place the test directory
82    # elsewhere:
83    #
84    #     cd /tmp
85    #     CCACHE=$DIR/ccache $DIR/test.sh
86
87    prepare_cleanup_test_dir $CCACHE_DIR/a
88
89    $CCACHE -F 0 -M 256K >/dev/null
90    $CCACHE -c >/dev/null
91    expect_file_count 3 '*.o' $CCACHE_DIR
92    expect_file_count 4 '*.d' $CCACHE_DIR
93    expect_file_count 4 '*.stderr' $CCACHE_DIR
94    expect_stat 'files in cache' 11
95    expect_stat 'cleanups performed' 1
96    for i in 0 1 2 3 4 5 6; do
97        file=$CCACHE_DIR/a/result$i-4017.o
98        expect_file_missing $file
99    done
100    for i in 7 8 9; do
101        file=$CCACHE_DIR/a/result$i-4017.o
102        expect_file_exists $file
103    done
104
105    # -------------------------------------------------------------------------
106    TEST "Automatic cache cleanup, limit_multiple 0.9"
107
108    for x in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
109        prepare_cleanup_test_dir $CCACHE_DIR/$x
110    done
111
112    $CCACHE -F 480 -M 0 >/dev/null
113
114    expect_file_count 160 '*.o' $CCACHE_DIR
115    expect_file_count 160 '*.d' $CCACHE_DIR
116    expect_file_count 160 '*.stderr' $CCACHE_DIR
117    expect_stat 'files in cache' 480
118    expect_stat 'cleanups performed' 0
119
120    touch empty.c
121    CCACHE_LIMIT_MULTIPLE=0.9 $CCACHE_COMPILE -c empty.c -o empty.o
122    expect_file_count 159 '*.o' $CCACHE_DIR
123    expect_file_count 159 '*.d' $CCACHE_DIR
124    expect_file_count 159 '*.stderr' $CCACHE_DIR
125    expect_stat 'files in cache' 477
126    expect_stat 'cleanups performed' 1
127
128    # -------------------------------------------------------------------------
129    TEST "Automatic cache cleanup, limit_multiple 0.7"
130
131    for x in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
132        prepare_cleanup_test_dir $CCACHE_DIR/$x
133    done
134
135    $CCACHE -F 480 -M 0 >/dev/null
136
137    expect_file_count 160 '*.o' $CCACHE_DIR
138    expect_file_count 160 '*.d' $CCACHE_DIR
139    expect_file_count 160 '*.stderr' $CCACHE_DIR
140    expect_stat 'files in cache' 480
141    expect_stat 'cleanups performed' 0
142
143    touch empty.c
144    CCACHE_LIMIT_MULTIPLE=0.7 $CCACHE_COMPILE -c empty.c -o empty.o
145    expect_file_count 157 '*.o' $CCACHE_DIR
146    expect_file_count 157 '*.d' $CCACHE_DIR
147    expect_file_count 157 '*.stderr' $CCACHE_DIR
148    expect_stat 'files in cache' 471
149    expect_stat 'cleanups performed' 1
150
151    # -------------------------------------------------------------------------
152    TEST ".o file is removed before .stderr"
153
154    prepare_cleanup_test_dir $CCACHE_DIR/a
155    $CCACHE -F 464 -M 0 >/dev/null
156    backdate 0 $CCACHE_DIR/a/result9-4017.stderr
157    $CCACHE -c >/dev/null
158    expect_file_missing $CCACHE_DIR/a/result9-4017.stderr
159    expect_file_missing $CCACHE_DIR/a/result9-4017.o
160
161    # Counters expectedly doesn't match reality if x.stderr is found before
162    # x.o and the cleanup stops before x.o is found.
163    expect_stat 'files in cache' 29
164    expect_file_count 28 '*.*' $CCACHE_DIR/a
165
166    # -------------------------------------------------------------------------
167    TEST ".stderr file is not removed before .o"
168
169    prepare_cleanup_test_dir $CCACHE_DIR/a
170    $CCACHE -F 464 -M 0 >/dev/null
171    backdate 0 $CCACHE_DIR/a/result9-4017.o
172    $CCACHE -c >/dev/null
173    expect_file_exists $CCACHE_DIR/a/result9-4017.stderr
174    expect_file_missing $CCACHE_DIR/a/result9-4017.o
175
176    expect_stat 'files in cache' 29
177    expect_file_count 29 '*.*' $CCACHE_DIR/a
178
179    # -------------------------------------------------------------------------
180    TEST "No cleanup of new unknown file"
181
182    prepare_cleanup_test_dir $CCACHE_DIR/a
183
184    touch $CCACHE_DIR/a/abcd.unknown
185    $CCACHE -F 0 -M 0 -c >/dev/null # update counters
186    expect_stat 'files in cache' 31
187
188    $CCACHE -F 480 -M 0 >/dev/null
189    $CCACHE -c >/dev/null
190    expect_file_exists $CCACHE_DIR/a/abcd.unknown
191    expect_stat 'files in cache' 30
192
193    # -------------------------------------------------------------------------
194    TEST "Cleanup of old unknown file"
195
196    prepare_cleanup_test_dir $CCACHE_DIR/a
197    $CCACHE -F 480 -M 0 >/dev/null
198    touch $CCACHE_DIR/a/abcd.unknown
199    backdate $CCACHE_DIR/a/abcd.unknown
200    $CCACHE -F 0 -M 0 -c >/dev/null # update counters
201    expect_stat 'files in cache' 31
202
203    $CCACHE -F 480 -M 0 -c >/dev/null
204    expect_file_missing $CCACHE_DIR/a/abcd.unknown
205    expect_stat 'files in cache' 30
206
207    # -------------------------------------------------------------------------
208    TEST "Cleanup of tmp file"
209
210    mkdir -p $CCACHE_DIR/a
211    touch $CCACHE_DIR/a/abcd.tmp.efgh
212    $CCACHE -c >/dev/null # update counters
213    expect_stat 'files in cache' 1
214    backdate $CCACHE_DIR/a/abcd.tmp.efgh
215    $CCACHE -c >/dev/null
216    expect_file_missing $CCACHE_DIR/a/abcd.tmp.efgh
217    expect_stat 'files in cache' 0
218
219    # -------------------------------------------------------------------------
220    TEST "No cleanup of .nfs* files"
221
222    prepare_cleanup_test_dir $CCACHE_DIR/a
223
224    touch $CCACHE_DIR/a/.nfs0123456789
225    $CCACHE -F 0 -M 0 >/dev/null
226    $CCACHE -c >/dev/null
227    expect_file_count 1 '.nfs*' $CCACHE_DIR
228    expect_stat 'files in cache' 30
229}
230