volume/local: make "validateOpts()" a method on Root

This way we can validate if Root supports quotaCtl, allowing us to
fail early, before creating any of the directories.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-05-17 15:54:17 +02:00
parent 29c6224fe9
commit e106e3f5c6
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
3 changed files with 38 additions and 35 deletions

View file

@ -136,7 +136,7 @@ func (r *Root) Create(name string, opts map[string]string) (volume.Volume, error
if err := r.validateName(name); err != nil {
return nil, err
}
if err := validateOpts(opts); err != nil {
if err := r.validateOpts(opts); err != nil {
return nil, err
}

View file

@ -47,6 +47,36 @@ func (o *optsConfig) String() string {
return fmt.Sprintf("type='%s' device='%s' o='%s' size='%d'", o.MountType, o.MountDevice, o.MountOpts, o.Quota.Size)
}
func (r *Root) validateOpts(opts map[string]string) error {
if len(opts) == 0 {
return nil
}
for opt := range opts {
if _, ok := validOpts[opt]; !ok {
return errdefs.InvalidParameter(errors.Errorf("invalid option: %q", opt))
}
}
if val, ok := opts["size"]; ok {
size, err := units.RAMInBytes(val)
if err != nil {
return errdefs.InvalidParameter(err)
}
if size > 0 && r.quotaCtl == nil {
return errdefs.InvalidParameter(errors.New("quota size requested but no quota support"))
}
}
for opt, reqopts := range mandatoryOpts {
if _, ok := opts[opt]; ok {
for _, reqopt := range reqopts {
if _, ok := opts[reqopt]; !ok {
return errdefs.InvalidParameter(errors.Errorf("missing required option: %q", reqopt))
}
}
}
}
return nil
}
func (v *localVolume) setOpts(opts map[string]string) error {
if len(opts) == 0 {
return nil
@ -69,33 +99,6 @@ func (v *localVolume) setOpts(opts map[string]string) error {
return nil
}
func validateOpts(opts map[string]string) error {
if len(opts) == 0 {
return nil
}
for opt := range opts {
if _, ok := validOpts[opt]; !ok {
return errdefs.InvalidParameter(errors.Errorf("invalid option: %q", opt))
}
}
if val, ok := opts["size"]; ok {
_, err := units.RAMInBytes(val)
if err != nil {
return errdefs.InvalidParameter(err)
}
}
for opt, reqopts := range mandatoryOpts {
if _, ok := opts[opt]; ok {
for _, reqopt := range reqopts {
if _, ok := opts[reqopt]; !ok {
return errdefs.InvalidParameter(errors.Errorf("missing required option: %q", reqopt))
}
}
}
}
return nil
}
func unmount(path string) {
_ = mount.Unmount(path)
}

View file

@ -14,15 +14,15 @@ import (
type optsConfig struct{}
func (v *localVolume) setOpts(opts map[string]string) error {
// Windows does not support any options currently
return nil
func (r *Root) validateOpts(opts map[string]string) error {
if len(opts) == 0 {
return nil
}
return errdefs.InvalidParameter(errors.New("options are not supported on this platform"))
}
func validateOpts(opts map[string]string) error {
if len(opts) > 0 {
return errdefs.InvalidParameter(errors.New("options are not supported on this platform"))
}
func (v *localVolume) setOpts(opts map[string]string) error {
// Windows does not support any options currently
return nil
}