Subject: [bug#55080] [PATCH shepherd] service: Gracefully handle non-existing log directories.

From: Liliana Marie Prikler <[email-masked]>
Date: Sat, 23 Apr 2022 15:11:50 +0200

* gnu/packages/services.scm (%service-file-logger): New variable,
implementing...
(service-file-logger): ... the old behaviour of this variable.  Catch system
errors from %service-file-logger and handle them.
---
 modules/shepherd/service.scm | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 013347b..567a08b 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -873,9 +873,9 @@ daemon writing FILE is running in a separate PID namespace."
               (try-again)
               (apply throw args)))))))
 
-(define (service-file-logger file input)
-  "Return a thunk meant to run as a fiber that reads from INPUT and logs it to
-FILE."
+(define (%service-file-logger file input)
+  "Like 'service-file-logger', but doesn't handle the case in which FILE does
+not exist."
   (let* ((fd     (open-fdes file (logior O_CREAT O_WRONLY O_APPEND) #o640))
          (output (fdopen fd "al")))
     (set-port-encoding! output "UTF-8")
@@ -894,6 +894,19 @@ FILE."
                  (format output "~a~a~%" prefix line)
                  (loop))))))))))
 
+(define (service-file-logger file input)
+  "Return a thunk meant to run as a fiber that reads from INPUT and logs it to
+FILE."
+  (catch 'system-error
+    (lambda ()
+      (%service-file-logger file input))
+    (lambda args
+      (if (= ENOENT (system-error-errno args))
+          (begin
+            (mkdir-p (dirname file))
+            (%service-file-logger file input))
+          (apply throw args)))))
+
 (define (service-builtin-logger command input)
   "Return a thunk meant to run as a fiber that reads from INPUT and logs to
 'log-output-port'."
-- 
2.35.1