SFTP implementation of vfs.FS on a conn.Lease: passes the conformance suite on the in-process stand (and on a real sshd under the integration tag), including RemoveTree on a symlink to a directory (N-P0-3). Volume is the host key fingerprint (N-P0-5); metadata calls return on ctx cancel, a watchdog probes hung calls; io.Copy uses concurrent reads/writes (N-P1-6). Connection errors map to vfs.ErrConnLost. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.1 KiB
Go
69 lines
2.1 KiB
Go
package sftpfs
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
"vcom/internal/ssh/conn"
|
|
"vcom/internal/vfs"
|
|
)
|
|
|
|
// sshFxOPUnsupported is SSH_FX_OP_UNSUPPORTED.
|
|
const sshFxOPUnsupported = 8
|
|
|
|
// mapErr turns an SFTP/connection error into vfs sentinels: a dead or closed
|
|
// connection becomes vfs.ErrConnLost (still matching conn.ErrConnLost /
|
|
// conn.ErrClosed), SSH_FX_OP_UNSUPPORTED becomes vfs.ErrUnsupported.
|
|
// ENOENT/EACCES already arrive as fs.ErrNotExist/fs.ErrPermission from
|
|
// pkg/sftp. Context errors and io.EOF pass through untouched.
|
|
func (f *FS) mapErr(err error) error {
|
|
if err == nil || err == io.EOF || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
return err
|
|
}
|
|
err = f.c.MapErr(err)
|
|
if errors.Is(err, conn.ErrConnLost) || errors.Is(err, conn.ErrClosed) {
|
|
if errors.Is(err, vfs.ErrConnLost) {
|
|
return err
|
|
}
|
|
return fmt.Errorf("%w: %w", vfs.ErrConnLost, err)
|
|
}
|
|
var se *sftp.StatusError
|
|
if errors.As(err, &se) && se.Code == sshFxOPUnsupported {
|
|
return fmt.Errorf("%w: %w", vfs.ErrUnsupported, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// wrap maps err and attaches op and path (unless the error already is a
|
|
// *fs.PathError, as pkg/sftp returns for some calls).
|
|
func (f *FS) wrap(op, p string, err error) error {
|
|
err = f.mapErr(err)
|
|
if err == nil || err == io.EOF || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
return err
|
|
}
|
|
var pe *fs.PathError
|
|
if errors.As(err, &pe) && !errors.Is(err, vfs.ErrConnLost) {
|
|
return err
|
|
}
|
|
return &fs.PathError{Op: op, Path: p, Err: err}
|
|
}
|
|
|
|
// existErr refines a failed create/mkdir/rename: SFTP v3 has no
|
|
// "already exists" status (OpenSSH and pkg/sftp send SSH_FX_FAILURE), so if
|
|
// the target now exists the error becomes vfs.ErrExist.
|
|
func (f *FS) existErr(ctx context.Context, op, p string, err error) error {
|
|
werr := f.wrap(op, p, err)
|
|
if werr == nil || errors.Is(werr, vfs.ErrNotExist) || errors.Is(werr, vfs.ErrPermission) ||
|
|
errors.Is(werr, vfs.ErrConnLost) || ctx.Err() != nil {
|
|
return werr
|
|
}
|
|
if _, lerr := f.lstatRaw(ctx, p); lerr == nil {
|
|
return &fs.PathError{Op: op, Path: p, Err: vfs.ErrExist}
|
|
}
|
|
return werr
|
|
}
|