From 09dbd3eda5c43949a9fc61068997080211e87b4e Mon Sep 17 00:00:00 2001
From: Adithya Balakumar <adithya.balakumar@toshiba-tsip.com>
Date: Thu, 30 Jan 2025 23:37:02 -0500
Subject: [PATCH] setupcon: Create temp files with consistent names

Currently temp files are created with mktemp which creates a file with
a random file name each time. This creates reproducibility issues due to
different temp filenames across multiple builds.

Attempt to solve this with a consistent suffix to be appended to the
filename

Solves #977877

Signed-off-by: Adithya Balakumar <adithya.balakumar@toshiba-tsip.com>
---
 setupcon | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/setupcon b/setupcon
index b3df2f4..534c53d 100755
--- a/setupcon
+++ b/setupcon
@@ -59,9 +59,10 @@ tempfiles=''
 trap 'rm -f $tempfiles >/dev/null 2>&1' 0
 trap "exit 2" 1 2 3 13 15
 tempfile () {
+    SUFFIX=$1
     if \
-        TMPFILE=`mktemp /run/tmpkbd.XXXXXX 2>/dev/null` \
-            || TMPFILE=`mktemp /tmp/tmpkbd.XXXXXX 2>/dev/null`
+        TMPFILE=`touch "/run/tmpkbd.${SUFFIX}" 2>/dev/null && echo "/run/tmpkbd.${SUFFIX}"` \
+            || TMPFILE=`touch "/tmp/tmpkbd.${SUFFIX}" 2>/dev/null && echo "/tmp/tmpkbd.${SUFFIX}"`
     then
         tempfiles="$tempfiles $TMPFILE"
         return 0
@@ -925,7 +926,7 @@ done
 if [ "$savekbdfile" ]; then
     case "$kernel" in
         linux)
-            tempfile || { echo setupcon: Can not create temporary file >&2; exit 1; }
+            tempfile "kbdfile" || { echo setupcon: Can not create temporary file >&2; exit 1; }
             {
 	        $installdir/bin/ckbcomp -backspace "$backspace" $acm_option \
                     $rules_option -model "$XKBMODEL" \
@@ -1181,7 +1182,7 @@ if [ "$KMAP" ]; then
         linux) run plain NONE loadkeys "$KMAP" ;;
     esac
 else
-    tempfile || { echo setupcon: Can not create temporary file >&2; exit 1; }
+    tempfile "kbd_linux" || { echo setupcon: Can not create temporary file >&2; exit 1; }
     case "$do_kbd" in
         linux)
 	    $installdir/bin/ckbcomp -backspace "$backspace" $acm_option \
@@ -1286,7 +1287,7 @@ if [ "$setupdir" ]; then
     echo '#!/bin/sh' >"$setupdir"/bin/setupcon
     echo '# A micro-version of setupcon with static configuration.' >>"$setupdir"/bin/setupcon
     chmod +x "$setupdir"/bin/setupcon
-    tempfile || { echo setupcon: Can not create temporary file >&2; exit 1; }
+    tempfile "setupdir" || { echo setupcon: Can not create temporary file >&2; exit 1; }
     printf "%s" "$SETUP" |
     while read -r cmd args; do
         which "$cmd" >>$TMPFILE || true
-- 
2.39.5

