github.com/cdoern/storage@v1.12.13/drivers/template.go (about)

     1  package graphdriver
     2  
     3  import (
     4  	"github.com/sirupsen/logrus"
     5  
     6  	"github.com/containers/storage/pkg/idtools"
     7  )
     8  
     9  // TemplateDriver is just barely enough of a driver that we can implement a
    10  // naive version of CreateFromTemplate on top of it.
    11  type TemplateDriver interface {
    12  	DiffDriver
    13  	CreateReadWrite(id, parent string, opts *CreateOpts) error
    14  	Create(id, parent string, opts *CreateOpts) error
    15  	Remove(id string) error
    16  }
    17  
    18  // CreateFromTemplate creates a layer with the same contents and parent as
    19  // another layer.  Internally, it may even depend on that other layer
    20  // continuing to exist, as if it were actually a child of the child layer.
    21  func NaiveCreateFromTemplate(d TemplateDriver, id, template string, templateIDMappings *idtools.IDMappings, parent string, parentIDMappings *idtools.IDMappings, opts *CreateOpts, readWrite bool) error {
    22  	var err error
    23  	if readWrite {
    24  		err = d.CreateReadWrite(id, parent, opts)
    25  	} else {
    26  		err = d.Create(id, parent, opts)
    27  	}
    28  	if err != nil {
    29  		return err
    30  	}
    31  	diff, err := d.Diff(template, templateIDMappings, parent, parentIDMappings, opts.MountLabel)
    32  	if err != nil {
    33  		if err2 := d.Remove(id); err2 != nil {
    34  			logrus.Errorf("error removing layer %q: %v", id, err2)
    35  		}
    36  		return err
    37  	}
    38  	if _, err = d.ApplyDiff(id, templateIDMappings, parent, opts.MountLabel, diff); err != nil {
    39  		if err2 := d.Remove(id); err2 != nil {
    40  			logrus.Errorf("error removing layer %q: %v", id, err2)
    41  		}
    42  		return err
    43  	}
    44  	return nil
    45  }