2021-08-23 09:14:53 -04:00
|
|
|
//go:build ignore
|
2014-10-28 16:59:27 -04:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// Simple tool to create an archive stream from an old and new directory
|
|
|
|
//
|
|
|
|
// By default it will stream the comparison of two temporary directories with junk files
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-10-28 16:59:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
flDebug = flag.Bool("D", false, "debugging output")
|
|
|
|
flNewDir = flag.String("newdir", "", "")
|
|
|
|
flOldDir = flag.String("olddir", "", "")
|
|
|
|
log = logrus.New()
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Println("Produce a tar from comparing two directory paths. By default a demo tar is created of around 200 files (including hardlinks)")
|
|
|
|
fmt.Printf("%s [OPTIONS]\n", os.Args[0])
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
|
|
|
flag.Parse()
|
|
|
|
log.Out = os.Stderr
|
|
|
|
if (len(os.Getenv("DEBUG")) > 0) || *flDebug {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
var newDir, oldDir string
|
|
|
|
|
|
|
|
if len(*flNewDir) == 0 {
|
|
|
|
var err error
|
2021-08-24 06:10:50 -04:00
|
|
|
newDir, err = os.MkdirTemp("", "docker-test-newDir")
|
2014-10-28 16:59:27 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(newDir)
|
|
|
|
if _, err := prepareUntarSourceDirectory(100, newDir, true); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newDir = *flNewDir
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(*flOldDir) == 0 {
|
2021-08-24 06:10:50 -04:00
|
|
|
oldDir, err := os.MkdirTemp("", "docker-test-oldDir")
|
2014-10-28 16:59:27 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(oldDir)
|
|
|
|
} else {
|
|
|
|
oldDir = *flOldDir
|
|
|
|
}
|
|
|
|
|
|
|
|
changes, err := archive.ChangesDirs(newDir, oldDir)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a, err := archive.ExportChanges(newDir, changes)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer a.Close()
|
|
|
|
|
|
|
|
i, err := io.Copy(os.Stdout, a)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "wrote archive of %d bytes", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareUntarSourceDirectory(numberOfFiles int, targetPath string, makeLinks bool) (int, error) {
|
|
|
|
fileData := []byte("fooo")
|
|
|
|
for n := 0; n < numberOfFiles; n++ {
|
|
|
|
fileName := fmt.Sprintf("file-%d", n)
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.WriteFile(path.Join(targetPath, fileName), fileData, 0700); err != nil {
|
2014-10-28 16:59:27 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if makeLinks {
|
|
|
|
if err := os.Link(path.Join(targetPath, fileName), path.Join(targetPath, fileName+"-link")); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
totalSize := numberOfFiles * len(fileData)
|
|
|
|
return totalSize, nil
|
|
|
|
}
|