From 333c32d610105a8eba00c2a6ec44aba24ac86e54 Mon Sep 17 00:00:00 2001 From: "D.Maier" <73213965+denMaier@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:38:33 +0100 Subject: [PATCH] sftp: keep a folder a folder when its mode is changed (#13341) chmod on a folder over SFTP failed with "existing entry is a directory". SETSTAT copied the client's mode into the entry as-is. A client sends only the permission bits, so the directory bit was dropped and the filer refused an update that would turn the folder into a file. Files lost their stored type bits the same way, just without an error. Keep the stored type bits and take only the permission, setuid, setgid and sticky bits from the client. Co-authored-by: Denis Maier Co-authored-by: Claude Opus 5.5 --- weed/sftpd/sftp_filer.go | 6 +++++- weed/sftpd/sftp_object_lock_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) 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) + } + } +}