cache/filecache: Add a filecache root dir

This is just a safe guard to make sure we don't evict/remove files that do not belong to the cache.
This commit is contained in:
Bjørn Erik Pedersen 2018-11-14 11:53:45 +01:00
parent d3489eba5d
commit 33502667fb
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
5 changed files with 38 additions and 3 deletions

View file

@ -31,6 +31,10 @@ import (
"github.com/spf13/afero"
)
const (
filecacheRootDirname = "filecache"
)
// Cache caches a set of files in a directory. This is usually a file on
// disk, but since this is backed by an Afero file system, it can be anything.
type Cache struct {
@ -276,11 +280,24 @@ func NewCachesFromPaths(p *paths.Paths) (Caches, error) {
return nil, err
}
genDir := filepath.FromSlash("/_gen")
fs := p.Fs.Source
m := make(Caches)
for k, v := range dcfg {
baseDir := filepath.Join(v.Dir, k)
var baseDir string
if !strings.Contains(v.Dir, genDir) {
// We do cache eviction (file removes) and since the user can set
// his/hers own cache directory, we really want to make sure
// we do not delete any files that do not belong to this cache.
// We do add the cache name as the root, but this is an extra safe
// guard. We skip the files inside /resources/_gen/ because
// that would be breaking.
baseDir = filepath.Join(v.Dir, filecacheRootDirname, k)
} else {
baseDir = filepath.Join(v.Dir, k)
}
if err = fs.MkdirAll(baseDir, 0777); err != nil {
return nil, err
}

View file

@ -157,6 +157,10 @@ func decodeConfig(p *paths.Paths) (cachesConfig, error) {
return c, errors.Errorf("%q must either start with a placeholder (e.g. :cacheDir, :resourceDir) or be absolute", v.Dir)
}
if len(v.Dir) < 5 {
return c, errors.Errorf("%q is not a valid cache dir", v.Dir)
}
if disabled {
v.MaxAge = 0
}

View file

@ -33,6 +33,7 @@ func TestDecodeConfig(t *testing.T) {
assert := require.New(t)
configStr := `
resourceDir = "myresources"
[caches]
[caches.getJSON]
maxAge = "10m"
@ -72,6 +73,7 @@ func TestDecodeConfigIgnoreCache(t *testing.T) {
assert := require.New(t)
configStr := `
resourceDir = "myresources"
ignoreCache = true
[caches]
[caches.getJSON]

View file

@ -41,6 +41,8 @@ func TestFileCache(t *testing.T) {
for _, cacheDir := range []string{"mycache", ""} {
configStr := `
workingDir = "/my/work"
resourceDir = "resources"
cacheDir = "CACHEDIR"
[caches]
[caches.getJSON]
@ -69,12 +71,20 @@ dir = ":cacheDir/c"
filename, err := bfs.RealPath("key")
assert.NoError(err)
if cacheDir != "" {
assert.Equal(filepath.FromSlash(cacheDir+"/c/getjson/key"), filename)
assert.Equal(filepath.FromSlash(cacheDir+"/c/"+filecacheRootDirname+"/getjson/key"), filename)
} else {
// Temp dir.
assert.Regexp(regexp.MustCompile("hugo_cache.*key"), filename)
assert.Regexp(regexp.MustCompile(".*hugo_cache.*"+filecacheRootDirname+".*key"), filename)
}
c = caches.Get("images")
assert.NotNil(c)
assert.Equal(time.Duration(-1), c.maxAge)
bfs, ok = c.Fs.(*afero.BasePathFs)
assert.True(ok)
filename, _ = bfs.RealPath("key")
assert.Equal(filepath.FromSlash("/my/work/resources/_gen/images/key"), filename)
rf := func(s string) func() (io.ReadCloser, error) {
return func() (io.ReadCloser, error) {
return struct {
@ -149,6 +159,7 @@ func TestFileCacheConcurrent(t *testing.T) {
assert := require.New(t)
configStr := `
resourceDir = "myresources"
[caches]
[caches.getjson]
maxAge = "1s"

View file

@ -180,6 +180,7 @@ func TestScpGetRemoteParallel(t *testing.T) {
}
func newDeps(cfg config.Provider) *deps.Deps {
cfg.Set("resourceDir", "resources")
l := langs.NewLanguage("en", cfg)
l.Set("i18nDir", "i18n")
cs, err := helpers.NewContentSpec(l)