]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.util
wireless-ap: Check that secret has the correct length and no invalid characters
[people/ms/network.git] / src / functions / functions.util
index 8831d84b3953c0aca5918bbc3840e6e5f4861edd..7379a9887de812acf0eb812ef2cb2b0b7d0091e4 100644 (file)
@@ -248,10 +248,34 @@ file_get_age() {
        return ${EXIT_ERROR}
 }
 
+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}"
 
-       mkdir -p "${path}"
+       # 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() {
@@ -521,11 +545,11 @@ cmd_quiet() {
 }
 
 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}
@@ -721,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}
@@ -862,6 +899,11 @@ 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}
@@ -873,3 +915,18 @@ normalize() {
 
        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}
+}