Skip to content

Commit

Permalink
Use ToSlash / FromSlash to normalize filepaths
Browse files Browse the repository at this point in the history
  • Loading branch information
Racer159 committed Oct 31, 2023
1 parent d7affc8 commit d8cf223
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
14 changes: 9 additions & 5 deletions src/pkg/layout/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ func (pp *PackagePaths) SetFromLayers(layers []ocispec.Descriptor) {
// SetFromPaths maps paths to package paths.
func (pp *PackagePaths) SetFromPaths(paths []string) {
for _, rel := range paths {
switch path := rel; {
// Convert from the standard '/' to the OS path separator for Windows support
switch path := filepath.FromSlash(rel); {
case path == ZarfYAML:
pp.ZarfYAML = filepath.Join(pp.Base, path)
case path == Signature:
Expand All @@ -186,11 +187,11 @@ func (pp *PackagePaths) SetFromPaths(paths []string) {
pp.Checksums = filepath.Join(pp.Base, path)
case path == SBOMTar:
pp.SBOMs.Path = filepath.Join(pp.Base, path)
case path == strings.Join([]string{ImagesDir, OCILayout}, "/"):
case path == filepath.Join(ImagesDir, OCILayout):
pp.Images.OCILayout = filepath.Join(pp.Base, path)
case path == strings.Join([]string{ImagesDir, IndexJSON}, "/"):
case path == filepath.Join(ImagesDir, IndexJSON):
pp.Images.Index = filepath.Join(pp.Base, path)
case strings.HasPrefix(path, strings.Join([]string{ImagesDir, "blobs", "sha256"}, "/")):
case strings.HasPrefix(path, filepath.Join(ImagesDir, "blobs", "sha256")):
if pp.Images.Base == "" {
pp.Images.Base = filepath.Join(pp.Base, ImagesDir)
}
Expand All @@ -213,16 +214,19 @@ func (pp *PackagePaths) SetFromPaths(paths []string) {
// Files returns a map of all the files in the package.
func (pp *PackagePaths) Files() map[string]string {
pathMap := make(map[string]string)

stripBase := func(path string) string {
rel, _ := filepath.Rel(pp.Base, path)
return rel
return filepath.ToSlash(rel)
}

add := func(path string) {
if path == "" {
return
}
pathMap[stripBase(path)] = path
}

add(pp.ZarfYAML)
add(pp.Signature)
add(pp.Checksums)
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/oci/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func NewZarfOCIManifest(manifest *ocispec.Manifest) *ZarfOCIManifest {
}

// Locate returns the descriptor for the first layer with the given path or digest.
func (m *ZarfOCIManifest) Locate(uri string) ocispec.Descriptor {
func (m *ZarfOCIManifest) Locate(pathOrDigest string) ocispec.Descriptor {
return helpers.Find(m.Layers, func(layer ocispec.Descriptor) bool {
return layer.Annotations[ocispec.AnnotationTitle] == uri || layer.Digest.Encoded() == uri
return layer.Annotations[ocispec.AnnotationTitle] == filepath.ToSlash(pathOrDigest) || layer.Digest.Encoded() == pathOrDigest
})
}

Expand Down
11 changes: 9 additions & 2 deletions src/pkg/oci/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ var (

// FileDescriptorExists returns true if the given file exists in the given directory with the expected SHA.
func (o *OrasRemote) FileDescriptorExists(desc ocispec.Descriptor, destinationDir string) bool {
destinationPath := filepath.Join(destinationDir, desc.Annotations[ocispec.AnnotationTitle])
// Convert from the standard '/' to the OS path separator for Windows support
rel := filepath.FromSlash(desc.Annotations[ocispec.AnnotationTitle])
destinationPath := filepath.Join(destinationDir, rel)

info, err := os.Stat(destinationPath)
if err != nil {
return false
Expand Down Expand Up @@ -250,7 +253,11 @@ func (o *OrasRemote) PullLayer(desc ocispec.Descriptor, destinationDir string) e
if err != nil {
return err
}
return utils.WriteFile(filepath.Join(destinationDir, desc.Annotations[ocispec.AnnotationTitle]), b)

// Convert from the standard '/' to the OS path separator for Windows support
rel := filepath.FromSlash(desc.Annotations[ocispec.AnnotationTitle])

return utils.WriteFile(filepath.Join(destinationDir, rel), b)
}

// PullPackagePaths pulls multiple files from the remote repository and saves them to `destinationDir`.
Expand Down
4 changes: 4 additions & 0 deletions src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ func (p *Packager) generatePackageChecksums() (string, error) {
if rel == layout.ZarfYAML || rel == layout.Checksums {
continue
}

// Convert from the OS path separator to '/' for Windows support
rel = filepath.ToSlash(rel)

sum, err := utils.GetSHA256OfFile(abs)
if err != nil {
return "", err
Expand Down
5 changes: 4 additions & 1 deletion src/pkg/packager/sources/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ func ValidatePackageIntegrity(loaded *layout.PackagePaths, aggregateChecksum str
err = lineByLine(checksumPath, func(line string) error {
split := strings.Split(line, " ")
sha := split[0]
rel := split[1]

// Convert from the standard '/' to the OS path separator for Windows support
rel := filepath.FromSlash(split[1])

if sha == "" || rel == "" {
return fmt.Errorf("invalid checksum line: %s", line)
}
Expand Down

0 comments on commit d8cf223

Please sign in to comment.