Skip to content

Commit

Permalink
fix: windows issues (#690)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 authored Jul 13, 2023
1 parent d710da8 commit d309eb6
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 46 deletions.
8 changes: 3 additions & 5 deletions apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,26 +401,24 @@ func createBuilderData(info *nfpm.Info, sizep *int64) func(tw *tar.Writer) error

func createFilesInsideTarGz(info *nfpm.Info, tw *tar.Writer, sizep *int64) (err error) {
for _, file := range info.Contents {
file.Destination = strings.TrimPrefix(file.Destination, "./")
file.Destination = files.AsRelativePath(file.Destination)

switch file.Type {
case files.TypeDir, files.TypeImplicitDir:
err = tw.WriteHeader(&tar.Header{
Name: files.AsRelativePath(file.Destination),
Name: file.Destination,
Mode: int64(file.FileInfo.Mode),
Typeflag: tar.TypeDir,
Format: tar.FormatGNU,
Uname: file.FileInfo.Owner,
Gname: file.FileInfo.Group,
ModTime: file.FileInfo.MTime,
})
case files.TypeSymlink:
err = newItemInsideTarGz(tw, []byte{}, &tar.Header{
Name: files.AsRelativePath(file.Destination),
Name: file.Destination,
Linkname: file.Source,
Typeflag: tar.TypeSymlink,
ModTime: file.FileInfo.MTime,
Format: tar.FormatGNU,
})
default:
err = copyToTarAndDigest(file, tw, sizep)
Expand Down
2 changes: 1 addition & 1 deletion arch/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func writeScripts(w io.Writer, scripts map[string]string) error {
return err
}

fl.Close()
_ = fl.Close()

_, err = io.WriteString(w, "\n}\n\n")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ func conffiles(info *nfpm.Info) []byte {
for _, file := range info.Contents {
switch file.Type {
case files.TypeConfig, files.TypeConfigNoReplace:
confs = append(confs, filepath.Join("/", file.Destination))
confs = append(confs, files.NormalizeAbsoluteFilePath(file.Destination))
}
}
return []byte(strings.Join(confs, "\n") + "\n")
Expand Down
42 changes: 12 additions & 30 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,11 @@ func PrepareForPackager(rawContents Contents, umask fs.FileMode, packager string
return nil, fmt.Errorf("add tree: %w", err)
}
case TypeConfig, TypeConfigNoReplace, TypeFile, "":
globbed, err := glob.Glob(content.Source, content.Destination, disableGlobbing)
globbed, err := glob.Glob(
filepath.ToSlash(content.Source),
filepath.ToSlash(content.Destination),
disableGlobbing,
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -474,7 +478,7 @@ func addTree(all map[string]*Content, tree *Content, umask os.FileMode) error {
return err
}

c.Source = linkDestination
c.Source = filepath.ToSlash(strings.TrimPrefix(linkDestination, filepath.VolumeName(linkDestination)))
c.Destination = NormalizeAbsoluteFilePath(destination)
c.Type = TypeSymlink
default:
Expand Down Expand Up @@ -515,42 +519,20 @@ func ToNixPath(path string) string {
}

// As relative path converts a path to an explicitly relative path starting with
// a dot (e.g. it converts /foo -> ./foo).
// a dot (e.g. it converts /foo -> ./foo and foo -> ./foo).
func AsExplicitRelativePath(path string) string {
if path == "/" {
return "./"
}

cleanedPath := filepath.Clean(path)

end := ""
if len(cleanedPath) > 1 && strings.HasSuffix(path, "/") {
end = "/"
}

if !filepath.IsAbs(cleanedPath) {
cleanedPath = filepath.Join("/", cleanedPath)
}

return "." + cleanedPath + end
return "./" + AsRelativePath(path)
}

// AsRelativePath converts a path to a relative path without a "./" prefix. This
// function leaves trailing slashes to indicate that the path refers to a
// directory.
// directory, and converts the path to Unix path.
func AsRelativePath(path string) string {
cleanedPath := filepath.Clean(path)

end := ""
cleanedPath := strings.TrimLeft(ToNixPath(path), "/")
if len(cleanedPath) > 1 && strings.HasSuffix(path, "/") {
end = "/"
return cleanedPath + "/"
}

if filepath.IsAbs(cleanedPath) {
return strings.TrimLeft(cleanedPath, "/") + end
}

return cleanedPath + end
return cleanedPath
}

// NormalizeAbsoluteFilePath returns an absolute cleaned path separated by
Expand Down
21 changes: 16 additions & 5 deletions files/files_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package files_test

import (
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -353,6 +355,9 @@ func withoutImplicitDirs(contents files.Contents) files.Contents {
}

func TestGlobbingWhenFilesHaveBrackets(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("doesn't work on windows")
}
result, err := files.PrepareForPackager(files.Contents{
{
Source: "./testdata/\\{test\\}/",
Expand Down Expand Up @@ -633,7 +638,7 @@ func TestRelevantFiles(t *testing.T) {
func TestTree(t *testing.T) {
results, err := files.PrepareForPackager(files.Contents{
{
Source: "testdata/tree",
Source: filepath.Join("testdata", "tree"),
Destination: "/base",
Type: files.TypeTree,
},
Expand All @@ -652,7 +657,7 @@ func TestTree(t *testing.T) {
Type: files.TypeDir,
},
{
Source: "testdata/tree/files/a",
Source: filepath.Join("testdata", "tree", "files", "a"),
Destination: "/base/files/a",
Type: files.TypeFile,
},
Expand All @@ -662,7 +667,7 @@ func TestTree(t *testing.T) {
Type: files.TypeDir,
},
{
Source: "testdata/tree/files/b/c",
Source: filepath.Join("testdata", "tree", "files", "b", "c"),
Destination: "/base/files/b/c",
Type: files.TypeFile,
},
Expand Down Expand Up @@ -697,11 +702,14 @@ func withoutFileInfo(contents files.Contents) files.Contents {
}

func TestAsRelativePath(t *testing.T) {
sep := fmt.Sprintf("%c", filepath.Separator)
testCases := map[string]string{
"/etc/foo/": "etc/foo/",
"./etc/foo": "etc/foo",
"./././foo/../bar/": "bar/",
"/": "",
sep: "",
sep + sep: "",
sep + strings.Join([]string{"foo", "bar", "zazz"}, sep): "foo/bar/zazz",
}

for input, expected := range testCases {
Expand All @@ -710,11 +718,14 @@ func TestAsRelativePath(t *testing.T) {
}

func TestAsExplicitRelativePath(t *testing.T) {
sep := fmt.Sprintf("%c", filepath.Separator)
testCases := map[string]string{
"/etc/foo/": "./etc/foo/",
"./etc/foo": "./etc/foo",
"./././foo/../bar/": "./bar/",
"/": "./",
sep: "./",
sep: "./",
sep + strings.Join([]string{"foo", "bar", "zazz"}, sep): "./foo/bar/zazz",
}

for input, expected := range testCases {
Expand Down
5 changes: 4 additions & 1 deletion internal/glob/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func longestCommonPrefix(strs []string) string {
}
lcp := strs[0]
for _, str := range strs {
lcp = strlcp(lcp, str)
lcp = strlcp(
filepath.ToSlash(lcp),
filepath.ToSlash(str),
)
}
return lcp
}
Expand Down
2 changes: 1 addition & 1 deletion internal/glob/glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestGlob(t *testing.T) {
files, err := Glob("./testdata/dir_a/dir_b/test_b.txt", "/foo/bar/", false)
require.NoError(t, err)
require.Len(t, files, 1)
require.Equal(t, "/foo/bar/test_b.txt", files["testdata/dir_a/dir_b/test_b.txt"])
require.Equal(t, "/foo/bar/test_b.txt", filepath.ToSlash(files["testdata/dir_a/dir_b/test_b.txt"]))
})

t.Run("to parent", func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -378,7 +377,7 @@ func createFilesInsideRPM(info *nfpm.Info, rpm *rpmpack.RPM) (err error) {
}

// clean assures that even folders do not have a trailing slash
file.Name = filepath.Clean(file.Name)
file.Name = files.ToNixPath(file.Name)
rpm.AddFile(*file)

}
Expand Down

0 comments on commit d309eb6

Please sign in to comment.