Subject: bug#78363: [PATCH 2/3] environment: Translate only file names under home directory.

From: Tomas Volf <[email-masked]>
Date: Sun, 18 May 2025 15:39:35 +0200

* guix/scripts/environment.scm (file-name-equal-or-under?): New procedure.
(override-user-mappings, override-user-dir): Use it.

Change-Id: Iadd9b838f6442a8080998ed7e07414db562068bf
---
 guix/scripts/environment.scm | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm
index 3c66906793..bc06e97d7b 100644
--- a/guix/scripts/environment.scm
+++ b/guix/scripts/environment.scm
@@ -979,6 +979,12 @@ (define (user-override-home user)
   "Return home directory for override user USER."
   (string-append "/home/" user))
 
+(define (file-name-equal-or-under? file-name directory)
+  "Is @var{file-name} equal to or under @var{directory}?"
+  (or (string=? directory file-name)
+      (and (string-prefix? directory file-name)
+           (char=? #\/ (string-ref file-name (string-length directory))))))
+
 (define (override-user-mappings user home mappings)
   "If a username USER is provided, rewrite each HOME prefix in file system
 mappings MAPPINGS to a home directory determined by 'override-user-dir';
@@ -987,7 +993,7 @@ (define (override-user-mappings user home mappings)
       mappings
       (map (lambda (mapping)
              (let ((target (file-system-mapping-target mapping)))
-               (if (string-prefix? home target)
+               (if (file-name-equal-or-under? target home)
                    (file-system-mapping
                     (inherit mapping)
                     (target (override-user-dir user home target)))
@@ -997,7 +1003,7 @@ (define (override-user-mappings user home mappings)
 (define (override-user-dir user home dir)
   "If username USER is provided, overwrite string prefix HOME in DIR with a
 directory determined by 'user-override-home'; otherwise, return DIR."
-  (if (and user (string-prefix? home dir))
+  (if (and user (file-name-equal-or-under? dir home))
       (string-append (user-override-home user)
                      (substring dir (string-length home)))
       dir))
-- 
2.49.0