Skip to content

Commit

Permalink
cache/filecache: Add a filecache root dir
Browse files Browse the repository at this point in the history
This is just a safe guard to make sure we don't evict/remove files that do not belong to the cache.
  • Loading branch information
bep committed Nov 14, 2018
1 parent d3489eb commit 3350266
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
19 changes: 18 additions & 1 deletion cache/filecache/filecache.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions cache/filecache/filecache_config.go
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions cache/filecache/filecache_config_test.go
Expand Up @@ -33,6 +33,7 @@ func TestDecodeConfig(t *testing.T) {
assert := require.New(t)

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

configStr := `
resourceDir = "myresources"
ignoreCache = true
[caches]
[caches.getJSON]
Expand Down
15 changes: 13 additions & 2 deletions cache/filecache/filecache_test.go
Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -149,6 +159,7 @@ func TestFileCacheConcurrent(t *testing.T) {
assert := require.New(t)

configStr := `
resourceDir = "myresources"
[caches]
[caches.getjson]
maxAge = "1s"
Expand Down
1 change: 1 addition & 0 deletions tpl/data/resources_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit 3350266

Please sign in to comment.