diff options
| author | Ophestra Umiker <cat@ophivana.moe> | 2024-09-16 20:31:15 +0900 |
|---|---|---|
| committer | Ophestra Umiker <cat@ophivana.moe> | 2024-09-16 20:31:35 +0900 |
| commit | 8bdae74ebe172239573bab9fca634cf350cbd29d (patch) | |
| tree | d81b8b650b53c4dddba0f417e2baf03d2a647bc2 /internal/app/ensure.go | |
| parent | d49b97b1d4fcd07a21aa14bb1727d64dd0214b09 (diff) | |
final: refactor for removal of system package and reduction of interactions to state package
State query command has been moved to main where it belongs, "system" information are now fetched in app.New and stored in *App with accessors for relevant values. Exit (cleanup-related) functions are separated into its dedicated "final" package.
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'internal/app/ensure.go')
| -rw-r--r-- | internal/app/ensure.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/internal/app/ensure.go b/internal/app/ensure.go new file mode 100644 index 00000000..9ab63ff2 --- /dev/null +++ b/internal/app/ensure.go @@ -0,0 +1,62 @@ +package app + +import ( + "errors" + "fmt" + "git.ophivana.moe/cat/fortify/internal/acl" + "git.ophivana.moe/cat/fortify/internal/final" + "git.ophivana.moe/cat/fortify/internal/verbose" + "io/fs" + "os" + "path" +) + +func (a *App) EnsureRunDir() { + if err := os.Mkdir(a.runDirPath, 0700); err != nil && !errors.Is(err, fs.ErrExist) { + final.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) { + final.Fatal("Runtime directory does not exist") + } + final.Fatal("Error accessing runtime directory:", err) + } else if !s.IsDir() { + final.Fatal(fmt.Sprintf("Path '%s' is not a directory", a.runtimePath)) + } else { + if err = acl.UpdatePerm(a.runtimePath, a.UID(), acl.Execute); err != nil { + final.Fatal("Error preparing runtime directory:", err) + } else { + final.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) { + final.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) { + final.Fatal("Error creating child runtime directory:", err) + } else { + if err = acl.UpdatePerm(cr, a.UID(), acl.Read, acl.Write, acl.Execute); err != nil { + final.Fatal("Error preparing child runtime directory:", err) + } else { + final.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) + } + } +} |
