From d6a32a3da3d4904ba20bd2592b816ccb9545e920 Mon Sep 17 00:00:00 2001
From: Daniel J Walsh <dwalsh@redhat.com>
Date: Wed, 26 Jul 2023 16:53:52 -0400
Subject: [PATCH] If quadlets have same name, only use first

If a user puts a quadlet file in his homedirectory with
the same name as one in /etc/containers/systemd/user or
/etc/containers/systemd/user/$UID, then only use the one in
homedir and ignore the others.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
---
 cmd/quadlet/main.go          |  3 ++-
 test/system/252-quadlet.bats | 26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/cmd/quadlet/main.go b/cmd/quadlet/main.go
index ae75344ce5..5cc17f7e08 100644
--- a/cmd/quadlet/main.go
+++ b/cmd/quadlet/main.go
@@ -194,6 +194,8 @@ func isExtSupported(filename string) bool {
 	return ok
 }
 
+var seen = make(map[string]struct{})
+
 func loadUnitsFromDir(sourcePath string) ([]*parser.UnitFile, error) {
 	var prevError error
 	files, err := os.ReadDir(sourcePath)
@@ -205,7 +207,6 @@ func loadUnitsFromDir(sourcePath string) ([]*parser.UnitFile, error) {
 	}
 
 	var units []*parser.UnitFile
-	var seen = make(map[string]struct{})
 
 	for _, file := range files {
 		name := file.Name()
diff --git a/test/system/252-quadlet.bats b/test/system/252-quadlet.bats
index 7b824be00a..f9ea013122 100644
--- a/test/system/252-quadlet.bats
+++ b/test/system/252-quadlet.bats
@@ -176,6 +176,32 @@ EOF
     service_cleanup $QUADLET_SERVICE_NAME failed
 }
 
+@test "quadlet conflict names" {
+    # If two directories in the search have files with the same name, quadlet should
+    # only process the first name
+    dir1=$PODMAN_TMPDIR/$(random_string)
+    dir2=$PODMAN_TMPDIR/$(random_string)
+    local quadlet_file=basic_$(random_string).container
+    mkdir -p $dir1 $dir2
+
+    cat > $dir1/$quadlet_file <<EOF
+[Container]
+Image=$IMAGE
+Notify=yes
+EOF
+
+    cat > $dir2/$quadlet_file <<EOF
+[Container]
+Image=$IMAGE
+Notify=no
+EOF
+    QUADLET_UNIT_DIRS="$dir1:$dir2" run \
+                    timeout --foreground -v --kill=10 $PODMAN_TIMEOUT \
+                    $QUADLET --dryrun
+    assert "$output" =~ "Notify=yes" "quadlet should show Notify=yes"
+    assert "$output" !~ "Notify=no" "quadlet should not show Notify=no"
+}
+
 @test "quadlet - envvar" {
     local quadlet_file=$PODMAN_TMPDIR/envvar_$(random_string).container
     cat > $quadlet_file <<EOF