]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/functions/functions.ports
Convert HOOK_SETTINGS into an array
[people/stevee/network.git] / src / functions / functions.ports
index fcb92066cadca8d728a4edaaf8d30e6135fd31f1..fb227150fc287d51f4ccea9c28708d1edbcbabf7 100644 (file)
 #                                                                             #
 ###############################################################################
 
-port_dir() {
-       local port="${1}"
-       echo "${NETWORK_CONFIG_DIR}/ports/${port}"
-}
-
-port_list() {
+ports_get_all() {
        local port
-       for port in $(port_dir)/*; do
-               port="$(basename "${port}")"
+       for port in $(list_directory "${NETWORK_PORTS_DIR}"); do
                if port_exists "${port}"; then
                        print "${port}"
                fi
        done
 }
 
+# XXX TO BE REMOVED
+port_list() {
+       ports_get_all "$@"
+}
+
 port_list_in_use() {
        local ports_in_use
 
@@ -86,7 +85,7 @@ port_settings_read() {
        # Save the HOOK variable.
        local hook="${HOOK}"
 
-       settings_read "$(port_file "${port}")" ${HOOK_SETTINGS}
+       settings_read "$(port_file "${port}")" ${HOOK_SETTINGS[*]}
 
        # Restore hook.
        HOOK="${hook}"
@@ -101,20 +100,16 @@ port_settings_write() {
        if function_exists "hook_check_settings"; then
                list_append args "--check=\"hook_check_settings\""
        fi
-       list_append args ${HOOK_SETTINGS}
+       list_append args HOOK ${HOOK_SETTINGS[*]}
 
        settings_write "$(port_file "${port}")" ${args}
 }
 
-ports_get_all() {
-       port_list
-}
-
 port_file() {
        local port="${1}"
        assert isset port
 
-       echo "$(port_dir ${port})/settings"
+       echo "${NETWORK_PORTS_DIR}/${port}/settings"
 }
 
 port_exists() {
@@ -139,10 +134,6 @@ port_is_attached() {
 
        local zone
        for zone in $(zones_get_all); do
-
-               assert isset zone
-               assert zone_exists ${zone}
-
                if list_match ${port} $(zone_get_ports ${zone}); then
                        echo "${zone}"
                        return ${EXIT_OK}
@@ -153,7 +144,7 @@ port_is_attached() {
 }
 
 port_is_up() {
-       device_is_up $@
+       device_is_up "$@"
 }
 
 port_new() {
@@ -165,7 +156,7 @@ port_new() {
                return ${EXIT_ERROR}
        fi
 
-       hook_exec port "${hook}" new $@
+       hook_exec port "${hook}" new "$@"
 }
 
 port_destroy() {
@@ -178,13 +169,12 @@ port_destroy() {
                return ${EXIT_ERROR}
        fi
 
-       # Check if the port is attached to any zone and don't delete it.
-       local ok=${EXIT_OK}
-
        local attached_zone=$(port_is_attached ${port})
        if [ -n "${attached_zone}" ]; then
-               error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
-               ok=${EXIT_ERROR}
+               if ! zone_port_detach "${attached_zone}" "${port}"; then
+                       error "Could not remove port ${port} from zone ${zone}"
+                       return ${EXIT_ERROR}
+               fi
        fi
 
        # Check if the port is linked to any other port and don't allow the user
@@ -193,29 +183,28 @@ port_destroy() {
        for other_port in $(ports_get); do
                [ "${other_port}" = "${port}" ] && continue
 
-               if list_match ${port} $(port_get_parents ${other_port}); then
-                       error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
-                       ok=${EXIT_ERROR}
-               fi
-
                if list_match ${port} $(port_get_children ${other_port}); then
-                       error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
-                       ok=${EXIT_ERROR}
+                       log ERROR "Cannot destroy port '${port}' which is child of port '${other_port}'."
+                       return ${EXIT_ERROR}
                fi
        done
 
-       # If ok says we are not okay --> exit
-       if [ ${ok} -ne ${EXIT_OK} ]; then
+       # Shut down the port before destroying it
+       if ! port_remove "${port}"; then
                return ${EXIT_ERROR}
        fi
 
-       port_remove "${port}"
+       if ! rm -rf "${NETWORK_PORTS_DIR}/${port}"; then
+               log ERROR "Could not destroy port ${port}"
+               return ${EXIT_ERROR}
+       fi
 
-       rm -rf $(port_dir ${port})
+       log INFO "Destroyed port ${port}"
+       return ${EXIT_OK}
 }
 
 port_create() {
-       port_cmd "create" $@
+       port_cmd "create" "$@"
 }
 
 port_remove() {
@@ -246,23 +235,29 @@ port_restart() {
 }
 
 port_edit() {
-       port_cmd edit $@
+       port_cmd edit "$@"
 }
 
 port_up() {
-       port_cmd up $@
+       assert [ $# -eq 1 ]
+
+       local port="${1}"
+
+       # Check if the port exists
+       if ! device_exists "${port}"; then
+               log ERROR "Could not bring up port ${port} which has not been created"
+               return ${EXIT_ERROR}
+       fi
+
+       port_cmd up "${port}"
 }
 
 port_down() {
-       port_cmd down $@
+       port_cmd down "$@"
 }
 
 port_status() {
-       port_cmd status $@
-}
-
-port_info() {
-       port_cmd info $@
+       port_cmd status "$@"
 }
 
 port_cmd() {
@@ -281,13 +276,12 @@ port_cmd() {
                return ${EXIT_ERROR}
        fi
 
-       hook_exec port ${hook} ${cmd} ${port} $@
+       hook_exec port ${hook} ${cmd} ${port} "$@"
 }
 
 ports_get() {
        local port
-       for port in $(port_dir)/*; do
-               port=$(basename ${port})
+       for port in $(list_directory "${NETWORK_PORTS_DIR}"); do
                if port_exists ${port}; then
                        echo "${port}"
                fi
@@ -314,30 +308,12 @@ port_find_free() {
        return ${EXIT_ERROR}
 }
 
-port_get_info() {
-       local port=${1}
-       local key=${2}
-
-       assert isset port
-       assert port_exists ${port}
-       assert isset key
-
-       (
-               eval $(port_info ${port})
-               echo "${!key}"
-       )
-}
-
-port_get_parents() {
-       local port=${1}
-
-       port_get_info ${port} PORT_PARENTS
-}
-
 port_get_children() {
        local port=${1}
 
-       port_get_info ${port} PORT_CHILDREN
+       assert port_exists "${port}"
+
+       port_cmd "children" "${port}"
 }
 
 port_zone() {
@@ -438,7 +414,7 @@ ports_lowest_address() {
 }
 
 port_identify() {
-       device_identify $@
+       device_identify "$@"
 }
 
 port_get_color() {