2016-11-22 19:32:10 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
drivers "github.com/containers/storage/drivers"
|
|
|
|
"github.com/containers/storage/pkg/archive"
|
|
|
|
"github.com/containers/storage/pkg/ioutils"
|
|
|
|
"github.com/containers/storage/pkg/stringid"
|
2017-04-27 17:59:23 +00:00
|
|
|
"github.com/containers/storage/pkg/truncindex"
|
2017-07-20 20:31:51 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
2016-11-22 19:32:10 +00:00
|
|
|
"github.com/vbatts/tar-split/tar/asm"
|
|
|
|
"github.com/vbatts/tar-split/tar/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tarSplitSuffix = ".tar-split.gz"
|
|
|
|
incompleteFlag = "incomplete"
|
|
|
|
compressionFlag = "diff-compression"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Layer is a record of a copy-on-write layer that's stored by the lower
|
|
|
|
// level graph driver.
|
|
|
|
type Layer struct {
|
|
|
|
// ID is either one which was specified at create-time, or a random
|
|
|
|
// value which was generated by the library.
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
|
|
|
// Names is an optional set of user-defined convenience values. The
|
|
|
|
// layer can be referred to by its ID or any of its names. Names are
|
|
|
|
// unique among layers.
|
|
|
|
Names []string `json:"names,omitempty"`
|
|
|
|
|
|
|
|
// Parent is the ID of a layer from which this layer inherits data.
|
|
|
|
Parent string `json:"parent,omitempty"`
|
|
|
|
|
|
|
|
// Metadata is data we keep for the convenience of the caller. It is not
|
|
|
|
// expected to be large, since it is kept in memory.
|
|
|
|
Metadata string `json:"metadata,omitempty"`
|
|
|
|
|
|
|
|
// MountLabel is an SELinux label which should be used when attempting to mount
|
|
|
|
// the layer.
|
|
|
|
MountLabel string `json:"mountlabel,omitempty"`
|
|
|
|
|
|
|
|
// MountPoint is the path where the layer is mounted, or where it was most
|
|
|
|
// recently mounted. This can change between subsequent Unmount() and
|
|
|
|
// Mount() calls, so the caller should consult this value after Mount()
|
|
|
|
// succeeds to find the location of the container's root filesystem.
|
|
|
|
MountPoint string `json:"-"`
|
|
|
|
|
|
|
|
// MountCount is used as a reference count for the container's layer being
|
|
|
|
// mounted at the mount point.
|
|
|
|
MountCount int `json:"-"`
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Created is the datestamp for when this layer was created. Older
|
|
|
|
// versions of the library did not track this information, so callers
|
|
|
|
// will likely want to use the IsZero() method to verify that a value
|
|
|
|
// is set before using it.
|
|
|
|
Created time.Time `json:"created,omitempty"`
|
|
|
|
|
|
|
|
// CompressedDigest is the digest of the blob that was last passed to
|
|
|
|
// ApplyDiff() or Put(), as it was presented to us.
|
|
|
|
CompressedDigest digest.Digest `json:"compressed-diff-digest,omitempty"`
|
|
|
|
|
|
|
|
// CompressedSize is the length of the blob that was last passed to
|
|
|
|
// ApplyDiff() or Put(), as it was presented to us. If
|
|
|
|
// CompressedDigest is not set, this should be treated as if it were an
|
|
|
|
// uninitialized value.
|
|
|
|
CompressedSize int64 `json:"compressed-size,omitempty"`
|
|
|
|
|
|
|
|
// UncompressedDigest is the digest of the blob that was last passed to
|
|
|
|
// ApplyDiff() or Put(), after we decompressed it. Often referred to
|
|
|
|
// as a DiffID.
|
|
|
|
UncompressedDigest digest.Digest `json:"diff-digest,omitempty"`
|
|
|
|
|
|
|
|
// UncompressedSize is the length of the blob that was last passed to
|
|
|
|
// ApplyDiff() or Put(), after we decompressed it. If
|
|
|
|
// UncompressedDigest is not set, this should be treated as if it were
|
|
|
|
// an uninitialized value.
|
|
|
|
UncompressedSize int64 `json:"diff-size,omitempty"`
|
|
|
|
|
|
|
|
// CompressionType is the type of compression which we detected on the blob
|
|
|
|
// that was last passed to ApplyDiff() or Put().
|
|
|
|
CompressionType archive.Compression `json:"compression,omitempty"`
|
|
|
|
|
|
|
|
// Flags is arbitrary data about the layer.
|
2016-11-22 19:32:10 +00:00
|
|
|
Flags map[string]interface{} `json:"flags,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type layerMountPoint struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
MountPoint string `json:"path"`
|
|
|
|
MountCount int `json:"count"`
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// DiffOptions override the default behavior of Diff() methods.
|
|
|
|
type DiffOptions struct {
|
|
|
|
// Compression, if set overrides the default compressor when generating a diff.
|
|
|
|
Compression *archive.Compression
|
|
|
|
}
|
|
|
|
|
|
|
|
// ROLayerStore wraps a graph driver, adding the ability to refer to layers by
|
|
|
|
// name, and keeping track of parent-child relationships, along with a list of
|
|
|
|
// all known layers.
|
|
|
|
type ROLayerStore interface {
|
|
|
|
ROFileBasedStore
|
|
|
|
ROMetadataStore
|
|
|
|
|
|
|
|
// Exists checks if a layer with the specified name or ID is known.
|
|
|
|
Exists(id string) bool
|
|
|
|
|
|
|
|
// Get retrieves information about a layer given an ID or name.
|
|
|
|
Get(id string) (*Layer, error)
|
|
|
|
|
|
|
|
// Status returns an slice of key-value pairs, suitable for human consumption,
|
|
|
|
// relaying whatever status information the underlying driver can share.
|
|
|
|
Status() ([][2]string, error)
|
|
|
|
|
|
|
|
// Changes returns a slice of Change structures, which contain a pathname
|
|
|
|
// (Path) and a description of what sort of change (Kind) was made by the
|
|
|
|
// layer (either ChangeModify, ChangeAdd, or ChangeDelete), relative to a
|
|
|
|
// specified layer. By default, the layer's parent is used as a reference.
|
|
|
|
Changes(from, to string) ([]archive.Change, error)
|
|
|
|
|
|
|
|
// Diff produces a tarstream which can be applied to a layer with the contents
|
|
|
|
// of the first layer to produce a layer with the contents of the second layer.
|
|
|
|
// By default, the parent of the second layer is used as the first
|
|
|
|
// layer, so it need not be specified. Options can be used to override
|
|
|
|
// default behavior, but are also not required.
|
|
|
|
Diff(from, to string, options *DiffOptions) (io.ReadCloser, error)
|
|
|
|
|
|
|
|
// DiffSize produces an estimate of the length of the tarstream which would be
|
|
|
|
// produced by Diff.
|
|
|
|
DiffSize(from, to string) (int64, error)
|
|
|
|
|
|
|
|
// Size produces a cached value for the uncompressed size of the layer,
|
|
|
|
// if one is known, or -1 if it is not known. If the layer can not be
|
|
|
|
// found, it returns an error.
|
|
|
|
Size(name string) (int64, error)
|
|
|
|
|
|
|
|
// Lookup attempts to translate a name to an ID. Most methods do this
|
|
|
|
// implicitly.
|
|
|
|
Lookup(name string) (string, error)
|
|
|
|
|
|
|
|
// LayersByCompressedDigest returns a slice of the layers with the
|
|
|
|
// specified compressed digest value recorded for them.
|
|
|
|
LayersByCompressedDigest(d digest.Digest) ([]Layer, error)
|
|
|
|
|
|
|
|
// LayersByUncompressedDigest returns a slice of the layers with the
|
|
|
|
// specified uncompressed digest value recorded for them.
|
|
|
|
LayersByUncompressedDigest(d digest.Digest) ([]Layer, error)
|
|
|
|
|
|
|
|
// Layers returns a slice of the known layers.
|
|
|
|
Layers() ([]Layer, error)
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
// LayerStore wraps a graph driver, adding the ability to refer to layers by
|
|
|
|
// name, and keeping track of parent-child relationships, along with a list of
|
|
|
|
// all known layers.
|
|
|
|
type LayerStore interface {
|
2017-07-20 20:31:51 +00:00
|
|
|
ROLayerStore
|
|
|
|
RWFileBasedStore
|
|
|
|
RWMetadataStore
|
2016-11-22 19:32:10 +00:00
|
|
|
FlaggableStore
|
|
|
|
|
|
|
|
// Create creates a new layer, optionally giving it a specified ID rather than
|
|
|
|
// a randomly-generated one, either inheriting data from another specified
|
|
|
|
// layer or the empty base layer. The new layer can optionally be given names
|
|
|
|
// and have an SELinux label specified for use when mounting it. Some
|
|
|
|
// underlying drivers can accept a "size" option. At this time, most
|
|
|
|
// underlying drivers do not themselves distinguish between writeable
|
|
|
|
// and read-only layers.
|
|
|
|
Create(id, parent string, names []string, mountLabel string, options map[string]string, writeable bool) (*Layer, error)
|
|
|
|
|
|
|
|
// CreateWithFlags combines the functions of Create and SetFlag.
|
|
|
|
CreateWithFlags(id, parent string, names []string, mountLabel string, options map[string]string, writeable bool, flags map[string]interface{}) (layer *Layer, err error)
|
|
|
|
|
|
|
|
// Put combines the functions of CreateWithFlags and ApplyDiff.
|
2017-09-12 23:29:24 +00:00
|
|
|
Put(id, parent string, names []string, mountLabel string, options map[string]string, writeable bool, flags map[string]interface{}, diff io.Reader) (*Layer, int64, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
// SetNames replaces the list of names associated with a layer with the
|
|
|
|
// supplied values.
|
|
|
|
SetNames(id string, names []string) error
|
|
|
|
|
|
|
|
// Delete deletes a layer with the specified name or ID.
|
|
|
|
Delete(id string) error
|
|
|
|
|
|
|
|
// Wipe deletes all layers.
|
|
|
|
Wipe() error
|
|
|
|
|
|
|
|
// Mount mounts a layer for use. If the specified layer is the parent of other
|
|
|
|
// layers, it should not be written to. An SELinux label to be applied to the
|
|
|
|
// mount can be specified to override the one configured for the layer.
|
|
|
|
Mount(id, mountLabel string) (string, error)
|
|
|
|
|
|
|
|
// Unmount unmounts a layer when it is no longer in use.
|
|
|
|
Unmount(id string) error
|
|
|
|
|
|
|
|
// ApplyDiff reads a tarstream which was created by a previous call to Diff and
|
|
|
|
// applies its changes to a specified layer.
|
2017-09-12 23:29:24 +00:00
|
|
|
ApplyDiff(to string, diff io.Reader) (int64, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type layerStore struct {
|
2017-07-20 20:31:51 +00:00
|
|
|
lockfile Locker
|
|
|
|
rundir string
|
|
|
|
driver drivers.Driver
|
|
|
|
layerdir string
|
|
|
|
layers []*Layer
|
|
|
|
idindex *truncindex.TruncIndex
|
|
|
|
byid map[string]*Layer
|
|
|
|
byname map[string]*Layer
|
|
|
|
bymount map[string]*Layer
|
|
|
|
bycompressedsum map[digest.Digest][]string
|
|
|
|
byuncompressedsum map[digest.Digest][]string
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Layers() ([]Layer, error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
layers := make([]Layer, len(r.layers))
|
|
|
|
for i := range r.layers {
|
|
|
|
layers[i] = *(r.layers[i])
|
|
|
|
}
|
|
|
|
return layers, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) mountspath() string {
|
|
|
|
return filepath.Join(r.rundir, "mountpoints.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) layerspath() string {
|
|
|
|
return filepath.Join(r.layerdir, "layers.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Load() error {
|
2017-07-20 20:31:51 +00:00
|
|
|
shouldSave := false
|
2016-11-22 19:32:10 +00:00
|
|
|
rpath := r.layerspath()
|
|
|
|
data, err := ioutil.ReadFile(rpath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
layers := []*Layer{}
|
2017-04-27 17:59:23 +00:00
|
|
|
idlist := []string{}
|
2016-11-22 19:32:10 +00:00
|
|
|
ids := make(map[string]*Layer)
|
|
|
|
names := make(map[string]*Layer)
|
|
|
|
mounts := make(map[string]*Layer)
|
2017-07-20 20:31:51 +00:00
|
|
|
compressedsums := make(map[digest.Digest][]string)
|
|
|
|
uncompressedsums := make(map[digest.Digest][]string)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err = json.Unmarshal(data, &layers); len(data) == 0 || err == nil {
|
|
|
|
for n, layer := range layers {
|
2017-07-20 20:31:51 +00:00
|
|
|
ids[layer.ID] = layers[n]
|
2017-04-27 17:59:23 +00:00
|
|
|
idlist = append(idlist, layer.ID)
|
2016-11-22 19:32:10 +00:00
|
|
|
for _, name := range layer.Names {
|
|
|
|
if conflict, ok := names[name]; ok {
|
|
|
|
r.removeName(conflict, name)
|
2017-07-20 20:31:51 +00:00
|
|
|
shouldSave = true
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
names[name] = layers[n]
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if layer.CompressedDigest != "" {
|
|
|
|
compressedsums[layer.CompressedDigest] = append(compressedsums[layer.CompressedDigest], layer.ID)
|
|
|
|
}
|
|
|
|
if layer.UncompressedDigest != "" {
|
|
|
|
uncompressedsums[layer.UncompressedDigest] = append(uncompressedsums[layer.UncompressedDigest], layer.ID)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if shouldSave && !r.IsReadWrite() {
|
2017-09-12 23:29:24 +00:00
|
|
|
return ErrDuplicateLayerNames
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
mpath := r.mountspath()
|
|
|
|
data, err = ioutil.ReadFile(mpath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
layerMounts := []layerMountPoint{}
|
|
|
|
if err = json.Unmarshal(data, &layerMounts); len(data) == 0 || err == nil {
|
|
|
|
for _, mount := range layerMounts {
|
|
|
|
if mount.MountPoint != "" {
|
|
|
|
if layer, ok := ids[mount.ID]; ok {
|
|
|
|
mounts[mount.MountPoint] = layer
|
|
|
|
layer.MountPoint = mount.MountPoint
|
|
|
|
layer.MountCount = mount.MountCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.layers = layers
|
2017-04-27 17:59:23 +00:00
|
|
|
r.idindex = truncindex.NewTruncIndex(idlist)
|
2016-11-22 19:32:10 +00:00
|
|
|
r.byid = ids
|
|
|
|
r.byname = names
|
|
|
|
r.bymount = mounts
|
2017-07-20 20:31:51 +00:00
|
|
|
r.bycompressedsum = compressedsums
|
|
|
|
r.byuncompressedsum = uncompressedsums
|
2016-11-22 19:32:10 +00:00
|
|
|
err = nil
|
2017-07-20 20:31:51 +00:00
|
|
|
// Last step: if we're writable, try to remove anything that a previous
|
|
|
|
// user of this storage area marked for deletion but didn't manage to
|
|
|
|
// actually delete.
|
|
|
|
if r.IsReadWrite() {
|
|
|
|
for _, layer := range r.layers {
|
|
|
|
if cleanup, ok := layer.Flags[incompleteFlag]; ok {
|
|
|
|
if b, ok := cleanup.(bool); ok && b {
|
|
|
|
err = r.Delete(layer.ID)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
shouldSave = true
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if shouldSave {
|
|
|
|
return r.Save()
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Save() error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to modify the layer store at %q", r.layerspath())
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
rpath := r.layerspath()
|
2017-03-28 19:56:09 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(rpath), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
jldata, err := json.Marshal(&r.layers)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mpath := r.mountspath()
|
2017-03-28 19:56:09 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(mpath), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
mounts := []layerMountPoint{}
|
|
|
|
for _, layer := range r.layers {
|
|
|
|
if layer.MountPoint != "" && layer.MountCount > 0 {
|
|
|
|
mounts = append(mounts, layerMountPoint{
|
|
|
|
ID: layer.ID,
|
|
|
|
MountPoint: layer.MountPoint,
|
|
|
|
MountCount: layer.MountCount,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jmdata, err := json.Marshal(&mounts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutils.AtomicWriteFile(rpath, jldata, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
defer r.Touch()
|
2016-11-22 19:32:10 +00:00
|
|
|
return ioutils.AtomicWriteFile(mpath, jmdata, 0600)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLayerStore(rundir string, layerdir string, driver drivers.Driver) (LayerStore, error) {
|
|
|
|
if err := os.MkdirAll(rundir, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(layerdir, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lockfile, err := GetLockfile(filepath.Join(layerdir, "layers.lock"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lockfile.Lock()
|
|
|
|
defer lockfile.Unlock()
|
|
|
|
rlstore := layerStore{
|
|
|
|
lockfile: lockfile,
|
|
|
|
driver: driver,
|
|
|
|
rundir: rundir,
|
|
|
|
layerdir: layerdir,
|
|
|
|
byid: make(map[string]*Layer),
|
|
|
|
bymount: make(map[string]*Layer),
|
|
|
|
byname: make(map[string]*Layer),
|
|
|
|
}
|
|
|
|
if err := rlstore.Load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &rlstore, nil
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func newROLayerStore(rundir string, layerdir string, driver drivers.Driver) (ROLayerStore, error) {
|
|
|
|
lockfile, err := GetROLockfile(filepath.Join(layerdir, "layers.lock"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lockfile.Lock()
|
|
|
|
defer lockfile.Unlock()
|
|
|
|
rlstore := layerStore{
|
|
|
|
lockfile: lockfile,
|
|
|
|
driver: driver,
|
|
|
|
rundir: rundir,
|
|
|
|
layerdir: layerdir,
|
|
|
|
byid: make(map[string]*Layer),
|
|
|
|
bymount: make(map[string]*Layer),
|
|
|
|
byname: make(map[string]*Layer),
|
|
|
|
}
|
|
|
|
if err := rlstore.Load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &rlstore, nil
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:59:23 +00:00
|
|
|
func (r *layerStore) lookup(id string) (*Layer, bool) {
|
|
|
|
if layer, ok := r.byid[id]; ok {
|
|
|
|
return layer, ok
|
|
|
|
} else if layer, ok := r.byname[id]; ok {
|
|
|
|
return layer, ok
|
|
|
|
} else if longid, err := r.idindex.Get(id); err == nil {
|
|
|
|
layer, ok := r.byid[longid]
|
|
|
|
return layer, ok
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (r *layerStore) Size(name string) (int64, error) {
|
|
|
|
layer, ok := r.lookup(name)
|
|
|
|
if !ok {
|
|
|
|
return -1, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
// We use the presence of a non-empty digest as an indicator that the size value was intentionally set, and that
|
|
|
|
// a zero value is not just present because it was never set to anything else (which can happen if the layer was
|
|
|
|
// created by a version of this library that didn't keep track of digest and size information).
|
|
|
|
if layer.UncompressedDigest != "" {
|
|
|
|
return layer.UncompressedSize, nil
|
|
|
|
}
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:59:23 +00:00
|
|
|
func (r *layerStore) ClearFlag(id string, flag string) error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to clear flags on layers at %q", r.layerspath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
layer, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return ErrLayerUnknown
|
|
|
|
}
|
|
|
|
delete(layer.Flags, flag)
|
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) SetFlag(id string, flag string, value interface{}) error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to set flags on layers at %q", r.layerspath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
layer, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return ErrLayerUnknown
|
|
|
|
}
|
|
|
|
layer.Flags[flag] = value
|
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Status() ([][2]string, error) {
|
|
|
|
return r.driver.Status(), nil
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
func (r *layerStore) Put(id, parent string, names []string, mountLabel string, options map[string]string, writeable bool, flags map[string]interface{}, diff io.Reader) (layer *Layer, size int64, err error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return nil, -1, errors.Wrapf(ErrStoreIsReadOnly, "not allowed to create new layers at %q", r.layerspath())
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
size = -1
|
|
|
|
if err := os.MkdirAll(r.rundir, 0700); err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(r.layerdir, 0700); err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
if parent != "" {
|
|
|
|
if parentLayer, ok := r.lookup(parent); ok {
|
|
|
|
parent = parentLayer.ID
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
id = stringid.GenerateRandomID()
|
|
|
|
_, idInUse := r.byid[id]
|
|
|
|
for idInUse {
|
|
|
|
id = stringid.GenerateRandomID()
|
|
|
|
_, idInUse = r.byid[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, idInUse := r.byid[id]; idInUse {
|
|
|
|
return nil, -1, ErrDuplicateID
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
if _, nameInUse := r.byname[name]; nameInUse {
|
|
|
|
return nil, -1, ErrDuplicateName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if writeable {
|
|
|
|
err = r.driver.CreateReadWrite(id, parent, mountLabel, options)
|
|
|
|
} else {
|
|
|
|
err = r.driver.Create(id, parent, mountLabel, options)
|
|
|
|
}
|
|
|
|
if err == nil {
|
2017-07-20 20:31:51 +00:00
|
|
|
layer = &Layer{
|
2016-11-22 19:32:10 +00:00
|
|
|
ID: id,
|
|
|
|
Parent: parent,
|
|
|
|
Names: names,
|
|
|
|
MountLabel: mountLabel,
|
2017-07-20 20:31:51 +00:00
|
|
|
Created: time.Now().UTC(),
|
2016-11-22 19:32:10 +00:00
|
|
|
Flags: make(map[string]interface{}),
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
r.layers = append(r.layers, layer)
|
2017-04-27 17:59:23 +00:00
|
|
|
r.idindex.Add(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
r.byid[id] = layer
|
|
|
|
for _, name := range names {
|
|
|
|
r.byname[name] = layer
|
|
|
|
}
|
|
|
|
for flag, value := range flags {
|
|
|
|
layer.Flags[flag] = value
|
|
|
|
}
|
|
|
|
if diff != nil {
|
|
|
|
layer.Flags[incompleteFlag] = true
|
|
|
|
err = r.Save()
|
|
|
|
if err != nil {
|
|
|
|
// We don't have a record of this layer, but at least
|
|
|
|
// try to clean it up underneath us.
|
|
|
|
r.driver.Remove(id)
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
size, err = r.ApplyDiff(layer.ID, diff)
|
|
|
|
if err != nil {
|
|
|
|
if r.Delete(layer.ID) != nil {
|
|
|
|
// Either a driver error or an error saving.
|
|
|
|
// We now have a layer that's been marked for
|
|
|
|
// deletion but which we failed to remove.
|
|
|
|
}
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
delete(layer.Flags, incompleteFlag)
|
|
|
|
}
|
|
|
|
err = r.Save()
|
|
|
|
if err != nil {
|
|
|
|
// We don't have a record of this layer, but at least
|
|
|
|
// try to clean it up underneath us.
|
|
|
|
r.driver.Remove(id)
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return layer, size, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) CreateWithFlags(id, parent string, names []string, mountLabel string, options map[string]string, writeable bool, flags map[string]interface{}) (layer *Layer, err error) {
|
|
|
|
layer, _, err = r.Put(id, parent, names, mountLabel, options, writeable, flags, nil)
|
|
|
|
return layer, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Create(id, parent string, names []string, mountLabel string, options map[string]string, writeable bool) (layer *Layer, err error) {
|
|
|
|
return r.CreateWithFlags(id, parent, names, mountLabel, options, writeable, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Mount(id, mountLabel string) (string, error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return "", errors.Wrapf(ErrStoreIsReadOnly, "not allowed to update mount locations for layers at %q", r.mountspath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
layer, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return "", ErrLayerUnknown
|
|
|
|
}
|
|
|
|
if layer.MountCount > 0 {
|
|
|
|
layer.MountCount++
|
|
|
|
return layer.MountPoint, r.Save()
|
|
|
|
}
|
|
|
|
if mountLabel == "" {
|
2017-04-27 17:59:23 +00:00
|
|
|
mountLabel = layer.MountLabel
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
mountpoint, err := r.driver.Get(id, mountLabel)
|
|
|
|
if mountpoint != "" && err == nil {
|
2017-04-27 17:59:23 +00:00
|
|
|
if layer.MountPoint != "" {
|
|
|
|
delete(r.bymount, layer.MountPoint)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
layer.MountPoint = filepath.Clean(mountpoint)
|
|
|
|
layer.MountCount++
|
|
|
|
r.bymount[layer.MountPoint] = layer
|
|
|
|
err = r.Save()
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
return mountpoint, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Unmount(id string) error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to update mount locations for layers at %q", r.mountspath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
layer, ok := r.lookup(id)
|
|
|
|
if !ok {
|
|
|
|
layerByMount, ok := r.bymount[filepath.Clean(id)]
|
|
|
|
if !ok {
|
|
|
|
return ErrLayerUnknown
|
|
|
|
}
|
|
|
|
layer = layerByMount
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
if layer.MountCount > 1 {
|
|
|
|
layer.MountCount--
|
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
err := r.driver.Put(id)
|
|
|
|
if err == nil || os.IsNotExist(err) {
|
|
|
|
if layer.MountPoint != "" {
|
|
|
|
delete(r.bymount, layer.MountPoint)
|
|
|
|
}
|
|
|
|
layer.MountCount--
|
|
|
|
layer.MountPoint = ""
|
|
|
|
err = r.Save()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) removeName(layer *Layer, name string) {
|
2017-04-27 17:59:23 +00:00
|
|
|
layer.Names = stringSliceWithoutValue(layer.Names, name)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) SetNames(id string, names []string) error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to change layer name assignments at %q", r.layerspath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
if layer, ok := r.lookup(id); ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
for _, name := range layer.Names {
|
|
|
|
delete(r.byname, name)
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
if otherLayer, ok := r.byname[name]; ok {
|
|
|
|
r.removeName(otherLayer, name)
|
|
|
|
}
|
|
|
|
r.byname[name] = layer
|
|
|
|
}
|
|
|
|
layer.Names = names
|
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
return ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (r *layerStore) Metadata(id string) (string, error) {
|
2017-04-27 17:59:23 +00:00
|
|
|
if layer, ok := r.lookup(id); ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return layer.Metadata, nil
|
|
|
|
}
|
|
|
|
return "", ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) SetMetadata(id, metadata string) error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to modify layer metadata at %q", r.layerspath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
if layer, ok := r.lookup(id); ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
layer.Metadata = metadata
|
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
return ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) tspath(id string) string {
|
|
|
|
return filepath.Join(r.layerdir, id+tarSplitSuffix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Delete(id string) error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to delete layers at %q", r.layerspath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
layer, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return ErrLayerUnknown
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
id = layer.ID
|
|
|
|
for layer.MountCount > 0 {
|
2016-11-22 19:32:10 +00:00
|
|
|
if err := r.Unmount(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := r.driver.Remove(id)
|
|
|
|
if err == nil {
|
|
|
|
os.Remove(r.tspath(id))
|
2017-04-27 17:59:23 +00:00
|
|
|
delete(r.byid, id)
|
|
|
|
r.idindex.Delete(id)
|
|
|
|
if layer.MountPoint != "" {
|
|
|
|
delete(r.bymount, layer.MountPoint)
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
toDeleteIndex := -1
|
|
|
|
for i, candidate := range r.layers {
|
|
|
|
if candidate.ID == id {
|
|
|
|
toDeleteIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if toDeleteIndex != -1 {
|
|
|
|
// delete the layer at toDeleteIndex
|
|
|
|
if toDeleteIndex == len(r.layers)-1 {
|
|
|
|
r.layers = r.layers[:len(r.layers)-1]
|
|
|
|
} else {
|
|
|
|
r.layers = append(r.layers[:toDeleteIndex], r.layers[toDeleteIndex+1:]...)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
if err = r.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Lookup(name string) (id string, err error) {
|
2017-04-27 17:59:23 +00:00
|
|
|
if layer, ok := r.lookup(name); ok {
|
|
|
|
return layer.ID, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
return "", ErrLayerUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Exists(id string) bool {
|
2017-04-27 17:59:23 +00:00
|
|
|
_, ok := r.lookup(id)
|
|
|
|
return ok
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Get(id string) (*Layer, error) {
|
2017-04-27 17:59:23 +00:00
|
|
|
if layer, ok := r.lookup(id); ok {
|
|
|
|
return layer, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
return nil, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Wipe() error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to delete layers at %q", r.layerspath())
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
ids := []string{}
|
|
|
|
for id := range r.byid {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
for _, id := range ids {
|
|
|
|
if err := r.Delete(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (r *layerStore) findParentAndLayer(from, to string) (fromID string, toID string, toLayer *Layer, err error) {
|
2017-04-27 17:59:23 +00:00
|
|
|
var ok bool
|
2017-05-17 17:18:35 +00:00
|
|
|
var fromLayer *Layer
|
2017-04-27 17:59:23 +00:00
|
|
|
toLayer, ok = r.lookup(to)
|
|
|
|
if !ok {
|
2017-05-17 17:18:35 +00:00
|
|
|
return "", "", nil, ErrLayerUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
to = toLayer.ID
|
2016-11-22 19:32:10 +00:00
|
|
|
if from == "" {
|
2017-04-27 17:59:23 +00:00
|
|
|
from = toLayer.Parent
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
if from != "" {
|
|
|
|
fromLayer, ok = r.lookup(from)
|
2017-05-17 17:18:35 +00:00
|
|
|
if ok {
|
|
|
|
from = fromLayer.ID
|
|
|
|
} else {
|
2017-04-27 17:59:23 +00:00
|
|
|
fromLayer, ok = r.lookup(toLayer.Parent)
|
2017-05-17 17:18:35 +00:00
|
|
|
if ok {
|
|
|
|
from = fromLayer.ID
|
2017-04-27 17:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
return from, to, toLayer, nil
|
2017-04-27 17:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Changes(from, to string) ([]archive.Change, error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
from, to, _, err := r.findParentAndLayer(from, to)
|
2017-04-27 17:59:23 +00:00
|
|
|
if err != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
return nil, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
return r.driver.Changes(to, from)
|
|
|
|
}
|
|
|
|
|
|
|
|
type simpleGetCloser struct {
|
|
|
|
r *layerStore
|
|
|
|
path string
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *simpleGetCloser) Get(path string) (io.ReadCloser, error) {
|
|
|
|
return os.Open(filepath.Join(s.path, path))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *simpleGetCloser) Close() error {
|
|
|
|
return s.r.Unmount(s.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) newFileGetter(id string) (drivers.FileGetCloser, error) {
|
|
|
|
if getter, ok := r.driver.(drivers.DiffGetterDriver); ok {
|
|
|
|
return getter.DiffGetter(id)
|
|
|
|
}
|
|
|
|
path, err := r.Mount(id, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &simpleGetCloser{
|
|
|
|
r: r,
|
|
|
|
path: path,
|
|
|
|
id: id,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (r *layerStore) Diff(from, to string, options *DiffOptions) (io.ReadCloser, error) {
|
2016-11-22 19:32:10 +00:00
|
|
|
var metadata storage.Unpacker
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
from, to, toLayer, err := r.findParentAndLayer(from, to)
|
2017-04-27 17:59:23 +00:00
|
|
|
if err != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
return nil, ErrLayerUnknown
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
// Default to applying the type of compression that we noted was used
|
|
|
|
// for the layerdiff when it was applied.
|
|
|
|
compression := toLayer.CompressionType
|
|
|
|
// If a particular compression type (or no compression) was selected,
|
|
|
|
// use that instead.
|
|
|
|
if options != nil && options.Compression != nil {
|
|
|
|
compression = *options.Compression
|
|
|
|
}
|
|
|
|
maybeCompressReadCloser := func(rc io.ReadCloser) (io.ReadCloser, error) {
|
|
|
|
// Depending on whether or not compression is desired, return either the
|
|
|
|
// passed-in ReadCloser, or a new one that provides its readers with a
|
|
|
|
// compressed version of the data that the original would have provided
|
|
|
|
// to its readers.
|
|
|
|
if compression == archive.Uncompressed {
|
|
|
|
return rc, nil
|
|
|
|
}
|
|
|
|
preader, pwriter := io.Pipe()
|
|
|
|
compressor, err := archive.CompressStream(pwriter, compression)
|
|
|
|
if err != nil {
|
|
|
|
rc.Close()
|
|
|
|
pwriter.Close()
|
|
|
|
preader.Close()
|
|
|
|
return nil, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
go func() {
|
|
|
|
defer pwriter.Close()
|
|
|
|
defer compressor.Close()
|
|
|
|
defer rc.Close()
|
|
|
|
io.Copy(compressor, rc)
|
|
|
|
}()
|
|
|
|
return preader, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
|
2017-04-27 17:59:23 +00:00
|
|
|
if from != toLayer.Parent {
|
2016-11-22 19:32:10 +00:00
|
|
|
diff, err := r.driver.Diff(to, from)
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return maybeCompressReadCloser(diff)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tsfile, err := os.Open(r.tspath(to))
|
|
|
|
if err != nil {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
diff, err := r.driver.Diff(to, from)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return maybeCompressReadCloser(diff)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
defer tsfile.Close()
|
|
|
|
|
|
|
|
decompressor, err := gzip.NewReader(tsfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer decompressor.Close()
|
|
|
|
|
|
|
|
tsbytes, err := ioutil.ReadAll(decompressor)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata = storage.NewJSONUnpacker(bytes.NewBuffer(tsbytes))
|
|
|
|
|
2017-02-01 00:45:59 +00:00
|
|
|
fgetter, err := r.newFileGetter(to)
|
|
|
|
if err != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
return nil, err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
tarstream := asm.NewOutputTarStream(fgetter, metadata)
|
|
|
|
rc := ioutils.NewReadCloserWrapper(tarstream, func() error {
|
|
|
|
err1 := tarstream.Close()
|
2017-02-01 00:45:59 +00:00
|
|
|
err2 := fgetter.Close()
|
|
|
|
if err2 == nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
return err2
|
2017-07-20 20:31:51 +00:00
|
|
|
})
|
|
|
|
return maybeCompressReadCloser(rc)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) DiffSize(from, to string) (size int64, err error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
from, to, _, err = r.findParentAndLayer(from, to)
|
2017-04-27 17:59:23 +00:00
|
|
|
if err != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
return -1, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
return r.driver.DiffSize(to, from)
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
func (r *layerStore) ApplyDiff(to string, diff io.Reader) (size int64, err error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return -1, errors.Wrapf(ErrStoreIsReadOnly, "not allowed to modify layer contents at %q", r.layerspath())
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:59:23 +00:00
|
|
|
layer, ok := r.lookup(to)
|
2016-11-22 19:32:10 +00:00
|
|
|
if !ok {
|
|
|
|
return -1, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
header := make([]byte, 10240)
|
|
|
|
n, err := diff.Read(header)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
compression := archive.DetectCompression(header[:n])
|
2017-07-20 20:31:51 +00:00
|
|
|
compressedDigest := digest.Canonical.Digester()
|
|
|
|
compressedCounter := ioutils.NewWriteCounter(compressedDigest.Hash())
|
|
|
|
defragmented := io.TeeReader(io.MultiReader(bytes.NewBuffer(header[:n]), diff), compressedCounter)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
tsdata := bytes.Buffer{}
|
|
|
|
compressor, err := gzip.NewWriterLevel(&tsdata, gzip.BestSpeed)
|
|
|
|
if err != nil {
|
|
|
|
compressor = gzip.NewWriter(&tsdata)
|
|
|
|
}
|
|
|
|
metadata := storage.NewJSONPacker(compressor)
|
2017-07-20 20:31:51 +00:00
|
|
|
uncompressed, err := archive.DecompressStream(defragmented)
|
2017-02-01 00:45:59 +00:00
|
|
|
if err != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
return -1, err
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
uncompressedDigest := digest.Canonical.Digester()
|
|
|
|
uncompressedCounter := ioutils.NewWriteCounter(uncompressedDigest.Hash())
|
|
|
|
payload, err := asm.NewInputTarStream(io.TeeReader(uncompressed, uncompressedCounter), metadata, storage.NewDiscardFilePutter())
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
size, err = r.driver.ApplyDiff(layer.ID, layer.Parent, payload)
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
compressor.Close()
|
|
|
|
if err == nil {
|
2017-03-28 19:56:09 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(r.tspath(layer.ID)), 0700); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
if err := ioutils.AtomicWriteFile(r.tspath(layer.ID), tsdata.Bytes(), 0600); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
updateDigestMap := func(m *map[digest.Digest][]string, oldvalue, newvalue digest.Digest, id string) {
|
|
|
|
var newList []string
|
|
|
|
if oldvalue != "" {
|
|
|
|
for _, value := range (*m)[oldvalue] {
|
|
|
|
if value != id {
|
|
|
|
newList = append(newList, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(newList) > 0 {
|
|
|
|
(*m)[oldvalue] = newList
|
|
|
|
} else {
|
|
|
|
delete(*m, oldvalue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if newvalue != "" {
|
|
|
|
(*m)[newvalue] = append((*m)[newvalue], id)
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
updateDigestMap(&r.bycompressedsum, layer.CompressedDigest, compressedDigest.Digest(), layer.ID)
|
|
|
|
layer.CompressedDigest = compressedDigest.Digest()
|
|
|
|
layer.CompressedSize = compressedCounter.Count
|
|
|
|
updateDigestMap(&r.byuncompressedsum, layer.UncompressedDigest, uncompressedDigest.Digest(), layer.ID)
|
|
|
|
layer.UncompressedDigest = uncompressedDigest.Digest()
|
|
|
|
layer.UncompressedSize = uncompressedCounter.Count
|
|
|
|
layer.CompressionType = compression
|
|
|
|
|
|
|
|
err = r.Save()
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (r *layerStore) layersByDigestMap(m map[digest.Digest][]string, d digest.Digest) ([]Layer, error) {
|
|
|
|
var layers []Layer
|
|
|
|
for _, layerID := range m[d] {
|
|
|
|
layer, ok := r.lookup(layerID)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
layers = append(layers, *layer)
|
|
|
|
}
|
|
|
|
return layers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) LayersByCompressedDigest(d digest.Digest) ([]Layer, error) {
|
|
|
|
return r.layersByDigestMap(r.bycompressedsum, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) LayersByUncompressedDigest(d digest.Digest) ([]Layer, error) {
|
|
|
|
return r.layersByDigestMap(r.byuncompressedsum, d)
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
func (r *layerStore) Lock() {
|
|
|
|
r.lockfile.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Unlock() {
|
|
|
|
r.lockfile.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Touch() error {
|
|
|
|
return r.lockfile.Touch()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *layerStore) Modified() (bool, error) {
|
|
|
|
return r.lockfile.Modified()
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (r *layerStore) IsReadWrite() bool {
|
|
|
|
return r.lockfile.IsReadWrite()
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
func (r *layerStore) TouchedSince(when time.Time) bool {
|
|
|
|
return r.lockfile.TouchedSince(when)
|
|
|
|
}
|