package archive import ( "archive/tar" "archive/zip" "compress/gzip" "context" "errors" "fmt" "io" "path" "strings" "vcom/internal/transfer" "vcom/internal/vfs" ) // IndexFile opens p on fsys, detects the format from the name and builds the // index. It reads the archive once and creates no temp files. func IndexFile(ctx context.Context, fsys vfs.FS, p string, progress func(n int)) (*Index, error) { f, ok := DetectFormat(p) if !ok { return nil, fmt.Errorf("archive: %s: unknown archive format", p) } file, err := fsys.Open(ctx, p) if err != nil { return nil, err } defer file.Close() fi, err := file.Stat() if err != nil { return nil, err } return BuildIndex(ctx, file, fi.Size, f, progress) } // NewSource returns a transfer.Source with the given members (archive paths; // nil or empty = the whole archive). A member's Rel starts at its own name, // so extracting "docs/a" yields "a" in the target. Stream reads the archive // in one pass: directories and links come first (from the index), then file // contents in archive order. func NewSource(fsys vfs.FS, archivePath string, idx *Index, members []string) transfer.Source { return &source{fs: fsys, path: archivePath, idx: idx, members: members} } type source struct { fs vfs.FS path string idx *Index members []string ordRel map[string]int // Rel of a file item → its ordinal in the archive } func (s *source) Label() string { return s.fs.Paths().Base(s.path) } func (s *source) Walk(ctx context.Context, fn func(transfer.Item) error) error { s.ordRel = map[string]int{} emit := func(e Entry, rel string, root int) error { it := transfer.Item{Rel: rel, Size: e.Size, Mode: e.Mode, ModTime: e.ModTime, LinkTarget: e.Link, Root: root} switch { case e.IsDir(): it.Kind = transfer.Dir case e.IsSymlink(): it.Kind = transfer.Symlink case e.IsRegular(): it.Kind = transfer.File s.ordRel[rel] = e.ord default: it.Kind = transfer.Special } return fn(it) } if len(s.members) == 0 { roots := map[string]int{} for i, e := range s.idx.Entries { if i%256 == 0 { if err := ctx.Err(); err != nil { return err } } top, _, _ := strings.Cut(e.Path, "/") r, ok := roots[top] if !ok { r = len(roots) roots[top] = r } if err := emit(e, e.Path, r); err != nil { return err } } return nil } for r, m := range s.members { m = strings.Trim(path.Clean("/"+m), "/") if _, ok := s.idx.Lookup(m); !ok { return fmt.Errorf("%s: %w", m, vfs.ErrNotExist) } parent := path.Dir(m) cut := 0 if parent != "." { cut = len(parent) + 1 } for _, e := range s.idx.Entries { if e.Path != m && !strings.HasPrefix(e.Path, m+"/") { continue } if err := emit(e, e.Path[cut:], r); err != nil { return err } } } return ctx.Err() } func (s *source) Stream(ctx context.Context, items []transfer.Item, fn func(transfer.Item, io.Reader) error) error { wanted := map[int]transfer.Item{} for _, it := range items { if it.Kind == transfer.File { ord, ok := s.ordRel[it.Rel] if !ok { return fmt.Errorf("archive: %s is not in the plan of this source", it.Rel) } wanted[ord] = it continue } if err := fn(it, nil); err != nil { return err } } if len(wanted) == 0 { return nil } file, err := s.fs.Open(ctx, s.path) if err != nil { return err } defer file.Close() switch s.idx.Format { case Zip: err = s.streamZip(ctx, file, wanted, fn) default: err = s.streamTar(ctx, file, wanted, fn) } if err != nil { return err } for _, it := range wanted { // members that vanished from the archive since indexing if err := fn(it, errReader{fmt.Errorf("%s: %w in the archive", it.Rel, vfs.ErrNotExist)}); err != nil { return err } } return nil } func (s *source) streamZip(ctx context.Context, file vfs.File, wanted map[int]transfer.Item, fn func(transfer.Item, io.Reader) error) error { fi, err := file.Stat() if err != nil { return err } zr, err := zip.NewReader(file, fi.Size) if err != nil && !errors.Is(err, zip.ErrInsecurePath) { return err } for i, zf := range zr.File { it, ok := wanted[i] if !ok { continue } if err := ctx.Err(); err != nil { return err } delete(wanted, i) rc, err := zf.Open() if err != nil { if err := fn(it, errReader{err}); err != nil { return err } continue } err = fn(it, rc) rc.Close() if err != nil { return err } } return nil } func (s *source) streamTar(ctx context.Context, file vfs.File, wanted map[int]transfer.Item, fn func(transfer.Item, io.Reader) error) error { var r io.Reader = &ctxReader{ctx: ctx, r: file} if s.idx.Format == TarGz { zr, err := gzip.NewReader(r) if err != nil { return err } defer zr.Close() r = zr } tr := tar.NewReader(r) for i := 0; len(wanted) > 0; i++ { _, err := tr.Next() if err == io.EOF { return nil } if errors.Is(err, tar.ErrInsecurePath) { err = nil } if err != nil { if ctx.Err() != nil { return ctx.Err() } return err } it, ok := wanted[i] // i counts headers exactly as BuildIndex does if !ok { continue } delete(wanted, i) if err := fn(it, tr); err != nil { return err } } return nil } type errReader struct{ err error } func (r errReader) Read([]byte) (int, error) { return 0, r.err } // Unpack extracts members (nil = all) of the indexed archive into toDir on // any FS through the transfer engine: conflicts are resolved by opts/hooks // instead of silent overwrites, files are written via temp + rename, and an // existing symlink in the target is never written through (U-P1-11). func Unpack(ctx context.Context, fsys vfs.FS, archivePath string, idx *Index, members []string, to vfs.FS, toDir string, opts transfer.Options, h transfer.Hooks) transfer.Report { req := transfer.Request{Op: transfer.Copy, From: NewSource(fsys, archivePath, idx, members), To: to, ToDir: toDir, Opts: opts} plan, err := transfer.Prepare(ctx, req, h.Progress) if err != nil { return transfer.Report{Err: err, Cancelled: ctx.Err() != nil} } return transfer.Execute(ctx, plan, h) }