Skip to content

Commit

Permalink
bake: handle path evaluation with multiple drives for windows entitle…
Browse files Browse the repository at this point in the history
…ment

Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Nov 21, 2024
1 parent a2adbe0 commit 11e9bd2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
18 changes: 1 addition & 17 deletions bake/entitlements.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func isParentOrEqualPath(p, parent string) bool {
if p == parent || parent == "/" {
return true
}
if strings.HasPrefix(p, parent+string(filepath.Separator)) {
if strings.HasPrefix(p, filepath.Clean(parent+string(filepath.Separator))) {
return true
}
return false
Expand Down Expand Up @@ -441,22 +441,6 @@ func removeCommonPaths(in, common []string) []string {
return filtered
}

func evaluatePaths(in []string) ([]string, error) {
out := make([]string, 0, len(in))
for _, p := range in {
v, err := filepath.Abs(p)
if err != nil {
return nil, err
}
v, err = filepath.EvalSymlinks(v)
if err != nil {
return nil, errors.Wrapf(err, "failed to evaluate path %q", p)
}
out = append(out, v)
}
return out, nil
}

func evaluateToExistingPaths(in map[string]struct{}) (map[string]struct{}, error) {
m := make(map[string]struct{}, len(in))
for p := range in {
Expand Down
26 changes: 26 additions & 0 deletions bake/entitlements_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build !windows
// +build !windows

package bake

import (
"path/filepath"

"github.com/pkg/errors"
)

func evaluatePaths(in []string) ([]string, error) {
out := make([]string, 0, len(in))
for _, p := range in {
v, err := filepath.Abs(p)
if err != nil {
return nil, err
}
v, err = filepath.EvalSymlinks(v)
if err != nil {
return nil, errors.Wrapf(err, "failed to evaluate path %q", p)
}
out = append(out, v)
}
return out, nil
}
39 changes: 39 additions & 0 deletions bake/entitlements_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package bake

import (
"os"
"path/filepath"

"github.com/pkg/errors"
)

func evaluatePaths(in []string) ([]string, error) {
out := make([]string, 0, len(in))
for _, p := range in {
if p == "/" {
out = append(out, getAllVolumes()...)
continue
}
v, err := filepath.Abs(p)
if err != nil {
return nil, err
}
v, err = filepath.EvalSymlinks(v)
if err != nil {
return nil, errors.Wrapf(err, "failed to evaluate path %q", p)
}
out = append(out, v)
}
return out, nil
}

func getAllVolumes() []string {
var volumes []string
for _, drive := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" {
p := string(drive) + ":" + string(filepath.Separator)
if _, err := os.Stat(p); !os.IsNotExist(err) {
volumes = append(volumes, p)
}
}
return volumes
}

0 comments on commit 11e9bd2

Please sign in to comment.