Files
seaweedfs/weed/sftpd/sftp_stdio.go
T
c46567084f sftp: read downloads as the file's logical content (#13355)
* sftp: read downloads as the file's logical content

Downloads read through filer.NewFileReader, which stops at the last stored
byte: holes between chunks, a tail no chunk stores and the zeros after
partial inline content came back missing, so sparse files downloaded short
or with later bytes shifted.

Read chunked files through filer.ChunkReadAt with one shared ReaderCache,
set up as WebDAV's: it serves FileSize bytes, zero-fills what no chunk
stores, resolves manifests, prefetches and re-looks-up moved volumes.
Closing a handle only releases its stream, as S3 does. Inline content is
read up to the file's size, then zeros. This replaces sftpd's own block
cache, whose LRU helper is now unused.

* sftp: read large downloads in sections, as the FUSE mount does

ChunkReadAt walks the file's chunk views from the start on every read, so
a sequential download of a file with many chunks costs time quadratic in
its chunk count. Read through filer.ChunkGroup instead, the reader of a
mount's file handle: it keeps chunks in 64 MiB sections, returns EOF at
the file size for any offset, and holds every open file's buffers to one
shared budget (the mount's default 256 MiB).

* sftp: cache remote-only content on open instead of refusing it

A file whose content only a remote store holds is copied into the cluster
with CacheRemoteObjectToLocalCluster when it is opened for reading, as a
mount's read and the filer's HTTP read do, and then read from the cached
chunks. It is still refused when the remote store cannot be read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Denis Maier <denis@dAir.lab>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 11:13:40 -08:00

82 lines
2.5 KiB
Go

package sftpd
import (
"errors"
"fmt"
"io"
"os"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/sftpd/user"
)
// StdioUser picks the account for an SFTP session on stdin/stdout, where
// sshd has already authenticated username. With a user store the user must
// be in it, and its home and permissions apply. Without one the session gets
// the whole filer, as the process's own filer access already allows.
//
// The session runs as the logged-in OS user, so that user must be able to
// read the store. Only usernames, homes and permissions are needed: sshd
// does the authentication, so this store should hold no passwords or keys,
// and can then be shared with the sshd users' group (for example 0640).
func StdioUser(userStoreFile, username string, uid, gid uint32) (*user.User, error) {
if userStoreFile == "" {
return &user.User{
Username: username,
HomeDir: "/",
Uid: uid,
Gid: gid,
Permissions: map[string][]string{"/": {PermAll}},
}, nil
}
f, err := os.Open(userStoreFile)
if err != nil {
if errors.Is(err, os.ErrPermission) {
return nil, fmt.Errorf("user store %s is not readable by %s; as an sshd Subsystem, use a store without passwords or keys that the SSH users can read", userStoreFile, username)
}
return nil, fmt.Errorf("user store: %w", err)
}
f.Close()
store, err := user.NewFileStore(userStoreFile)
if err != nil {
return nil, fmt.Errorf("user store: %w", err)
}
u, err := store.GetUser(username)
if err != nil {
return nil, fmt.Errorf("user %q is not in %s", username, userStoreFile)
}
return u, nil
}
// ServeStdio serves one SFTP session for u on rw, normally the stdin and
// stdout sshd gives a Subsystem command. Nothing else may write to stdout.
func ServeStdio(options *SFTPServiceOptions, u *user.User, rw io.ReadWriteCloser) error {
fs := NewSftpServer(
options.Filer,
options.GrpcDialOption,
options.DataCenter,
options.FilerGroup,
u,
options.FilerSigningKey,
options.FilerSigningExpiresAfter,
options.BucketsDir,
)
fs.downloads = newDownloads(options)
if u.HomeDir != "/" {
if err := fs.EnsureHomeDirectory(); err != nil {
glog.Errorf("Failed to ensure home directory for user %s: %v", u.Username, err)
}
}
return handleSFTP(rw, &fs)
}
// Stdio joins stdin and stdout into one stream
type Stdio struct{}
func (Stdio) Read(p []byte) (int, error) { return os.Stdin.Read(p) }
func (Stdio) Write(p []byte) (int, error) { return os.Stdout.Write(p) }
func (Stdio) Close() error {
_ = os.Stdin.Close()
return os.Stdout.Close()
}