2015-11-18 17:18:07 -05:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/distribution/digest"
|
2016-04-20 20:08:47 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-11-18 17:18:07 -05:00
|
|
|
)
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
// DigestWalkFunc is function called by StoreBackend.Walk
|
|
|
|
type DigestWalkFunc func(id digest.Digest) error
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
// StoreBackend provides interface for image.Store persistence
|
|
|
|
type StoreBackend interface {
|
2016-09-15 19:37:32 -04:00
|
|
|
Walk(f DigestWalkFunc) error
|
|
|
|
Get(id digest.Digest) ([]byte, error)
|
|
|
|
Set(data []byte) (digest.Digest, error)
|
|
|
|
Delete(id digest.Digest) error
|
|
|
|
SetMetadata(id digest.Digest, key string, data []byte) error
|
|
|
|
GetMetadata(id digest.Digest, key string) ([]byte, error)
|
|
|
|
DeleteMetadata(id digest.Digest, key string) error
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// fs implements StoreBackend using the filesystem.
|
|
|
|
type fs struct {
|
|
|
|
sync.RWMutex
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
contentDirName = "content"
|
|
|
|
metadataDirName = "metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewFSStoreBackend returns new filesystem based backend for image.Store
|
|
|
|
func NewFSStoreBackend(root string) (StoreBackend, error) {
|
|
|
|
return newFSStore(root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFSStore(root string) (*fs, error) {
|
|
|
|
s := &fs{
|
|
|
|
root: root,
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Join(root, contentDirName, string(digest.Canonical)), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Join(root, metadataDirName, string(digest.Canonical)), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
func (s *fs) contentFile(dgst digest.Digest) string {
|
2015-11-18 17:18:07 -05:00
|
|
|
return filepath.Join(s.root, contentDirName, string(dgst.Algorithm()), dgst.Hex())
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
func (s *fs) metadataDir(dgst digest.Digest) string {
|
2015-11-18 17:18:07 -05:00
|
|
|
return filepath.Join(s.root, metadataDirName, string(dgst.Algorithm()), dgst.Hex())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk calls the supplied callback for each image ID in the storage backend.
|
2016-09-15 19:37:32 -04:00
|
|
|
func (s *fs) Walk(f DigestWalkFunc) error {
|
2015-11-18 17:18:07 -05:00
|
|
|
// Only Canonical digest (sha256) is currently supported
|
|
|
|
s.RLock()
|
|
|
|
dir, err := ioutil.ReadDir(filepath.Join(s.root, contentDirName, string(digest.Canonical)))
|
|
|
|
s.RUnlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, v := range dir {
|
|
|
|
dgst := digest.NewDigestFromHex(string(digest.Canonical), v.Name())
|
|
|
|
if err := dgst.Validate(); err != nil {
|
|
|
|
logrus.Debugf("Skipping invalid digest %s: %s", dgst, err)
|
|
|
|
continue
|
|
|
|
}
|
2016-09-15 19:37:32 -04:00
|
|
|
if err := f(dgst); err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
// Get returns the content stored under a given digest.
|
|
|
|
func (s *fs) Get(dgst digest.Digest) ([]byte, error) {
|
2015-11-18 17:18:07 -05:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
return s.get(dgst)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
func (s *fs) get(dgst digest.Digest) ([]byte, error) {
|
|
|
|
content, err := ioutil.ReadFile(s.contentFile(dgst))
|
2015-11-18 17:18:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: maybe optional
|
2016-09-15 19:37:32 -04:00
|
|
|
if digest.FromBytes(content) != dgst {
|
|
|
|
return nil, fmt.Errorf("failed to verify: %v", dgst)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
// Set stores content by checksum.
|
|
|
|
func (s *fs) Set(data []byte) (digest.Digest, error) {
|
2015-11-18 17:18:07 -05:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return "", fmt.Errorf("Invalid empty data")
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
dgst := digest.FromBytes(data)
|
|
|
|
if err := ioutils.AtomicWriteFile(s.contentFile(dgst), data, 0600); err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
return dgst, nil
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
// Delete removes content and metadata files associated with the digest.
|
|
|
|
func (s *fs) Delete(dgst digest.Digest) error {
|
2015-11-18 17:18:07 -05:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
if err := os.RemoveAll(s.metadataDir(dgst)); err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-09-15 19:37:32 -04:00
|
|
|
if err := os.Remove(s.contentFile(dgst)); err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMetadata sets metadata for a given ID. It fails if there's no base file.
|
2016-09-15 19:37:32 -04:00
|
|
|
func (s *fs) SetMetadata(dgst digest.Digest, key string, data []byte) error {
|
2015-11-18 17:18:07 -05:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2016-09-15 19:37:32 -04:00
|
|
|
if _, err := s.get(dgst); err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
baseDir := filepath.Join(s.metadataDir(dgst))
|
2015-11-18 17:18:07 -05:00
|
|
|
if err := os.MkdirAll(baseDir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-15 19:37:32 -04:00
|
|
|
return ioutils.AtomicWriteFile(filepath.Join(s.metadataDir(dgst), key), data, 0600)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
// GetMetadata returns metadata for a given digest.
|
|
|
|
func (s *fs) GetMetadata(dgst digest.Digest, key string) ([]byte, error) {
|
2015-11-18 17:18:07 -05:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
if _, err := s.get(dgst); err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-15 19:37:32 -04:00
|
|
|
return ioutil.ReadFile(filepath.Join(s.metadataDir(dgst), key))
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
// DeleteMetadata removes the metadata associated with a digest.
|
|
|
|
func (s *fs) DeleteMetadata(dgst digest.Digest, key string) error {
|
2015-11-18 17:18:07 -05:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
return os.RemoveAll(filepath.Join(s.metadataDir(dgst), key))
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|