Skip to content

Commit

Permalink
fix: properly handle windows file paths in utility functions
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <kzantow@gmail.com>
  • Loading branch information
kzantow committed May 17, 2024
1 parent 99f2750 commit 61ccee8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
28 changes: 6 additions & 22 deletions builder/build_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ package builder

import (
"fmt"
"path/filepath"
"regexp"
"runtime"

"github.com/spdx/tools-golang/spdx"
"github.com/spdx/tools-golang/spdx/v2/common"
Expand All @@ -21,32 +18,19 @@ import (
func BuildPackageSection(packageName string, dirRoot string, pathsIgnore []string) (*spdx.Package, error) {
// build the file section first, so we'll have it available
// for calculating the package verification code
filepaths, err := utils.GetAllFilePaths(dirRoot, pathsIgnore)
osType := runtime.GOOS
relativePaths, err := utils.GetAllFilePaths(dirRoot, pathsIgnore)

if err != nil {
return nil, err
}

re, ok := regexp.Compile("/+")
if ok != nil {
return nil, err
}
dirRootLen := 0
if osType == "windows" {
dirRootLen = len(dirRoot)
}

files := []*spdx.File{}
fileNumber := 0
for _, fp := range filepaths {
newFilePatch := ""
if osType == "windows" {
newFilePatch = filepath.FromSlash("." + fp[dirRootLen:])
} else {
newFilePatch = filepath.FromSlash("./" + fp)
}
newFile, err := BuildFileSection(re.ReplaceAllLiteralString(newFilePatch, string(filepath.Separator)), dirRoot, fileNumber)
for _, filePath := range relativePaths {
// SPDX spec says file names should generally start with ./
// see: https://spdx.github.io/spdx-spec/v2.3/file-information/#81-file-name-field
relativePath := "." + filePath
newFile, err := BuildFileSection(relativePath, dirRoot, fileNumber)
if err != nil {
return nil, err
}
Expand Down
11 changes: 7 additions & 4 deletions utils/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
)

// GetAllFilePaths takes a path to a directory (including an optional slice of
// path patterns to ignore), and returns a slice of relative paths to all files
// in that directory and its subdirectories (excluding those that are ignored).
// These paths are always normalized to use URI-like forward-slashes but begin with /
func GetAllFilePaths(dirRoot string, pathsIgnored []string) ([]string, error) {
// paths is a _pointer_ to a slice -- not just a slice.
// this is so that it can be appropriately modified by append
// in the sub-function.
paths := &[]string{}
prefix := strings.TrimSuffix(dirRoot, "/")
prefix := strings.TrimSuffix(filepath.ToSlash(dirRoot), "/")

err := filepath.Walk(dirRoot, func(path string, fi os.FileInfo, err error) error {
if err != nil {
Expand All @@ -37,6 +39,7 @@ func GetAllFilePaths(dirRoot string, pathsIgnored []string) ([]string, error) {
return nil
}

path = filepath.ToSlash(path)
shortPath := strings.TrimPrefix(path, prefix)

// don't include path if it should be ignored
Expand All @@ -55,7 +58,7 @@ func GetAllFilePaths(dirRoot string, pathsIgnored []string) ([]string, error) {
// GetHashesForFilePath takes a path to a file on disk, and returns
// SHA1, SHA256 and MD5 hashes for that file as strings.
func GetHashesForFilePath(p string) (string, string, string, error) {
f, err := os.Open(p)
f, err := os.Open(filepath.FromSlash(p))
if err != nil {
return "", "", "", err
}
Expand All @@ -82,11 +85,11 @@ func GetHashesForFilePath(p string) (string, string, string, error) {
// and determines whether that file should be ignored because it matches
// any of those patterns.
func ShouldIgnore(fileName string, pathsIgnored []string) bool {
fDirs, fFile := filepath.Split(fileName)
fDirs, fFile := path.Split(fileName)

for _, pattern := range pathsIgnored {
// split into dir(s) and filename
patternDirs, patternFile := filepath.Split(pattern)
patternDirs, patternFile := path.Split(pattern)
patternDirStars := strings.HasPrefix(patternDirs, "**")
if patternDirStars {
patternDirs = patternDirs[2:]
Expand Down

0 comments on commit 61ccee8

Please sign in to comment.