]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/functions/functions.util
network fix parameter passing when using ""
[people/stevee/network.git] / src / functions / functions.util
index 83edf69bc6a7d16a94d4880b26b25c085a428622..24e3e66b1682ed536722744a66a618af2f3f8d01 100644 (file)
@@ -26,6 +26,17 @@ print() {
        printf -- "${fmt}\n" "$@"
 }
 
+print_indent() {
+       local i=${1}
+       shift
+
+       while (( i-- )); do
+               printf "\t"
+       done
+
+       print "%s" "$@"
+}
+
 # The args() function takes a number of arguments like
 #   var1="abc d" var2="abc" var3="abcd e"
 # and splits them into several arguments, devided by newline
@@ -123,7 +134,7 @@ parse_time() {
        local ret=0
 
        local arg
-       for arg in $@; do
+       for arg in "$@"; do
                local unit
 
                case "${arg}" in
@@ -179,7 +190,20 @@ fwrite() {
        assert isset file
        shift
 
-       if [ ! -w "${file}" ]; then
+       if ! print "%s" "$@" > ${file} 2>/dev/null; then
+               error "Could not write to file: ${file}"
+               return ${EXIT_ERROR}
+       fi
+
+       return ${EXIT_OK}
+}
+
+fappend() {
+       local file=${1}
+       assert isset file
+       shift
+
+       if [ -e "${file}" ] && [ ! -w "${file}" ]; then
                log ERROR "${file}: No such file"
                return ${EXIT_ERROR}
        fi
@@ -187,6 +211,43 @@ fwrite() {
        print "%s" "$@" >> ${file} 2>/dev/null
 }
 
+file_delete() {
+       local file=${1}
+
+       unlink "${file}" 2>/dev/null
+}
+
+file_exists() {
+       local file=${1}
+
+       [ -e "${file}" ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
+}
+
+file_is_newer_than() {
+       local file1="${1}"
+       local file2="${2}"
+
+       local age1=$(file_get_age "${file1}")
+       local age2=$(file_get_age "${file2}")
+
+       if [ ${age1} -gt ${age2} ]; then
+               return ${EXIT_TRUE}
+       else
+               return ${EXIT_FALSE}
+       fi
+}
+
+file_get_age() {
+       local file="${1}"
+
+       if [ -e "${file}" ]; then
+               stat --format="%Y" "${file}"
+               return $?
+       fi
+
+       return ${EXIT_ERROR}
+}
+
 make_parent_dir() {
        local path="${1}"
 
@@ -415,16 +476,26 @@ cmd() {
 
        log DEBUG "Running command: ${cmd}"
 
-       ${cmd}
+       env -i -- \
+               HOME="${HOME}" \
+               PATH="${PATH}" \
+               TERM="${TERM}" \
+               ${cmd}
        local ret=$?
 
-       log DEBUG "Returned with code '${ret}'"
-
-       return ${ret}
+       case "${ret}" in
+               ${EXIT_OK})
+                       return ${EXIT_OK}
+                       ;;
+               *)
+                       log DEBUG "Returned with code '${ret}'"
+                       return ${ret}
+                       ;;
+       esac
 }
 
 cmd_quiet() {
-       cmd $@ &>/dev/null
+       cmd "$@" &>/dev/null
 }
 
 cmd_exec() {
@@ -442,22 +513,9 @@ cmd_not_implemented() {
        assert false "not implemented"
 }
 
-# Runs a command in a clean environment so that no confidential information
-# is leaked to any untrusted commands.
-cmd_clean_environment() {
-       local cmd=$@
-
-       log DEBUG "Running command in a clean environment: ${cmd}"
-       env -i -- ${cmd}
-       local ret=${?}
-
-       log DEBUG "Returned with code '${ret}'"
-       return ${ret}
-}
-
 # Executes the given command in background
 cmd_background() {
-       cmd_quiet $@ &
+       cmd_quiet "$@" &
 }
 
 # Prints the PID of the process that was started last
@@ -473,7 +531,7 @@ cmd_background_result() {
 
 # Increase security of the read command
 read() {
-       builtin read -r $@
+       builtin read -r "$@"
 }
 
 seq() {
@@ -499,7 +557,7 @@ count() {
 }
 
 which() {
-       type -P $@
+       type -P "$@"
 }
 
 # Prints the number of seconds since epoch.
@@ -549,10 +607,13 @@ beautify_bytes() {
 module_load() {
        local module=${1}
 
-       if ! grep -q "^${module}" /proc/modules; then
-               log DEBUG "Loading module '${module}'."
-               modprobe ${module}
+       # Do nothing if the module is already loaded
+       if [ -d "/sys/module/${module//-/_}" ]; then
+               return ${EXIT_OK}
        fi
+
+       log DEBUG "Loading kernel module ${module}"
+       modprobe "${module}"
 }
 
 binary_exists() {
@@ -638,6 +699,13 @@ contains_spaces() {
        return ${EXIT_FALSE}
 }
 
+string_match() {
+       local match=${1}
+       local string=${2}
+
+       [[ ${string} =~ ${match} ]] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
+}
+
 string_split() {
        local string="$@"
 
@@ -760,9 +828,15 @@ copy() {
        local src=${1}
        local dst=${2}
 
+       # Check if we can read from the source
+       if [ ! -r "${src}" ]; then
+               log ERROR "Cannot read ${src}"
+               return ${EXIT_ERROR}
+       fi
+
        # Check if ${dst} is a directory
        if [ -d "${dst}" ]; then
-               log ERROR "${dst} is a directory."
+               log ERROR "${dst} is a directory"
                return ${EXIT_ERROR}
        fi