mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
sftp: keep an existing file's mode when an upload overwrites it
An overwrite went through the same PUT as a new file, so a 0600 file came back with the default mode. Send the stored mode instead, as open(2) on an existing file keeps it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a4fae5a244
commit
ce7a80ca9f
3 files changed
+24
-10
No files matched your search
@@ -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)
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user