Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to make tar work on Windows #11

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions fileutil/tarball_compressor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fileutil

import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)

Expand All @@ -21,42 +20,6 @@ func (c tarballCompressor) CompressFilesInDir(dir string) (string, error) {
return c.CompressSpecificFilesInDir(dir, []string{"."})
}

func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string) (string, error) {
tarball, err := c.fs.TempFile("bosh-platform-disk-TarballCompressor-CompressSpecificFilesInDir")
if err != nil {
return "", bosherr.WrapError(err, "Creating temporary file for tarball")
}

tarballPath := tarball.Name()

args := []string{"czf", tarballPath, "-C", dir}

for _, file := range files {
args = append(args, file)
}

_, _, _, err = c.cmdRunner.RunCommand("tar", args...)
if err != nil {
return "", bosherr.WrapError(err, "Shelling out to tar")
}

return tarballPath, nil
}

func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, options CompressorOptions) error {
sameOwnerOption := "--no-same-owner"
if options.SameOwner {
sameOwnerOption = "--same-owner"
}

_, _, _, err := c.cmdRunner.RunCommand("tar", sameOwnerOption, "-xzvf", tarballPath, "-C", dir)
if err != nil {
return bosherr.WrapError(err, "Shelling out to tar")
}

return nil
}

func (c tarballCompressor) CleanUp(tarballPath string) error {
return c.fs.RemoveAll(tarballPath)
}
26 changes: 17 additions & 9 deletions fileutil/tarball_compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -29,6 +30,13 @@ func fixtureSrcTgz() string {
return filepath.Join(pwd, "test_assets", "compressor-decompress-file-to-dir.tgz")
}

func makePathTarCompatible(path string) string {
if runtime.GOOS == "windows" {
path = strings.Replace("/"+string(path[0])+string(path[2:]), "\\", "/", -1)
}
return path
}

