[arch-projects] [mkinitcpio][PATCH 2/4] mkinitcpio: avoid stomping on TMPDIR from the environment

Dave Reisner d at falconindy.com
Sun Oct 16 11:53:33 EDT 2011


cc36db45 was a bit too strict about localizing variables and blew out
TMPDIR, which might be a legit variable sourced from the calling
environment. Restore this behavior with a bit of refactoring, and
additionally add in a check to make sure the temporary directory is
writeable before proceeding.

Signed-off-by: Dave Reisner <dreisner at archlinux.org>
---
 install/autodetect |    2 +-
 mkinitcpio         |   23 +++++++++++++----------
 mkinitcpio.8.txt   |    3 ++-
 3 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/install/autodetect b/install/autodetect
index 4001977..35f1cb2 100644
--- a/install/autodetect
+++ b/install/autodetect
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 build() {
-    MODULE_FILE=$TMPDIR/autodetect_modules
+    MODULE_FILE=$workdir/autodetect_modules
 
     if [[ ! -d /sys/devices ]]; then
         error "/sys does not appear to be mounted. Unable to use autodetection"
diff --git a/mkinitcpio b/mkinitcpio
index d0ae08c..db62d92 100755
--- a/mkinitcpio
+++ b/mkinitcpio
@@ -21,7 +21,7 @@ INSTDIR=install
 PRESETDIR=mkinitcpio.d
 COMPRESSION=gzip
 
-declare TMPDIR= BASEDIR= MODULE_FILE= GENIMG= PRESET= COMPRESSION_OPTIONS= BUILDROOT=
+declare BASEDIR= MODULE_FILE= GENIMG= PRESET= COMPRESSION_OPTIONS= BUILDROOT=
 declare NC= BOLD= BLUE= GREEN= RED= YELLOW=
 declare -i QUIET=1 SHOW_AUTOMODS=0 SAVELIST=0 COLOR=1
 declare -a SKIPHOOKS ADDED_MODULES MODPATHS
@@ -52,7 +52,7 @@ usage: ${0##*/} [options]
    -p PRESET        Build specified preset from /etc/mkinitcpio.d
    -S SKIPHOOKS     Skip SKIPHOOKS (comma-separated) when building the image.
    -s               Save build directory. default: no
-   -t TMPDIR        Use TMPDIR as the temporary build directory.
+   -t DIR           Use DIR as the temporary build directory.
    -v               Verbose output. Default: no.
    -z COMPRESS      Use COMPRESS on resulting image
 
@@ -61,12 +61,12 @@ EOF
 }
 
 cleanup() {
-    if [[ $TMPDIR ]]; then
+    if [[ $workdir ]]; then
         # when PRESET is set, we're in the main loop, not a worker process
         if (( SAVELIST )) && [[ -z $PRESET ]]; then
-            msg "build directory saved in %s" "$TMPDIR"
+            msg "build directory saved in %s" "$workdir"
         else
-            rm -rf "$TMPDIR"
+            rm -rf "$workdir"
         fi
     fi
 
@@ -182,15 +182,18 @@ fi
 if [[ $TMPDIR ]]; then
     if [[ ! -d $TMPDIR ]]; then
         error "Temporary directory does not exist or is not a directory: \`%s'" "$TMPDIR"
-        unset TMPDIR
+        cleanup 1
+    fi
+    if [[ ! -w $TMPDIR ]]; then
+        error "Temporary directory is not writeable: \`%s'" "$TMPDIR"
         cleanup 1
     fi
 fi
-TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/mkinitcpio.XXXXXX")
-declare BUILDROOT=$TMPDIR/root
+workdir=$(TMPDIR=$TMPDIR mktemp -d --tmpdir mkinitcpio.XXXXXX)
+BUILDROOT=$workdir/root
 
 # explicitly create the buildroot
-mkdir "$TMPDIR/root"
+mkdir "$BUILDROOT"
 
 # use preset $PRESET
 if [[ $PRESET ]]; then
@@ -335,7 +338,7 @@ if (( ${#ADDED_MODULES[*]} )); then
     popd >/dev/null
 
     msg "Generating module dependencies"
-    /sbin/depmod -b "${TMPDIR}/root" "${KERNELVERSION}"
+    /sbin/depmod -b "$BUILDROOT" "${KERNELVERSION}"
     rm "$BUILDROOT/lib/modules/$KERNELVERSION"/modules.!(dep.bin|alias.bin|symbols.bin)
 fi
 
diff --git a/mkinitcpio.8.txt b/mkinitcpio.8.txt
index f963dac..79c4bb4 100644
--- a/mkinitcpio.8.txt
+++ b/mkinitcpio.8.txt
@@ -72,7 +72,8 @@ Options
 
 *-t* 'tmpdir'::
 	Use 'tmpdir' as the temporary build directory instead of /tmp. 'tmpdir'
-	must exist.
+	must exist. The 'TMPDIR' environment variable is also honored to set this
+	location, but the command line option will take precedence.
 
 *-v*::
 	Verbose output. Outputs more information about what's happening during
-- 
1.7.7



More information about the arch-projects mailing list