From f7c4b3a9221af15668c7ed36dfeeb39a775ec428 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 2 Apr 2017 19:18:03 +0200 Subject: [PATCH] Fix layout detection --- src/restic/backend/layout.go | 27 ++++++++++++++++++--------- src/restic/backend/layout_test.go | 7 ++++++- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/restic/backend/layout.go b/src/restic/backend/layout.go index cff77e25a..a6087c502 100644 --- a/src/restic/backend/layout.go +++ b/src/restic/backend/layout.go @@ -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{ diff --git a/src/restic/backend/layout_test.go b/src/restic/backend/layout_test.go index 3fe9dcf6e..e336f8178 100644 --- a/src/restic/backend/layout_test.go +++ b/src/restic/backend/layout_test.go @@ -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)