Fix oci.hasPrefix to operate correctly on Windows

Due to use of filepath.Clean(), on Windows we end up operating on the
paths in Windows-slashed form, even if the inputs were Unix-slashed.

Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
v0.8
Paul "TBBle" Hampson 2020-07-28 03:56:33 +10:00
parent 13bf35d640
commit 9fd4d731d6
2 changed files with 42 additions and 2 deletions

View File

@ -76,11 +76,11 @@ func withROBind(src, dest string) func(m []specs.Mount) ([]specs.Mount, error) {
func hasPrefix(p, prefixDir string) bool {
prefixDir = filepath.Clean(prefixDir)
if prefixDir == "/" {
if filepath.Base(prefixDir) == string(filepath.Separator) {
return true
}
p = filepath.Clean(p)
return p == prefixDir || strings.HasPrefix(p, prefixDir+"/")
return p == prefixDir || strings.HasPrefix(p, prefixDir+string(filepath.Separator))
}
func removeMountsWithPrefix(mounts []specs.Mount, prefixDir string) []specs.Mount {

View File

@ -1,6 +1,7 @@
package oci
import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
@ -49,6 +50,45 @@ func TestHasPrefix(t *testing.T) {
expected: false,
},
}
if runtime.GOOS == "windows" {
testCases = append(testCases,
testCase{
path: "C:\\foo\\bar",
prefix: "C:\\foo",
expected: true,
},
testCase{
path: "C:\\foo\\bar",
prefix: "C:\\foo\\",
expected: true,
},
testCase{
path: "C:\\foo\\bar",
prefix: "C:\\",
expected: true,
},
testCase{
path: "C:\\foo",
prefix: "C:\\foo",
expected: true,
},
testCase{
path: "C:\\foo\\bar",
prefix: "C:\\bar",
expected: false,
},
testCase{
path: "C:\\foo\\bar",
prefix: "foo",
expected: false,
},
testCase{
path: "C:\\foobar",
prefix: "C:\\foo",
expected: false,
},
)
}
for i, tc := range testCases {
actual := hasPrefix(tc.path, tc.prefix)
assert.Equal(t, tc.expected, actual, "#%d: under(%q,%q)", i, tc.path, tc.prefix)