diff --git a/weed/sftpd/sftp_filer.go b/weed/sftpd/sftp_filer.go index 7151e81baf..edc951be6c 100644 --- a/weed/sftpd/sftp_filer.go +++ b/weed/sftpd/sftp_filer.go @@ -507,7 +507,11 @@ func (fs *SftpServer) setFileStat(absPath string, flags sftp.FileAttrFlags, attr func applyFileStat(entry *filer_pb.Entry, flags sftp.FileAttrFlags, attrs *sftp.FileStat) { if flags.Permissions { - entry.Attributes.FileMode = uint32(attrs.FileMode()) + // chmod sends only permission bits: keep the stored type bits, or a + // folder would turn into a file and the filer refuses the update. + const changeable = os.ModePerm | os.ModeSetuid | os.ModeSetgid | os.ModeSticky + stored := os.FileMode(entry.Attributes.FileMode) + entry.Attributes.FileMode = uint32(stored&^changeable | attrs.FileMode()&changeable) } if flags.UidGid { entry.Attributes.Uid = uint32(attrs.UID) diff --git a/weed/sftpd/sftp_object_lock_test.go b/weed/sftpd/sftp_object_lock_test.go index 156b927a6c..5f7802cd2b 100644 --- a/weed/sftpd/sftp_object_lock_test.go +++ b/weed/sftpd/sftp_object_lock_test.go @@ -452,3 +452,27 @@ func TestPutFileAppliesBucketDefaultRetention(t *testing.T) { } } } + +// chmod sends only permission bits: the stored type must stay, or the filer +// refuses to turn a folder into a file. +func TestChmodKeepsEntryType(t *testing.T) { + fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{ + "/buckets/d": {Name: "d", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{FileMode: uint32(os.ModeDir | 0o700)}}, + "/buckets/f": {Name: "f", Attributes: &filer_pb.FuseAttributes{FileMode: 0o644}}, + }) + for _, c := range []struct { + path string + sent uint32 + want os.FileMode + }{ + {"/buckets/d", 0o1750, os.ModeDir | os.ModeSticky | 0o750}, + {"/buckets/f", 0o600, 0o600}, + } { + if err := fs.setFileStat(c.path, sftp.FileAttrFlags{Permissions: true}, &sftp.FileStat{Mode: c.sent}); err != nil { + t.Fatalf("chmod %s: %v", c.path, err) + } + if got := os.FileMode(fake.lastUpdate.GetEntry().GetAttributes().GetFileMode()); got != c.want { + t.Errorf("chmod %s stored mode %v, want %v", c.path, got, c.want) + } + } +}