Create refuses an archive path 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 Resolve (Overwrite / KeepBoth "name (1).tar.gz" / Skip). The archive is written via transfer.WriteAtomic, so cancel or failure removes the partial file and keeps a previous archive; Close/flush errors of the zip, tar and gzip writers fail the operation (N-P2-13). Symlinks are stored as links; sources and target can be remote (N-P1-15, tested on the stand). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
214 lines
7.5 KiB
Go
214 lines
7.5 KiB
Go
package archive
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"vcom/internal/testutil/fstree"
|
|
"vcom/internal/transfer"
|
|
"vcom/internal/vfs"
|
|
"vcom/internal/vfs/localfs"
|
|
"vcom/internal/vfs/vfstest"
|
|
)
|
|
|
|
var local = localfs.New()
|
|
|
|
func noTemps(t *testing.T, dir string) {
|
|
t.Helper()
|
|
ents, _ := os.ReadDir(dir)
|
|
for _, e := range ents {
|
|
if strings.HasPrefix(e.Name(), ".vcom-") {
|
|
t.Errorf("temp left: %s", e.Name())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUP01_ArchiveDoesNotDestroySource(t *testing.T) {
|
|
dir := t.TempDir()
|
|
fstree.Build(t, dir, fstree.Spec{fstree.File("data.tar", "").WithSize(64 << 10)})
|
|
before, _ := os.ReadFile(filepath.Join(dir, "data.tar"))
|
|
name := Name([]string{"data.tar"}, Tar)
|
|
if name != "data.tar" {
|
|
t.Fatalf("name %s", name)
|
|
}
|
|
rep := Create(bg, local, []string{filepath.Join(dir, "data.tar")}, local, filepath.Join(dir, name), Tar,
|
|
transfer.Hooks{Resolve: func(context.Context, transfer.Conflict) (transfer.Resolution, error) {
|
|
return transfer.Resolution{Action: transfer.ResolveOverwrite}, nil
|
|
}})
|
|
if !errors.Is(rep.Err, ErrSameFile) {
|
|
t.Fatalf("err = %v, want ErrSameFile (U-P0-1)", rep.Err)
|
|
}
|
|
if after, _ := os.ReadFile(filepath.Join(dir, "data.tar")); string(after) != string(before) {
|
|
t.Fatal("source damaged")
|
|
}
|
|
// With another name (what the UI offers) it works.
|
|
free, err := FreeName(bg, local, filepath.Join(dir, name))
|
|
if err != nil || filepath.Base(free) != "data (1).tar" {
|
|
t.Fatalf("free name %q %v", free, err)
|
|
}
|
|
rep = Create(bg, local, []string{filepath.Join(dir, "data.tar")}, local, free, Tar, transfer.Hooks{})
|
|
if rep.Err != nil || rep.Done != 1 {
|
|
t.Fatalf("%+v", rep)
|
|
}
|
|
idx, err := IndexFile(bg, local, free, nil)
|
|
if err != nil || len(idx.Entries) != 1 || idx.Entries[0].Size != 64<<10 {
|
|
t.Fatalf("index %+v %v", idx, err)
|
|
}
|
|
}
|
|
|
|
func TestArchiveIntoSourceDirRejected(t *testing.T) {
|
|
dir := t.TempDir()
|
|
fstree.Build(t, dir, fstree.Spec{fstree.File("proj/a", "a"), fstree.Dir("proj/sub"), fstree.Symlink("alias", "proj")})
|
|
for _, target := range []string{
|
|
filepath.Join(dir, "proj", "proj.zip"),
|
|
filepath.Join(dir, "proj", "sub", "x.zip"),
|
|
filepath.Join(dir, "alias", "x.zip"), // through a symlink
|
|
} {
|
|
rep := Create(bg, local, []string{filepath.Join(dir, "proj")}, local, target, Zip, transfer.Hooks{})
|
|
if !errors.Is(rep.Err, ErrIntoSource) {
|
|
t.Errorf("%s: %v", target, rep.Err)
|
|
}
|
|
if _, err := os.Lstat(target); !os.IsNotExist(err) {
|
|
t.Errorf("%s written", target)
|
|
}
|
|
}
|
|
if rep := Create(bg, local, []string{filepath.Join(dir, "proj")}, local, filepath.Join(dir, "proj.zip"), Zip, transfer.Hooks{}); rep.Err != nil {
|
|
t.Fatal(rep.Err)
|
|
}
|
|
}
|
|
|
|
func TestArchiveCancelRemovesPartial(t *testing.T) {
|
|
src := t.TempDir()
|
|
fstree.Build(t, src, fstree.Spec{fstree.File("d/big", "").WithSize(4 << 20), fstree.File("d/b", "b")})
|
|
out := t.TempDir()
|
|
old := filepath.Join(out, "d.tar.gz")
|
|
if err := os.WriteFile(old, []byte("previous archive"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dst := &vfstest.FaultFS{FS: local, Hook: func(op vfstest.Op, _ string) error {
|
|
if op == vfstest.OpWrite {
|
|
time.Sleep(2 * time.Millisecond)
|
|
}
|
|
return nil
|
|
}}
|
|
ctx, cancel := context.WithCancel(bg)
|
|
time.AfterFunc(50*time.Millisecond, cancel)
|
|
rep := Create(ctx, local, []string{filepath.Join(src, "d")}, dst, old, TarGz, transfer.Hooks{
|
|
Resolve: func(context.Context, transfer.Conflict) (transfer.Resolution, error) {
|
|
return transfer.Resolution{Action: transfer.ResolveOverwrite}, nil
|
|
}})
|
|
if !rep.Cancelled || rep.Done != 0 {
|
|
t.Fatalf("%+v", rep)
|
|
}
|
|
if b, _ := os.ReadFile(old); string(b) != "previous archive" {
|
|
t.Fatal("previous archive damaged by a cancelled overwrite")
|
|
}
|
|
noTemps(t, out)
|
|
}
|
|
|
|
func TestArchiveCloseErrorReported(t *testing.T) {
|
|
src := t.TempDir()
|
|
fstree.Build(t, src, fstree.Spec{fstree.File("a", "data")})
|
|
for _, f := range []Format{Zip, Tar, TarGz} {
|
|
out := t.TempDir()
|
|
dst := &vfstest.FaultFS{FS: local, Hook: vfstest.FailNth(vfstest.OpClose, 1, syscall.EIO)}
|
|
target := filepath.Join(out, "a"+f.Ext())
|
|
rep := Create(bg, local, []string{filepath.Join(src, "a")}, dst, target, f, transfer.Hooks{})
|
|
if !errors.Is(rep.Err, syscall.EIO) || rep.Done != 0 {
|
|
t.Fatalf("%v: close error not reported (N-P2-13): %+v", f, rep)
|
|
}
|
|
if _, err := os.Lstat(target); !os.IsNotExist(err) {
|
|
t.Fatalf("%v: archive committed", f)
|
|
}
|
|
noTemps(t, out)
|
|
// A write error while the writers flush on Close is reported too.
|
|
dst = &vfstest.FaultFS{FS: local, Hook: vfstest.FailNth(vfstest.OpWrite, 1, syscall.ENOSPC)}
|
|
rep = Create(bg, local, []string{filepath.Join(src, "a")}, dst, target, f, transfer.Hooks{})
|
|
if !errors.Is(rep.Err, syscall.ENOSPC) {
|
|
t.Fatalf("%v: flush error: %+v", f, rep)
|
|
}
|
|
noTemps(t, out)
|
|
}
|
|
}
|
|
|
|
func TestArchiveNameTarGzToZip(t *testing.T) {
|
|
if got := Name([]string{"/srv/foo.tar.gz"}, Zip); got != "foo.zip" {
|
|
t.Fatalf("got %s (U-P2: was foo.tar.zip)", got)
|
|
}
|
|
out := t.TempDir()
|
|
fstree.Build(t, out, fstree.Spec{fstree.File("a (1).tar.gz", ""), fstree.File("a.tar.gz", "")})
|
|
if p, _ := FreeName(bg, local, filepath.Join(out, "a.tar.gz")); filepath.Base(p) != "a (2).tar.gz" {
|
|
t.Fatalf("free %s", p)
|
|
}
|
|
}
|
|
|
|
func TestArchiveSymlinksPreserved(t *testing.T) {
|
|
src := t.TempDir()
|
|
mt := time.Date(2019, 3, 4, 5, 6, 7, 0, time.UTC)
|
|
spec := fstree.Spec{
|
|
fstree.File("d/run.sh", "#!/bin/sh").WithMode(0o755).WithMTime(mt),
|
|
fstree.Symlink("d/ln", "run.sh"),
|
|
fstree.Symlink("d/broken", "../nowhere"),
|
|
fstree.Dir("d/empty"),
|
|
}
|
|
fstree.Build(t, src, spec)
|
|
for _, f := range []Format{Zip, Tar, TarGz} {
|
|
out := t.TempDir()
|
|
arch := filepath.Join(out, "d"+f.Ext())
|
|
rep := Create(bg, local, []string{filepath.Join(src, "d")}, local, arch, f, transfer.Hooks{})
|
|
if rep.Err != nil || rep.Done != 3 {
|
|
t.Fatalf("%v: %+v", f, rep)
|
|
}
|
|
idx, err := IndexFile(bg, local, arch, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for p, want := range map[string]string{"d/ln": "run.sh", "d/broken": "../nowhere"} {
|
|
if e, ok := idx.Lookup(p); !ok || !e.IsSymlink() || e.Link != want {
|
|
t.Fatalf("%v: %s = %+v", f, p, e)
|
|
}
|
|
}
|
|
// Round trip through Unpack gives the same tree (modes, mtimes, links).
|
|
back := t.TempDir()
|
|
if rep := Unpack(bg, local, arch, idx, nil, local, back, transfer.DefaultOptions(), transfer.Hooks{}); rep.Err != nil || rep.Failed != 0 {
|
|
t.Fatalf("%v: unpack %+v", f, rep)
|
|
}
|
|
fstree.Equal(t, fstree.Snapshot(t, filepath.Join(src, "d")), fstree.Snapshot(t, filepath.Join(back, "d")),
|
|
fstree.Opts{Mode: true, Links: true})
|
|
if fi, _ := os.Stat(filepath.Join(back, "d", "run.sh")); !fi.ModTime().Equal(mt) {
|
|
t.Fatalf("%v: mtime %v", f, fi.ModTime())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestArchiveExistingTargetIsConflict(t *testing.T) {
|
|
src, out := t.TempDir(), t.TempDir()
|
|
fstree.Build(t, src, fstree.Spec{fstree.File("a", "new")})
|
|
target := filepath.Join(out, "a.zip")
|
|
_ = os.WriteFile(target, []byte("old"), 0o644)
|
|
if rep := Create(bg, local, []string{filepath.Join(src, "a")}, local, target, Zip, transfer.Hooks{}); !errors.Is(rep.Err, vfs.ErrExist) {
|
|
t.Fatalf("no Resolve: %v", rep.Err)
|
|
}
|
|
rep := Create(bg, local, []string{filepath.Join(src, "a")}, local, target, Zip, transfer.Hooks{
|
|
Resolve: func(_ context.Context, c transfer.Conflict) (transfer.Resolution, error) {
|
|
if c.Item.Existing == nil {
|
|
t.Error("no Existing")
|
|
}
|
|
return transfer.Resolution{Action: transfer.ResolveKeepBoth}, nil
|
|
}})
|
|
if rep.Err != nil || len(rep.Affected) != 1 {
|
|
t.Fatalf("%+v", rep)
|
|
}
|
|
if b, _ := os.ReadFile(target); string(b) != "old" {
|
|
t.Fatal("KeepBoth overwrote")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(out, "a (1).zip")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|