387 lines
9.3 KiB
Go
387 lines
9.3 KiB
Go
|
|
package archive
|
||
|
|
|
||
|
|
import (
|
||
|
|
"archive/tar"
|
||
|
|
"archive/zip"
|
||
|
|
"compress/gzip"
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"io/fs"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"vcom/internal/transfer"
|
||
|
|
"vcom/internal/vfs"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Create errors.
|
||
|
|
var (
|
||
|
|
ErrSameFile = errors.New("the archive would overwrite one of its sources")
|
||
|
|
ErrIntoSource = errors.New("the archive would be written inside a source directory")
|
||
|
|
)
|
||
|
|
|
||
|
|
// Create writes the given sources (any FS) into archivePath on dst (any FS)
|
||
|
|
// in format f (design §5.1). The archive is written through
|
||
|
|
// transfer.WriteAtomic: a failed or cancelled run leaves neither a partial
|
||
|
|
// archive nor a damaged previous one, and a source is never truncated.
|
||
|
|
// Refused up front: an archivePath that is one of the sources or lies inside
|
||
|
|
// a source directory (also through symlinks; U-P0-1). An existing archive is
|
||
|
|
// a conflict for h.Resolve (Overwrite / KeepBoth = "name (1).zip" / Skip /
|
||
|
|
// Abort); without Resolve it is refused. Symlinks are stored as links.
|
||
|
|
// Errors of the zip/tar/gzip writers' Close fail the operation (N-P2-13).
|
||
|
|
func Create(ctx context.Context, src vfs.FS, paths []string, dst vfs.FS, archivePath string, f Format,
|
||
|
|
h transfer.Hooks) transfer.Report {
|
||
|
|
var rep transfer.Report
|
||
|
|
fail := func(err error) transfer.Report {
|
||
|
|
rep.Err = err
|
||
|
|
rep.Cancelled = ctx.Err() != nil
|
||
|
|
return rep
|
||
|
|
}
|
||
|
|
if dst.Caps().ReadOnly {
|
||
|
|
return fail(fmt.Errorf("%w: %s", transfer.ErrReadOnly, dst.Label()))
|
||
|
|
}
|
||
|
|
archivePath = dst.Paths().Clean(archivePath)
|
||
|
|
if err := checkSources(ctx, src, paths, dst, archivePath); err != nil {
|
||
|
|
return fail(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
target, replace, skip, err := resolveTarget(ctx, dst, archivePath, h)
|
||
|
|
if err != nil {
|
||
|
|
return fail(err)
|
||
|
|
}
|
||
|
|
if skip {
|
||
|
|
rep.Skipped = 1
|
||
|
|
return rep
|
||
|
|
}
|
||
|
|
|
||
|
|
var items []transfer.Item
|
||
|
|
var total int64
|
||
|
|
seen := map[string]bool{}
|
||
|
|
err = transfer.FSSource(src, paths, transfer.CopyLinks).Walk(ctx, func(it transfer.Item) error {
|
||
|
|
if !strings.Contains(it.Rel, "/") {
|
||
|
|
if seen[it.Rel] {
|
||
|
|
return fmt.Errorf("%w: %s", transfer.ErrDuplicateName, it.Rel)
|
||
|
|
}
|
||
|
|
seen[it.Rel] = true
|
||
|
|
}
|
||
|
|
items = append(items, it)
|
||
|
|
if it.Kind == transfer.File {
|
||
|
|
total += it.Size
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return fail(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
w := &writer{ctx: ctx, src: src, f: f, h: h, rep: &rep, items: items, total: total}
|
||
|
|
err = transfer.WriteAtomic(ctx, dst, target, transfer.WriteOptions{Perm: 0o644, Replace: replace, Fsync: true}, w.fill)
|
||
|
|
if ctx.Err() != nil {
|
||
|
|
rep.Cancelled = true
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
if !rep.Cancelled && rep.Err == nil {
|
||
|
|
rep.Err = err
|
||
|
|
}
|
||
|
|
rep.Done, rep.Bytes = 0, 0
|
||
|
|
return rep
|
||
|
|
}
|
||
|
|
rep.Affected = []vfs.Location{{FS: dst, Path: dst.Paths().Dir(target)}}
|
||
|
|
w.progress(true)
|
||
|
|
return rep
|
||
|
|
}
|
||
|
|
|
||
|
|
func checkSources(ctx context.Context, src vfs.FS, paths []string, dst vfs.FS, archivePath string) error {
|
||
|
|
for _, p := range paths {
|
||
|
|
same, err := vfs.SameFile(ctx, src, p, dst, archivePath)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if same {
|
||
|
|
return fmt.Errorf("%w: %s", ErrSameFile, p)
|
||
|
|
}
|
||
|
|
if src.Volume() != dst.Volume() {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
fi, err := src.Lstat(ctx, p)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if !fi.IsDir() {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
in, err := vfs.Within(ctx, dst, archivePath, p)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if in {
|
||
|
|
return fmt.Errorf("%w: %s", ErrIntoSource, p)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// resolveTarget handles an existing archive file.
|
||
|
|
func resolveTarget(ctx context.Context, dst vfs.FS, p string, h transfer.Hooks) (target string, replace, skip bool, err error) {
|
||
|
|
fi, err := dst.Lstat(ctx, p)
|
||
|
|
if errors.Is(err, vfs.ErrNotExist) {
|
||
|
|
return p, false, false, nil
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
return "", false, false, err
|
||
|
|
}
|
||
|
|
if fi.IsDir() {
|
||
|
|
return "", false, false, fmt.Errorf("%s is a directory", p)
|
||
|
|
}
|
||
|
|
if h.Resolve == nil {
|
||
|
|
return "", false, false, fmt.Errorf("%s: %w", p, vfs.ErrExist)
|
||
|
|
}
|
||
|
|
res, err := h.Resolve(ctx, transfer.Conflict{
|
||
|
|
Item: transfer.Item{Rel: dst.Paths().Base(p), Kind: transfer.File, Existing: &fi},
|
||
|
|
Kind: transfer.FileFile,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return "", false, false, err
|
||
|
|
}
|
||
|
|
switch res.Action {
|
||
|
|
case transfer.ResolveOverwrite:
|
||
|
|
return p, true, false, nil
|
||
|
|
case transfer.ResolveSkip:
|
||
|
|
return "", false, true, nil
|
||
|
|
case transfer.ResolveKeepBoth:
|
||
|
|
free, err := FreeName(ctx, dst, p)
|
||
|
|
return free, false, false, err
|
||
|
|
}
|
||
|
|
return "", false, false, transfer.ErrAborted
|
||
|
|
}
|
||
|
|
|
||
|
|
// FreeName returns p or the first free "name (n).ext" next to it (the
|
||
|
|
// archive extension, e.g. .tar.gz, stays together).
|
||
|
|
func FreeName(ctx context.Context, fsys vfs.FS, p string) (string, error) {
|
||
|
|
ops := fsys.Paths()
|
||
|
|
dir, base := ops.Dir(p), ops.Base(p)
|
||
|
|
stem := stripArchiveExt(base)
|
||
|
|
ext := base[len(stem):]
|
||
|
|
for n := 0; n <= 10000; n++ {
|
||
|
|
cand := p
|
||
|
|
if n > 0 {
|
||
|
|
cand = ops.Join(dir, fmt.Sprintf("%s (%d)%s", stem, n, ext))
|
||
|
|
}
|
||
|
|
_, err := fsys.Lstat(ctx, cand)
|
||
|
|
if errors.Is(err, vfs.ErrNotExist) {
|
||
|
|
return cand, nil
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return "", fmt.Errorf("no free name for %s", p)
|
||
|
|
}
|
||
|
|
|
||
|
|
// writer streams the items into the archive format.
|
||
|
|
type writer struct {
|
||
|
|
ctx context.Context
|
||
|
|
src vfs.FS
|
||
|
|
f Format
|
||
|
|
h transfer.Hooks
|
||
|
|
rep *transfer.Report
|
||
|
|
items []transfer.Item
|
||
|
|
total int64
|
||
|
|
done int64
|
||
|
|
files int
|
||
|
|
last time.Time
|
||
|
|
}
|
||
|
|
|
||
|
|
func (w *writer) progress(force bool) {
|
||
|
|
if w.h.Progress == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
every := w.h.ProgressInterval
|
||
|
|
if every <= 0 {
|
||
|
|
every = 50 * time.Millisecond
|
||
|
|
}
|
||
|
|
if !force && time.Since(w.last) < every {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
w.last = time.Now()
|
||
|
|
w.h.Progress(transfer.Progress{Phase: transfer.Copying, FilesDone: w.files, BytesDone: w.done, BytesTotal: w.total})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (w *writer) fill(out io.Writer) error {
|
||
|
|
switch w.f {
|
||
|
|
case Zip:
|
||
|
|
zw := zip.NewWriter(out)
|
||
|
|
if err := w.each(zipAdder{zw}); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return zw.Close()
|
||
|
|
case Tar, TarGz:
|
||
|
|
var gz *gzip.Writer
|
||
|
|
if w.f == TarGz {
|
||
|
|
gz = gzip.NewWriter(out)
|
||
|
|
out = gz
|
||
|
|
}
|
||
|
|
tw := tar.NewWriter(out)
|
||
|
|
if err := w.each(tarAdder{tw}); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := tw.Close(); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if gz != nil {
|
||
|
|
return gz.Close()
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
return fmt.Errorf("archive: unknown format %d", w.f)
|
||
|
|
}
|
||
|
|
|
||
|
|
// adder writes one entry; body is nil except for regular files.
|
||
|
|
type adder interface {
|
||
|
|
add(it transfer.Item, body io.Reader) error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (w *writer) each(a adder) error {
|
||
|
|
for _, it := range w.items {
|
||
|
|
if err := w.ctx.Err(); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
switch it.Kind {
|
||
|
|
case transfer.Special:
|
||
|
|
w.rep.Skipped++
|
||
|
|
w.rep.Warnings = append(w.rep.Warnings, it.Rel+": special file skipped")
|
||
|
|
continue
|
||
|
|
case transfer.Dir, transfer.Symlink:
|
||
|
|
if err := a.add(it, nil); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if it.Kind == transfer.Symlink {
|
||
|
|
w.rep.Done++
|
||
|
|
w.files++
|
||
|
|
}
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if err := w.addFile(a, it); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// addFile opens the source first, so an unreadable file can be skipped
|
||
|
|
// (OnError) before its header is written. An error in the middle of a file
|
||
|
|
// aborts the archive (a tar header already promised its size).
|
||
|
|
func (w *writer) addFile(a adder, it transfer.Item) error {
|
||
|
|
for {
|
||
|
|
r, err := w.src.Open(w.ctx, it.Src)
|
||
|
|
if err == nil {
|
||
|
|
start := w.done
|
||
|
|
pr := &countReader{ctx: w.ctx, r: r, onRead: func(n int64) { w.done = start + n; w.progress(false) }}
|
||
|
|
err = a.add(it, pr)
|
||
|
|
r.Close()
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("%s: %w", it.Rel, err)
|
||
|
|
}
|
||
|
|
if pr.n != it.Size {
|
||
|
|
return fmt.Errorf("%s: changed while archiving (%d of %d bytes)", it.Rel, pr.n, it.Size)
|
||
|
|
}
|
||
|
|
w.files++
|
||
|
|
w.rep.Done++
|
||
|
|
w.rep.Bytes += it.Size
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
ie := transfer.ItemError{Item: it, Op: "read", Err: err}
|
||
|
|
act := transfer.ErrorSkip
|
||
|
|
switch {
|
||
|
|
case w.h.OnError != nil:
|
||
|
|
var herr error
|
||
|
|
if act, herr = w.h.OnError(w.ctx, ie); herr != nil {
|
||
|
|
return herr
|
||
|
|
}
|
||
|
|
}
|
||
|
|
switch act {
|
||
|
|
case transfer.ErrorRetry:
|
||
|
|
continue
|
||
|
|
case transfer.ErrorStop:
|
||
|
|
w.rep.Errors = append(w.rep.Errors, ie)
|
||
|
|
w.rep.Failed++
|
||
|
|
return fmt.Errorf("%w: %v", transfer.ErrAborted, ie)
|
||
|
|
}
|
||
|
|
w.rep.Errors = append(w.rep.Errors, ie)
|
||
|
|
w.rep.Failed++
|
||
|
|
w.done += it.Size
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type countReader struct {
|
||
|
|
ctx context.Context
|
||
|
|
r io.Reader
|
||
|
|
n int64
|
||
|
|
onRead func(int64)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *countReader) Read(p []byte) (int, error) {
|
||
|
|
if err := c.ctx.Err(); err != nil {
|
||
|
|
return 0, err
|
||
|
|
}
|
||
|
|
n, err := c.r.Read(p)
|
||
|
|
c.n += int64(n)
|
||
|
|
if n > 0 {
|
||
|
|
c.onRead(c.n)
|
||
|
|
}
|
||
|
|
return n, err
|
||
|
|
}
|
||
|
|
|
||
|
|
type zipAdder struct{ zw *zip.Writer }
|
||
|
|
|
||
|
|
func (z zipAdder) add(it transfer.Item, body io.Reader) error {
|
||
|
|
h := &zip.FileHeader{Name: it.Rel, Modified: it.ModTime, Method: zip.Deflate}
|
||
|
|
switch it.Kind {
|
||
|
|
case transfer.Dir:
|
||
|
|
h.Name += "/"
|
||
|
|
h.Method = zip.Store
|
||
|
|
h.SetMode(fs.ModeDir | it.Mode.Perm())
|
||
|
|
case transfer.Symlink:
|
||
|
|
h.Method = zip.Store
|
||
|
|
h.SetMode(fs.ModeSymlink | 0o777)
|
||
|
|
body = strings.NewReader(it.LinkTarget)
|
||
|
|
default:
|
||
|
|
h.SetMode(it.Mode.Perm())
|
||
|
|
h.UncompressedSize64 = uint64(it.Size)
|
||
|
|
}
|
||
|
|
w, err := z.zw.CreateHeader(h)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if body != nil {
|
||
|
|
_, err = io.Copy(w, body)
|
||
|
|
}
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
type tarAdder struct{ tw *tar.Writer }
|
||
|
|
|
||
|
|
func (t tarAdder) add(it transfer.Item, body io.Reader) error {
|
||
|
|
h := &tar.Header{Name: it.Rel, ModTime: it.ModTime, Mode: int64(it.Mode.Perm())}
|
||
|
|
switch it.Kind {
|
||
|
|
case transfer.Dir:
|
||
|
|
h.Typeflag, h.Name = tar.TypeDir, it.Rel+"/"
|
||
|
|
case transfer.Symlink:
|
||
|
|
h.Typeflag, h.Linkname, h.Mode = tar.TypeSymlink, it.LinkTarget, 0o777
|
||
|
|
default:
|
||
|
|
h.Typeflag, h.Size = tar.TypeReg, it.Size
|
||
|
|
}
|
||
|
|
if err := t.tw.WriteHeader(h); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if body != nil {
|
||
|
|
if _, err := io.Copy(t.tw, body); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|