func createTestSymlink() (string, error) {
srcDir := fixtureSrcDir()
symlinkPath := filepath.Join(srcDir, "symlink_dir")
Expand Down Expand Up @@ -112,7 +120,7 @@ var _ = Describe("tarballCompressor", func() {
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tgzName)

tarballContents, _, _, err := cmdRunner.RunCommand("tar", "-tf", tgzName)
tarballContents, _, _, err := cmdRunner.RunCommand("tar", "-tf", makePathTarCompatible(tgzName))
Expect(err).ToNot(HaveOccurred())

contentElements := strings.Fields(strings.TrimSpace(tarballContents))
Expand All @@ -133,7 +141,7 @@ var _ = Describe("tarballCompressor", func() {
"./other_logs/more_logs/more.stdout.log",
))

_, _, _, err = cmdRunner.RunCommand("tar", "-xzpf", tgzName, "-C", dstDir)
_, _, _, err = cmdRunner.RunCommand("tar", "-xzpf", makePathTarCompatible(tgzName), "-C", makePathTarCompatible(dstDir))
Expect(err).ToNot(HaveOccurred())

content, err := fs.ReadFileString(dstDir + "/app.stdout.log")
Expand Down Expand Up @@ -162,7 +170,7 @@ var _ = Describe("tarballCompressor", func() {
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tgzName)

tarballContents, _, _, err := cmdRunner.RunCommand("tar", "-tf", tgzName)
tarballContents, _, _, err := cmdRunner.RunCommand("tar", "-tf", makePathTarCompatible(tgzName))
Expect(err).ToNot(HaveOccurred())

contentElements := strings.Fields(strings.TrimSpace(tarballContents))
Expand All @@ -178,7 +186,7 @@ var _ = Describe("tarballCompressor", func() {

_, _, _, err = cmdRunner.RunCommand("cp", tgzName, "/tmp")

_, _, _, err = cmdRunner.RunCommand("tar", "-xzpf", tgzName, "-C", dstDir)
_, _, _, err = cmdRunner.RunCommand("tar", "-xzpf", makePathTarCompatible(tgzName), "-C", makePathTarCompatible(dstDir))
Expect(err).ToNot(HaveOccurred())

content, err := fs.ReadFileString(dstDir + "/app.stdout.log")
Expand Down Expand Up @@ -221,7 +229,7 @@ var _ = Describe("tarballCompressor", func() {

err := compressor.DecompressFileToDir(fixtureSrcTgz(), dstDir, CompressorOptions{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(dstDir))
Expect(err.Error()).To(ContainSubstring(makePathTarCompatible(dstDir)))
})

It("uses no same owner option", func() {
Expand All @@ -236,8 +244,8 @@ var _ = Describe("tarballCompressor", func() {
Expect(cmdRunner.RunCommands[0]).To(Equal(
[]string{
"tar", "--no-same-owner",
"-xzvf", tarballPath,
"-C", dstDir,
"-xzvf", makePathTarCompatible(tarballPath),
"-C", makePathTarCompatible(dstDir),
},
))
})
Expand All @@ -258,8 +266,8 @@ var _ = Describe("tarballCompressor", func() {
Expect(cmdRunner.RunCommands[0]).To(Equal(
[]string{
"tar", "--same-owner",
"-xzvf", tarballPath,
"-C", dstDir,
"-xzvf", makePathTarCompatible(tarballPath),
"-C", makePathTarCompatible(dstDir),
},
))
})
Expand Down
45 changes: 45 additions & 0 deletions fileutil/tarball_compressor_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// +build !windows

package fileutil

import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)

func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string) (string, error) {
tarball, err := c.fs.TempFile("bosh-platform-disk-TarballCompressor-CompressSpecificFilesInDir")
if err != nil {
return "", bosherr.WrapError(err, "Creating temporary file for tarball")
}

defer tarball.Close()

tarballPath := tarball.Name()

args := []string{"czf", tarballPath, "-C", dir}

for _, file := range files {
args = append(args, file)
}

_, _, _, err = c.cmdRunner.RunCommand("tar", args...)
if err != nil {
return "", bosherr.WrapError(err, "Shelling out to tar")
}

return tarballPath, nil
}

func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, options CompressorOptions) error {
sameOwnerOption := "--no-same-owner"
if options.SameOwner {
sameOwnerOption = "--same-owner"
}

_, _, _, err := c.cmdRunner.RunCommand("tar", sameOwnerOption, "-xzvf", tarballPath, "-C", dir)
if err != nil {
return bosherr.WrapError(err, "Shelling out to tar")
}

return nil
}
79 changes: 79 additions & 0 deletions fileutil/tarball_compressor_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// +build windows

package fileutil

import (
"regexp"
"strings"

bosherr "github.com/cloudfoundry/bosh-utils/errors"
)

func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string) (string, error) {
tarball, err := c.fs.TempFile("bosh-platform-disk-TarballCompressor-CompressSpecificFilesInDir")
if err != nil {
return "", bosherr.WrapError(err, "Creating temporary file for tarball")
}

defer tarball.Close()

tarballPath := tarball.Name()

tarballPathForTar, err := makePathTarCompatible(tarballPath)
if err != nil {
return "", bosherr.WrapError(err, "Converting tarballPath path failed")
}
dir, err = makePathTarCompatible(dir)
if err != nil {
return "", bosherr.WrapError(err, "Converting dir path failed")
}

args := []string{"czf", tarballPathForTar, "-C", dir}

for _, file := range files {
args = append(args, file)
}

_, _, _, err = c.cmdRunner.RunCommand("tar", args...)
if err != nil {
return "", bosherr.WrapError(err, "Shelling out to tar")
}

return tarballPath, nil
}

func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, options CompressorOptions) error {
sameOwnerOption := "--no-same-owner"
if options.SameOwner {
sameOwnerOption = "--same-owner"
}

tarballPath, err := makePathTarCompatible(tarballPath)
if err != nil {
return bosherr.WrapError(err, "Converting tarballPath path failed")
}
dir, err = makePathTarCompatible(dir)
if err != nil {
return bosherr.WrapError(err, "Converting dir path failed")
}

_, _, _, err = c.cmdRunner.RunCommand("tar", sameOwnerOption, "-xzf", tarballPath, "-C", dir)
if err != nil {
return bosherr.WrapError(err, "Shelling out to tar")
}

return nil
}

func makePathTarCompatible(path string) (string, error) {
doesPathContainDrive, err := regexp.MatchString("^[a-zA-Z]:", path)
if err != nil {
return "", bosherr.WrapError(err, "Test for drive in path failed")
}
if doesPathContainDrive {
path = "/" + string(path[0]) + string(path[2:])
}
path = strings.Replace(path, "\\", "/", -1)

return path, nil
}