Skip to content

Commit 567f1aa

Browse files
author
zhouhao
committed
validate: add root.path validation when platform is windows
Signed-off-by: zhouhao <[email protected]>
1 parent 4882fc1 commit 567f1aa

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

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
@@ -114,6 +114,8 @@ func TestCheckRoot(t *testing.T) {
114114
}{
115115
{rspec.Spec{Windows: &rspec.Windows{HyperV: &rspec.WindowsHyperV{}}, Root: &rspec.Root{}}, "windows", specerror.RootOnHyperV},
116116
{rspec.Spec{Windows: &rspec.Windows{HyperV: &rspec.WindowsHyperV{}}, Root: nil}, "windows", specerror.NonError},
117+
{rspec.Spec{Windows: &rspec.Windows{}, Root: &rspec.Root{Path: filepath.Join(tmpBundle, "rootfs")}}, "windows", specerror.PathFormatOnWindows},
118+
{rspec.Spec{Windows: &rspec.Windows{}, Root: &rspec.Root{Path: "\\\\?\\Volume{ec84d99e-3f02-11e7-ac6c-00155d7682cf}\\"}}, "windows", specerror.NonError},
117119
{rspec.Spec{Root: nil}, "linux", specerror.RootOnNonHyperV},
118120
{rspec.Spec{Root: &rspec.Root{Path: "maverick-rootfs"}}, "linux", specerror.PathName},
119121
{rspec.Spec{Root: &rspec.Root{Path: "rootfs"}}, "linux", specerror.NonError},

0 commit comments

Comments
 (0)