From cd58d11b2ab10131b9ab8e835c39ffd6e53917e3 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 8 Oct 2020 00:54:40 +0200 Subject: [PATCH] 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 --- volume/local/local.go | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/volume/local/local.go b/volume/local/local.go index caf58e5ac9..aca3871b2e 100644 --- a/volume/local/local.go +++ b/volume/local/local.go @@ -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)