diff options
| author | Ophestra Umiker <cat@ophivana.moe> | 2024-09-17 13:48:42 +0900 |
|---|---|---|
| committer | Ophestra Umiker <cat@ophivana.moe> | 2024-09-17 13:48:42 +0900 |
| commit | 4b7d616862dc7d8c73561b85deb0ca1fabf30781 (patch) | |
| tree | d069af086d1fcfeca2f9a629e0f3eb3e2f19500f /internal/state | |
| parent | 6a6f62efa672bdb439c2f9056055193a53c1df01 (diff) | |
exit: move final and early code to internal package
Exit cleanup state information is now stored in a dedicated struct and built up using methods of that struct.
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'internal/state')
| -rw-r--r-- | internal/state/data.go | 14 | ||||
| -rw-r--r-- | internal/state/enablement.go | 34 | ||||
| -rw-r--r-- | internal/state/print.go | 5 | ||||
| -rw-r--r-- | internal/state/track.go | 4 |
4 files changed, 17 insertions, 40 deletions
diff --git a/internal/state/data.go b/internal/state/data.go index 65b6395e..45493885 100644 --- a/internal/state/data.go +++ b/internal/state/data.go @@ -4,6 +4,8 @@ import ( "encoding/gob" "os" "path" + + "git.ophivana.moe/cat/fortify/internal" ) // we unfortunately have to assume there are never races between processes @@ -14,10 +16,12 @@ type launcherState struct { Launcher string Argv []string Command []string - Capability Enablements + Capability internal.Enablements } -func ReadLaunchers(runDirPath, uid string) ([]*launcherState, error) { +// ReadLaunchers reads all launcher state file entries for the requested user +// and if decode is true decodes these launchers as well. +func ReadLaunchers(runDirPath, uid string, decode bool) ([]*launcherState, error) { var f *os.File var r []*launcherState launcherPrefix := path.Join(runDirPath, uid) @@ -39,7 +43,11 @@ func ReadLaunchers(runDirPath, uid string) ([]*launcherState, error) { var s launcherState r = append(r, &s) - return gob.NewDecoder(f).Decode(&s) + if decode { + return gob.NewDecoder(f).Decode(&s) + } else { + return nil + } } }(); err != nil { return nil, err diff --git a/internal/state/enablement.go b/internal/state/enablement.go deleted file mode 100644 index 9358f432..00000000 --- a/internal/state/enablement.go +++ /dev/null @@ -1,34 +0,0 @@ -package state - -type ( - Enablement uint8 - Enablements uint64 -) - -const ( - EnableWayland Enablement = iota - EnableX - EnableDBus - EnablePulse - - enableLength -) - -var enablementString = [enableLength]string{ - "Wayland", - "X11", - "D-Bus", - "PulseAudio", -} - -func (e Enablement) String() string { - return enablementString[e] -} - -func (e Enablement) Mask() Enablements { - return 1 << e -} - -func (es Enablements) Has(e Enablement) bool { - return es&e.Mask() != 0 -} diff --git a/internal/state/print.go b/internal/state/print.go index 83912d32..838cacb0 100644 --- a/internal/state/print.go +++ b/internal/state/print.go @@ -7,6 +7,7 @@ import ( "strings" "text/tabwriter" + "git.ophivana.moe/cat/fortify/internal" "git.ophivana.moe/cat/fortify/internal/verbose" ) @@ -31,7 +32,7 @@ func MustPrintLauncherStateGlobal(w **tabwriter.Writer, runDirPath string) { } func MustPrintLauncherState(w **tabwriter.Writer, runDirPath, uid string) { - launchers, err := ReadLaunchers(runDirPath, uid) + launchers, err := ReadLaunchers(runDirPath, uid, true) if err != nil { fmt.Println("Error reading launchers:", err) os.Exit(1) @@ -49,7 +50,7 @@ func MustPrintLauncherState(w **tabwriter.Writer, runDirPath, uid string) { for _, state := range launchers { enablementsDescription := strings.Builder{} - for i := Enablement(0); i < enableLength; i++ { + for i := internal.Enablement(0); i < internal.EnableLength; i++ { if state.Capability.Has(i) { enablementsDescription.WriteString(", " + i.String()) } diff --git a/internal/state/track.go b/internal/state/track.go index 308be28a..8ecad134 100644 --- a/internal/state/track.go +++ b/internal/state/track.go @@ -8,10 +8,12 @@ import ( "os/exec" "path" "strconv" + + "git.ophivana.moe/cat/fortify/internal" ) // SaveProcess called after process start, before wait -func SaveProcess(uid string, cmd *exec.Cmd, runDirPath string, command []string, enablements Enablements) (string, error) { +func SaveProcess(uid string, cmd *exec.Cmd, runDirPath string, command []string, enablements internal.Enablements) (string, error) { statePath := path.Join(runDirPath, uid, strconv.Itoa(cmd.Process.Pid)) state := launcherState{ PID: cmd.Process.Pid, |
