Skip to content

Commit

Permalink
Fix StripComponents behavior on Windows
Browse files Browse the repository at this point in the history
Tar archives use forward slashes to separate path segments, so we should
not use os-specific separators for splitting paths.

Co-authored-by: Brian Upton <brian.upton@broadcom.com>
  • Loading branch information
selzoc and ystros committed Dec 3, 2024
1 parent ccf95f5 commit 145cfe0
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions fileutil/tarball_compressor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fileutil

import (
"archive/tar"
"fmt"
"io"
"io/fs"
Expand All @@ -9,14 +10,14 @@ import (
"runtime"
"strings"

"archive/tar"

"github.com/klauspost/pgzip"

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

const forwardSlash string = "/"

type tarballCompressor struct {
fs boshsys.FileSystem
}
Expand Down Expand Up @@ -72,7 +73,7 @@ func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string

header.Name = relPath
if runtime.GOOS == "windows" {
header.Name = strings.ReplaceAll(relPath, "\\", "/")
header.Name = strings.ReplaceAll(relPath, "\\", forwardSlash)
}

if err := tw.WriteHeader(header); err != nil {
Expand Down Expand Up @@ -146,7 +147,7 @@ func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, o
fullName := filepath.Join(dir, filepath.FromSlash(header.Name))

if options.StripComponents > 0 {
components := strings.Split(header.Name, string(filepath.Separator))
components := strings.Split(header.Name, forwardSlash)
if len(components) <= options.StripComponents {
continue
}
Expand Down

0 comments on commit 145cfe0

Please sign in to comment.