2023-12-23 15:50:47 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base32"
|
2024-01-02 13:33:50 +03:00
|
|
|
"errors"
|
2023-12-23 15:50:47 +03:00
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-12-24 14:59:05 +03:00
|
|
|
"time"
|
2023-12-23 15:50:47 +03:00
|
|
|
|
|
|
|
"github.com/adrg/xdg"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
modifiedBucket = "modified"
|
|
|
|
)
|
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
// Entry represents a cache entry, indicating the last size and modified time for a file path.
|
|
|
|
type Entry struct {
|
|
|
|
Size int64
|
|
|
|
Modified time.Time
|
|
|
|
}
|
|
|
|
|
2023-12-23 15:50:47 +03:00
|
|
|
var db *bolt.DB
|
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
// Open creates an instance of bolt.DB for a given treeRoot path.
|
|
|
|
// If clean is true, Open will delete any existing data in the cache.
|
|
|
|
//
|
|
|
|
// The database will be located in `XDG_CACHE_DIR/treefmt/eval-cache/<id>.db`, where <id> is determined by hashing
|
|
|
|
// the treeRoot path. This associates a given treeRoot with a given instance of the cache.
|
2023-12-23 15:50:47 +03:00
|
|
|
func Open(treeRoot string, clean bool) (err error) {
|
|
|
|
// determine a unique and consistent db name for the tree root
|
|
|
|
h := sha1.New()
|
|
|
|
h.Write([]byte(treeRoot))
|
|
|
|
digest := h.Sum(nil)
|
|
|
|
|
|
|
|
name := base32.StdEncoding.EncodeToString(digest)
|
|
|
|
path, err := xdg.CacheFile(fmt.Sprintf("treefmt/eval-cache/%v.db", name))
|
2024-01-02 13:33:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: could not resolve local path for the cache", err)
|
|
|
|
}
|
2023-12-23 15:50:47 +03:00
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
// force a clean of the cache if specified
|
2023-12-23 15:50:47 +03:00
|
|
|
if clean {
|
|
|
|
err := os.Remove(path)
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
err = nil
|
|
|
|
} else if err != nil {
|
2024-01-02 13:33:50 +03:00
|
|
|
return fmt.Errorf("%w: failed to clear cache", err)
|
2023-12-23 15:50:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err = bolt.Open(path, 0o600, nil)
|
|
|
|
if err != nil {
|
2024-01-02 13:33:50 +03:00
|
|
|
return fmt.Errorf("%w: failed to open cache", err)
|
2023-12-23 15:50:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Update(func(tx *bolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucket([]byte(modifiedBucket))
|
|
|
|
if errors.Is(err, bolt.ErrBucketExists) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
// Close closes any open instance of the cache.
|
2023-12-23 15:50:47 +03:00
|
|
|
func Close() error {
|
2023-12-23 18:00:39 +03:00
|
|
|
if db == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-23 15:50:47 +03:00
|
|
|
return db.Close()
|
|
|
|
}
|
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
// getEntry is a helper for reading cache entries from bolt.
|
|
|
|
func getEntry(bucket *bolt.Bucket, path string) (*Entry, error) {
|
2023-12-23 16:31:08 +03:00
|
|
|
b := bucket.Get([]byte(path))
|
|
|
|
if b != nil {
|
2023-12-24 14:59:05 +03:00
|
|
|
var cached Entry
|
2023-12-23 16:31:08 +03:00
|
|
|
if err := msgpack.Unmarshal(b, &cached); err != nil {
|
2024-01-02 13:33:50 +03:00
|
|
|
return nil, fmt.Errorf("%w: failed to unmarshal cache info for path '%v'", err, path)
|
2023-12-23 16:31:08 +03:00
|
|
|
}
|
|
|
|
return &cached, nil
|
|
|
|
} else {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
// ChangeSet is used to walk a filesystem, starting at root, and outputting any new or changed paths using pathsCh.
|
|
|
|
// It determines if a path is new or has changed by comparing against cache entries.
|
2023-12-23 15:50:47 +03:00
|
|
|
func ChangeSet(ctx context.Context, root string, pathsCh chan<- string) error {
|
|
|
|
return db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket([]byte(modifiedBucket))
|
|
|
|
|
|
|
|
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2024-01-02 13:33:50 +03:00
|
|
|
return fmt.Errorf("%w: failed to walk path", err)
|
2023-12-23 15:50:47 +03:00
|
|
|
} else if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
} else if info.IsDir() {
|
|
|
|
// todo what about symlinks?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
// skip symlinks
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
cached, err := getEntry(bucket, path)
|
2023-12-23 16:31:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-12-23 15:50:47 +03:00
|
|
|
}
|
|
|
|
|
2023-12-23 16:31:08 +03:00
|
|
|
changedOrNew := cached == nil || !(cached.Modified == info.ModTime() && cached.Size == info.Size())
|
2023-12-23 15:50:47 +03:00
|
|
|
|
|
|
|
if !changedOrNew {
|
|
|
|
// no change
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass on the path
|
|
|
|
pathsCh <- path
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
// Update is used to record updated cache information for the specified list of paths.
|
2023-12-23 16:31:08 +03:00
|
|
|
func Update(paths []string) (int, error) {
|
2023-12-23 15:50:47 +03:00
|
|
|
if len(paths) == 0 {
|
2023-12-23 16:31:08 +03:00
|
|
|
return 0, nil
|
2023-12-23 15:50:47 +03:00
|
|
|
}
|
|
|
|
|
2023-12-23 16:31:08 +03:00
|
|
|
var changes int
|
|
|
|
|
|
|
|
return changes, db.Update(func(tx *bolt.Tx) error {
|
2023-12-23 15:50:47 +03:00
|
|
|
bucket := tx.Bucket([]byte(modifiedBucket))
|
|
|
|
|
|
|
|
for _, path := range paths {
|
|
|
|
if path == "" {
|
|
|
|
continue
|
|
|
|
}
|
2023-12-23 16:31:08 +03:00
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
cached, err := getEntry(bucket, path)
|
2023-12-23 16:31:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-23 15:50:47 +03:00
|
|
|
pathInfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-23 16:31:08 +03:00
|
|
|
if cached == nil || !(cached.Modified == pathInfo.ModTime() && cached.Size == pathInfo.Size()) {
|
|
|
|
changes += 1
|
|
|
|
} else {
|
|
|
|
// no change to write
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-24 14:59:05 +03:00
|
|
|
cacheInfo := Entry{
|
2023-12-23 15:50:47 +03:00
|
|
|
Size: pathInfo.Size(),
|
|
|
|
Modified: pathInfo.ModTime(),
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := msgpack.Marshal(cacheInfo)
|
|
|
|
if err != nil {
|
2024-01-02 13:33:50 +03:00
|
|
|
return fmt.Errorf("%w: failed to marshal mod time", err)
|
2023-12-23 15:50:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = bucket.Put([]byte(path), bytes); err != nil {
|
2024-01-02 13:33:50 +03:00
|
|
|
return fmt.Errorf("%w: failed to put mode time", err)
|
2023-12-23 15:50:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|