diff --git a/cache/filecache/filecache.go b/cache/filecache/filecache.go index da0a90d57..a934dd89c 100644 --- a/cache/filecache/filecache.go +++ b/cache/filecache/filecache.go @@ -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 } diff --git a/cache/filecache/filecache_config.go b/cache/filecache/filecache_config.go index 9913fd7e2..f0dd7295a 100644 --- a/cache/filecache/filecache_config.go +++ b/cache/filecache/filecache_config.go @@ -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 } diff --git a/cache/filecache/filecache_config_test.go b/cache/filecache/filecache_config_test.go index 22fb1b56b..abba6c25f 100644 --- a/cache/filecache/filecache_config_test.go +++ b/cache/filecache/filecache_config_test.go @@ -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] diff --git a/cache/filecache/filecache_test.go b/cache/filecache/filecache_test.go index b3b8376ed..986d41f7b 100644 --- a/cache/filecache/filecache_test.go +++ b/cache/filecache/filecache_test.go @@ -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" diff --git a/tpl/data/resources_test.go b/tpl/data/resources_test.go index 54eb123ee..b143bb1a3 100644 --- a/tpl/data/resources_test.go +++ b/tpl/data/resources_test.go @@ -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)