Skip to content

Commit

Permalink
Fix path mangling on Windows. (#112)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
evankanderson authored and jonjohnsonjr committed Dec 2, 2019
1 parent 1c54dd6 commit 6aff039
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
19 changes: 10 additions & 9 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"io"
"io/ioutil"
"path"
"path/filepath"
"testing"
"time"
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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: ".",
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6aff039

Please sign in to comment.