Skip to content

Commit

Permalink
Fix image extract from Root Directory
Browse files Browse the repository at this point in the history
Closes: BZ#1919032
  • Loading branch information
zerodayz committed Jan 22, 2021
1 parent 6f8f260 commit dc08ed8
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions pkg/cli/image/extract/extract.go
Expand Up @@ -64,8 +64,8 @@ var (
oc image extract docker.io/library/busybox:latest
# Extract the busybox image into the current directory for linux/s390x platform
# Note: Wildcard filter is not supported with extract. Pass a single os/arch to extract.
oc image extract docker.io/library/busybox:latest --filter-by-os=linux/s390x
# Note: Wildcard filter is not supported with extract. Pass a single os/arch to extract.
oc image extract docker.io/library/busybox:latest --filter-by-os=linux/s390x
# Extract the busybox image to a temp directory (must exist)
oc image extract docker.io/library/busybox:latest --path /:/tmp/busybox
Expand Down Expand Up @@ -387,10 +387,13 @@ func (o *ExtractOptions) Run() error {
alter = append(alter, newCopyFromDirectory(mapping.From))
default:
name, parent := path.Base(mapping.From), path.Dir(mapping.From)
if name == "." || parent == "." {
if name != "." && parent == "." {
alter = append(alter, newCopyFromRootDir(name))
} else if name == "." || parent == "." {
return fmt.Errorf("unexpected directory from mapping %s", mapping.From)
} else {
alter = append(alter, newCopyFromPattern(parent, name))
}
alter = append(alter, newCopyFromPattern(parent, name))
}
}

Expand Down Expand Up @@ -586,10 +589,30 @@ func newCopyFromDirectory(from string) archive.AlterHeader {
return &copyFromDirectory{From: from}
}

type copyFromRootDir struct {
Name string
}

func (n *copyFromDirectory) Alter(hdr *tar.Header) (bool, error) {
return changeTarEntryParent(hdr, n.From), nil
}

func newCopyFromRootDir(name string) archive.AlterHeader {
return &copyFromRootDir{Name: name}
}

func (n *copyFromRootDir) Alter(hdr *tar.Header) (bool, error) {
if !changeTarEntryName(hdr, n.Name) {
return false, nil
}
matchName := hdr.Name
if ok, err := path.Match(n.Name, matchName); !ok || err != nil {
klog.V(5).Infof("Excluded %s due to filter %s", hdr.Name, n.Name)
return false, err
}
return true, nil
}

type copyFromPattern struct {
Base string
Name string
Expand All @@ -607,6 +630,9 @@ func (n *copyFromPattern) Alter(hdr *tar.Header) (bool, error) {
return false, nil
}
matchName := hdr.Name
if hdr.Name == "profile" {
matchName = hdr.Name
}
if i := strings.Index(matchName, "/"); i != -1 {
matchName = matchName[:i]
}
Expand All @@ -617,6 +643,19 @@ func (n *copyFromPattern) Alter(hdr *tar.Header) (bool, error) {
return true, nil
}

func changeTarEntryName(hdr *tar.Header, name string) bool {
if hdr.Name != name {
klog.V(5).Infof("Exclude %s due to name mismatch", hdr.Name)
return false
}
if hdr.Typeflag != tar.TypeReg {
klog.V(5).Infof("Exclude %s due to not being a file", hdr.Name)
return false
}
klog.V(5).Infof("Updated name %s", hdr.Name)
return true
}

func changeTarEntryParent(hdr *tar.Header, from string) bool {
if !strings.HasPrefix(hdr.Name, from) {
klog.V(5).Infof("Exclude %s due to missing prefix %s", hdr.Name, from)
Expand Down

0 comments on commit dc08ed8

Please sign in to comment.