-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix WCOW COPY --from failure in multistage builds on Windows
Fixes: #5193 Signed-off-by: Billy Owire <billyowire@microsoft.com>
- Loading branch information
Billy Owire
committed
Sep 3, 2024
1 parent
b60d621
commit f2af587
Showing
3 changed files
with
194 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package contenthash | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func (cc *cacheContext) scanPath(ctx context.Context, m *mount, p string, followTrailing bool) (retErr error) { | ||
p = path.Join("/", p) | ||
|
||
mp, err := m.mount(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
n := cc.tree.Root() | ||
txn := cc.tree.Txn() | ||
|
||
resolvedPath, err := rootPath(mp, filepath.FromSlash(p), followTrailing, func(p, link string) error { | ||
cr := &CacheRecord{ | ||
Type: CacheRecordTypeSymlink, | ||
Linkname: filepath.ToSlash(link), | ||
} | ||
p = path.Join("/", filepath.ToSlash(p)) | ||
txn.Insert(convertPathToKey(p), cr) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Scan the parent directory of the path we resolved, unless we're at the | ||
// root (in which case we scan the root). | ||
scanPath := filepath.Dir(resolvedPath) | ||
if !strings.HasPrefix(filepath.ToSlash(scanPath)+"/", filepath.ToSlash(mp)+"/") { | ||
scanPath = resolvedPath | ||
} | ||
|
||
err = filepath.Walk(scanPath, func(itemPath string, fi os.FileInfo, err error) error { | ||
if scanCounterEnable { | ||
scanCounter.Add(1) | ||
} | ||
if err != nil { | ||
// If the root doesn't exist, ignore the error. | ||
if itemPath == scanPath && errors.Is(err, os.ErrNotExist) { | ||
return nil | ||
} | ||
return errors.Wrapf(err, "failed to walk %s", itemPath) | ||
} | ||
rel, err := filepath.Rel(mp, itemPath) | ||
if err != nil { | ||
return err | ||
} | ||
k := convertPathToKey(keyPath(rel)) | ||
if _, ok := n.Get(k); !ok { | ||
cr := &CacheRecord{ | ||
Type: CacheRecordTypeFile, | ||
} | ||
if fi.Mode()&os.ModeSymlink != 0 { | ||
cr.Type = CacheRecordTypeSymlink | ||
link, err := os.Readlink(itemPath) | ||
if err != nil { | ||
return err | ||
} | ||
cr.Linkname = filepath.ToSlash(link) | ||
} | ||
if fi.IsDir() { | ||
cr.Type = CacheRecordTypeDirHeader | ||
cr2 := &CacheRecord{ | ||
Type: CacheRecordTypeDir, | ||
} | ||
txn.Insert(k, cr2) | ||
k = append(k, 0) | ||
} | ||
txn.Insert(k, cr) | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
cc.tree = txn.Commit() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package contenthash | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/Microsoft/go-winio" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func (cc *cacheContext) scanPath(ctx context.Context, m *mount, p string, followTrailing bool) (retErr error) { | ||
p = path.Join("/", p) | ||
|
||
mp, err := m.mount(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
n := cc.tree.Root() | ||
txn := cc.tree.Txn() | ||
|
||
resolvedPath, err := rootPath(mp, filepath.FromSlash(p), followTrailing, func(p, link string) error { | ||
cr := &CacheRecord{ | ||
Type: CacheRecordTypeSymlink, | ||
Linkname: filepath.ToSlash(link), | ||
} | ||
p = path.Join("/", filepath.ToSlash(p)) | ||
txn.Insert(convertPathToKey(p), cr) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Scan the parent directory of the path we resolved, unless we're at the | ||
// root (in which case we scan the root). | ||
scanPath := filepath.Dir(resolvedPath) | ||
if !strings.HasPrefix(filepath.ToSlash(scanPath)+"/", filepath.ToSlash(mp)+"/") { | ||
scanPath = resolvedPath | ||
} | ||
|
||
walkFn := func(itemPath string, fi os.FileInfo, err error) error { | ||
if scanCounterEnable { | ||
scanCounter.Add(1) | ||
} | ||
if err != nil { | ||
// If the root doesn't exist, ignore the error. | ||
if itemPath == scanPath && errors.Is(err, os.ErrNotExist) { | ||
return nil | ||
} | ||
return errors.Wrapf(err, "failed to walk %s", itemPath) | ||
} | ||
rel, err := filepath.Rel(mp, itemPath) | ||
if err != nil { | ||
return err | ||
} | ||
k := convertPathToKey(keyPath(rel)) | ||
if _, ok := n.Get(k); !ok { | ||
cr := &CacheRecord{ | ||
Type: CacheRecordTypeFile, | ||
} | ||
if fi.Mode()&os.ModeSymlink != 0 { | ||
cr.Type = CacheRecordTypeSymlink | ||
link, err := os.Readlink(itemPath) | ||
if err != nil { | ||
return err | ||
} | ||
cr.Linkname = filepath.ToSlash(link) | ||
} | ||
if fi.IsDir() { | ||
cr.Type = CacheRecordTypeDirHeader | ||
cr2 := &CacheRecord{ | ||
Type: CacheRecordTypeDir, | ||
} | ||
txn.Insert(k, cr2) | ||
k = append(k, 0) | ||
} | ||
txn.Insert(k, cr) | ||
} | ||
return nil | ||
} | ||
|
||
privileges := []string{winio.SeBackupPrivilege} | ||
err = winio.RunWithPrivileges(privileges, func() error { | ||
return filepath.Walk(scanPath, walkFn) | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
cc.tree = txn.Commit() | ||
return nil | ||
} |