From 29d428040cccbead2d40a47da47aece8cf55a94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=B6ller?= Date: Tue, 30 Jan 2018 14:35:40 +0100 Subject: [PATCH] cache: clean root path (#2023) Trim "/" from the root path to fix "slice bounds out of range" panic in cache.go:1272. Fixes #1945 --- backend/cache/cache.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/backend/cache/cache.go b/backend/cache/cache.go index 6a08dba5f..ee8b04bba 100644 --- a/backend/cache/cache.go +++ b/backend/cache/cache.go @@ -175,13 +175,23 @@ type Fs struct { cleanupChan chan bool } +// parseRootPath returns a cleaned root path and a nil error or "" and an error when the path is invalid +func parseRootPath(path string) (string, error) { + return strings.Trim(path, "/"), nil +} + // NewFs constructs a Fs from the path, container:path -func NewFs(name, rpath string) (fs.Fs, error) { +func NewFs(name, rootPath string) (fs.Fs, error) { remote := config.FileGet(name, "remote") if strings.HasPrefix(remote, name+":") { return nil, errors.New("can't point cache remote at itself - check the value of the remote setting") } - rpath = strings.Trim(rpath, "/") + + rpath, err := parseRootPath(rootPath) + if err != nil { + return nil, errors.Wrapf(err, "failed to clean root path %q", rootPath) + } + remotePath := path.Join(remote, rpath) wrappedFs, wrapErr := fs.NewFs(remotePath) if wrapErr != nil && wrapErr != fs.ErrorIsFile { @@ -200,7 +210,7 @@ func NewFs(name, rpath string) (fs.Fs, error) { if *cacheChunkSize != DefCacheChunkSize { chunkSizeString = *cacheChunkSize } - err := chunkSize.Set(chunkSizeString) + err = chunkSize.Set(chunkSizeString) if err != nil { return nil, errors.Wrapf(err, "failed to understand chunk size %v", chunkSizeString) }