2016-11-22 19:32:10 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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-10-14 09:38:04 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2017-07-20 20:31:51 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-11-22 19:32:10 +00:00
|
|
|
)
|
|
|
|
|
2017-11-03 17:36:13 +00:00
|
|
|
const (
|
|
|
|
// ImageDigestBigDataKey is the name of the big data item whose
|
|
|
|
// contents we consider useful for computing a "digest" of the
|
|
|
|
// image, by which we can locate the image later.
|
|
|
|
ImageDigestBigDataKey = "manifest"
|
|
|
|
)
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
// An Image is a reference to a layer and an associated metadata string.
|
|
|
|
type Image 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"`
|
|
|
|
|
2017-11-03 17:36:13 +00:00
|
|
|
// Digest is a digest value that we can use to locate the image.
|
|
|
|
Digest digest.Digest `json:"digest,omitempty"`
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
// Names is an optional set of user-defined convenience values. The
|
|
|
|
// image can be referred to by its ID or any of its names. Names are
|
|
|
|
// unique among images.
|
|
|
|
Names []string `json:"names,omitempty"`
|
|
|
|
|
2017-09-26 19:58:51 +00:00
|
|
|
// TopLayer is the ID of the topmost layer of the image itself, if the
|
|
|
|
// image contains one or more layers. Multiple images can refer to the
|
|
|
|
// same top layer.
|
2017-11-03 17:36:13 +00:00
|
|
|
TopLayer string `json:"layer,omitempty"`
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
// 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"`
|
|
|
|
|
|
|
|
// BigDataNames is a list of names of data items that we keep for the
|
|
|
|
// convenience of the caller. They can be large, and are only in
|
|
|
|
// memory when being read from or written to disk.
|
|
|
|
BigDataNames []string `json:"big-data-names,omitempty"`
|
|
|
|
|
|
|
|
// BigDataSizes maps the names in BigDataNames to the sizes of the data
|
|
|
|
// that has been stored, if they're known.
|
|
|
|
BigDataSizes map[string]int64 `json:"big-data-sizes,omitempty"`
|
|
|
|
|
2017-10-14 09:38:04 +00:00
|
|
|
// BigDataDigests maps the names in BigDataNames to the digests of the
|
|
|
|
// data that has been stored, if they're known.
|
|
|
|
BigDataDigests map[string]digest.Digest `json:"big-data-digests,omitempty"`
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Created is the datestamp for when this image 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"`
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
Flags map[string]interface{} `json:"flags,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// ROImageStore provides bookkeeping for information about Images.
|
|
|
|
type ROImageStore interface {
|
|
|
|
ROFileBasedStore
|
|
|
|
ROMetadataStore
|
|
|
|
ROBigDataStore
|
|
|
|
|
|
|
|
// Exists checks if there is an image with the given ID or name.
|
|
|
|
Exists(id string) bool
|
|
|
|
|
|
|
|
// Get retrieves information about an image given an ID or name.
|
|
|
|
Get(id string) (*Image, error)
|
|
|
|
|
|
|
|
// Lookup attempts to translate a name to an ID. Most methods do this
|
|
|
|
// implicitly.
|
|
|
|
Lookup(name string) (string, error)
|
|
|
|
|
|
|
|
// Images returns a slice enumerating the known images.
|
|
|
|
Images() ([]Image, error)
|
2017-11-03 17:36:13 +00:00
|
|
|
|
|
|
|
// Images returns a slice enumerating the images which have a big data
|
|
|
|
// item with the name ImageDigestBigDataKey and the specified digest.
|
|
|
|
ByDigest(d digest.Digest) ([]*Image, error)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
// ImageStore provides bookkeeping for information about Images.
|
|
|
|
type ImageStore interface {
|
2017-07-20 20:31:51 +00:00
|
|
|
ROImageStore
|
|
|
|
RWFileBasedStore
|
|
|
|
RWMetadataStore
|
|
|
|
RWBigDataStore
|
2016-11-22 19:32:10 +00:00
|
|
|
FlaggableStore
|
|
|
|
|
|
|
|
// Create creates an image that has a specified ID (or a random one) and
|
|
|
|
// optional names, using the specified layer as its topmost (hopefully
|
|
|
|
// read-only) layer. That layer can be referenced by multiple images.
|
2017-11-03 17:36:13 +00:00
|
|
|
Create(id string, names []string, layer, metadata string, created time.Time, searchableDigest digest.Digest) (*Image, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
// SetNames replaces the list of names associated with an image with the
|
|
|
|
// supplied values.
|
|
|
|
SetNames(id string, names []string) error
|
|
|
|
|
|
|
|
// Delete removes the record of the image.
|
|
|
|
Delete(id string) error
|
|
|
|
|
|
|
|
// Wipe removes records of all images.
|
|
|
|
Wipe() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageStore struct {
|
|
|
|
lockfile Locker
|
|
|
|
dir string
|
2017-07-20 20:31:51 +00:00
|
|
|
images []*Image
|
2017-04-27 17:59:23 +00:00
|
|
|
idindex *truncindex.TruncIndex
|
2016-11-22 19:32:10 +00:00
|
|
|
byid map[string]*Image
|
|
|
|
byname map[string]*Image
|
2017-11-03 17:36:13 +00:00
|
|
|
bydigest map[digest.Digest][]*Image
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Images() ([]Image, error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
images := make([]Image, len(r.images))
|
|
|
|
for i := range r.images {
|
|
|
|
images[i] = *(r.images[i])
|
|
|
|
}
|
|
|
|
return images, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) imagespath() string {
|
|
|
|
return filepath.Join(r.dir, "images.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) datadir(id string) string {
|
|
|
|
return filepath.Join(r.dir, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) datapath(id, key string) string {
|
|
|
|
return filepath.Join(r.datadir(id), makeBigDataBaseName(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Load() error {
|
2017-07-20 20:31:51 +00:00
|
|
|
shouldSave := false
|
2016-11-22 19:32:10 +00:00
|
|
|
rpath := r.imagespath()
|
|
|
|
data, err := ioutil.ReadFile(rpath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
images := []*Image{}
|
2017-04-27 17:59:23 +00:00
|
|
|
idlist := []string{}
|
2016-11-22 19:32:10 +00:00
|
|
|
ids := make(map[string]*Image)
|
|
|
|
names := make(map[string]*Image)
|
2017-11-03 17:36:13 +00:00
|
|
|
digests := make(map[digest.Digest][]*Image)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err = json.Unmarshal(data, &images); len(data) == 0 || err == nil {
|
2017-10-14 09:38:04 +00:00
|
|
|
idlist = make([]string, 0, len(images))
|
2016-11-22 19:32:10 +00:00
|
|
|
for n, image := range images {
|
2017-07-20 20:31:51 +00:00
|
|
|
ids[image.ID] = images[n]
|
2017-04-27 17:59:23 +00:00
|
|
|
idlist = append(idlist, image.ID)
|
2016-11-22 19:32:10 +00:00
|
|
|
for _, name := range image.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] = images[n]
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-11-03 17:36:13 +00:00
|
|
|
// Implicit digest
|
|
|
|
if digest, ok := image.BigDataDigests[ImageDigestBigDataKey]; ok {
|
|
|
|
digests[digest] = append(digests[digest], images[n])
|
|
|
|
}
|
|
|
|
// Explicit digest
|
|
|
|
if image.Digest == "" {
|
|
|
|
image.Digest = image.BigDataDigests[ImageDigestBigDataKey]
|
|
|
|
} else if image.Digest != image.BigDataDigests[ImageDigestBigDataKey] {
|
|
|
|
digests[image.Digest] = append(digests[image.Digest], images[n])
|
|
|
|
}
|
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 ErrDuplicateImageNames
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
r.images = images
|
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
|
2017-11-03 17:36:13 +00:00
|
|
|
r.bydigest = digests
|
2017-07-20 20:31:51 +00:00
|
|
|
if shouldSave {
|
2016-11-22 19:32:10 +00:00
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Save() error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to modify the image store at %q", r.imagespath())
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
rpath := r.imagespath()
|
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
|
|
|
jdata, err := json.Marshal(&r.images)
|
|
|
|
if 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(rpath, jdata, 0600)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newImageStore(dir string) (ImageStore, error) {
|
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lockfile, err := GetLockfile(filepath.Join(dir, "images.lock"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lockfile.Lock()
|
|
|
|
defer lockfile.Unlock()
|
|
|
|
istore := imageStore{
|
|
|
|
lockfile: lockfile,
|
|
|
|
dir: dir,
|
2017-07-20 20:31:51 +00:00
|
|
|
images: []*Image{},
|
|
|
|
byid: make(map[string]*Image),
|
|
|
|
byname: make(map[string]*Image),
|
2017-11-03 17:36:13 +00:00
|
|
|
bydigest: make(map[digest.Digest][]*Image),
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
if err := istore.Load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &istore, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newROImageStore(dir string) (ROImageStore, error) {
|
|
|
|
lockfile, err := GetROLockfile(filepath.Join(dir, "images.lock"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lockfile.Lock()
|
|
|
|
defer lockfile.Unlock()
|
|
|
|
istore := imageStore{
|
|
|
|
lockfile: lockfile,
|
|
|
|
dir: dir,
|
|
|
|
images: []*Image{},
|
2016-11-22 19:32:10 +00:00
|
|
|
byid: make(map[string]*Image),
|
|
|
|
byname: make(map[string]*Image),
|
2017-11-03 17:36:13 +00:00
|
|
|
bydigest: make(map[digest.Digest][]*Image),
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
if err := istore.Load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &istore, nil
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:59:23 +00:00
|
|
|
func (r *imageStore) lookup(id string) (*Image, bool) {
|
|
|
|
if image, ok := r.byid[id]; ok {
|
|
|
|
return image, ok
|
|
|
|
} else if image, ok := r.byname[id]; ok {
|
|
|
|
return image, ok
|
|
|
|
} else if longid, err := r.idindex.Get(id); err == nil {
|
|
|
|
image, ok := r.byid[longid]
|
|
|
|
return image, ok
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
func (r *imageStore) 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 images at %q", r.imagespath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return ErrImageUnknown
|
|
|
|
}
|
|
|
|
delete(image.Flags, flag)
|
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) 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 images at %q", r.imagespath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return ErrImageUnknown
|
|
|
|
}
|
2017-10-14 09:38:04 +00:00
|
|
|
if image.Flags == nil {
|
|
|
|
image.Flags = make(map[string]interface{})
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
image.Flags[flag] = value
|
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:36:13 +00:00
|
|
|
func (r *imageStore) Create(id string, names []string, layer, metadata string, created time.Time, searchableDigest digest.Digest) (image *Image, err error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return nil, errors.Wrapf(ErrStoreIsReadOnly, "not allowed to create new images at %q", r.imagespath())
|
|
|
|
}
|
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, ErrDuplicateID
|
|
|
|
}
|
2017-09-26 19:58:51 +00:00
|
|
|
names = dedupeNames(names)
|
2016-11-22 19:32:10 +00:00
|
|
|
for _, name := range names {
|
|
|
|
if _, nameInUse := r.byname[name]; nameInUse {
|
|
|
|
return nil, ErrDuplicateName
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if created.IsZero() {
|
|
|
|
created = time.Now().UTC()
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
if err == nil {
|
2017-07-20 20:31:51 +00:00
|
|
|
image = &Image{
|
2017-10-14 09:38:04 +00:00
|
|
|
ID: id,
|
2017-11-03 17:36:13 +00:00
|
|
|
Digest: searchableDigest,
|
2017-10-14 09:38:04 +00:00
|
|
|
Names: names,
|
|
|
|
TopLayer: layer,
|
|
|
|
Metadata: metadata,
|
|
|
|
BigDataNames: []string{},
|
|
|
|
BigDataSizes: make(map[string]int64),
|
|
|
|
BigDataDigests: make(map[string]digest.Digest),
|
|
|
|
Created: created,
|
|
|
|
Flags: make(map[string]interface{}),
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
r.images = append(r.images, image)
|
2017-04-27 17:59:23 +00:00
|
|
|
r.idindex.Add(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
r.byid[id] = image
|
2017-11-03 17:36:13 +00:00
|
|
|
if searchableDigest != "" {
|
|
|
|
list := r.bydigest[searchableDigest]
|
|
|
|
r.bydigest[searchableDigest] = append(list, image)
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
for _, name := range names {
|
|
|
|
r.byname[name] = image
|
|
|
|
}
|
|
|
|
err = r.Save()
|
|
|
|
}
|
|
|
|
return image, err
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (r *imageStore) Metadata(id string) (string, error) {
|
2017-04-27 17:59:23 +00:00
|
|
|
if image, ok := r.lookup(id); ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return image.Metadata, nil
|
|
|
|
}
|
|
|
|
return "", ErrImageUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) SetMetadata(id, metadata string) error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to modify image metadata at %q", r.imagespath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
if image, ok := r.lookup(id); ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
image.Metadata = metadata
|
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
return ErrImageUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) removeName(image *Image, name string) {
|
2017-04-27 17:59:23 +00:00
|
|
|
image.Names = stringSliceWithoutValue(image.Names, name)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) 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 image name assignments at %q", r.imagespath())
|
|
|
|
}
|
2017-09-26 19:58:51 +00:00
|
|
|
names = dedupeNames(names)
|
2017-04-27 17:59:23 +00:00
|
|
|
if image, ok := r.lookup(id); ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
for _, name := range image.Names {
|
|
|
|
delete(r.byname, name)
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
if otherImage, ok := r.byname[name]; ok {
|
|
|
|
r.removeName(otherImage, name)
|
|
|
|
}
|
|
|
|
r.byname[name] = image
|
|
|
|
}
|
|
|
|
image.Names = names
|
|
|
|
return r.Save()
|
|
|
|
}
|
|
|
|
return ErrImageUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Delete(id string) error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to delete images at %q", r.imagespath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return ErrImageUnknown
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
id = image.ID
|
2017-07-20 20:31:51 +00:00
|
|
|
toDeleteIndex := -1
|
|
|
|
for i, candidate := range r.images {
|
|
|
|
if candidate.ID == id {
|
|
|
|
toDeleteIndex = i
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
delete(r.byid, id)
|
|
|
|
r.idindex.Delete(id)
|
|
|
|
for _, name := range image.Names {
|
|
|
|
delete(r.byname, name)
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if toDeleteIndex != -1 {
|
|
|
|
// delete the image at toDeleteIndex
|
|
|
|
if toDeleteIndex == len(r.images)-1 {
|
|
|
|
r.images = r.images[:len(r.images)-1]
|
|
|
|
} else {
|
|
|
|
r.images = append(r.images[:toDeleteIndex], r.images[toDeleteIndex+1:]...)
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 17:36:13 +00:00
|
|
|
if digest, ok := image.BigDataDigests[ImageDigestBigDataKey]; ok {
|
|
|
|
// remove the image from the digest-based index
|
|
|
|
if list, ok := r.bydigest[digest]; ok {
|
|
|
|
prunedList := imageSliceWithoutValue(list, image)
|
|
|
|
if len(prunedList) == 0 {
|
|
|
|
delete(r.bydigest, digest)
|
|
|
|
} else {
|
|
|
|
r.bydigest[digest] = prunedList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if image.Digest != "" {
|
|
|
|
// remove the image's hard-coded digest from the digest-based index
|
|
|
|
if list, ok := r.bydigest[image.Digest]; ok {
|
|
|
|
prunedList := imageSliceWithoutValue(list, image)
|
|
|
|
if len(prunedList) == 0 {
|
|
|
|
delete(r.bydigest, image.Digest)
|
|
|
|
} else {
|
|
|
|
r.bydigest[image.Digest] = prunedList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
if err := r.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(r.datadir(id)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Get(id string) (*Image, error) {
|
2017-04-27 17:59:23 +00:00
|
|
|
if image, ok := r.lookup(id); ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return image, nil
|
|
|
|
}
|
|
|
|
return nil, ErrImageUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Lookup(name string) (id string, err error) {
|
2017-04-27 17:59:23 +00:00
|
|
|
if image, ok := r.lookup(name); ok {
|
|
|
|
return image.ID, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
return "", ErrImageUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) 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
|
|
|
}
|
|
|
|
|
2017-11-03 17:36:13 +00:00
|
|
|
func (r *imageStore) ByDigest(d digest.Digest) ([]*Image, error) {
|
|
|
|
if images, ok := r.bydigest[d]; ok {
|
|
|
|
return images, nil
|
|
|
|
}
|
|
|
|
return nil, ErrImageUnknown
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (r *imageStore) BigData(id, key string) ([]byte, error) {
|
2017-10-14 09:38:04 +00:00
|
|
|
if key == "" {
|
|
|
|
return nil, errors.Wrapf(ErrInvalidBigDataName, "can't retrieve image big data value for empty name")
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return nil, ErrImageUnknown
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
return ioutil.ReadFile(r.datapath(image.ID, key))
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (r *imageStore) BigDataSize(id, key string) (int64, error) {
|
2017-10-14 09:38:04 +00:00
|
|
|
if key == "" {
|
|
|
|
return -1, errors.Wrapf(ErrInvalidBigDataName, "can't retrieve size of image big data with empty name")
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return -1, ErrImageUnknown
|
|
|
|
}
|
2017-10-14 09:38:04 +00:00
|
|
|
if image.BigDataSizes == nil {
|
|
|
|
image.BigDataSizes = make(map[string]int64)
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
if size, ok := image.BigDataSizes[key]; ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return size, nil
|
|
|
|
}
|
2017-10-14 09:38:04 +00:00
|
|
|
if data, err := r.BigData(id, key); err == nil && data != nil {
|
|
|
|
if r.SetBigData(id, key, data) == nil {
|
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
|
|
|
return -1, ErrImageUnknown
|
|
|
|
}
|
|
|
|
if size, ok := image.BigDataSizes[key]; ok {
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
return -1, ErrSizeUnknown
|
|
|
|
}
|
|
|
|
|
2017-10-14 09:38:04 +00:00
|
|
|
func (r *imageStore) BigDataDigest(id, key string) (digest.Digest, error) {
|
|
|
|
if key == "" {
|
|
|
|
return "", errors.Wrapf(ErrInvalidBigDataName, "can't retrieve digest of image big data value with empty name")
|
|
|
|
}
|
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
|
|
|
return "", ErrImageUnknown
|
|
|
|
}
|
|
|
|
if image.BigDataDigests == nil {
|
|
|
|
image.BigDataDigests = make(map[string]digest.Digest)
|
|
|
|
}
|
|
|
|
if d, ok := image.BigDataDigests[key]; ok {
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
if data, err := r.BigData(id, key); err == nil && data != nil {
|
|
|
|
if r.SetBigData(id, key, data) == nil {
|
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
|
|
|
return "", ErrImageUnknown
|
|
|
|
}
|
|
|
|
if d, ok := image.BigDataDigests[key]; ok {
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ErrDigestUnknown
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (r *imageStore) BigDataNames(id string) ([]string, error) {
|
2017-04-27 17:59:23 +00:00
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return nil, ErrImageUnknown
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
return image.BigDataNames, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 17:36:13 +00:00
|
|
|
func imageSliceWithoutValue(slice []*Image, value *Image) []*Image {
|
|
|
|
modified := make([]*Image, 0, len(slice))
|
|
|
|
for _, v := range slice {
|
|
|
|
if v == value {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
modified = append(modified, v)
|
|
|
|
}
|
|
|
|
return modified
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
func (r *imageStore) SetBigData(id, key string, data []byte) error {
|
2017-10-14 09:38:04 +00:00
|
|
|
if key == "" {
|
|
|
|
return errors.Wrapf(ErrInvalidBigDataName, "can't set empty name for image big data item")
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to save data items associated with images at %q", r.imagespath())
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
image, ok := r.lookup(id)
|
|
|
|
if !ok {
|
2016-11-22 19:32:10 +00:00
|
|
|
return ErrImageUnknown
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
if err := os.MkdirAll(r.datadir(image.ID), 0700); err != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
err := ioutils.AtomicWriteFile(r.datapath(image.ID, key), data, 0600)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err == nil {
|
|
|
|
save := false
|
2017-10-14 09:38:04 +00:00
|
|
|
if image.BigDataSizes == nil {
|
|
|
|
image.BigDataSizes = make(map[string]int64)
|
|
|
|
}
|
|
|
|
oldSize, sizeOk := image.BigDataSizes[key]
|
2017-04-27 17:59:23 +00:00
|
|
|
image.BigDataSizes[key] = int64(len(data))
|
2017-10-14 09:38:04 +00:00
|
|
|
if image.BigDataDigests == nil {
|
|
|
|
image.BigDataDigests = make(map[string]digest.Digest)
|
|
|
|
}
|
|
|
|
oldDigest, digestOk := image.BigDataDigests[key]
|
|
|
|
newDigest := digest.Canonical.FromBytes(data)
|
|
|
|
image.BigDataDigests[key] = newDigest
|
|
|
|
if !sizeOk || oldSize != image.BigDataSizes[key] || !digestOk || oldDigest != newDigest {
|
2016-11-22 19:32:10 +00:00
|
|
|
save = true
|
|
|
|
}
|
2017-10-14 09:38:04 +00:00
|
|
|
addName := true
|
2017-04-27 17:59:23 +00:00
|
|
|
for _, name := range image.BigDataNames {
|
2016-11-22 19:32:10 +00:00
|
|
|
if name == key {
|
2017-10-14 09:38:04 +00:00
|
|
|
addName = false
|
2016-11-22 19:32:10 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-10-14 09:38:04 +00:00
|
|
|
if addName {
|
2017-04-27 17:59:23 +00:00
|
|
|
image.BigDataNames = append(image.BigDataNames, key)
|
2016-11-22 19:32:10 +00:00
|
|
|
save = true
|
|
|
|
}
|
2017-11-03 17:36:13 +00:00
|
|
|
if key == ImageDigestBigDataKey {
|
|
|
|
if oldDigest != "" && oldDigest != newDigest && oldDigest != image.Digest {
|
|
|
|
// remove the image from the list of images in the digest-based
|
|
|
|
// index which corresponds to the old digest for this item, unless
|
|
|
|
// it's also the hard-coded digest
|
|
|
|
if list, ok := r.bydigest[oldDigest]; ok {
|
|
|
|
prunedList := imageSliceWithoutValue(list, image)
|
|
|
|
if len(prunedList) == 0 {
|
|
|
|
delete(r.bydigest, oldDigest)
|
|
|
|
} else {
|
|
|
|
r.bydigest[oldDigest] = prunedList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add the image to the list of images in the digest-based index which
|
|
|
|
// corresponds to the new digest for this item, unless it's already there
|
|
|
|
list := r.bydigest[newDigest]
|
|
|
|
if len(list) == len(imageSliceWithoutValue(list, image)) {
|
|
|
|
// the list isn't shortened by trying to prune this image from it,
|
|
|
|
// so it's not in there yet
|
|
|
|
r.bydigest[newDigest] = append(list, image)
|
|
|
|
}
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
if save {
|
|
|
|
err = r.Save()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Wipe() error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if !r.IsReadWrite() {
|
|
|
|
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to delete images at %q", r.imagespath())
|
|
|
|
}
|
2017-10-14 09:38:04 +00:00
|
|
|
ids := make([]string, 0, len(r.byid))
|
2016-11-22 19:32:10 +00:00
|
|
|
for id := range r.byid {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
for _, id := range ids {
|
|
|
|
if err := r.Delete(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Lock() {
|
|
|
|
r.lockfile.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Unlock() {
|
|
|
|
r.lockfile.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Touch() error {
|
|
|
|
return r.lockfile.Touch()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageStore) Modified() (bool, error) {
|
|
|
|
return r.lockfile.Modified()
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (r *imageStore) IsReadWrite() bool {
|
|
|
|
return r.lockfile.IsReadWrite()
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
func (r *imageStore) TouchedSince(when time.Time) bool {
|
|
|
|
return r.lockfile.TouchedSince(when)
|
|
|
|
}
|