139 lines
4.0 KiB
Go
139 lines
4.0 KiB
Go
// Package store keeps the lab records in one JSON file.
|
|
//
|
|
// labd holds a few dozen labs at most, so the whole file is rewritten on
|
|
// every change: the new content goes to a temporary file in the same
|
|
// directory, which is synced, renamed over the old file, and followed by a
|
|
// sync of the directory. A crash leaves either the old or the new file,
|
|
// never a mix. This needs no database engine, no cgo, and no extra
|
|
// dependency, and an operator can read the file during an incident. The file
|
|
// holds variant seeds, so it is created with mode 0600 in a directory that
|
|
// only the labd user can read.
|
|
package store
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
"lincodex/labd/internal/lab"
|
|
)
|
|
|
|
const (
|
|
fileVersion = 1
|
|
fileMode = 0o600
|
|
// maxFileBytes refuses a state file that cannot come from labd.
|
|
maxFileBytes = 16 << 20
|
|
)
|
|
|
|
type document struct {
|
|
Version int `json:"version"`
|
|
Labs []lab.Lab `json:"labs"`
|
|
}
|
|
|
|
// Store reads and writes the state file.
|
|
type Store struct {
|
|
path string
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// New returns a store for the state file at path. The directory must exist.
|
|
func New(path string) (*Store, error) {
|
|
if !filepath.IsAbs(path) {
|
|
return nil, errors.New("store.New - the state file path must be absolute")
|
|
}
|
|
info, err := os.Stat(filepath.Dir(path))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store.New - state directory: %w", err)
|
|
}
|
|
if !info.IsDir() {
|
|
return nil, errors.New("store.New - the parent of the state file is not a directory")
|
|
}
|
|
return &Store{path: path}, nil
|
|
}
|
|
|
|
// Load returns the stored labs. A missing file is an empty store; a file
|
|
// that cannot be decoded is an error, because labd must not forget labs whose
|
|
// LXD objects may still exist.
|
|
func (s *Store) Load() ([]lab.Lab, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
content, err := os.ReadFile(s.path)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Store.Load - read: %w", err)
|
|
}
|
|
if len(content) > maxFileBytes {
|
|
return nil, errors.New("Store.Load - the state file is too large")
|
|
}
|
|
var decoded document
|
|
if err := json.Unmarshal(content, &decoded); err != nil {
|
|
return nil, fmt.Errorf("Store.Load - decode: %w", err)
|
|
}
|
|
if decoded.Version != fileVersion {
|
|
return nil, fmt.Errorf("Store.Load - unsupported state file version %d", decoded.Version)
|
|
}
|
|
return decoded.Labs, nil
|
|
}
|
|
|
|
// Save replaces the stored labs atomically and durably.
|
|
func (s *Store) Save(labs []lab.Lab) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if labs == nil {
|
|
labs = []lab.Lab{}
|
|
}
|
|
content, err := json.MarshalIndent(document{Version: fileVersion, Labs: labs}, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("Store.Save - encode: %w", err)
|
|
}
|
|
directory := filepath.Dir(s.path)
|
|
temporary, err := os.CreateTemp(directory, ".labs-*.json")
|
|
if err != nil {
|
|
return fmt.Errorf("Store.Save - create temporary file: %w", err)
|
|
}
|
|
name := temporary.Name()
|
|
committed := false
|
|
defer func() {
|
|
if !committed {
|
|
_ = temporary.Close() // Already closed on the success path.
|
|
_ = os.Remove(name) // Best effort; a stale temporary file is harmless.
|
|
}
|
|
}()
|
|
if err := temporary.Chmod(fileMode); err != nil {
|
|
return fmt.Errorf("Store.Save - chmod: %w", err)
|
|
}
|
|
if _, err := temporary.Write(append(content, '\n')); err != nil {
|
|
return fmt.Errorf("Store.Save - write: %w", err)
|
|
}
|
|
if err := temporary.Sync(); err != nil {
|
|
return fmt.Errorf("Store.Save - sync file: %w", err)
|
|
}
|
|
if err := temporary.Close(); err != nil {
|
|
return fmt.Errorf("Store.Save - close: %w", err)
|
|
}
|
|
if err := os.Rename(name, s.path); err != nil {
|
|
return fmt.Errorf("Store.Save - rename: %w", err)
|
|
}
|
|
committed = true
|
|
return syncDirectory(directory)
|
|
}
|
|
|
|
// syncDirectory makes the rename durable.
|
|
func syncDirectory(path string) error {
|
|
directory, err := os.Open(path)
|
|
if err != nil {
|
|
return fmt.Errorf("store.syncDirectory - open: %w", err)
|
|
}
|
|
defer func() { _ = directory.Close() }() // Read-only handle.
|
|
if err := directory.Sync(); err != nil {
|
|
return fmt.Errorf("store.syncDirectory - sync: %w", err)
|
|
}
|
|
return nil
|
|
}
|