From e3f93c9c215e5ec0a8b612a7478b3b3c8975105c Mon Sep 17 00:00:00 2001 From: AdamMagued Date: Wed, 11 Nov 2026 12:53:23 +0200 Subject: [PATCH] feat(config): add appropriate default directories for non-Linux platforms Introduce platform-specific directory defaults for Windows and Darwin/BSD: - Windows: default configuration in %ProgramData%\maddy, state in %ProgramData%\maddy\data, runtime in %ProgramData%\maddy\run, and libexec in %ProgramFiles%\maddy with C:\ fallback helpers - Darwin / BSD: default runtime directory to /var/run/maddy (since /run does not exist) and libexec to /usr/lib/maddy - Fix handleSignals signature in signal_nonposix.go to allow non-posix compilation in reload loop - Add unit tests verifying default directory constants and Windows path helper logic Fixes #290 Assisted-by: Gemini CLI Agent (gemini-2.5-pro) Signed-off-by: AdamMagued --- directories.go | 4 +-- directories_bsd.go | 47 ++++++++++++++++++++++++++ directories_test.go | 67 +++++++++++++++++++++++++++++++++++++ directories_windows.go | 58 ++++++++++++++++++++++++++++++++ directories_windows_test.go | 59 ++++++++++++++++++++++++++++++++ signal_nonposix.go | 6 ++-- 6 files changed, 236 insertions(+), 5 deletions(-) create mode 100644 directories_bsd.go create mode 100644 directories_test.go create mode 100644 directories_windows.go create mode 100644 directories_windows_test.go diff --git a/directories.go b/directories.go index b3930db6..551ab60d 100644 --- a/directories.go +++ b/directories.go @@ -1,5 +1,5 @@ -//go:build !docker -// +build !docker +//go:build !windows && !darwin && !dragonfly && !freebsd && !netbsd && !openbsd && !docker +// +build !windows,!darwin,!dragonfly,!freebsd,!netbsd,!openbsd,!docker package maddy diff --git a/directories_bsd.go b/directories_bsd.go new file mode 100644 index 00000000..3aef6bbb --- /dev/null +++ b/directories_bsd.go @@ -0,0 +1,47 @@ +//go:build (darwin || dragonfly || freebsd || netbsd || openbsd) && !docker +// +build darwin dragonfly freebsd netbsd openbsd +// +build !docker + +package maddy + +var ( + // ConfigDirectory specifies platform-specific value + // that should be used as a location of default configuration. + // + // It should not be changed and is defined as a variable + // only for purposes of modification using -X linker flag. + ConfigDirectory = "/etc/maddy" + + // DefaultStateDirectory specifies platform-specific + // default for StateDirectory. + // + // Most code should use StateDirectory instead since + // it will contain the effective location of the state + // directory. + // + // It should not be changed and is defined as a variable + // only for purposes of modification using -X linker flag. + DefaultStateDirectory = "/var/lib/maddy" + + // DefaultRuntimeDirectory specifies platform-specific + // default for RuntimeDirectory. + // + // Most code should use RuntimeDirectory instead since + // it will contain the effective location of the state + // directory. + // + // It should not be changed and is defined as a variable + // only for purposes of modification using -X linker flag. + DefaultRuntimeDirectory = "/var/run/maddy" + + // DefaultLibexecDirectory specifies platform-specific + // default for LibexecDirectory. + // + // Most code should use LibexecDirectory since it will + // contain the effective location of the libexec + // directory. + // + // It should not be changed and is defined as a variable + // only for purposes of modification using -X linker flag. + DefaultLibexecDirectory = "/usr/lib/maddy" +) diff --git a/directories_test.go b/directories_test.go new file mode 100644 index 00000000..30311d44 --- /dev/null +++ b/directories_test.go @@ -0,0 +1,67 @@ +//go:build !docker +// +build !docker + +package maddy + +import ( + "path/filepath" + "runtime" + "testing" +) + +func TestDefaultDirectories(t *testing.T) { + if ConfigDirectory == "" { + t.Error("ConfigDirectory is empty") + } + if DefaultStateDirectory == "" { + t.Error("DefaultStateDirectory is empty") + } + if DefaultRuntimeDirectory == "" { + t.Error("DefaultRuntimeDirectory is empty") + } + if DefaultLibexecDirectory == "" { + t.Error("DefaultLibexecDirectory is empty") + } + + switch runtime.GOOS { + case "linux": + if ConfigDirectory != "/etc/maddy" { + t.Errorf("ConfigDirectory on Linux: got %q, want %q", ConfigDirectory, "/etc/maddy") + } + if DefaultStateDirectory != "/var/lib/maddy" { + t.Errorf("DefaultStateDirectory on Linux: got %q, want %q", DefaultStateDirectory, "/var/lib/maddy") + } + if DefaultRuntimeDirectory != "/run/maddy" { + t.Errorf("DefaultRuntimeDirectory on Linux: got %q, want %q", DefaultRuntimeDirectory, "/run/maddy") + } + if DefaultLibexecDirectory != "/usr/lib/maddy" { + t.Errorf("DefaultLibexecDirectory on Linux: got %q, want %q", DefaultLibexecDirectory, "/usr/lib/maddy") + } + case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd": + if ConfigDirectory != "/etc/maddy" { + t.Errorf("ConfigDirectory on BSD/Darwin: got %q, want %q", ConfigDirectory, "/etc/maddy") + } + if DefaultStateDirectory != "/var/lib/maddy" { + t.Errorf("DefaultStateDirectory on BSD/Darwin: got %q, want %q", DefaultStateDirectory, "/var/lib/maddy") + } + if DefaultRuntimeDirectory != "/var/run/maddy" { + t.Errorf("DefaultRuntimeDirectory on BSD/Darwin: got %q, want %q", DefaultRuntimeDirectory, "/var/run/maddy") + } + if DefaultLibexecDirectory != "/usr/lib/maddy" { + t.Errorf("DefaultLibexecDirectory on BSD/Darwin: got %q, want %q", DefaultLibexecDirectory, "/usr/lib/maddy") + } + case "windows": + if !filepath.IsAbs(ConfigDirectory) { + t.Errorf("ConfigDirectory on Windows should be absolute: got %q", ConfigDirectory) + } + if !filepath.IsAbs(DefaultStateDirectory) { + t.Errorf("DefaultStateDirectory on Windows should be absolute: got %q", DefaultStateDirectory) + } + if !filepath.IsAbs(DefaultRuntimeDirectory) { + t.Errorf("DefaultRuntimeDirectory on Windows should be absolute: got %q", DefaultRuntimeDirectory) + } + if !filepath.IsAbs(DefaultLibexecDirectory) { + t.Errorf("DefaultLibexecDirectory on Windows should be absolute: got %q", DefaultLibexecDirectory) + } + } +} diff --git a/directories_windows.go b/directories_windows.go new file mode 100644 index 00000000..4a850954 --- /dev/null +++ b/directories_windows.go @@ -0,0 +1,58 @@ +//go:build windows && !docker +// +build windows,!docker + +package maddy + +import ( + "os" + "path/filepath" +) + +const ( + defaultProgramData = `C:\ProgramData` + defaultProgramFiles = `C:\Program Files` +) + +func programData() string { + if dir := os.Getenv("ProgramData"); dir != "" { + return dir + } + return defaultProgramData +} + +func programFiles() string { + if dir := os.Getenv("ProgramFiles"); dir != "" { + return dir + } + return defaultProgramFiles +} + +var ( + // ConfigDirectory specifies platform-specific value + // that should be used as a location of default configuration. + ConfigDirectory = filepath.Join(programData(), `maddy`) + + // DefaultStateDirectory specifies platform-specific + // default for StateDirectory. + // + // Most code should use StateDirectory instead since + // it will contain the effective location of the state + // directory. + DefaultStateDirectory = filepath.Join(programData(), `maddy`, `data`) + + // DefaultRuntimeDirectory specifies platform-specific + // default for RuntimeDirectory. + // + // Most code should use RuntimeDirectory instead since + // it will contain the effective location of the runtime + // directory. + DefaultRuntimeDirectory = filepath.Join(programData(), `maddy`, `run`) + + // DefaultLibexecDirectory specifies platform-specific + // default for LibexecDirectory. + // + // Most code should use LibexecDirectory since it will + // contain the effective location of the libexec + // directory. + DefaultLibexecDirectory = filepath.Join(programFiles(), `maddy`) +) diff --git a/directories_windows_test.go b/directories_windows_test.go new file mode 100644 index 00000000..7fc2a23e --- /dev/null +++ b/directories_windows_test.go @@ -0,0 +1,59 @@ +//go:build windows && !docker +// +build windows,!docker + +package maddy + +import ( + "path/filepath" + "testing" +) + +func TestWindowsDirectoryHelpers(t *testing.T) { + t.Run("programData default fallback", func(t *testing.T) { + t.Setenv("ProgramData", "") + if got := programData(); got != defaultProgramData { + t.Errorf("programData(): got %q, want %q", got, defaultProgramData) + } + }) + + t.Run("programData from env", func(t *testing.T) { + want := `D:\CustomData` + t.Setenv("ProgramData", want) + if got := programData(); got != want { + t.Errorf("programData(): got %q, want %q", got, want) + } + }) + + t.Run("programFiles default fallback", func(t *testing.T) { + t.Setenv("ProgramFiles", "") + if got := programFiles(); got != defaultProgramFiles { + t.Errorf("programFiles(): got %q, want %q", got, defaultProgramFiles) + } + }) + + t.Run("programFiles from env", func(t *testing.T) { + want := `D:\CustomFiles` + t.Setenv("ProgramFiles", want) + if got := programFiles(); got != want { + t.Errorf("programFiles(): got %q, want %q", got, want) + } + }) + + t.Run("initialized directory variables", func(t *testing.T) { + wantData := programData() + wantFiles := programFiles() + + if got, want := ConfigDirectory, filepath.Join(wantData, `maddy`); got != want { + t.Errorf("ConfigDirectory: got %q, want %q", got, want) + } + if got, want := DefaultStateDirectory, filepath.Join(wantData, `maddy`, `data`); got != want { + t.Errorf("DefaultStateDirectory: got %q, want %q", got, want) + } + if got, want := DefaultRuntimeDirectory, filepath.Join(wantData, `maddy`, `run`); got != want { + t.Errorf("DefaultRuntimeDirectory: got %q, want %q", got, want) + } + if got, want := DefaultLibexecDirectory, filepath.Join(wantFiles, `maddy`); got != want { + t.Errorf("DefaultLibexecDirectory: got %q, want %q", got, want) + } + }) +} diff --git a/signal_nonposix.go b/signal_nonposix.go index f5750a61..987a31ea 100644 --- a/signal_nonposix.go +++ b/signal_nonposix.go @@ -29,17 +29,17 @@ import ( "github.com/foxcpp/maddy/framework/log" ) -func handleSignals() os.Signal { +func handleSignals() (reload bool) { sig := make(chan os.Signal, 5) signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT) s := <-sig go func() { - s := handleSignals() + handleSignals() log.Printf("forced shutdown due to signal (%v)!", s) os.Exit(1) }() log.Printf("signal received (%v)", s) - return s + return false }