diff --git a/weed/sftpd/sftp_file_writer.go b/weed/sftpd/sftp_file_writer.go index 3c99b5fc3a..13ac526b7b 100644 --- a/weed/sftpd/sftp_file_writer.go +++ b/weed/sftpd/sftp_file_writer.go @@ -88,10 +88,17 @@ func (w *SeaweedSftpFileWriter) Close() error { if err := w.fs.requireParentDir(w.absPath); err != nil { return err } - if err := w.fs.checkWritable(w.absPath); err != nil { + existing, err := w.fs.checkWritable(w.absPath) + if err != nil { return err } + // Like open(2) on an existing file, an overwrite keeps the file's mode; + // a zero mode is an entry written without one. + mode := w.permissions + if perm := os.FileMode(existing.GetAttributes().GetFileMode()).Perm(); perm != 0 { + mode = perm + } // Stream the file to the absolute path (after HomeDir translation) - return w.fs.putFile(w.absPath, w.tmpFile, w.fs.user, w.permissions) + return w.fs.putFile(w.absPath, w.tmpFile, w.fs.user, mode) } diff --git a/weed/sftpd/sftp_filer.go b/weed/sftpd/sftp_filer.go index daca4811f9..f4e5fc3735 100644 --- a/weed/sftpd/sftp_filer.go +++ b/weed/sftpd/sftp_filer.go @@ -162,7 +162,7 @@ func (fs *SftpServer) newFileWriter(r *sftp.Request) (io.WriterAt, error) { if err := fs.requireParentDir(absPath); err != nil { return nil, err } - if err := fs.checkWritable(absPath); err != nil { + if _, err := fs.checkWritable(absPath); err != nil { return nil, err } // Create a temporary file to buffer writes @@ -192,19 +192,20 @@ func (fs *SftpServer) newFileWriter(r *sftp.Request) (io.WriterAt, error) { } // checkWritable refuses an upload onto a directory, which the filer would -// redirect to a child, or onto a protected entry. -func (fs *SftpServer) checkWritable(absPath string) error { +// redirect to a child, or onto a protected entry. It returns the existing +// file, or nil when there is none. +func (fs *SftpServer) checkWritable(absPath string) (*filer_pb.Entry, error) { entry, err := fs.getEntry(absPath) if errors.Is(err, os.ErrNotExist) { - return nil + return nil, nil } if err != nil { - return err + return nil, err } if entry.IsDirectory { - return fmt.Errorf("%s is a directory", absPath) + return nil, fmt.Errorf("%s is a directory", absPath) } - return fs.checkEntryProtected(absPath, entry) + return entry, fs.checkEntryProtected(absPath, entry) } func (fs *SftpServer) copyExistingContent(absPath string, dst io.Writer) error { diff --git a/weed/sftpd/sftp_filer_test.go b/weed/sftpd/sftp_filer_test.go index 4ddc2c18b3..7b2169127d 100644 --- a/weed/sftpd/sftp_filer_test.go +++ b/weed/sftpd/sftp_filer_test.go @@ -276,7 +276,10 @@ func TestListAllPagesMissingDirectory(t *testing.T) { func TestUploadFileMode(t *testing.T) { fs, _ := newLockTestServer(t, map[string]*filer_pb.Entry{ - "/buckets/plain": {Name: "plain", IsDirectory: true}, + "/buckets/plain": {Name: "plain", IsDirectory: true}, + "/buckets/plain/private": {Name: "private", Attributes: &filer_pb.FuseAttributes{FileMode: 0600}}, + "/buckets/plain/shared": {Name: "shared", Attributes: &filer_pb.FuseAttributes{FileMode: 0664}}, + "/buckets/plain/unset": {Name: "unset", Attributes: &filer_pb.FuseAttributes{}}, }) var gotMode string ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -293,6 +296,9 @@ func TestUploadFileMode(t *testing.T) { want string }{ {"default", sftp.NewRequest("Put", "/plain/new"), "644"}, + {"overwrite keeps private mode", sftp.NewRequest("Put", "/plain/private"), "600"}, + {"overwrite keeps shared mode", sftp.NewRequest("Put", "/plain/shared"), "664"}, + {"overwrite of a file without a mode", sftp.NewRequest("Put", "/plain/unset"), "644"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {