]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.settings
settings: Use file_delete to delete a file
[people/ms/network.git] / src / functions / functions.settings
index 8b5ac98ec74efa636aadd1a1d5f1bdbd211af40d..a7d94640546b5402e677d331638bb78f280c539e 100644 (file)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
-function settings_read() {
+settings_read() {
        local file="${1}"
        assert isset file
        shift
@@ -55,13 +55,7 @@ function settings_read() {
 
                                # If valid keys is set, key must be in the list.
                                if [ -n "${valid_keys}" ]; then
-                                       if ! listmatch ${key} ${valid_keys}; then
-                                               if ! enabled ignore_superfluous_settings; then
-                                                       log DEBUG "Ignoring configuration setting: ${key}"
-                                               fi
-
-                                               continue
-                                       fi
+                                       list_match ${key} ${valid_keys} || continue
                                fi
 
                                val=$(cli_get_val ${line})
@@ -77,7 +71,7 @@ function settings_read() {
        done < ${file}
 }
 
-function settings_read_array() {
+settings_read_array() {
        local file=${1}
        assert isset file
        shift
@@ -99,7 +93,7 @@ function settings_read_array() {
 
                                # If valid_keys is set, key must be in the list.
                                if [ -n "${valid_keys}" ]; then
-                                       if ! listmatch ${key} ${valid_keys}; then
+                                       if ! list_match ${key} ${valid_keys}; then
                                                log DEBUG "Ignoring configuration setting: ${key}"
                                                continue
                                        fi
@@ -119,7 +113,7 @@ function settings_read_array() {
 }
 
 # Strip leading and trailing "s.
-function settings_strip() {
+settings_strip() {
        local var="$@"
 
        # Do nothing for strings that contain spaces.
@@ -131,29 +125,46 @@ function settings_strip() {
        unquote "${var}"
 }
 
-function settings_write() {
-       local settings_file=${1}
+settings_write() {
+       local settings_file="${1}"
        assert isset settings_file
        shift
 
+       local check_func
+
+       local arg
+       while read arg; do
+               case "${arg}" in
+                       --check=*)
+                               check_func="$(cli_get_val "${arg}")"
+                               ;;
+
+                       # Stop argument processing when reaching the first
+                       # configuration parameter
+                       *)
+                               break
+                               ;;
+               esac
+               shift
+       done <<< "$(args $@)"
+
        # Check if all values to be written are sane
-       if ! settings_check; then
-               log CRITICAL "Configuration check failed. No settings have been written."
+       if isset check_func && ! settings_check "${check_func}"; then
                return ${EXIT_ERROR}
        fi
 
-       log DEBUG "Writing settings file ${settings_file}."
+       log DEBUG "Writing settings file '${settings_file}'"
 
        mkdir -p $(dirname ${settings_file}) 2>/dev/null
        > ${settings_file}
 
        local param
-       for param in $(listsort $@); do
+       for param in $(list_sort $@); do
                echo "${param}=\"${!param}\"" >> ${settings_file}
        done
 }
 
-function settings_remove() {
+settings_remove() {
        local settings_file="${1}"
 
        local abspath="$(readlink -e "${settings_file}")"
@@ -162,28 +173,49 @@ function settings_remove() {
                return ${EXIT_ERROR}
        fi
 
-       rm -f "${settings_file}"
+       file_delete "${settings_file}"
 }
 
-function settings_print() {
+settings_print() {
        local param
 
-       for param in $(listsort $@); do
+       for param in $(list_sort $@); do
                printf "%-32s = %s\n" "${param}" "${!param}"
        done
 }
 
-function settings_check() {
-       # If there is a function defined that is called __check
-       # we call that function
-       if [ -n "$(type -t hook_check)" ]; then
-               hook_check || return $?
-       fi
-
-       return ${EXIT_OK}
+settings_check() {
+       local check_func="${1}"
+
+       # Execute the check function
+       "${check_func}"
+       local ret="${?}"
+
+       case "${ret}" in
+               # OK
+               ${EXIT_OK}|${EXIT_TRUE})
+                       log DEBUG "Configuration check succeeded."
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Error
+               ${EXIT_ERROR}|${EXIT_FALSE})
+                       log CRITICAL "Configuration check failed. No settings have been written."
+                       return ${EXIT_FALSE}
+                       ;;
+
+               # Command not found
+               ${EXIT_COMMAND_NOT_FOUND})
+                       log CRITICAL "Configuration check function '${check_func}' was not found."
+                       return ${EXIT_FALSE}
+                       ;;
+       esac
+
+       log CRITICAL "Unhandled exit code for '${check_func}': ${ret}"
+       return ${EXIT_ERROR}
 }
 
-function settings_set() {
+settings_set() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        *=*)
@@ -202,7 +234,7 @@ function settings_set() {
        done
 }
 
-function network_settings_read() {
+network_settings_read() {
        local options="${NETWORK_SETTINGS_FILE_PARAMS}"
 
        # If the DEBUG variable has already been set,
@@ -214,25 +246,63 @@ function network_settings_read() {
        settings_read "${NETWORK_SETTINGS_FILE}" ${options}
 }
 
-function network_settings_write() {
+network_settings_write() {
        settings_write "${NETWORK_SETTINGS_FILE}" ${NETWORK_SETTINGS_FILE_PARAMS}
+}
+
+network_settings_set() {
+       # Process any settings that require immediate actin
+       while [ $# -gt 0 ]; do
+               local arg=${1}
+               shift
+
+               case "${arg}" in
+                       *=*)
+                               local key=$(cli_get_key ${arg})
+                               local val=$(cli_get_val ${arg})
+
+                               case "${key}" in
+                                       DNS_RANDOMIZE|DNS_SEARCH_DOMAIN|DNS_USE_LOCAL_RESOLVER)
+                                               dns_generate_resolvconf
+                                               ;;
+
+                                       WIRELESS_REGULATORY_DOMAIN)
+                                               if ! wireless_valid_reg_domain "${val}"; then
+                                                       warning "Ignoring invalid wireless regulatory domain: ${val}"
+                                                       continue
+                                               fi
 
-       # Update DNS configuration.
-       dns_generate_resolvconf
+                                               if ! wireless_set_reg_domain "${val}"; then
+                                                       error "Error setting wireless regulatory domain: ${val}"
+                                               fi
+                                               ;;
+                               esac
+                               ;;
+               esac
+
+               # Save setting
+               settings_set ${arg}
+       done
+
+       return ${EXIT_OK}
 }
 
-function network_settings_print() {
+network_settings_print() {
        settings_print ${NETWORK_SETTINGS_FILE_PARAMS}
 }
 
-function firewall_settings_read() {
+network_settings_list() {
+       print "${NETWORK_SETTINGS_FILE_PARAMS}"
+}
+
+firewall_settings_read() {
        settings_read "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}"
 }
 
-function firewall_settings_write() {
+firewall_settings_write() {
        settings_write "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}"
 }
 
-function firewall_settings_print() {
+firewall_settings_print() {
        settings_print "${FIREWALL_SETTINGS_PARAMS}"
 }