Files
Reims-Go/internal/dataroot/dataroot.go
T
NwflowerandClaude Opus 5.5 b203bde650 refactor: merge duplicated helpers and the window event contract
Events to the frontend go through App.emit, and a test keeps their names
in step with wails.ts. QEMU argument splitting, hidden console processes,
the macOS release record, the running check and a few smaller helpers
now exist once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:09:19 -07:00

153 lines
5.0 KiB
Go

// Package dataroot is the one way the GUI, first-run setup, switching the
// data folder and the CLI open a data folder: it checks the layout version,
// migrates under a cross-process lock when needed, and says whether the
// folder may be written.
//
// A folder this build cannot safely write (written by a newer release, a
// damaged version stamp, a failed migration) opens read-only, and opening it
// that way creates or changes nothing, not even the lock file.
package dataroot
import (
"context"
"errors"
"os"
"path/filepath"
"time"
"github.com/Nwflower/reims-go/internal/datamigrate"
"github.com/Nwflower/reims-go/internal/filelock"
"github.com/Nwflower/reims-go/internal/i18n"
)
// LockFile is taken while a folder is migrated, so two processes opening the
// same folder at once do not both migrate it.
const LockFile = ".reims-go.lock"
// lockWait bounds how long Open waits for another process to finish
// migrating the same folder.
var lockWait = 30 * time.Second
// Reason says why a folder opened read-only.
type Reason string
const (
ReasonNewer Reason = "newer" // written by a newer release
ReasonDamaged Reason = "damaged" // version stamp cannot be read
ReasonMigrationFailed Reason = "migration_failed" // an upgrade step failed
ReasonBusy Reason = "busy" // another process kept the folder locked
)
// Status describes an opened data folder.
type Status struct {
Dir string `json:"dir"`
ReadOnly bool `json:"read_only"`
Reason Reason `json:"reason,omitempty"`
Detail string `json:"detail,omitempty"`
// Version is the layout version on disk (-1 when unknown); Known is the
// newest layout this build writes.
Version int `json:"version"`
Known int `json:"known"`
// Migrated lists the upgrade steps this Open ran.
Migrated []string `json:"migrated,omitempty"`
}
// Writable reports whether callers may write to the folder.
func (s Status) Writable() bool { return !s.ReadOnly }
// Open checks dir and brings it to this build's layout. A folder that does
// not exist yet is writable: there is nothing to migrate, and the caller
// creates it.
func Open(dir, appVersion string) Status {
return open(dir, appVersion, false)
}
// Repair is Open for a folder whose version stamp is damaged: it rebuilds the
// stamp by running every upgrade step from version 0. Only on the user's
// explicit request.
func Repair(dir, appVersion string) Status {
return open(dir, appVersion, true)
}
func open(dir, appVersion string, repair bool) Status {
st := Status{Dir: dir, Version: -1, Known: datamigrate.CurrentVersion}
if dir == "" {
return st
}
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
return st
}
// Decide without the lock first: a folder this build must not touch is
// left exactly as it is, and an up-to-date folder needs no lock at all.
stamp, err := datamigrate.ReadStamp(dir)
switch {
case err != nil && !repair:
return readOnly(st, ReasonDamaged, err.Error())
case err == nil && stamp.DataVersion > datamigrate.CurrentVersion:
st.Version = stamp.DataVersion
return readOnly(st, ReasonNewer, "")
case err == nil && stamp.DataVersion == datamigrate.CurrentVersion && datamigrate.HasStamp(dir):
st.Version = stamp.DataVersion
return st
}
ctx, cancel := context.WithTimeout(context.Background(), lockWait)
defer cancel()
release, err := filelock.Acquire(ctx, filepath.Join(dir, LockFile), 100*time.Millisecond)
if err != nil {
return readOnly(st, ReasonBusy, err.Error())
}
defer release()
// The migrator reads the stamp again under the lock, so a folder another
// process finished migrating meanwhile is left as it is.
migrate := datamigrate.Run
if repair {
migrate = datamigrate.Repair
}
res, err := migrate(dir, appVersion)
st.Version = res.To
switch {
case errors.Is(err, datamigrate.ErrDamagedStamp):
st.Version = -1
return readOnly(st, ReasonDamaged, err.Error())
case err != nil:
return readOnly(st, ReasonMigrationFailed, err.Error())
case res.Newer:
return readOnly(st, ReasonNewer, "")
}
st.Migrated = res.Applied
return st
}
func readOnly(st Status, reason Reason, detail string) Status {
st.ReadOnly = true
st.Reason = reason
st.Detail = detail
return st
}
// Error explains why st cannot be written, for the GUI and the CLI alike. It
// is nil for a writable folder.
func Error(loc i18n.Locale, st Status) error {
if !st.ReadOnly {
return nil
}
var cause error
if st.Detail != "" {
cause = errors.New(st.Detail)
}
vars := i18n.Vars{"dir": st.Dir, "version": st.Version, "known": st.Known}
switch st.Reason {
case ReasonNewer:
return i18n.NewCodedError(loc, "E_DATAROOT_NEWER", "err.dataroot.newer", vars, cause)
case ReasonDamaged:
return i18n.NewCodedError(loc, "E_DATAROOT_DAMAGED", "err.dataroot.damaged", vars, cause)
case ReasonBusy:
return i18n.NewCodedError(loc, "E_DATAROOT_LOCKED", "err.dataroot.locked", vars, cause)
default:
return i18n.NewCodedError(loc, "E_DATAROOT_MIGRATION", "err.dataroot.migration_failed", vars, cause)
}
}