Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 45056ce

Browse files
authored
Merge pull request #106 from dlorenc/tmp
Unpack tars into tempdirs.
2 parents b089b9b + 43aaee6 commit 45056ce

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

pkg/util/docker_utils.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ func unpackDockerSave(tarPath string, target string) error {
7474
os.MkdirAll(target, 0777)
7575
}
7676

77-
tempLayerDir := target + "-temp"
78-
err := UnTar(tarPath, tempLayerDir)
77+
tempLayerDir, err := ioutil.TempDir("", ".container-diff")
7978
if err != nil {
79+
return err
80+
}
81+
defer os.RemoveAll(tempLayerDir)
82+
83+
if err := UnTar(tarPath, tempLayerDir); err != nil {
8084
errMsg := fmt.Sprintf("Could not unpack saved Docker image %s: %s", tarPath, err)
8185
return errors.New(errMsg)
8286
}
@@ -93,15 +97,10 @@ func unpackDockerSave(tarPath string, target string) error {
9397
glog.Infof("Did not unpack layer %s because no layer.tar found", layer)
9498
continue
9599
}
96-
err = UnTar(layerTar, target)
97-
if err != nil {
100+
if err = UnTar(layerTar, target); err != nil {
98101
glog.Errorf("Could not unpack layer %s: %s", layer, err)
99102
}
100103
}
101-
err = os.RemoveAll(tempLayerDir)
102-
if err != nil {
103-
glog.Errorf("Error deleting temp image layer filesystem: %s", err)
104-
}
105104
return nil
106105
}
107106

pkg/util/image_prep_utils.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import (
2020
"archive/tar"
2121
"encoding/json"
2222
"errors"
23-
"fmt"
2423
"io/ioutil"
2524
"os"
26-
"path/filepath"
2725
"strings"
2826

2927
"github.com/containers/image/pkg/compression"
@@ -76,14 +74,15 @@ func getImage(p Prepper) (Image, error) {
7674
FSPath: imgPath,
7775
Config: config,
7876
}, nil
79-
return Image{}, fmt.Errorf("Could not retrieve image %s from source", p.GetSource())
8077
}
8178

8279
func getImageFromTar(tarPath string) (string, error) {
8380
glog.Info("Extracting image tar to obtain image file system")
84-
path := strings.TrimSuffix(tarPath, filepath.Ext(tarPath))
85-
err := unpackDockerSave(tarPath, path)
86-
return path, err
81+
tempPath, err := ioutil.TempDir("", ".container-diff")
82+
if err != nil {
83+
return "", err
84+
}
85+
return tempPath, unpackDockerSave(tarPath, tempPath)
8786
}
8887

8988
func getFileSystemFromReference(ref types.ImageReference, imageName string) (string, error) {

pkg/util/tar_prepper.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"io/ioutil"
2323
"os"
2424
"path/filepath"
25-
"strings"
2625

2726
"github.com/containers/image/docker/tarfile"
2827
"github.com/docker/docker/client"
@@ -51,10 +50,12 @@ func (p TarPrepper) GetFileSystem() (string, error) {
5150
}
5251

5352
func (p TarPrepper) GetConfig() (ConfigSchema, error) {
54-
tempDir := strings.TrimSuffix(p.Source, filepath.Ext(p.Source)) + "-config"
55-
defer os.RemoveAll(tempDir)
56-
err := UnTar(p.Source, tempDir)
53+
tempDir, err := ioutil.TempDir("", ".container-diff")
5754
if err != nil {
55+
return ConfigSchema{}, nil
56+
}
57+
defer os.RemoveAll(tempDir)
58+
if err := UnTar(p.Source, tempDir); err != nil {
5859
return ConfigSchema{}, err
5960
}
6061

0 commit comments

Comments
 (0)