diff --git a/repo/blob/sftp/sftp_storage.go b/repo/blob/sftp/sftp_storage.go index 73f0d4729..200f8f8f5 100644 --- a/repo/blob/sftp/sftp_storage.go +++ b/repo/blob/sftp/sftp_storage.go @@ -86,12 +86,10 @@ func (s *sftpImpl) PutBlobInPath(ctx context.Context, dirPath, path string, data tempFile := fmt.Sprintf("%s.tmp.%x", path, randSuffix) f, err := s.createTempFileAndDir(tempFile) if err != nil { - fmt.Printf("putblob-createtempfileanddir(%s)\n", err) return errors.Wrap(err, "cannot create temporary file") } if _, err = f.Write(data); err != nil { - fmt.Printf("putblob-write(%s)\n", err) return errors.Wrap(err, "can't write temporary file") } @@ -99,7 +97,7 @@ func (s *sftpImpl) PutBlobInPath(ctx context.Context, dirPath, path string, data return errors.Wrap(err, "can't close temporary file") } - err = s.cli.Rename(tempFile, path) + err = s.cli.PosixRename(tempFile, path) if err != nil { if removeErr := s.cli.Remove(tempFile); removeErr != nil { fmt.Printf("warning: can't remove temp file: %v", removeErr) @@ -259,7 +257,13 @@ func New(ctx context.Context, opts *Options) (blob.Storage, error) { } if _, err = c.Stat(opts.Path); err != nil { - return nil, errors.Wrapf(err, "path doesn't exist: %s", opts.Path) + if os.IsNotExist(err) { + if err = c.MkdirAll(opts.Path); err != nil { + return nil, errors.Wrap(err, "cannot create path") + } + } else { + return nil, errors.Wrapf(err, "path doesn't exist: %s", opts.Path) + } } r := &sftpStorage{ diff --git a/repo/blob/sftp/sftp_storage_test.go b/repo/blob/sftp/sftp_storage_test.go index 6c2e230f6..ef92f29e4 100644 --- a/repo/blob/sftp/sftp_storage_test.go +++ b/repo/blob/sftp/sftp_storage_test.go @@ -20,8 +20,7 @@ func TestSFTPStorageValid(t *testing.T) { ctx := context.Background() - additionalPath := "" - st, err := createSFTPStorage(ctx, additionalPath, t) + st, err := createSFTPStorage(ctx, t) if err != nil { t.Fatalf("unable to connect to SSH: %v", err) @@ -46,22 +45,6 @@ func TestSFTPStorageValid(t *testing.T) { } } -func TestSFTPStorageInvalid(t *testing.T) { - ctx := context.Background() - additionalPath := "-no-such-path" - st, err := createSFTPStorage(ctx, additionalPath, t) - - if err != nil { - t.Fatalf("unable to connect to SSH: %v", err) - } - - defer st.Close(ctx) - - if err := st.PutBlob(ctx, t1, []byte{1}); err == nil { - t.Errorf("unexpected success when adding to non-existent path") - } -} - func assertNoError(t *testing.T, err error) { t.Helper() if err != nil { @@ -77,7 +60,7 @@ func deleteBlobs(ctx context.Context, t *testing.T, st blob.Storage) { } } -func createSFTPStorage(ctx context.Context, additionalPath string, t *testing.T) (blob.Storage, error) { +func createSFTPStorage(ctx context.Context, t *testing.T) (blob.Storage, error) { host := os.Getenv("KOPIA_SFTP_TEST_HOST") if host == "" { t.Skip("KOPIA_SFTP_TEST_HOST not provided") @@ -113,7 +96,7 @@ func createSFTPStorage(ctx context.Context, additionalPath string, t *testing.T) } return sftp.New(ctx, &sftp.Options{ - Path: path + additionalPath, + Path: path, Host: host, Username: usr, Port: int(port),