vcom/internal/transfer/source.go

191 lines
4.6 KiB
Go
Raw Normal View History

package transfer
import (
"context"
"fmt"
"io"
"sort"
"strings"
"vcom/internal/vfs"
)
// Source yields the items of a transfer.
type Source interface {
Label() string
// Walk emits every item once, parents before children (plan phase).
Walk(ctx context.Context, fn func(Item) error) error
// Stream calls fn for items in source order; r is the content of File
// items and nil otherwise (execute phase).
Stream(ctx context.Context, items []Item, fn func(Item, io.Reader) error) error
}
// Opener is a Source with random access: Execute reopens an item for every
// attempt, so Retry after a read error works.
type Opener interface {
Open(ctx context.Context, it Item) (io.ReadCloser, error)
}
// FSBacked is a Source over a vfs.FS (FSSource): Prepare checks identity
// against the target and Move can rename and remove source items.
type FSBacked interface {
Source
FS() vfs.FS
Roots() []string
}
// FSSource reads the given paths (files, directories, links) from fsys.
func FSSource(fsys vfs.FS, paths []string, sym SymlinkPolicy) Source {
roots := make([]string, len(paths))
for i, p := range paths {
roots[i] = fsys.Paths().Clean(p)
}
return &fsSource{fs: fsys, roots: roots, sym: sym}
}
type fsSource struct {
fs vfs.FS
roots []string
sym SymlinkPolicy
}
func (s *fsSource) Label() string { return s.fs.Label() }
func (s *fsSource) FS() vfs.FS { return s.fs }
func (s *fsSource) Roots() []string { return s.roots }
// maxFollowDepth bounds FollowLinks recursion (design §4.3).
const maxFollowDepth = 40
func (s *fsSource) Walk(ctx context.Context, fn func(Item) error) error {
ops := s.fs.Paths()
for i, root := range s.roots {
base := ops.Base(root)
if base == "" || base == "." || base == ".." || base == string(ops.Sep()) {
return fmt.Errorf("transfer: cannot use %q as a source", root)
}
fi, err := s.fs.Lstat(ctx, root)
if err != nil {
return err
}
if err := s.walk(ctx, i, root, base, fi, 0, map[string]bool{}, fn); err != nil {
return err
}
}
return nil
}
func (s *fsSource) walk(ctx context.Context, root int, p, rel string, fi vfs.FileInfo, depth int,
ancestors map[string]bool, fn func(Item) error) error {
if err := ctx.Err(); err != nil {
return err
}
it := Item{Rel: rel, Root: root, Src: p, Mode: fi.Mode, ModTime: fi.ModTime, Size: fi.Size}
follow := s.sym == FollowLinks && fi.IsSymlink()
var real string
if follow {
switch {
case fi.Target == nil:
it.Warning = "broken symlink copied as a link"
follow = false
case fi.Target.IsDir():
rp, err := s.fs.RealPath(ctx, p)
if err != nil || ancestors[rp] || depth >= maxFollowDepth {
it.Warning = "symlink loop copied as a link"
follow = false
} else {
real = rp
}
}
if follow {
t := *fi.Target
it.Mode, it.ModTime, it.Size = t.Mode, t.ModTime, t.Size
}
}
switch {
case it.Mode.IsDir():
it.Kind, it.Size = Dir, 0
case it.Mode.IsRegular():
it.Kind = File
case fi.IsSymlink() && !follow:
it.Kind, it.Size = Symlink, 0
t, err := s.fs.ReadLink(ctx, p)
if err != nil {
return err
}
it.LinkTarget = t
default:
it.Kind, it.Size = Special, 0
}
if it.Kind != Dir {
return fn(it)
}
ents, err := s.fs.ReadDir(ctx, p)
if err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
it.ScanErr = err
return fn(it)
}
if err := fn(it); err != nil {
return err
}
if s.sym == FollowLinks {
if real == "" {
if real, err = s.fs.RealPath(ctx, p); err != nil {
return err
}
}
ancestors[real] = true
defer delete(ancestors, real)
}
sort.Slice(ents, func(i, j int) bool { return ents[i].Name < ents[j].Name })
ops := s.fs.Paths()
for _, e := range ents {
if strings.HasPrefix(e.Name, partPrefix) {
continue // leftovers of an interrupted transfer are never copied
}
if err := s.walk(ctx, root, ops.Join(p, e.Name), rel+"/"+e.Name, e, depth+1, ancestors, fn); err != nil {
return err
}
}
return nil
}
func (s *fsSource) Open(ctx context.Context, it Item) (io.ReadCloser, error) {
return s.fs.Open(ctx, it.Src)
}
func (s *fsSource) Stream(ctx context.Context, items []Item, fn func(Item, io.Reader) error) error {
for _, it := range items {
if err := ctx.Err(); err != nil {
return err
}
if it.Kind != File {
if err := fn(it, nil); err != nil {
return err
}
continue
}
f, err := s.Open(ctx, it)
if err != nil {
if err := fn(it, errReader{err}); err != nil {
return err
}
continue
}
err = fn(it, f)
f.Close()
if err != nil {
return err
}
}
return nil
}
// errReader reports an open error through the first Read.
type errReader struct{ err error }
func (r errReader) Read([]byte) (int, error) { return 0, r.err }