volume/local.New(): extract loading options to a function

Note that Windows does not support options, so strictly doesn't need
to have this code, but keeping it in case we're adding support.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-10-08 00:54:40 +02:00
parent 7e907e29a3
commit cd58d11b2a
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 22 additions and 10 deletions

View File

@ -83,16 +83,8 @@ func New(scope string, rootIdentity idtools.Identity) (*Root, error) {
// unclean shutdown). This is a no-op on windows
unmount(v.path)
if b, err := os.ReadFile(filepath.Join(v.rootPath, "opts.json")); err == nil {
opts := optsConfig{}
if err := json.Unmarshal(b, &opts); err != nil {
return nil, errors.Wrapf(err, "error while unmarshaling volume options for volume: %s", name)
}
// Make sure this isn't an empty optsConfig.
// This could be empty due to buggy behavior in older versions of Docker.
if !reflect.DeepEqual(opts, optsConfig{}) {
v.opts = &opts
}
if err := v.loadOpts(); err != nil {
return nil, err
}
r.volumes[name] = v
}
@ -344,6 +336,26 @@ func (v *localVolume) Status() map[string]interface{} {
return nil
}
func (v *localVolume) loadOpts() error {
b, err := os.ReadFile(filepath.Join(v.rootPath, "opts.json"))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
logrus.WithError(err).Warnf("error while loading volume options for volume: %s", v.name)
}
return nil
}
opts := optsConfig{}
if err := json.Unmarshal(b, &opts); err != nil {
return errors.Wrapf(err, "error while unmarshaling volume options for volume: %s", v.name)
}
// Make sure this isn't an empty optsConfig.
// This could be empty due to buggy behavior in older versions of Docker.
if !reflect.DeepEqual(opts, optsConfig{}) {
v.opts = &opts
}
return nil
}
func (v *localVolume) saveOpts() error {
var b []byte
b, err := json.Marshal(v.opts)