Skip to content

Commit

Permalink
cmd/oci-image-tool: fix unpacking...
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
  • Loading branch information
runcom committed Jul 22, 2016
1 parent 6541392 commit 29dcda5
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions image/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/opencontainers/image-spec/schema"
"github.com/pkg/errors"
Expand Down Expand Up @@ -107,7 +106,7 @@ func (m *manifest) unpack(w walker, dest string) error {
}

dd, err := filepath.Rel("blobs", filepath.Clean(path))
if err != nil || d.Digest != dd {
if err != nil || d.getDigest() != dd {
return nil // ignore
}

Expand All @@ -134,6 +133,7 @@ func unpackLayer(dest string, r io.Reader) error {
}
defer gz.Close()

var dirs []*tar.Header
tr := tar.NewReader(gz)

loop:
Expand All @@ -148,7 +148,22 @@ loop:
return errors.Wrapf(err, "error advancing tar stream")
}

path := filepath.Join(dest, filepath.Clean(hdr.Name))
hdr.Name = filepath.Clean(hdr.Name)
// After calling filepath.Clean(hdr.Name) above, hdr.Name will now be in
// the filepath format for the OS on which the daemon is running. Hence
// the check for a slash-suffix MUST be done in an OS-agnostic way.
if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
// Not the root directory, ensure that the parent directory exists
parent := filepath.Dir(hdr.Name)
parentPath := filepath.Join(dest, parent)
if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
err = os.MkdirAll(parentPath, 0777)
if err != nil {
return err
}
}
}
path := filepath.Join(dest, hdr.Name)
info := hdr.FileInfo()

if strings.HasPrefix(info.Name(), ".wh.") {
Expand All @@ -163,8 +178,10 @@ loop:

switch hdr.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(path, info.Mode()); err != nil {
return errors.Wrap(err, "error creating directory")
if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
if err := os.MkdirAll(path, info.Mode()); err != nil {
return errors.Wrap(err, "error creating directory")
}
}

case tar.TypeReg, tar.TypeRegA:
Expand Down Expand Up @@ -200,13 +217,19 @@ loop:
if err := os.Symlink(hdr.Linkname, path); err != nil {
return err
}

}
// Directory mtimes must be handled at the end to avoid further
// file creation in them to modify the directory mtime
if hdr.Typeflag == tar.TypeDir {
dirs = append(dirs, hdr)
}
}
for _, hdr := range dirs {
path := filepath.Join(dest, hdr.Name)

if err := os.Chtimes(path, time.Now().UTC(), info.ModTime()); err != nil {
if err := os.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
return errors.Wrap(err, "error changing time")
}
}

return nil
}

0 comments on commit 29dcda5

Please sign in to comment.