package sftpd import ( "context" "fmt" "io" "math" "github.com/seaweedfs/seaweedfs/weed/filer" "github.com/seaweedfs/seaweedfs/weed/pb" filer_pb "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/security" "github.com/seaweedfs/seaweedfs/weed/util" "github.com/seaweedfs/seaweedfs/weed/util/chunk_cache" "github.com/seaweedfs/seaweedfs/weed/wdclient" ) // downloads is what all downloads share, as in the FUSE mount: volume // lookups that can be invalidated, and one memory budget (the mount's // default -readerCacheSizeMB) for every open file's read buffers. type downloads struct { filerClient *wdclient.FilerClient budget *filer.ReaderCacheBudget } func newDownloads(options *SFTPServiceOptions) *downloads { fc := wdclient.NewFilerClient([]pb.ServerAddress{options.Filer}, options.GrpcDialOption, options.DataCenter, &wdclient.FilerClientOption{ FilerTokenSource: security.FilerAdminToken, }) return &downloads{fc, filer.NewReaderCacheBudget(256 << 20)} } // newFileReaderAt reads FileSize bytes of the file, with zeros where no // chunk or inline content stores data. func (fs *SftpServer) newFileReaderAt(ctx context.Context, absPath string, entry *filer_pb.Entry) (io.ReaderAt, error) { if len(entry.Content) == 0 && entry.IsInRemoteOnly() { var err error if entry, err = fs.downloadRemoteEntry(ctx, absPath); err != nil { return nil, err } } size := int64(entry.GetAttributes().GetFileSize()) if len(entry.GetChunks()) == 0 { return inlineReaderAt{entry.Content, size}, nil } if fs.downloads == nil { return nil, fmt.Errorf("read %s: downloads are not set up", absPath) } // Like a mount's file handle: 64 MiB sections keep each read's chunk // lookup short however large the file is. dl := fs.downloads lookup := dl.filerClient.GetLookupFileIdFunction() // Resolve manifests here, under the request context and recursively. chunks, _, err := filer.ResolveChunkManifest(ctx, lookup, entry.GetChunks(), 0, math.MaxInt64, dl.filerClient, true) if err != nil { return nil, fmt.Errorf("read %s: %w", absPath, err) } group, err := filer.NewChunkGroup(lookup, (*chunk_cache.TieredChunkCache)(nil), chunks, 0, dl.filerClient, nil, dl.budget) if err != nil { return nil, fmt.Errorf("read %s: %w", absPath, err) } return chunkReaderAt{ctx, group, size}, nil } // downloadRemoteEntry has the filer copy content only a remote store holds // into the cluster, as a mount's read does, and returns the cached entry. func (fs *SftpServer) downloadRemoteEntry(ctx context.Context, absPath string) (*filer_pb.Entry, error) { dir, name := util.FullPath(absPath).DirAndName() var entry *filer_pb.Entry err := fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { resp, err := client.CacheRemoteObjectToLocalCluster(ctx, &filer_pb.CacheRemoteObjectToLocalClusterRequest{Directory: dir, Name: name}) entry = resp.GetEntry() return err }) if err != nil { return nil, fmt.Errorf("download %s from its remote store: %w", absPath, err) } if entry == nil || entry.IsInRemoteOnly() { return nil, fmt.Errorf("content of %s is not readable", absPath) } return entry, nil } type chunkReaderAt struct { ctx context.Context group *filer.ChunkGroup size int64 } func (r chunkReaderAt) ReadAt(p []byte, off int64) (int, error) { if off < 0 { return 0, fmt.Errorf("negative offset %d", off) } n, _, err := r.group.ReadDataAt(r.ctx, r.size, p, off) if err == nil && n < len(p) { if off+int64(n) < r.size { return n, io.ErrUnexpectedEOF } err = io.EOF } return n, err } func (r chunkReaderAt) Close() error { return r.group.Close() } // inlineReaderAt reads inline content up to size, then zeros. type inlineReaderAt struct { content []byte size int64 } func (r inlineReaderAt) ReadAt(p []byte, off int64) (int, error) { if off < 0 { return 0, fmt.Errorf("negative offset %d", off) } if off >= r.size { return 0, io.EOF } n := int(min(int64(len(p)), r.size-off)) clear(p[:n]) if off < int64(len(r.content)) { copy(p[:n], r.content[off:]) } if n < len(p) { return n, io.EOF } return n, nil }