Skip to content

Commit

Permalink
ensure tmp image is cleaned up on crash (addresses part of #58)
Browse files Browse the repository at this point in the history
  • Loading branch information
wagoodman committed Nov 25, 2018
1 parent 7d86809 commit b1962b9
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,17 @@ func saveImage(imageID string) (string, string) {
tmpDir, err := ioutil.TempDir("", "dive")
check(err)

cleanUpTmp := func() {
os.RemoveAll(tmpDir)
}

imageTarPath := filepath.Join(tmpDir, "image.tar")
imageFile, err := os.Create(imageTarPath)
check(err)

defer func() {
if err := imageFile.Close(); err != nil {
cleanUpTmp()
logrus.Panic(err)
}
}()
Expand All @@ -379,6 +384,7 @@ func saveImage(imageID string) (string, string) {
for {
n, err := readCloser.Read(buf)
if err != nil && err != io.EOF {
cleanUpTmp()
logrus.Panic(err)
}
if n == 0 {
Expand All @@ -392,11 +398,13 @@ func saveImage(imageID string) (string, string) {
}

if _, err := imageWriter.Write(buf[:n]); err != nil {
cleanUpTmp()
logrus.Panic(err)
}
}

if err = imageWriter.Flush(); err != nil {
cleanUpTmp()
logrus.Panic(err)
}

Expand Down

0 comments on commit b1962b9

Please sign in to comment.