From f6ab6d97b9f706206f4d2c7eff073686905ed6b6 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Tue, 30 Dec 2025 10:47:47 +0800 Subject: [PATCH] fix(logging): add `isDirWritable` utility to enhance log dir validation in `ConfigureLogOutput` --- internal/logging/global_logger.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/logging/global_logger.go b/internal/logging/global_logger.go index d2fbd425..f87b10e4 100644 --- a/internal/logging/global_logger.go +++ b/internal/logging/global_logger.go @@ -84,6 +84,26 @@ func SetupBaseLogger() { }) } +// isDirWritable checks if the specified directory exists and is writable by attempting to create and remove a test file. +func isDirWritable(dir string) bool { + info, err := os.Stat(dir) + if err != nil || !info.IsDir() { + return false + } + + testFile := filepath.Join(dir, ".perm_test") + f, err := os.Create(testFile) + if err != nil { + return false + } + + defer func() { + _ = f.Close() + _ = os.Remove(testFile) + }() + return true +} + // ConfigureLogOutput switches the global log destination between rotating files and stdout. // When logsMaxTotalSizeMB > 0, a background cleaner removes the oldest log files in the logs directory // until the total size is within the limit. @@ -96,7 +116,7 @@ func ConfigureLogOutput(cfg *config.Config) error { logDir := "logs" if base := util.WritablePath(); base != "" { logDir = filepath.Join(base, "logs") - } else { + } else if !isDirWritable(logDir) { logDir = filepath.Join(cfg.AuthDir, "logs") }