Debian Codegraph @

cache/filecache/filecache_pruner.go

raw
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package filecache
15
16 import (
17 "fmt"
18 "io"
19 "os"
20 "strings"
21
22 "github.com/gohugoio/hugo/common/herrors"
23 "github.com/gohugoio/hugo/hugofs"
24
25 "github.com/spf13/afero"
26 )
27
28 // Prune removes expired and unused items from this cache.
29 // The last one requires a full build so the cache usage can be tracked.
30 // Note that we operate directly on the filesystem here, so this is not
31 // thread safe.
32 func (c Caches) Prune() (int, error) {
33 counter := 0
34 for k, cache := range c {
35 count, err := cache.Prune(false)
36
37 counter += count
38
39 if err != nil {
40 if herrors.IsNotExist(err) {
41 continue
42 }
43 return counter, fmt.Errorf("failed to prune cache %q: %w", k, err)
44 }
45
46 }
47
48 return counter, nil
49 }
50
51 // Prune removes expired and unused items from this cache.
52 // If force is set, everything will be removed not considering expiry time.
53 func (c *Cache) Prune(force bool) (int, error) {
54 if c.cfg.entryIsDir {
55 return c.pruneRootDirs(force)
56 }
57 if err := c.init(); err != nil {
58 return 0, err
59 }
60
61 counter := 0
62
63 seen := c.entryLocker.seen
64 seenByLower := make(map[string]string, seen.Len())
65 for id := range seen.All() {
66 seenByLower[strings.ToLower(id)] = id
67 }
68
69 // Names on disk matching a used cache key except for the case, and the used keys
70 // actually walked. See the note about case-insensitive filesystems below.
71 var candidates map[string]string
72 visited := make(map[string]bool, seen.Len())
73
74 remove := func(name string) error {
75 err := c.Fs.Remove(name)
76 if err == nil {
77 counter++
78 return nil
79 }
80 if !herrors.IsNotExist(err) {
81 return err
82 }
83 return nil
84 }
85
86 err := afero.Walk(c.Fs, "", func(name string, info os.FileInfo, err error) error {
87 if info == nil {
88 return nil
89 }
90
91 name = cleanID(name)
92
93 if info.IsDir() {
94 f, err := c.Fs.Open(name)
95 if err != nil {
96 // This cache dir may not exist.
97 return nil
98 }
99 _, err = f.Readdirnames(1)
100 f.Close()
101 if err == io.EOF {
102 // Empty dir.
103 if name == "." {
104 // e.g. /_gen/images -- keep it even if empty.
105 err = nil
106 } else {
107 err = c.Fs.Remove(name)
108 }
109 }
110
111 if err != nil && !herrors.IsNotExist(err) {
112 return err
113 }
114
115 return nil
116 }
117
118 shouldRemove := force || c.isExpired(info.ModTime())
119
120 if seen.Has(name) {
121 visited[name] = true
122 } else if !shouldRemove && seen.Len() > 0 {
123 if id, found := seenByLower[strings.ToLower(name)]; found {
124 // On case-insensitive filesystems this is the same file as id; e.g. an
125 // entry created before Hugo started lowercasing the content paths in
126 // v0.123 (content/MyBundle => _gen/images/MyBundle). Decided once the
127 // walk is done: if id is walked too, they are distinct files and this
128 // one is stale. See issue 15101.
129 if candidates == nil {
130 candidates = make(map[string]string)
131 }
132 candidates[name] = id
133 } else {
134 // Remove it if it's not been touched/used in the last build.
135 shouldRemove = true
136 }
137 }
138
139 if shouldRemove {
140 return remove(name)
141 }
142
143 return nil
144 })
145 if err != nil {
146 return counter, err
147 }
148
149 for name, id := range candidates {
150 if visited[id] {
151 if err := remove(name); err != nil {
152 return counter, err
153 }
154 }
155 }
156
157 return counter, nil
158 }
159
160 func (c *Cache) pruneRootDirs(force bool) (int, error) {
161 dirs, err := afero.ReadDir(c.Fs, "")
162 if err != nil {
163 if herrors.IsNotExist(err) {
164 return 0, nil
165 }
166 return 0, err
167 }
168
169 counter := 0
170
171 for _, dir := range dirs {
172 if !dir.IsDir() {
173 continue
174 }
175
176 count, err := c.pruneRootDir(dir.Name(), force)
177 if err != nil {
178 return counter, err
179 }
180 counter += count
181 }
182
183 return counter, nil
184 }
185
186 func (c *Cache) pruneRootDir(dirname string, force bool) (int, error) {
187 if err := c.init(); err != nil {
188 return 0, err
189 }
190
191 // Sanity check.
192 if dirname != "pkg" && len(dirname) < 5 {
193 panic(fmt.Sprintf("invalid cache dir name: %q", dirname))
194 }
195
196 info, err := c.Fs.Stat(dirname)
197 if err != nil {
198 if herrors.IsNotExist(err) {
199 return 0, nil
200 }
201 return 0, err
202 }
203
204 if !force && !c.isExpired(info.ModTime()) {
205 return 0, nil
206 }
207
208 if c.cfg.isReadOnly {
209 return hugofs.MakeReadableAndRemoveAllModulePkgDir(c.Fs, dirname)
210 }
211
212 return 1, c.Fs.RemoveAll(dirname)
213 }