Files
blackbox/bin/_stack_lib.sh
John Michel 56232aafc8 Fix issue on OS X when creating a temporary file
This prevents the "mktemp: too few X's in template ‘_stacklib_’" error message from appearing when adding another admin to blackbox and allows the command to successfully execute.

Related:
rtomayko/shocco#5
http://unix.stackexchange.com/q/87638/63283
2015-05-13 14:17:38 -04:00

148 lines
2.9 KiB
Bash
Executable File

# Library functions for bash scripts at Stack Exchange.
# Usage:
# set -e
# . _stack_lib.sh
# ----- Utility Functions -----
function debugmsg() {
# Log to stderr.
echo 1>&2 LOG: """$@"""
:
}
function logit() {
# Log to stderr.
echo 1>&2 LOG: """$@"""
}
function fail_out() {
echo "FAILED:" "$*"
echo 'Exiting...'
exit 1
}
# on_exit and add_on_exit from http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
# Usage:
# add_on_exit rm -f /tmp/foo
# add_on_exit echo "I am exiting"
# tempfile=$(mktemp)
# add_on_exit rm -f "$tempfile"
function on_exit()
{
for i in "${on_exit_items[@]}"
do
eval $i
done
}
function add_on_exit()
{
local n=${#on_exit_items[*]}
on_exit_items[$n]="$*"
if [[ $n -eq 0 ]]; then
trap on_exit EXIT
fi
}
# Securely and portably create a temporary file that will be deleted
# on EXIT. $1 is the variable name to store the result.
function make_self_deleting_tempfile() {
local __resultvar="$1"
local name
case $(uname -s) in
Darwin )
: ${TMPDIR:=/tmp} ;
name=$(mktemp -t _stacklib_.XXXXXXX )
;;
Linux )
name=$(mktemp)
;;
CYGWIN* )
name=$(mktemp)
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
add_on_exit rm -f "$name"
eval $__resultvar="$name"
}
function make_tempdir() {
local __resultvar="$1"
local name
case $(uname -s) in
Darwin )
: "${TMPDIR:=/tmp}" ;
name=$(mktemp -d -t _stacklib_ )
;;
Linux )
name=$(mktemp -d)
;;
CYGWIN* )
name=$(mktemp -d)
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
eval $__resultvar="$name"
}
function make_self_deleting_tempdir() {
local __resultvar="$1"
local dirname
make_tempdir dirname
add_on_exit rm -rf "$dirname"
eval $__resultvar="$dirname"
}
function fail_if_not_running_as_root() {
if [[ $EUID -ne 0 ]]; then
echo 'ERROR: This command should only be run as root.'
echo 'Exiting...'
exit 1
fi
}
function fail_if_in_root_directory() {
# Verify nobody has tricked us into being in "/".
case $(uname -s) in
Darwin )
if [[ $(stat -f'%i' / ) == $(stat -f'%i' . ) ]] ; then
echo 'SECURITY ALERT: The current directory is the root directory.'
echo 'Exiting...'
exit 1
fi
;;
Linux )
if [[ $(stat -c'%i' / ) == $(stat -c'%i' . ) ]] ; then
echo 'SECURITY ALERT: The current directory is the root directory.'
echo 'Exiting...'
exit 1
fi
;;
CYGWIN* )
if [[ $(stat -c'%i' / ) == $(stat -c'%i' . ) ]] ; then
echo 'SECURITY ALERT: The current directory is the root directory.'
echo 'Exiting...'
exit 1
fi
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
}