Skip to content

Commit b00ed51

Browse files
authored
Merge pull request #472 from q384566678/windows-root-path
validate: add root.path validation when platform is windows
2 parents cee692b + 1a9532e commit b00ed51

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

generate/generate.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -1089,11 +1089,7 @@ func (g *Generator) DropProcessCapability(c string) error {
10891089
}
10901090
}
10911091

1092-
if err := validate.CapValid(cp, false); err != nil {
1093-
return err
1094-
}
1095-
1096-
return nil
1092+
return validate.CapValid(cp, false)
10971093
}
10981094

10991095
func mapStrToNamespace(ns string, path string) (rspec.LinuxNamespace, error) {

validate/validate.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ func (v *Validator) CheckRoot() (errs error) {
178178
return
179179
}
180180

181+
if v.platform == "windows" {
182+
matched, err := regexp.MatchString(`\\\\[?]\\Volume[{][a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}[}]\\`, v.spec.Root.Path)
183+
if err != nil {
184+
errs = multierror.Append(errs, err)
185+
} else if !matched {
186+
errs = multierror.Append(errs,
187+
specerror.NewError(specerror.PathFormatOnWindows, fmt.Errorf("root.path is %q, but it MUST be a volume GUID path when target platform is windows", v.spec.Root.Path), rspec.Version))
188+
}
189+
190+
if v.spec.Root.Readonly {
191+
errs = multierror.Append(errs,
192+
specerror.NewError(specerror.ReadonlyOnWindows, fmt.Errorf("root.readonly field MUST be omitted or false when target platform is windows"), rspec.Version))
193+
}
194+
195+
return
196+
}
197+
181198
absBundlePath, err := filepath.Abs(v.bundlePath)
182199
if err != nil {
183200
errs = multierror.Append(errs, fmt.Errorf("unable to convert %q to an absolute path", v.bundlePath))
@@ -218,13 +235,6 @@ func (v *Validator) CheckRoot() (errs error) {
218235
specerror.NewError(specerror.ArtifactsInSingleDir, fmt.Errorf("root.path is %q, but it MUST be a child of %q", v.spec.Root.Path, absBundlePath), rspec.Version))
219236
}
220237

221-
if v.platform == "windows" {
222-
if v.spec.Root.Readonly {
223-
errs = multierror.Append(errs,
224-
specerror.NewError(specerror.ReadonlyOnWindows, fmt.Errorf("root.readonly field MUST be omitted or false when target platform is windows"), rspec.Version))
225-
}
226-
}
227-
228238
return
229239
}
230240

validate/validate_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func TestCheckRoot(t *testing.T) {
119119
}{
120120
{rspec.Spec{Windows: &rspec.Windows{HyperV: &rspec.WindowsHyperV{}}, Root: &rspec.Root{}}, "windows", specerror.RootOnHyperV},
121121
{rspec.Spec{Windows: &rspec.Windows{HyperV: &rspec.WindowsHyperV{}}, Root: nil}, "windows", specerror.NonError},
122+
{rspec.Spec{Windows: &rspec.Windows{}, Root: &rspec.Root{Path: filepath.Join(tmpBundle, "rootfs")}}, "windows", specerror.PathFormatOnWindows},
123+
{rspec.Spec{Windows: &rspec.Windows{}, Root: &rspec.Root{Path: "\\\\?\\Volume{ec84d99e-3f02-11e7-ac6c-00155d7682cf}\\"}}, "windows", specerror.NonError},
122124
{rspec.Spec{Root: nil}, "linux", specerror.RootOnNonHyperV},
123125
{rspec.Spec{Root: &rspec.Root{Path: "maverick-rootfs"}}, "linux", specerror.PathName},
124126
{rspec.Spec{Root: &rspec.Root{Path: "rootfs"}}, "linux", specerror.NonError},

0 commit comments

Comments
 (0)