2018-02-05 21:05:59 +00:00
|
|
|
package metadata // import "github.com/docker/docker/distribution/metadata"
|
2015-11-18 22:18:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2016-04-21 00:08:47 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-11-18 22:18:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Store implements a K/V store for mapping distribution-related IDs
|
|
|
|
// to on-disk layer IDs and image IDs. The namespace identifies the type of
|
|
|
|
// mapping (i.e. "v1ids" or "artifacts"). MetadataStore is goroutine-safe.
|
|
|
|
type Store interface {
|
|
|
|
// Get retrieves data by namespace and key.
|
|
|
|
Get(namespace string, key string) ([]byte, error)
|
|
|
|
// Set writes data indexed by namespace and key.
|
|
|
|
Set(namespace, key string, value []byte) error
|
2016-01-05 22:17:42 +00:00
|
|
|
// Delete removes data indexed by namespace and key.
|
|
|
|
Delete(namespace, key string) error
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FSMetadataStore uses the filesystem to associate metadata with layer and
|
|
|
|
// image IDs.
|
|
|
|
type FSMetadataStore struct {
|
|
|
|
sync.RWMutex
|
|
|
|
basePath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFSMetadataStore creates a new filesystem-based metadata store.
|
2017-08-24 18:48:16 +00:00
|
|
|
func NewFSMetadataStore(basePath string) (*FSMetadataStore, error) {
|
2022-01-20 12:53:23 +00:00
|
|
|
if err := os.MkdirAll(basePath, 0o700); err != nil {
|
2015-11-18 22:18:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &FSMetadataStore{
|
|
|
|
basePath: basePath,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *FSMetadataStore) path(namespace, key string) string {
|
|
|
|
return filepath.Join(store.basePath, namespace, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves data by namespace and key. The data is read from a file named
|
|
|
|
// after the key, stored in the namespace's directory.
|
|
|
|
func (store *FSMetadataStore) Get(namespace string, key string) ([]byte, error) {
|
|
|
|
store.RLock()
|
|
|
|
defer store.RUnlock()
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
return os.ReadFile(store.path(namespace, key))
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set writes data indexed by namespace and key. The data is written to a file
|
|
|
|
// named after the key, stored in the namespace's directory.
|
|
|
|
func (store *FSMetadataStore) Set(namespace, key string, value []byte) error {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
|
|
|
|
path := store.path(namespace, key)
|
2022-01-20 12:53:23 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
2015-11-18 22:18:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-20 12:53:23 +00:00
|
|
|
return ioutils.AtomicWriteFile(path, value, 0o644)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
2016-01-05 22:17:42 +00:00
|
|
|
|
|
|
|
// Delete removes data indexed by namespace and key. The data file named after
|
|
|
|
// the key, stored in the namespace's directory is deleted.
|
|
|
|
func (store *FSMetadataStore) Delete(namespace, key string) error {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
|
|
|
|
path := store.path(namespace, key)
|
|
|
|
return os.Remove(path)
|
|
|
|
}
|