From 767f797d09af4d03b4390aeabb68bbd3912f9f5f Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Fri, 8 May 2020 13:31:56 -0400 Subject: [PATCH] archiveMapper.Filter: also map hard link target names Apply pathname rewriting to the target name field when a header indicates that the entry should be a hard link to another file, which we presumably encountered earlier in the archive. We would have rewritten that entry's pathname, so this should make our reference to it correct. Signed-off-by: Nalin Dahyabhai --- dockerclient/archive.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/dockerclient/archive.go b/dockerclient/archive.go index 84b10b6e..44fa8c5f 100644 --- a/dockerclient/archive.go +++ b/dockerclient/archive.go @@ -414,6 +414,25 @@ func (m *archiveMapper) Filter(h *tar.Header, r io.Reader) ([]byte, bool, bool, m.foundItems = true h.Name = newName + + if h.Typeflag == tar.TypeLink { + // run the link target name through the same mapping the Name + // in the target's entry would have gotten + linkName := strings.TrimPrefix(h.Linkname, "/") + if !strings.HasPrefix(linkName, m.prefix) { + klog.V(6).Infof("No prefix %q in link target %q", m.prefix, h.Linkname) + return nil, false, true, nil + } + linkName = strings.TrimPrefix(strings.TrimPrefix(linkName, m.prefix), "/") + newTarget, ok := m.rename(linkName, false) + if !ok { + klog.V(6).Infof("Transform link target %s -> %s: ok=%t skip=%t", h.Linkname, newTarget, ok, true) + return nil, false, true, nil + } + klog.V(6).Infof("Transform link target %s -> %s: ok=%t", h.Linkname, newTarget, ok) + h.Linkname = newTarget + } + // include all files return nil, false, false, nil }