From f6ad8f57cf9aacaa7acad4c1e0d32f1d5efd75b6 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Tue, 26 Nov 2019 13:41:38 -0600 Subject: [PATCH 1/3] Adding a function that creates a prefix and logic surrounding it to only create it if it does not already exist --- src/install/default.txt | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/install/default.txt b/src/install/default.txt index 7d812314..5504613d 100644 --- a/src/install/default.txt +++ b/src/install/default.txt @@ -339,6 +339,23 @@ extract_file() { return "${rcode}" } +#--- FUNCTION ---------------------------------------------------------------- +# NAME: create_prefix +# DESCRIPTION: Creates the install prefix (and any parent directories) +# PARAMETERS: $1 = prefix +# RETURNS: Return code of the tool used to make the directory +# 0 = Created the directory +# >0 = Failed to create directory +#------------------------------------------------------------------------------- +create_prefix() { + local prefix + local rcode + + prefix="${1}" + + mkdir -p "${prefix}" +} + #--- FUNCTION ---------------------------------------------------------------- # NAME: install_file_freebsd # DESCRIPTION: Installs a file into a location using 'install'. If EUID not @@ -377,7 +394,6 @@ install_file_freebsd() { return "${rcode}" } - #--- FUNCTION ---------------------------------------------------------------- # NAME: install_file_linux # DESCRIPTION: Installs a file into a location using 'install'. If EUID not @@ -485,6 +501,7 @@ main() { local checksum_check_rcode local extract_file_rcode local install_file_rcode + local create_prefix_rcode croc_bin_name="croc" croc_version="6.4.6" @@ -620,6 +637,20 @@ main() { exit 1 fi + if [[ ! -d "${prefix}" ]]; then + create_prefix "${prefix}" + create_prefix_rcode="${?}" + if [[ "${create_prefix_rcode}" -gt 0 ]]; then + print_message "== Failed to create the install prefix: ${prefix}" "error" + exit 1 + else + print_message "== Created install prefix at ${prefix}" "info" + fi + else + print_message "== Install prefix already exists. No need to create it." "info" + fi + + case "${croc_os}" in "Linux" ) install_file_linux "${tmpdir}/${croc_bin_name}" "${prefix}/"; install_file_rcode="${?}";; From 47d84b9947879b8830655820c57f9e99e408a9ac Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Tue, 26 Nov 2019 13:50:31 -0600 Subject: [PATCH 2/3] Adding rcode for completness sake on the create_prefix function. --- src/install/default.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/install/default.txt b/src/install/default.txt index 5504613d..a23ca20e 100644 --- a/src/install/default.txt +++ b/src/install/default.txt @@ -354,6 +354,9 @@ create_prefix() { prefix="${1}" mkdir -p "${prefix}" + rcode="${?}" + + return "${rcode}" } #--- FUNCTION ---------------------------------------------------------------- From 247a6987575ad8ed27f25a3a904b2c15b69603a2 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Tue, 26 Nov 2019 14:18:30 -0600 Subject: [PATCH 3/3] Adding in EUID check to create_prefix. Also adding in more checks to that function to make sure the tools needed are available. --- src/install/default.txt | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/install/default.txt b/src/install/default.txt index a23ca20e..eb300bc9 100644 --- a/src/install/default.txt +++ b/src/install/default.txt @@ -341,20 +341,36 @@ extract_file() { #--- FUNCTION ---------------------------------------------------------------- # NAME: create_prefix -# DESCRIPTION: Creates the install prefix (and any parent directories) +# DESCRIPTION: Creates the install prefix (and any parent directories). If +# EUID not 0, then attempt to use sudo. # PARAMETERS: $1 = prefix # RETURNS: Return code of the tool used to make the directory # 0 = Created the directory # >0 = Failed to create directory +# 20 = Could not find mkdir command +# 21 = Could not find sudo command #------------------------------------------------------------------------------- create_prefix() { local prefix local rcode prefix="${1}" - - mkdir -p "${prefix}" - rcode="${?}" + + if command -v mkdir >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + mkdir -p "${prefix}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo mkdir -p "${prefix}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi return "${rcode}" } @@ -643,11 +659,17 @@ main() { if [[ ! -d "${prefix}" ]]; then create_prefix "${prefix}" create_prefix_rcode="${?}" - if [[ "${create_prefix_rcode}" -gt 0 ]]; then - print_message "== Failed to create the install prefix: ${prefix}" "error" + if [[ "${create_prefix_rcode}" == "0" ]]; then + print_message "== Created install prefix at ${prefix}" "info" + elif [[ "${create_prefix_rcode}" == "20" ]]; then + print_message "== Failed to find mkdir in path" "error" + exit 1 + elif [[ "${create_prefix_rcode}" == "21" ]]; then + print_message "== Failed to find sudo in path" "error" exit 1 else - print_message "== Created install prefix at ${prefix}" "info" + print_message "== Failed to create the install prefix: ${prefix}" "error" + exit 1 fi else print_message "== Install prefix already exists. No need to create it." "info"