From b211f834fa393382cbd5f47bb01cba6eab341fd3 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Wed, 26 Aug 2015 21:51:40 +0200 Subject: [PATCH 1/2] Remove tests for directories For testing whether a repository already exists it is sufficient to test if the config file (and therefore the master key) exists. Closes #279 --- backend/local/local.go | 7 ------- backend/sftp/sftp.go | 7 ------- 2 files changed, 14 deletions(-) diff --git a/backend/local/local.go b/backend/local/local.go index 8ba369ca3..1e7ffedf2 100644 --- a/backend/local/local.go +++ b/backend/local/local.go @@ -62,13 +62,6 @@ func Create(dir string) (*Local, error) { return nil, errors.New("config file already exists") } - // test if directories already exist - for _, d := range dirs[1:] { - if _, err := os.Stat(d); err == nil { - return nil, fmt.Errorf("dir %s already exists", d) - } - } - // create paths for data, refs and temp for _, d := range dirs { err := os.MkdirAll(d, backend.Modes.Dir) diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index 90cb687a9..23db7011e 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -115,13 +115,6 @@ func Create(dir string, program string, args ...string) (*SFTP, error) { return nil, errors.New("config file already exists") } - // test if directories already exist - for _, d := range dirs[1:] { - if _, err := sftp.c.Lstat(d); err == nil { - return nil, fmt.Errorf("dir %s already exists", d) - } - } - // create paths for data, refs and temp blobs for _, d := range dirs { err = sftp.mkdirAll(d, backend.Modes.Dir) From 0a457eafed822f3a3f9b0c65d45ccc82c58eb61b Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Wed, 26 Aug 2015 22:06:52 +0200 Subject: [PATCH 2/2] Correctly test for config file --- backend/local/local.go | 2 +- backend/local_test.go | 11 +++++++---- backend/sftp/sftp.go | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/local/local.go b/backend/local/local.go index 1e7ffedf2..487397353 100644 --- a/backend/local/local.go +++ b/backend/local/local.go @@ -57,7 +57,7 @@ func Create(dir string) (*Local, error) { } // test if config file already exists - _, err := os.Lstat(backend.Paths.Config) + _, err := os.Lstat(filepath.Join(dir, backend.Paths.Config)) if err == nil { return nil, errors.New("config file already exists") } diff --git a/backend/local_test.go b/backend/local_test.go index 24c7091bd..462c4c3d6 100644 --- a/backend/local_test.go +++ b/backend/local_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "testing" + "github.com/restic/restic/backend" "github.com/restic/restic/backend/local" . "github.com/restic/restic/test" ) @@ -46,11 +47,13 @@ func TestLocalBackendCreationFailures(t *testing.T) { b := setupLocalBackend(t) defer teardownLocalBackend(t, b) + // create a fake config file + blob, err := b.Create() + OK(t, err) + fmt.Fprintf(blob, "config\n") + OK(t, blob.Finalize(backend.Config, "")) + // test failure to create a new repository at the same location b2, err := local.Create(b.Location()) Assert(t, err != nil && b2 == nil, fmt.Sprintf("creating a repository at %s for the second time should have failed", b.Location())) - - // test failure to create a new repository at the same location without a config file - b2, err = local.Create(b.Location()) - Assert(t, err != nil && b2 == nil, fmt.Sprintf("creating a repository at %s for the second time should have failed", b.Location())) } diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index 23db7011e..13bd54340 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -110,7 +110,7 @@ func Create(dir string, program string, args ...string) (*SFTP, error) { } // test if config file already exists - _, err = sftp.c.Lstat(backend.Paths.Config) + _, err = sftp.c.Lstat(filepath.Join(dir, backend.Paths.Config)) if err == nil { return nil, errors.New("config file already exists") }