Fix layout detection

This commit is contained in:
Alexander Neumann 2017-04-02 19:18:03 +02:00
parent 50dfa64a54
commit f7c4b3a922
2 changed files with 24 additions and 10 deletions

View file

@ -6,6 +6,7 @@ import (
"path/filepath"
"regexp"
"restic"
"restic/debug"
"restic/errors"
"restic/fs"
)
@ -140,28 +141,36 @@ func DetectLayout(repo Filesystem, dir string) (Layout, error) {
}
if foundKeysFile && foundDataFile && !foundKeyFile && !foundDataSubdirFile {
return &CloudLayout{}, nil
debug.Log("found cloud layout at %v", dir)
return &CloudLayout{
Path: dir,
Join: repo.Join,
}, nil
}
if foundKeysFile && foundDataSubdirFile && !foundKeyFile && !foundDataFile {
return &DefaultLayout{}, nil
debug.Log("found default layout at %v", dir)
return &DefaultLayout{
Path: dir,
Join: repo.Join,
}, nil
}
if foundKeyFile && foundDataFile && !foundKeysFile && !foundDataSubdirFile {
return &S3Layout{}, nil
debug.Log("found s3 layout at %v", dir)
return &S3Layout{
Path: dir,
Join: repo.Join,
}, nil
}
return nil, errors.New("auto-detecting the filesystem layout failed")
}
// ParseLayout parses the config string and returns a Layout. When layout is
// the empty string, DetectLayout is used. If repo is nil, an instance of LocalFilesystem
// is used.
// the empty string, DetectLayout is used.
func ParseLayout(repo Filesystem, layout, path string) (l Layout, err error) {
if repo == nil {
repo = &LocalFilesystem{}
}
debug.Log("parse layout string %q for backend at %v", layout, path)
switch layout {
case "default":
l = &DefaultLayout{

View file

@ -271,7 +271,7 @@ func TestParseLayout(t *testing.T) {
for _, test := range tests {
t.Run(test.layoutName, func(t *testing.T) {
layout, err := ParseLayout(nil, test.layoutName, filepath.Join(path, "repo"))
layout, err := ParseLayout(&LocalFilesystem{}, test.layoutName, filepath.Join(path, "repo"))
if err != nil {
t.Fatal(err)
}
@ -280,6 +280,11 @@ func TestParseLayout(t *testing.T) {
t.Fatal("wanted some layout, but detect returned nil")
}
// test that the functions work (and don't panic)
_ = layout.Dirname(restic.Handle{Type: restic.DataFile})
_ = layout.Filename(restic.Handle{Type: restic.DataFile, Name: "1234"})
_ = layout.Paths()
layoutName := fmt.Sprintf("%T", layout)
if layoutName != test.want {
t.Fatalf("want layout %v, got %v", test.want, layoutName)