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 <denis@dAir.lab>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-12-03 10:38:33 -08:00
1 parent 4cef7c1f41
commit 333c32d610
2 files changed
+29 -1

No files matched your search

+5 -1
View File
@@ -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)
+24
View File
@@ -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)
}
}
}