diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-11-14 11:53:45 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-11-14 23:14:51 +0100 |
commit | 33502667fbacf57167ede66df8f13e308a4a9aec (patch) | |
tree | f6fdd8e6c07b7f7bbf9d2b6d95d2f4b41511e402 /cache/filecache | |
parent | d3489eba5dfc0ecdc032016d9db0746213dd5f0e (diff) | |
download | hugo-33502667fbacf57167ede66df8f13e308a4a9aec.tar.gz hugo-33502667fbacf57167ede66df8f13e308a4a9aec.zip |
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.
Diffstat (limited to 'cache/filecache')
-rw-r--r-- | cache/filecache/filecache.go | 19 | ||||
-rw-r--r-- | cache/filecache/filecache_config.go | 4 | ||||
-rw-r--r-- | cache/filecache/filecache_config_test.go | 2 | ||||
-rw-r--r-- | cache/filecache/filecache_test.go | 15 |
4 files changed, 37 insertions, 3 deletions
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" |