Skip to content

Commit

Permalink
delete stuff from imported dirs when it dissapears
Browse files Browse the repository at this point in the history
In particular, when we do go vendoring updates and people move their code
around, we wouldn't delete their old code and would get lots of compiler
errors. It's incorrect to leave this code laying around anyway, so let's be
sure to delete it.

Signed-off-by: Tycho Andersen <tycho@tycho.ws>
  • Loading branch information
tych0 committed Sep 19, 2018
1 parent 35b9905 commit e160bad
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 22 deletions.
83 changes: 62 additions & 21 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"os/exec"
"path"

"github.com/pkg/errors"
"github.com/udhos/equalfile"
"github.com/vbatts/go-mtree"
)

func fileCopy(dest string, source string) error {
Expand Down Expand Up @@ -92,40 +94,79 @@ func importFile(imp string, cacheDir string) (string, error) {
return "", err
}

if e1.IsDir() {
binary := "cp"
// TODO: use rsync if it is available
output, err := exec.Command(binary, "-a", imp, cacheDir).CombinedOutput()
if !e1.IsDir() {
needsCopy := false
dest := path.Join(cacheDir, path.Base(imp))
e2, err := os.Stat(dest)
if err != nil {
return "", fmt.Errorf("%s", string(output))
needsCopy = true
} else {
differ, err := filesDiffer(imp, e1, dest, e2)
if err != nil {
return "", err
}

needsCopy = differ
}
return path.Join(cacheDir, path.Base(imp)), nil

if needsCopy {
fmt.Printf("copying %s\n", imp)
if err := fileCopy(dest, imp); err != nil {
return "", errors.Wrapf(err, "couldn't copy import %s", imp)
}
} else {
fmt.Println("using cached copy of", imp)
}

return dest, nil
}

existing, err := walkImport(cacheDir)
if err != nil {
return "", errors.Wrapf(err, "failed walking existing import dir")
}

needsCopy := false
dest := path.Join(cacheDir, path.Base(imp))
e2, err := os.Stat(dest)
if err := os.MkdirAll(dest, 0755); err != nil {
return "", err
}

toImport, err := walkImport(imp)
if err != nil {
needsCopy = true
} else {
differ, err := filesDiffer(imp, e1, dest, e2)
if err != nil {
return "", err
}
return "", errors.Wrapf(err, "failed walking dir to import")
}

needsCopy = differ
diff, err := mtree.Compare(existing, toImport, mtreeKeywords)
if err != nil {
return "", err
}

if needsCopy {
fmt.Printf("copying %s\n", imp)
if err := fileCopy(dest, imp); err != nil {
return "", err
for _, d := range diff {
switch d.Type() {
case mtree.Missing:
err := os.RemoveAll(path.Join(dest, d.Path()))
if err != nil {
return "", err
}
case mtree.Modified:
fallthrough
case mtree.Extra:
err := os.RemoveAll(path.Join(dest, d.Path()))
if err != nil && !os.IsNotExist(err) {
return "", err
}

output, err := exec.Command("cp", "-a", path.Join(imp, d.Path()), path.Join(dest, d.Path())).CombinedOutput()
if err != nil {
return "", errors.Wrapf(err, "couldn't copy %s: %s", path.Join(imp, d.Path()), string(output))
}
case mtree.ErrorDifference:
return "", errors.Errorf("failed to diff %s", d.Path())
}
} else {
fmt.Println("using cached copy of", imp)
}

return dest, nil

}

func acquireUrl(c StackerConfig, i string, cache string) (string, error) {
Expand Down
35 changes: 34 additions & 1 deletion test/caching.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ EOF

function teardown() {
cleanup
rm -rf tree1 tree2 link >& /dev/null || true
rm -rf tree1 tree2 link foo >& /dev/null || true
}

@test "import caching" {
Expand All @@ -30,3 +30,36 @@ function teardown() {
umoci unpack --image oci:import-cache dest
[ "$(sha tree2/foo/foo)" == "$(sha dest/rootfs/foo)" ]
}

@test "remove from a dir" {
cat > stacker.yaml <<EOF
a:
from:
type: docker
url: docker://centos:latest
import:
- foo
run: |
[ -f /stacker/foo/bar ]
EOF

mkdir -p foo
touch foo/bar
stacker build
[ "$status" -eq 0 ]

cat > stacker.yaml <<EOF
a:
from:
type: docker
url: docker://centos:latest
import:
- foo
run: |
[ -f /stacker/foo/baz ]
EOF
rm foo/bar
touch foo/baz
stacker build
[ "$status" -eq 0 ]
}

0 comments on commit e160bad

Please sign in to comment.