Skip to content

Commit

Permalink
Don't use named return values in layerStore.Put
Browse files Browse the repository at this point in the history
We will need want to refer to "layer" in a defer
block, in order to delete that layer. That doesn't work
with "layer" being a named return value, because a
(return nil, -1, ...) sets "layer" to nil.

So, turn "layer" into a local variable, and use an unnamed
return value. And beause all return values must be named,
or unnamed, consistently, turn "size" and "err" also into
local variables.

Then decrease the scope of the "size" and "err" local variables
to simplify understanding the code a bit.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Feb 23, 2022
1 parent c9e4aa1 commit 63edda6
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions layers.go
Expand Up @@ -683,11 +683,10 @@ func (r *layerStore) PutAdditionalLayer(id string, parentLayer *Layer, names []s
return copyLayer(layer), nil
}

func (r *layerStore) Put(id string, parentLayer *Layer, names []string, mountLabel string, options map[string]string, moreOptions *LayerOptions, writeable bool, flags map[string]interface{}, diff io.Reader) (layer *Layer, size int64, err error) {
func (r *layerStore) Put(id string, parentLayer *Layer, names []string, mountLabel string, options map[string]string, moreOptions *LayerOptions, writeable bool, flags map[string]interface{}, diff io.Reader) (*Layer, int64, error) {
if !r.IsReadWrite() {
return nil, -1, errors.Wrapf(ErrStoreIsReadOnly, "not allowed to create new layers at %q", r.layerspath())
}
size = -1
if err := os.MkdirAll(r.rundir, 0700); err != nil {
return nil, -1, err
}
Expand Down Expand Up @@ -740,24 +739,24 @@ func (r *layerStore) Put(id string, parentLayer *Layer, names []string, mountLab
IDMappings: idMappings,
}
if moreOptions.TemplateLayer != "" {
if err = r.driver.CreateFromTemplate(id, moreOptions.TemplateLayer, templateIDMappings, parent, parentMappings, &opts, writeable); err != nil {
if err := r.driver.CreateFromTemplate(id, moreOptions.TemplateLayer, templateIDMappings, parent, parentMappings, &opts, writeable); err != nil {
return nil, -1, errors.Wrapf(err, "error creating copy of template layer %q with ID %q", moreOptions.TemplateLayer, id)
}
oldMappings = templateIDMappings
} else {
if writeable {
if err = r.driver.CreateReadWrite(id, parent, &opts); err != nil {
if err := r.driver.CreateReadWrite(id, parent, &opts); err != nil {
return nil, -1, errors.Wrapf(err, "error creating read-write layer with ID %q", id)
}
} else {
if err = r.driver.Create(id, parent, &opts); err != nil {
if err := r.driver.Create(id, parent, &opts); err != nil {
return nil, -1, errors.Wrapf(err, "error creating layer with ID %q", id)
}
}
oldMappings = parentMappings
}
if !reflect.DeepEqual(oldMappings.UIDs(), idMappings.UIDs()) || !reflect.DeepEqual(oldMappings.GIDs(), idMappings.GIDs()) {
if err = r.driver.UpdateLayerIDMap(id, oldMappings, idMappings, mountLabel); err != nil {
if err := r.driver.UpdateLayerIDMap(id, oldMappings, idMappings, mountLabel); err != nil {
// We don't have a record of this layer, but at least
// try to clean it up underneath us.
if err2 := r.driver.Remove(id); err2 != nil {
Expand All @@ -767,7 +766,7 @@ func (r *layerStore) Put(id string, parentLayer *Layer, names []string, mountLab
}
}

layer = &Layer{
layer := &Layer{
ID: id,
Parent: parent,
Names: names,
Expand All @@ -788,7 +787,7 @@ func (r *layerStore) Put(id string, parentLayer *Layer, names []string, mountLab
layer.Flags[flag] = value
}
layer.Flags[incompleteFlag] = true
err = r.Save()
err := r.Save()
if err != nil {
// We don't have a presistent record of this layer, but
// try to remove both the driver’s data as well as
Expand All @@ -798,6 +797,7 @@ func (r *layerStore) Put(id string, parentLayer *Layer, names []string, mountLab
}
return nil, -1, err
}
var size int64 = -1
if diff != nil {
size, err = r.applyDiffWithOptions(layer.ID, moreOptions, diff)
if err != nil {
Expand Down

0 comments on commit 63edda6

Please sign in to comment.