diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-11-14 17:18:32 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-11-14 23:14:51 +0100 |
commit | 94f0f7e59788e802e706a55cac0d52a9e70ff745 (patch) | |
tree | d8e613f37940aeadd18703f35b9bf912c360be72 /cache | |
parent | 3c29c5af8ee865ef20741f576088e031e940c3d2 (diff) | |
download | hugo-94f0f7e59788e802e706a55cac0d52a9e70ff745.tar.gz hugo-94f0f7e59788e802e706a55cac0d52a9e70ff745.zip |
cache/filecache: Add a :project placeholder
This allows for "cache per Hugo project", making `hugo --gc` work as expected, even if you have several Hugo projects running on the same PC.
See #5439
Diffstat (limited to 'cache')
-rw-r--r-- | cache/filecache/filecache.go | 5 | ||||
-rw-r--r-- | cache/filecache/filecache_config.go | 37 | ||||
-rw-r--r-- | cache/filecache/filecache_config_test.go | 32 |
3 files changed, 59 insertions, 15 deletions
diff --git a/cache/filecache/filecache.go b/cache/filecache/filecache.go index e9f72cb92..9f50ad6b6 100644 --- a/cache/filecache/filecache.go +++ b/cache/filecache/filecache.go @@ -272,7 +272,10 @@ func (c *Cache) getOrRemove(id string) hugio.ReadSeekCloser { } func (c *Cache) isExpired(modTime time.Time) bool { - return c.maxAge >= 0 && time.Now().Sub(modTime) > c.maxAge + if c.maxAge < 0 { + return false + } + return c.maxAge == 0 || time.Now().Sub(modTime) > c.maxAge } // For testing diff --git a/cache/filecache/filecache_config.go b/cache/filecache/filecache_config.go index f0dd7295a..bb2cc36e7 100644 --- a/cache/filecache/filecache_config.go +++ b/cache/filecache/filecache_config.go @@ -35,7 +35,7 @@ const ( var defaultCacheConfig = cacheConfig{ MaxAge: -1, // Never expire - Dir: ":cacheDir", + Dir: ":cacheDir/:project", } const ( @@ -139,26 +139,33 @@ func decodeConfig(p *paths.Paths) (cachesConfig, error) { disabled := cfg.GetBool("ignoreCache") for k, v := range c { - v.Dir = filepath.Clean(v.Dir) - dir := filepath.ToSlash(v.Dir) + dir := filepath.ToSlash(filepath.Clean(v.Dir)) + hadSlash := strings.HasPrefix(dir, "/") parts := strings.Split(dir, "/") - first := parts[0] - if strings.HasPrefix(first, ":") { - resolved, err := resolveDirPlaceholder(p, first) - if err != nil { - return c, err + for i, part := range parts { + if strings.HasPrefix(part, ":") { + resolved, err := resolveDirPlaceholder(p, part) + if err != nil { + return c, err + } + parts[i] = resolved } - resolved = filepath.ToSlash(resolved) + } - v.Dir = filepath.FromSlash(path.Join((append([]string{resolved}, parts[1:]...))...)) + dir = path.Join(parts...) + if hadSlash { + dir = "/" + dir + } + v.Dir = filepath.Clean(filepath.FromSlash(dir)) - } else if isOsFs && !path.IsAbs(dir) { - return c, errors.Errorf("%q must either start with a placeholder (e.g. :cacheDir, :resourceDir) or be absolute", v.Dir) + if isOsFs && !filepath.IsAbs(v.Dir) { + return c, errors.Errorf("%q must resolve to an absolute directory", v.Dir) } - if len(v.Dir) < 5 { - return c, errors.Errorf("%q is not a valid cache dir", v.Dir) + // Avoid cache in root, e.g. / (Unix) or c:\ (Windows) + if len(strings.TrimPrefix(v.Dir, filepath.VolumeName(v.Dir))) == 1 { + return c, errors.Errorf("%q is a root folder and not allowed as cache dir", v.Dir) } if disabled { @@ -178,6 +185,8 @@ func resolveDirPlaceholder(p *paths.Paths, placeholder string) (string, error) { return p.AbsResourcesDir, nil case ":cachedir": return helpers.GetCacheDir(p.Fs.Source, p.Cfg) + case ":project": + return filepath.Base(p.WorkingDir), nil } return "", errors.Errorf("%q is not a valid placeholder (valid values are :cacheDir or :resourceDir)", placeholder) diff --git a/cache/filecache/filecache_config_test.go b/cache/filecache/filecache_config_test.go index abba6c25f..51126f080 100644 --- a/cache/filecache/filecache_config_test.go +++ b/cache/filecache/filecache_config_test.go @@ -16,6 +16,7 @@ package filecache import ( "path/filepath" "runtime" + "strings" "testing" "time" @@ -107,6 +108,8 @@ dir = "/path/to/c3" func TestDecodeConfigDefault(t *testing.T) { assert := require.New(t) cfg := viper.New() + cfg.Set("workingDir", filepath.FromSlash("/my/cool/hugoproject")) + if runtime.GOOS == "windows" { cfg.Set("resourceDir", "c:\\cache\\resources") cfg.Set("cacheDir", "c:\\cache\\thecache") @@ -130,5 +133,34 @@ func TestDecodeConfigDefault(t *testing.T) { assert.Equal("c:\\cache\\resources\\_gen", decoded[cacheKeyImages].Dir) } else { assert.Equal("/cache/resources/_gen", decoded[cacheKeyImages].Dir) + assert.Equal("/cache/thecache/hugoproject", decoded[cacheKeyGetJSON].Dir) + } +} + +func TestDecodeConfigInvalidDir(t *testing.T) { + t.Parallel() + + assert := require.New(t) + + configStr := ` +resourceDir = "myresources" +[caches] +[caches.getJSON] +maxAge = "10m" +dir = "/" + +` + if runtime.GOOS == "windows" { + configStr = strings.Replace(configStr, "/", "c:\\\\", 1) } + + cfg, err := config.FromConfigString(configStr, "toml") + assert.NoError(err) + fs := hugofs.NewMem(cfg) + p, err := paths.New(fs, cfg) + assert.NoError(err) + + _, err = decodeConfig(p) + assert.Error(err) + } |