github.com/akerouanton/docker@v1.11.0-rc3/pkg/archive/example_changes.go (about) 1 // +build ignore 2 3 // Simple tool to create an archive stream from an old and new directory 4 // 5 // By default it will stream the comparison of two temporary directories with junk files 6 package main 7 8 import ( 9 "flag" 10 "fmt" 11 "io" 12 "io/ioutil" 13 "os" 14 "path" 15 16 "github.com/Sirupsen/logrus" 17 "github.com/docker/docker/pkg/archive" 18 ) 19 20 var ( 21 flDebug = flag.Bool("D", false, "debugging output") 22 flNewDir = flag.String("newdir", "", "") 23 flOldDir = flag.String("olddir", "", "") 24 log = logrus.New() 25 ) 26 27 func main() { 28 flag.Usage = func() { 29 fmt.Println("Produce a tar from comparing two directory paths. By default a demo tar is created of around 200 files (including hardlinks)") 30 fmt.Printf("%s [OPTIONS]\n", os.Args[0]) 31 flag.PrintDefaults() 32 } 33 flag.Parse() 34 log.Out = os.Stderr 35 if (len(os.Getenv("DEBUG")) > 0) || *flDebug { 36 logrus.SetLevel(logrus.DebugLevel) 37 } 38 var newDir, oldDir string 39 40 if len(*flNewDir) == 0 { 41 var err error 42 newDir, err = ioutil.TempDir("", "docker-test-newDir") 43 if err != nil { 44 log.Fatal(err) 45 } 46 defer os.RemoveAll(newDir) 47 if _, err := prepareUntarSourceDirectory(100, newDir, true); err != nil { 48 log.Fatal(err) 49 } 50 } else { 51 newDir = *flNewDir 52 } 53 54 if len(*flOldDir) == 0 { 55 oldDir, err := ioutil.TempDir("", "docker-test-oldDir") 56 if err != nil { 57 log.Fatal(err) 58 } 59 defer os.RemoveAll(oldDir) 60 } else { 61 oldDir = *flOldDir 62 } 63 64 changes, err := archive.ChangesDirs(newDir, oldDir) 65 if err != nil { 66 log.Fatal(err) 67 } 68 69 a, err := archive.ExportChanges(newDir, changes) 70 if err != nil { 71 log.Fatal(err) 72 } 73 defer a.Close() 74 75 i, err := io.Copy(os.Stdout, a) 76 if err != nil && err != io.EOF { 77 log.Fatal(err) 78 } 79 fmt.Fprintf(os.Stderr, "wrote archive of %d bytes", i) 80 } 81 82 func prepareUntarSourceDirectory(numberOfFiles int, targetPath string, makeLinks bool) (int, error) { 83 fileData := []byte("fooo") 84 for n := 0; n < numberOfFiles; n++ { 85 fileName := fmt.Sprintf("file-%d", n) 86 if err := ioutil.WriteFile(path.Join(targetPath, fileName), fileData, 0700); err != nil { 87 return 0, err 88 } 89 if makeLinks { 90 if err := os.Link(path.Join(targetPath, fileName), path.Join(targetPath, fileName+"-link")); err != nil { 91 return 0, err 92 } 93 } 94 } 95 totalSize := numberOfFiles * len(fileData) 96 return totalSize, nil 97 }