From 62cb8a91b603546314034ded1e9d6ddfbd6ef106 Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Sun, 22 Sep 2024 00:29:36 +0900 Subject: app: clean up interactions and handle all application state and setup/teardown There was an earlier attempt of cleaning up the app package however it ended up creating even more of a mess and the code structure largely still looked like Ego with state setup scattered everywhere and a bunch of ugly hacks had to be implemented to keep track of all of them. In this commit the entire app package is rewritten to track everything that has to do with an app in one thread safe value. In anticipation of the client/server split also made changes: - Console messages are cleaned up to be consistent - State tracking is fully rewritten to be cleaner and usable for multiple process and client/server - Encapsulate errors to easier identify type of action causing the error as well as additional info - System-level setup operations is grouped in a way that can be collectively committed/reverted and gracefully handles errors returned by each operation - Resource sharing is made more fine-grained with PID-scoped resources whenever possible, a few remnants (X11, Wayland, PulseAudio) will be addressed when a generic proxy is available - Application setup takes a JSON-friendly config struct and deterministically generates system setup operations Signed-off-by: Ophestra Umiker --- internal/app/ensure.go | 63 -------------------------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 internal/app/ensure.go (limited to 'internal/app/ensure.go') diff --git a/internal/app/ensure.go b/internal/app/ensure.go deleted file mode 100644 index ed283a94..00000000 --- a/internal/app/ensure.go +++ /dev/null @@ -1,63 +0,0 @@ -package app - -import ( - "errors" - "fmt" - "io/fs" - "os" - "path" - - "git.ophivana.moe/cat/fortify/acl" - "git.ophivana.moe/cat/fortify/internal" - "git.ophivana.moe/cat/fortify/internal/verbose" -) - -func (a *App) EnsureRunDir() { - if err := os.Mkdir(a.runDirPath, 0700); err != nil && !errors.Is(err, fs.ErrExist) { - internal.Fatal("Error creating runtime directory:", err) - } -} - -func (a *App) EnsureRuntime() { - if s, err := os.Stat(a.runtimePath); err != nil { - if errors.Is(err, fs.ErrNotExist) { - internal.Fatal("Runtime directory does not exist") - } - internal.Fatal("Error accessing runtime directory:", err) - } else if !s.IsDir() { - internal.Fatal(fmt.Sprintf("Path '%s' is not a directory", a.runtimePath)) - } else { - if err = acl.UpdatePerm(a.runtimePath, a.UID(), acl.Execute); err != nil { - internal.Fatal("Error preparing runtime directory:", err) - } else { - a.exit.RegisterRevertPath(a.runtimePath) - } - verbose.Printf("Runtime data dir '%s' configured\n", a.runtimePath) - } -} - -func (a *App) EnsureShare() { - // acl is unnecessary as this directory is world executable - if err := os.Mkdir(a.sharePath, 0701); err != nil && !errors.Is(err, fs.ErrExist) { - internal.Fatal("Error creating shared directory:", err) - } - - // workaround for launch method sudo - if a.LaunchOption() == LaunchMethodSudo { - // ensure child runtime directory (e.g. `/tmp/fortify.%d/%d.share`) - cr := path.Join(a.sharePath, a.Uid+".share") - if err := os.Mkdir(cr, 0700); err != nil && !errors.Is(err, fs.ErrExist) { - internal.Fatal("Error creating child runtime directory:", err) - } else { - if err = acl.UpdatePerm(cr, a.UID(), acl.Read, acl.Write, acl.Execute); err != nil { - internal.Fatal("Error preparing child runtime directory:", err) - } else { - a.exit.RegisterRevertPath(cr) - } - a.AppendEnv("XDG_RUNTIME_DIR", cr) - a.AppendEnv("XDG_SESSION_CLASS", "user") - a.AppendEnv("XDG_SESSION_TYPE", "tty") - verbose.Printf("Child runtime data dir '%s' configured\n", cr) - } - } -} -- cgit v1.3.1