From c6d74458eeabb3076e0d83eda77809e5471c9975 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 10 May 2024 23:57:52 +0200 Subject: [PATCH] sftp: improve handling of too short files --- internal/backend/sftp/sftp.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/backend/sftp/sftp.go b/internal/backend/sftp/sftp.go index efd66f76c..dd95b3cf8 100644 --- a/internal/backend/sftp/sftp.go +++ b/internal/backend/sftp/sftp.go @@ -43,6 +43,8 @@ type SFTP struct { var _ backend.Backend = &SFTP{} +var errTooShort = fmt.Errorf("file is too short") + func NewFactory() location.Factory { return location.NewLimitedBackendFactory("sftp", ParseConfig, location.NoPassword, limiter.WrapBackendConstructor(Create), limiter.WrapBackendConstructor(Open)) } @@ -212,6 +214,10 @@ func (r *SFTP) IsNotExist(err error) bool { return errors.Is(err, os.ErrNotExist) } +func (r *SFTP) IsPermanentError(err error) bool { + return r.IsNotExist(err) || errors.Is(err, errTooShort) || errors.Is(err, os.ErrPermission) +} + func buildSSHCommand(cfg Config) (cmd string, args []string, err error) { if cfg.Command != "" { args, err := backend.SplitShellStrings(cfg.Command) @@ -428,6 +434,18 @@ func (r *SFTP) openReader(_ context.Context, h backend.Handle, length int, offse return nil, err } + fi, err := f.Stat() + if err != nil { + _ = f.Close() + return nil, err + } + + size := fi.Size() + if size < offset+int64(length) { + _ = f.Close() + return nil, errTooShort + } + if offset > 0 { _, err = f.Seek(offset, 0) if err != nil {