112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
// Package artifactcache stores accepted producer artifacts without execution or
|
|
// provenance envelopes. Cache I/O is best effort at the calling operation.
|
|
package artifactcache
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/fileio"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
// FilesystemStore is safe for concurrent operations. Construction and lookup do
|
|
// not create directories; only storing a newly accepted result can do so.
|
|
type FilesystemStore struct {
|
|
root string
|
|
}
|
|
|
|
var _ contracts.ArtifactCache = (*FilesystemStore)(nil)
|
|
|
|
func NewFilesystemStore(root string) (*FilesystemStore, error) {
|
|
if strings.TrimSpace(root) == "" || strings.ContainsRune(root, '\x00') {
|
|
return nil, fmt.Errorf("artifact cache root must be a nonempty path without NUL")
|
|
}
|
|
absolute, err := filepath.Abs(root)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("resolve artifact cache root: %w", err)
|
|
}
|
|
// Retain the exact selected directory, including literal trailing spaces,
|
|
// when passing it through the common confined-path helper.
|
|
return &FilesystemStore{root: absolute + string(filepath.Separator)}, nil
|
|
}
|
|
|
|
func (store *FilesystemStore) Lookup(ctx context.Context, mode contracts.CacheMode, hash string) ([]byte, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
switch mode {
|
|
case contracts.CacheBypass, contracts.CacheRefresh:
|
|
return nil, nil
|
|
case contracts.CacheAuto, "":
|
|
default:
|
|
return nil, fmt.Errorf("unknown artifact cache mode %q", mode)
|
|
}
|
|
name, err := cacheName(hash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
target, err := fileio.SafePath(store.root, name)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("locate artifact cache entry %q: %w", hash, err)
|
|
}
|
|
info, err := os.Lstat(target)
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("inspect artifact cache entry %q: %w", hash, err)
|
|
}
|
|
if !info.Mode().IsRegular() {
|
|
return nil, fmt.Errorf("artifact cache entry %q is not a regular file", hash)
|
|
}
|
|
content, err := os.ReadFile(target)
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read artifact cache entry %q: %w", hash, err)
|
|
}
|
|
if !json.Valid(content) {
|
|
return nil, fmt.Errorf("artifact cache entry %q contains malformed JSON", hash)
|
|
}
|
|
return content, nil
|
|
}
|
|
|
|
func (store *FilesystemStore) Store(ctx context.Context, mode contracts.CacheMode, hash string, content []byte) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
switch mode {
|
|
case contracts.CacheBypass:
|
|
return nil
|
|
case contracts.CacheAuto, contracts.CacheRefresh, "":
|
|
default:
|
|
return fmt.Errorf("unknown artifact cache mode %q", mode)
|
|
}
|
|
name, err := cacheName(hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !json.Valid(content) {
|
|
return fmt.Errorf("artifact cache entry %q is not valid JSON", hash)
|
|
}
|
|
if err := fileio.WriteBytesWithProcessPermissions(store.root, name, content); err != nil {
|
|
return fmt.Errorf("store artifact cache entry %q: %w", hash, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func cacheName(hash string) (string, error) {
|
|
// The native hash is opaque: do not strip a prefix, recalculate it, encode
|
|
// it, or enforce a particular digest format. It must identify one filename.
|
|
if hash == "" || strings.ContainsAny(hash, "/\\") {
|
|
return "", fmt.Errorf("artifact cache hash must be a nonempty filename component")
|
|
}
|
|
return hash + ".json", nil
|
|
}
|