]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.util
security-policies: Delete cached content when policy is deleted
[people/ms/network.git] / src / functions / functions.util
index 3f943e8d13e73111b01281aa7a0109816a376729..e083f6a7de3515a3a11ad88e46488c1012c9788a 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
@@ -179,7 +190,7 @@ fwrite() {
        assert isset file
        shift
 
-       if [ ! -w "${file}" ]; then
+       if [ -e "${file}" ] && [ ! -w "${file}" ]; then
                log ERROR "${file}: No such file"
                return ${EXIT_ERROR}
        fi
@@ -187,6 +198,37 @@ fwrite() {
        print "%s" "$@" >> ${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,12 +457,14 @@ cmd() {
 
        log DEBUG "Running command: ${cmd}"
 
-       ${cmd}
-       local ret=$?
+       if ! ${cmd}; then
+               local ret=$?
 
-       log DEBUG "Returned with code '${ret}'"
+               log DEBUG "Returned with code '${ret}'"
+               return ${ret}
+       fi
 
-       return ${ret}
+       return ${EXIT_OK}
 }
 
 cmd_quiet() {
@@ -638,6 +682,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="$@"
 
@@ -753,16 +804,22 @@ dec2hex() {
        printf "%02x\n" "${1}"
 }
 
+# This function just copy config files
 copy() {
-       # This function just copy config files
        assert [ $# -eq 2 ]
 
        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