2020-02-17 16:57:15 -05:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
2020-05-27 16:27:35 -05:00
|
|
|
"path/filepath"
|
2020-02-17 16:57:15 -05:00
|
|
|
"strings"
|
2021-10-21 08:05:50 -05:00
|
|
|
"time"
|
2020-02-17 16:57:15 -05:00
|
|
|
|
|
|
|
"github.com/anuvu/zot/errors"
|
|
|
|
zlog "github.com/anuvu/zot/pkg/log"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-10-21 08:05:50 -05:00
|
|
|
BlobsCache = "blobs"
|
|
|
|
dbCacheLockCheckTimeout = 10 * time.Second
|
2020-02-17 16:57:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Cache struct {
|
2020-05-27 16:27:35 -05:00
|
|
|
rootDir string
|
|
|
|
db *bbolt.DB
|
|
|
|
log zlog.Logger
|
2020-02-17 16:57:15 -05:00
|
|
|
}
|
|
|
|
|
2020-05-11 17:13:24 -05:00
|
|
|
// Blob is a blob record.
|
2020-02-17 16:57:15 -05:00
|
|
|
type Blob struct {
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCache(rootDir string, name string, log zlog.Logger) *Cache {
|
|
|
|
dbPath := path.Join(rootDir, name+".db")
|
2021-10-21 08:05:50 -05:00
|
|
|
dbOpts := &bbolt.Options{
|
|
|
|
Timeout: dbCacheLockCheckTimeout,
|
|
|
|
FreelistType: bbolt.FreelistArrayType,
|
|
|
|
}
|
|
|
|
db, err := bbolt.Open(dbPath, 0600, dbOpts)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Str("dbPath", dbPath).Msg("unable to create cache db")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
if _, err := tx.CreateBucketIfNotExists([]byte(BlobsCache)); err != nil {
|
|
|
|
// this is a serious failure
|
|
|
|
log.Error().Err(err).Str("dbPath", dbPath).Msg("unable to create a root bucket")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
// something went wrong
|
|
|
|
log.Error().Err(err).Msg("unable to create a cache")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 16:27:35 -05:00
|
|
|
return &Cache{rootDir: rootDir, db: db, log: log}
|
2020-02-17 16:57:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) PutBlob(digest string, path string) error {
|
2021-07-20 16:04:10 -05:00
|
|
|
if path == "" {
|
|
|
|
c.log.Error().Err(errors.ErrEmptyValue).Str("digest", digest).Msg("empty path provided")
|
|
|
|
|
|
|
|
return errors.ErrEmptyValue
|
|
|
|
}
|
|
|
|
|
2020-05-27 16:27:35 -05:00
|
|
|
// use only relative (to rootDir) paths on blobs
|
|
|
|
relp, err := filepath.Rel(c.rootDir, path)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Error().Err(err).Str("path", path).Msg("unable to get relative path")
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:57:15 -05:00
|
|
|
if err := c.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
root := tx.Bucket([]byte(BlobsCache))
|
|
|
|
if root == nil {
|
|
|
|
// this is a serious failure
|
|
|
|
err := errors.ErrCacheRootBucket
|
|
|
|
c.log.Error().Err(err).Msg("unable to access root bucket")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b, err := root.CreateBucketIfNotExists([]byte(digest))
|
|
|
|
if err != nil {
|
|
|
|
// this is a serious failure
|
|
|
|
c.log.Error().Err(err).Str("bucket", digest).Msg("unable to create a bucket")
|
|
|
|
return err
|
|
|
|
}
|
2020-05-27 16:27:35 -05:00
|
|
|
if err := b.Put([]byte(relp), nil); err != nil {
|
|
|
|
c.log.Error().Err(err).Str("bucket", digest).Str("value", relp).Msg("unable to put record")
|
2020-02-17 16:57:15 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) GetBlob(digest string) (string, error) {
|
|
|
|
var blobPath strings.Builder
|
|
|
|
|
|
|
|
if err := c.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
root := tx.Bucket([]byte(BlobsCache))
|
|
|
|
if root == nil {
|
|
|
|
// this is a serious failure
|
|
|
|
err := errors.ErrCacheRootBucket
|
|
|
|
c.log.Error().Err(err).Msg("unable to access root bucket")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := root.Bucket([]byte(digest))
|
|
|
|
if b != nil {
|
|
|
|
// get first key
|
|
|
|
c := b.Cursor()
|
|
|
|
k, _ := c.First()
|
|
|
|
blobPath.WriteString(string(k))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.ErrCacheMiss
|
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobPath.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) HasBlob(digest string, blob string) bool {
|
|
|
|
if err := c.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
root := tx.Bucket([]byte(BlobsCache))
|
|
|
|
if root == nil {
|
|
|
|
// this is a serious failure
|
|
|
|
err := errors.ErrCacheRootBucket
|
|
|
|
c.log.Error().Err(err).Msg("unable to access root bucket")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := root.Bucket([]byte(digest))
|
|
|
|
if b == nil {
|
|
|
|
return errors.ErrCacheMiss
|
|
|
|
}
|
|
|
|
if b.Get([]byte(blob)) == nil {
|
|
|
|
return errors.ErrCacheMiss
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DeleteBlob(digest string, path string) error {
|
2020-05-27 16:27:35 -05:00
|
|
|
// use only relative (to rootDir) paths on blobs
|
|
|
|
relp, err := filepath.Rel(c.rootDir, path)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Error().Err(err).Str("path", path).Msg("unable to get relative path")
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:57:15 -05:00
|
|
|
if err := c.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
root := tx.Bucket([]byte(BlobsCache))
|
|
|
|
if root == nil {
|
|
|
|
// this is a serious failure
|
|
|
|
err := errors.ErrCacheRootBucket
|
|
|
|
c.log.Error().Err(err).Msg("unable to access root bucket")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := root.Bucket([]byte(digest))
|
|
|
|
if b == nil {
|
|
|
|
return errors.ErrCacheMiss
|
|
|
|
}
|
|
|
|
|
2020-05-27 16:27:35 -05:00
|
|
|
if err := b.Delete([]byte(relp)); err != nil {
|
|
|
|
c.log.Error().Err(err).Str("digest", digest).Str("path", relp).Msg("unable to delete")
|
2020-02-17 16:57:15 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:17:24 -05:00
|
|
|
cur := b.Cursor()
|
|
|
|
k, _ := cur.First()
|
|
|
|
|
|
|
|
if k == nil {
|
2020-05-27 16:27:35 -05:00
|
|
|
c.log.Debug().Str("digest", digest).Str("path", relp).Msg("deleting empty bucket")
|
2020-04-06 20:17:24 -05:00
|
|
|
if err := root.DeleteBucket([]byte(digest)); err != nil {
|
2020-05-27 16:27:35 -05:00
|
|
|
c.log.Error().Err(err).Str("digest", digest).Str("path", relp).Msg("unable to delete")
|
2020-04-06 20:17:24 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:57:15 -05:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|