X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Fnetwork.git;a=blobdiff_plain;f=src%2Ffunctions%2Ffunctions.util;h=7379a9887de812acf0eb812ef2cb2b0b7d0091e4;hp=687d008f8f423f4e4d346325e6fc090f6f62869d;hb=d695b280e9972311ae8c4bc688c0898ade1281e6;hpb=dd891ec481f9278b4831d542db3ca1fef2556d51 diff --git a/src/functions/functions.util b/src/functions/functions.util index 687d008f..7379a988 100644 --- a/src/functions/functions.util +++ b/src/functions/functions.util @@ -134,7 +134,7 @@ parse_time() { local ret=0 local arg - for arg in $@; do + for arg in "$@"; do local unit case "${arg}" in @@ -248,11 +248,40 @@ file_get_age() { return ${EXIT_ERROR} } -make_parent_dir() { +file_to_log() { + local level="${1}" + assert isset level + + local file="${2}" + assert file_exists "${file}" + + local line + while read line; do + log "${level}" "${line}" + done < "${file}" +} + +make_directory() { + local path="${1}" + + # Exit silently when directory already exists + if [ -d "${path}" ]; then + return ${EXIT_OK} + fi + + if ! mkdir -p "${path}"; then + log ERROR "Could not create directory ${path}" + return ${EXIT_ERROR} + fi + + log DEBUG "Created directory ${path}" + return ${EXIT_OK} +} + +make_parent_directory() { local path="${1}" - local dirname="$(dirname "${path}")" - mkdir -p "${dirname}" + make_directory "$(dirname "${path}")" } enabled() { @@ -261,6 +290,12 @@ enabled() { list_match "${!param}" yes on true 1 } +disabled() { + local param="${1}" + + list_match "${!param}" no off false 0 +} + mac_generate() { local b="$(random 12)" @@ -311,6 +346,17 @@ mac_is_valid() { [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] } +# Converts the given string to lowercase and returns true if it is a valid FQDN +fqdn_is_valid() { + local fqdn="${1}" + + if grep -qP "^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$" <<< "${fqdn,,}"; then + return ${EXIT_TRUE} + fi + + return ${EXIT_FALSE} +} + uuid() { echo $(/dev/null + cmd "$@" &>/dev/null } cmd_exec() { - local cmd=$@ + local cmd=( "$@" ) log DEBUG "Exec'ing command: ${cmd}" - exec ${cmd} + exec "${cmd[@]}" log ERROR "Could not exec-ute: ${cmd}" exit ${EXIT_ERROR} @@ -515,7 +561,7 @@ cmd_not_implemented() { # Executes the given command in background cmd_background() { - cmd_quiet $@ & + cmd_quiet "$@" & } # Prints the PID of the process that was started last @@ -531,7 +577,7 @@ cmd_background_result() { # Increase security of the read command read() { - builtin read -r $@ + builtin read -r "$@" } seq() { @@ -557,7 +603,7 @@ count() { } which() { - type -P $@ + type -P "$@" } # Prints the number of seconds since epoch. @@ -699,6 +745,19 @@ contains_spaces() { return ${EXIT_FALSE} } +contains_non_ascii_characters() { + local value="$@" + + # Strip away all ASCII characters + local non_ascii="${value//[[:ascii:]]/}" + + if isset non_ascii; then + return ${EXIT_TRUE} + fi + + return ${EXIT_FALSE} +} + string_match() { local match=${1} local string=${2} @@ -840,8 +899,34 @@ copy() { return ${EXIT_ERROR} fi + # Create destination directory if it doesn't exist, yet + if ! make_parent_directory "${dst}"; then + return ${EXIT_ERROR} + fi + if ! fread "${src}" > "${dst}"; then log ERROR "Could not copy data from ${src} to ${dst}" return ${EXIT_ERROR} fi } + +normalize() { + local string="$@" + + tr -sc [:alnum:] "-" < <(printf "%s" "${string,,}") +} + +get_driver_from_path() { + local path="${1}" + + if file_exists "${path}"; then + # Resolve symlink + local driver="$(readlink "${path}")" + + # Print module name + basename "${driver}" + return ${EXIT_OK} + fi + + return ${EXIT_ERROR} +}