Initial DDC release

This commit is contained in:
SirBlob committed 2026-10-25 20:24:50 -04:00
commit 434f6441da
57 files changed
+4606

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
package preferences
import (
"errors"
"fmt"
)
const (
gigabyte = int64(1) << 30
maxConcurrentLimit = 10
maxReservedGigabytes = 100000
)
var ErrInvalid = errors.New("invalid settings")
type Preferences struct {
MaxConcurrentDownloads int `json:"maxConcurrentDownloads"`
MinFreeGB int `json:"minFreeGB"`
AllowedPorts []uint16 `json:"allowedPorts"`
AutoExtract bool `json:"autoExtract"`
CleanupAfterMove bool `json:"cleanupAfterMove"`
}
func (preferences Preferences) MinFreeBytes() int64 {
return int64(preferences.MinFreeGB) * gigabyte
}
func (preferences Preferences) Validate() error {
if preferences.MaxConcurrentDownloads < 1 || preferences.MaxConcurrentDownloads > maxConcurrentLimit {
return fmt.Errorf("%w: parallel downloads must be between 1 and %d", ErrInvalid, maxConcurrentLimit)
}
if preferences.MinFreeGB < 0 || preferences.MinFreeGB > maxReservedGigabytes {
return fmt.Errorf("%w: free space to keep must be between 0 and %d GB", ErrInvalid, maxReservedGigabytes)
}
for _, port := range preferences.AllowedPorts {
if port == 0 {
return fmt.Errorf("%w: port 0 is not allowed", ErrInvalid)
}
}
return nil
}
+87
View File
@@ -0,0 +1,87 @@
package preferences
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"sync"
)
type Store struct {
mutex sync.RWMutex
path string
current Preferences
}
func Open(path string, defaults Preferences) (*Store, error) {
store := &Store{path: path, current: defaults}
data, err := os.ReadFile(path)
if errors.Is(err, fs.ErrNotExist) {
return store, nil
}
if err != nil {
return nil, err
}
loaded := defaults
if err := json.Unmarshal(data, &loaded); err != nil {
return nil, fmt.Errorf("settings file %s: %w", path, err)
}
if err := loaded.Validate(); err != nil {
return nil, fmt.Errorf("settings file %s: %w", path, err)
}
store.current = loaded
return store, nil
}
func (store *Store) Get() Preferences {
store.mutex.RLock()
defer store.mutex.RUnlock()
current := store.current
current.AllowedPorts = slices.Clone(current.AllowedPorts)
if current.AllowedPorts == nil {
current.AllowedPorts = []uint16{}
}
return current
}
func (store *Store) Update(next Preferences) error {
if err := next.Validate(); err != nil {
return err
}
data, err := json.MarshalIndent(next, "", " ")
if err != nil {
return err
}
store.mutex.Lock()
defer store.mutex.Unlock()
if err := writeAtomically(store.path, data); err != nil {
return err
}
store.current = next
return nil
}
func writeAtomically(path string, data []byte) error {
temporary, err := os.CreateTemp(filepath.Dir(path), ".settings-*.tmp")
if err != nil {
return err
}
_, writeErr := temporary.Write(data)
closeErr := temporary.Close()
if writeErr != nil || closeErr != nil {
os.Remove(temporary.Name())
return errors.Join(writeErr, closeErr)
}
if err := os.Rename(temporary.Name(), path); err != nil {
os.Remove(temporary.Name())
return err
}
return nil
}