-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp-dir
executable file
·97 lines (86 loc) · 3.04 KB
/
tmp-dir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
PROGRAM="${0##*/}"
TMPDIR_TEMPLATE="$PROGRAM.XXXXXXXXXXXXX"
TMPFILE_TEMPLATE="XXXXXX"
# note: printf is used instead of echo to avoid backslash
# processing and to properly handle values that begin with a '-'.
log() { printf '%s\n' "$*"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$@"; exit 1; }
# appends a command to a trap
#
# - 1st arg: code to add
# - remaining args: names of traps to modify
#
trap_add() {
trap_add_cmd=$1; shift || fatal "${FUNCNAME} usage error"
for trap_add_name in "$@"; do
trap -- "$(
# helper fn to get existing trap command from output
# of trap -p
extract_trap_cmd() { printf '%s\n' "$3"; }
# print the new trap command
printf '%s\n' "${trap_add_cmd}"
# print existing trap command with newline
eval "extract_trap_cmd $(trap -p "${trap_add_name}")"
)" "${trap_add_name}" \
|| fatal "unable to add to trap ${trap_add_name}"
done
}
# set the trace attribute for the above function. this is
# required to modify DEBUG or RETURN traps because functions don't
# inherit them unless the trace attribute is set
declare -f -t trap_add
_maketmpdir() {
local tmpdirpath=${2:-${TMPDIR:-/tmp}}
eval $1="$(mktemp -d "${tmpdirpath}/${TMPDIR_TEMPLATE}")"
TRAP_NAME="rm_tmpdir${RANDOM}"
eval "${TRAP_NAME}() { rm -rf "${!1}"; }"
trap_add "${TRAP_NAME}" EXIT
}
maketmpdir() {
local tmpdir=${1:-LOCAL_TMPDIR}
if [[ -n "${!tmpdir}" && -d "${!tmpdir}" ]]
then
return
fi
_maketmpdir "${tmpdir}"
}
if [[ $(uname) == Darwin ]]; then
_makesecuretmpdir() {
_maketmpdir "$1"
DARWIN_RAMDISK_DEV="$(hdid -drivekey system-image=yes -nomount 'ram://32768' | cut -d ' ' -f 1)" # 32768 sectors = 16 mb
[[ -z "${DARWIN_RAMDISK_DEV}" ]] && { echo "Error: could not create ramdisk." 1>&2; exit 1; }
newfs_hfs -M 700 "${DARWIN_RAMDISK_DEV}" &>/dev/null || { echo "Error: could not create filesystem on ramdisk." 1>&2 ; exit 1; }
mount -t hfs -o noatime -o nobrowse "${DARWIN_RAMDISK_DEV}" "${!1}" || { echo "Error: could not mount filesystem on ramdisk." 1>&2 ; exit 1; }
TRAP_NAME="unmount_tmpdir${RANDOM}"
eval "${TRAP_NAME}() { [[ -n "${!1}" && -d "${!1}" && -n "${DARWIN_RAMDISK_DEV}" ]] || return; umount "${!1}"; diskutil quiet eject "${DARWIN_RAMDISK_DEV}"; }"
trap_add "${TRAP_NAME}" EXIT
}
else
_makesecuretmpdir() {
if [[ -d /dev/shm && -w /dev/shm && -x /dev/shm ]]; then
_maketmpdir "$1" /dev/shm
else
_maketmpdir "$1"
fi
}
fi
makesecuretmpdir() {
local tmpdir=${1:-LOCAL_SECURE_TMPDIR}
if [[ -n "${!tmpdir}" && -d "${!tmpdir}" ]]
then
return
fi
_makesecuretmpdir "${tmpdir}"
}
_gettmpfilename() {
eval $1="$(mktemp -u "${!2}/${TMPFILE_TEMPLATE}")"
}
gettmpfilename() {
maketmpdir
_gettmpfilename "${1:-LOCAL_TMPFILE}" LOCAL_TMPDIR
}
getsecuretmpfilename() {
makesecuretmpdir
_gettmpfilename "${1:-LOCAL_SECURE_TMPFILE}" LOCAL_SECURE_TMPDIR
}