From 61b5a1d9a3be9177f465f8718c52452a2ca8c2de Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 4 Aug 2019 11:02:38 +0200 Subject: [PATCH] simplify FileOpenFlags to os.Flags conversion --- sftpd/handler.go | 13 ++++++------- sftpd/internal_test.go | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/sftpd/handler.go b/sftpd/handler.go index fc6f510c..433fa929 100644 --- a/sftpd/handler.go +++ b/sftpd/handler.go @@ -385,11 +385,12 @@ func (c Connection) handleSFTPUploadToExistingFile(pflags sftp.FileOpenFlags, re return nil, sftp.ErrSshFxFailure } - osFlags, trunc := getOSOpenFlags(pflags) + osFlags := getOSOpenFlags(pflags) - if !trunc { + if osFlags&os.O_TRUNC == 0 { // see https://github.com/pkg/sftp/issues/295 - logger.Info(logSender, "upload resume is not supported, returning error") + logger.Info(logSender, "upload resume is not supported, returning error for file: %v user: %v", requestPath, + c.User.Username) return nil, sftp.ErrSshFxOpUnsupported } @@ -565,9 +566,8 @@ func (c Connection) createMissingDirs(filePath string) error { return nil } -func getOSOpenFlags(requestFlags sftp.FileOpenFlags) (flags int, trunc bool) { +func getOSOpenFlags(requestFlags sftp.FileOpenFlags) (flags int) { var osFlags int - truncateFile := false if requestFlags.Read && requestFlags.Write { osFlags |= os.O_RDWR } else if requestFlags.Write { @@ -581,12 +581,11 @@ func getOSOpenFlags(requestFlags sftp.FileOpenFlags) (flags int, trunc bool) { } if requestFlags.Trunc { osFlags |= os.O_TRUNC - truncateFile = true } if requestFlags.Excl { osFlags |= os.O_EXCL } - return osFlags, truncateFile + return osFlags } func getUploadTempFilePath(path string) string { diff --git a/sftpd/internal_test.go b/sftpd/internal_test.go index 2f656337..93540ad3 100644 --- a/sftpd/internal_test.go +++ b/sftpd/internal_test.go @@ -56,7 +56,7 @@ func TestGetOSOpenFlags(t *testing.T) { flags.Write = true flags.Append = true flags.Excl = true - osFlags, _ := getOSOpenFlags(flags) + osFlags := getOSOpenFlags(flags) if osFlags&os.O_WRONLY == 0 || osFlags&os.O_APPEND == 0 || osFlags&os.O_EXCL == 0 { t.Errorf("error getting os flags from sftp file open flags") }