Skip to content

Commit ff9fe31

Browse files
keymonmumoshu
andauthored
Use a mktemp random directory for installation (repeated PR) (#301)
* Use a mktemp random directory for installation Do not hardcode a static directory for temporary files, as it might cause problems when running in a multi-user system (ie: orphan files owned by other users). Use mktemp -d instead. Fixes #241 * Rename fail_trap > exit_trap Co-authored-by: Yusuke Kuoka <[email protected]>
1 parent 8a7f38e commit ff9fe31

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

install-binary.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,20 @@ getDownloadURL() {
8989
fi
9090
}
9191

92+
# Temporary dir
93+
mkTempDir() {
94+
HELM_TMP="$(mktemp -d -t "${PROJECT_NAME}-XXXX")"
95+
}
96+
rmTempDir() {
97+
if [ -d "${HELM_TMP:-/tmp/helm-diff-tmp}" ]; then
98+
rm -rf "${HELM_TMP:-/tmp/helm-diff-tmp}"
99+
fi
100+
}
101+
92102
# downloadFile downloads the latest binary package and also the checksum
93103
# for that binary.
94104
downloadFile() {
95-
PLUGIN_TMP_FILE="/tmp/${PROJECT_NAME}.tgz"
105+
PLUGIN_TMP_FILE="${HELM_TMP}/${PROJECT_NAME}.tgz"
96106
echo "Downloading $DOWNLOAD_URL"
97107
if type "curl" >/dev/null; then
98108
curl -L "$DOWNLOAD_URL" -o "$PLUGIN_TMP_FILE"
@@ -104,18 +114,17 @@ downloadFile() {
104114
# installFile verifies the SHA256 for the file, then unpacks and
105115
# installs it.
106116
installFile() {
107-
HELM_TMP="/tmp/$PROJECT_NAME"
108-
mkdir -p "$HELM_TMP"
109117
tar xvzf "$PLUGIN_TMP_FILE" -C "$HELM_TMP"
110118
HELM_TMP_BIN="$HELM_TMP/diff/bin/diff"
111119
echo "Preparing to install into ${HELM_PLUGIN_DIR}"
112120
mkdir -p "$HELM_PLUGIN_DIR/bin"
113121
cp "$HELM_TMP_BIN" "$HELM_PLUGIN_DIR/bin"
114122
}
115123

116-
# fail_trap is executed if an error occurs.
117-
fail_trap() {
124+
# exit_trap is executed if on exit (error or not).
125+
exit_trap() {
118126
result=$?
127+
rmTempDir
119128
if [ "$result" != "0" ]; then
120129
echo "Failed to install $PROJECT_NAME"
121130
printf '\tFor support, go to https://github.com/databus23/helm-diff.\n'
@@ -134,12 +143,13 @@ testVersion() {
134143
# Execution
135144

136145
#Stop execution on any error
137-
trap "fail_trap" EXIT
146+
trap "exit_trap" EXIT
138147
set -e
139148
initArch
140149
initOS
141150
verifySupported
142151
getDownloadURL
152+
mkTempDir
143153
downloadFile
144154
installFile
145155
testVersion

0 commit comments

Comments
 (0)