From 29b8a7c378cd275c4c1f8d8c995b65cdf19657af Mon Sep 17 00:00:00 2001 From: Jim Pryor Date: Tue, 11 Aug 2009 08:05:02 -0400 Subject: Improve listing/iterating over profiles Signed-off-by: Jim Pryor --- src/netcfg-menu | 9 +++++---- src/network | 37 ++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/netcfg-menu b/src/netcfg-menu index 3c800a1..055cdf0 100644 --- a/src/netcfg-menu +++ b/src/netcfg-menu @@ -5,16 +5,17 @@ # Scan all profiles i=0 -for prof in `find -L $PROFILE_DIR -maxdepth 1 -type f -printf "%f\n"|sort`; do +# JP: change for prof to while read prof to avoid assumption that profile names are always single tokens (no spaces etc.) +while read prof; do # if there is a profile called "main", Use as default [ "$prof" = "main" ] && DEFAULT=$prof - unset DESCRIPTION + unset DESCRIPTION # JP: we can''t sandbox the sourced profiles, because we need to expose profiles[] . $PROFILE_DIR/$prof profiles[$i]=$prof i=$((i+1)) - profiles[$i]=$DESCRIPTION + profiles[$i]="$DESCRIPTION" # JP: this will usually have spaces and must be quoted i=$((i+1)) -done +done < <(list_profiles | sort) # JP: re-use list_profiles instead of duplicating it; avoid subshell we'd get by piping it to the while read... if [ ${#profiles} -eq 0 ]; then exit_err "No profiles were found in $PROFILE_DIR" diff --git a/src/network b/src/network index e7e262a..94494cb 100644 --- a/src/network +++ b/src/network @@ -33,12 +33,13 @@ validate_profile() ### Profile up/down ## # all_down -# take all profiles down -# +# take all registered profiles down all_down() { - for prof in $(find ${STATE_DIR}/profiles/ -maxdepth 1 -type f -printf '%f\n'); do - profile_down $prof + find "$STATE_DIR/profiles/" -maxdepth 1 -type f -printf '%f\n' \ + | while read prof; do + # the pipe to while read... creates a subshell + profile_down "$prof" done } @@ -50,20 +51,26 @@ all_suspend() [[ ! -d $STATE_DIR ]] && mkdir -p $STATE_DIR/{interfaces,profiles} [[ ! -d $STATE_DIR/suspend ]] && mkdir $STATE_DIR/suspend - for prof in $(find $STATE_DIR/profiles/ -maxdepth 1 -type f); do - cp $prof $STATE_DIR/suspend/ - profile_down $(basename $prof) + find "$STATE_DIR/profiles/" -maxdepth 1 -type f -printf '%f\n' \ + | while read prof; do + # the pipe to "while read" will create a subshell, so sourced variables will already be in a sandbox + report_notify "suspending profile $prof" + cp "$STATE_DIR/profiles/$prof" "$STATE_DIR/suspend/" + profile_down "$prof" done } -# all_suspend -# store a list of running profiles and take them down -# +# all_resume +# resume suspended interfaces all_resume() { - for prof in $(find $STATE_DIR/suspend/ -maxdepth 1 -type f); do - profile_up $(basename $prof) - rm $prof + report_debug all_resume "$@" + find "$STATE_DIR/suspend/" -maxdepth 1 -type f -printf '%f\n' \ + | while read prof; do + # the pipe to "while read" will create a subshell, so sourced variables will already be in a sandbox + report_notify "resuming profile $prof" + profile_up "$prof" + rm -f "$STATE_DIR/suspend/$prof" # if profile_up succeeds, it will have already removed this done } @@ -235,9 +242,9 @@ get_iface_prof() { # list_profiles # Outputs a list of all profiles list_profiles() { - find $PROFILE_DIR/ -maxdepth 1 -type f -printf "%f\n" + # JP: follow aliases with -L, also skip profiles that start with '.' or end with '~' or '.conf' (so profile.conf can be the wpa.conf file for profile) + find -L "$PROFILE_DIR/" -maxdepth 1 -type f -not -name '*~' -not -name '*.conf' -not -name '.*' -printf "%f\n" } - # check_profile profile # Return 0 if profile up # Return 1 if profile down -- cgit v1.2.3-24-g4f1b