1
0
mirror of https://github.com/ko-build/ko.git synced 2025-03-03 15:32:20 +02:00

Fix path mangling on Windows. (#112)

Since we are always building linux containers, use `path.Join` rather than
`filepath.Join` when adding files to the tar.

Signed-off-by: Evan Anderson <evan.k.anderson@gmail.com>
This commit is contained in:
Evan Anderson 2019-12-02 11:05:48 -08:00 committed by jonjohnsonjr
parent 1c54dd6b3e
commit 6aff039ca9
2 changed files with 22 additions and 20 deletions

View File

@ -27,6 +27,7 @@ import (
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
@ -237,7 +238,7 @@ func tarBinary(name, binary string) (*bytes.Buffer, error) {
defer tw.Close()
// write the parent directories to the tarball archive
if err := tarAddDirectories(tw, filepath.Dir(name)); err != nil {
if err := tarAddDirectories(tw, path.Dir(name)); err != nil {
return nil, err
}
@ -286,8 +287,8 @@ const kodataRoot = "/var/run/ko"
// to the provided tar.Writer with root -> chroot. All symlinks are dereferenced,
// which is what leads to recursion when we encounter a directory symlink.
func walkRecursive(tw *tar.Writer, root, chroot string) error {
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if path == root {
return filepath.Walk(root, func(hostPath string, info os.FileInfo, err error) error {
if hostPath == root {
// Add an entry for the root directory of our walk.
return tw.WriteHeader(&tar.Header{
Name: chroot,
@ -305,25 +306,25 @@ func walkRecursive(tw *tar.Writer, root, chroot string) error {
if info.Mode().IsDir() {
return nil
}
newPath := filepath.Join(chroot, path[len(root):])
newPath := path.Join(chroot, filepath.ToSlash(hostPath[len(root):]))
path, err = filepath.EvalSymlinks(path)
hostPath, err = filepath.EvalSymlinks(hostPath)
if err != nil {
return err
}
// Chase symlinks.
info, err = os.Stat(path)
info, err = os.Stat(hostPath)
if err != nil {
return err
}
// Skip other directories.
if info.Mode().IsDir() {
return walkRecursive(tw, path, newPath)
return walkRecursive(tw, hostPath, newPath)
}
// Open the file to copy it into the tarball.
file, err := os.Open(path)
file, err := os.Open(hostPath)
if err != nil {
return err
}
@ -411,7 +412,7 @@ func (gb *gobuild) Build(ctx context.Context, s string) (v1.Image, error) {
},
})
appPath := filepath.Join(appDir, appFilename(s))
appPath := path.Join(appDir, appFilename(s))
// Construct a tarball with the binary and produce a layer.
binaryLayerBuf, err := tarBinary(appPath, file)

View File

@ -19,6 +19,7 @@ import (
"context"
"io"
"io/ioutil"
"path"
"path/filepath"
"testing"
"time"
@ -40,7 +41,7 @@ func TestGoBuildIsSupportedRef(t *testing.T) {
// Supported import paths.
for _, importpath := range []string{
filepath.FromSlash("github.com/google/ko/cmd/ko"), // ko can build itself.
"github.com/google/ko/cmd/ko", // ko can build itself.
} {
t.Run(importpath, func(t *testing.T) {
if !ng.IsSupportedReference(importpath) {
@ -51,8 +52,8 @@ func TestGoBuildIsSupportedRef(t *testing.T) {
// Unsupported import paths.
for _, importpath := range []string{
filepath.FromSlash("github.com/google/ko/pkg/build"), // not a command.
filepath.FromSlash("github.com/google/ko/pkg/nonexistent"), // does not exist.
"github.com/google/ko/pkg/build", // not a command.
"github.com/google/ko/pkg/nonexistent", // does not exist.
} {
t.Run(importpath, func(t *testing.T) {
if ng.IsSupportedReference(importpath) {
@ -68,7 +69,7 @@ func TestGoBuildIsSupportedRefWithModules(t *testing.T) {
t.Fatalf("random.Image() = %v", err)
}
mod := &modInfo{
Path: filepath.FromSlash("github.com/google/ko/cmd/ko/test"),
Path: "github.com/google/ko/cmd/ko/test",
Dir: ".",
}
@ -79,7 +80,7 @@ func TestGoBuildIsSupportedRefWithModules(t *testing.T) {
// Supported import paths.
for _, importpath := range []string{
filepath.FromSlash("github.com/google/ko/cmd/ko/test"), // ko can build the test package.
"github.com/google/ko/cmd/ko/test", // ko can build the test package.
} {
t.Run(importpath, func(t *testing.T) {
if !ng.IsSupportedReference(importpath) {
@ -90,9 +91,9 @@ func TestGoBuildIsSupportedRefWithModules(t *testing.T) {
// Unsupported import paths.
for _, importpath := range []string{
filepath.FromSlash("github.com/google/ko/pkg/build"), // not a command.
filepath.FromSlash("github.com/google/ko/pkg/nonexistent"), // does not exist.
filepath.FromSlash("github.com/google/ko/cmd/ko"), // not in this module.
"github.com/google/ko/pkg/build", // not a command.
"github.com/google/ko/pkg/nonexistent", // does not exist.
"github.com/google/ko/cmd/ko", // not in this module.
} {
t.Run(importpath, func(t *testing.T) {
if ng.IsSupportedReference(importpath) {
@ -138,7 +139,7 @@ func TestGoBuildNoKoData(t *testing.T) {
t.Fatalf("NewGo() = %v", err)
}
img, err := ng.Build(context.Background(), filepath.Join(importpath, "cmd", "ko"))
img, err := ng.Build(context.Background(), path.Join(importpath, "cmd", "ko"))
if err != nil {
t.Fatalf("Build() = %v", err)
}
@ -218,7 +219,7 @@ func TestGoBuild(t *testing.T) {
t.Fatalf("NewGo() = %v", err)
}
img, err := ng.Build(context.Background(), filepath.Join(importpath, "cmd", "ko", "test"))
img, err := ng.Build(context.Background(), path.Join(importpath, "cmd", "ko", "test"))
if err != nil {
t.Fatalf("Build() = %v", err)
}
@ -290,7 +291,7 @@ func TestGoBuild(t *testing.T) {
t.Errorf("Next() = %v", err)
continue
}
if header.Name != filepath.Join(kodataRoot, "kenobi") {
if header.Name != path.Join(kodataRoot, "kenobi") {
continue
}
found = true