state/simple: prefix store path

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
Ophestra Umiker 2024-10-10 11:03:31 +09:00
parent f4c44a9441
commit 2220055e26
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
3 changed files with 8 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package state package state
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path" "path"
@ -9,18 +10,16 @@ import (
"text/tabwriter" "text/tabwriter"
"time" "time"
"git.ophivana.moe/cat/fortify/internal"
"git.ophivana.moe/cat/fortify/internal/verbose" "git.ophivana.moe/cat/fortify/internal/verbose"
) )
// MustPrintLauncherStateSimpleGlobal prints active launcher states of all simple stores // MustPrintLauncherStateSimpleGlobal prints active launcher states of all simple stores
// in an implementation-specific way. // in an implementation-specific way.
func MustPrintLauncherStateSimpleGlobal(w **tabwriter.Writer) { func MustPrintLauncherStateSimpleGlobal(w **tabwriter.Writer, runDir string) {
sc := internal.GetSC()
now := time.Now().UTC() now := time.Now().UTC()
// read runtime directory to get all UIDs // read runtime directory to get all UIDs
if dirs, err := os.ReadDir(sc.RunDirPath); err != nil { if dirs, err := os.ReadDir(path.Join(runDir, "state")); err != nil && !errors.Is(err, os.ErrNotExist) {
fmt.Println("cannot read runtime directory:", err) fmt.Println("cannot read runtime directory:", err)
os.Exit(1) os.Exit(1)
} else { } else {
@ -38,7 +37,7 @@ func MustPrintLauncherStateSimpleGlobal(w **tabwriter.Writer) {
} }
// obtain temporary store // obtain temporary store
s := NewSimple(sc.RunDirPath, e.Name()).(*simpleStore) s := NewSimple(runDir, e.Name()).(*simpleStore)
// print states belonging to this store // print states belonging to this store
s.mustPrintLauncherState(w, now) s.mustPrintLauncherState(w, now)

View File

@ -211,9 +211,8 @@ func (b *simpleBackend) Len() (int, error) {
} }
// NewSimple returns an instance of a file-based store. // NewSimple returns an instance of a file-based store.
// Store prefix is typically (runDir, uid). func NewSimple(runDir string, prefix ...string) Store {
func NewSimple(prefix ...string) Store {
b := new(simpleStore) b := new(simpleStore)
b.path = prefix b.path = append([]string{runDir, "state"}, prefix...)
return b return b
} }

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"text/tabwriter" "text/tabwriter"
"git.ophivana.moe/cat/fortify/internal"
"git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/state"
) )
@ -21,7 +22,7 @@ func init() {
func tryState() { func tryState() {
if stateActionEarly { if stateActionEarly {
var w *tabwriter.Writer var w *tabwriter.Writer
state.MustPrintLauncherStateSimpleGlobal(&w) state.MustPrintLauncherStateSimpleGlobal(&w, internal.GetSC().RunDirPath)
if w != nil { if w != nil {
if err := w.Flush(); err != nil { if err := w.Flush(); err != nil {
fmt.Println("warn: error formatting output:", err) fmt.Println("warn: error formatting output:", err)