diff --git a/btrfs-balance.sh b/btrfs-balance.sh index 26013e0..689e6d5 100755 --- a/btrfs-balance.sh +++ b/btrfs-balance.sh @@ -15,7 +15,7 @@ if [ -f /etc/default/btrfsmaintenance ] ; then fi if [ "$BTRFS_BALANCE_WAIT_AC_POWER" = "true" ]; then - wait_ac_power $BTRFS_AC_POWER_TIMEOUT "$BTRFS_AC_POWER_DEVICE" + wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE fi LOGIDENTIFIER='btrfs-balance' diff --git a/btrfs-defrag.sh b/btrfs-defrag.sh index edf6b3b..68be48d 100755 --- a/btrfs-defrag.sh +++ b/btrfs-defrag.sh @@ -15,7 +15,7 @@ if [ -f /etc/default/btrfsmaintenance ] ; then fi if [ "$BTRFS_DEFRAG_WAIT_AC_POWER" = "true" ]; then - wait_ac_power $BTRFS_AC_POWER_TIMEOUT "$BTRFS_AC_POWER_DEVICE" + wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE fi LOGIDENTIFIER='btrfs-defrag' diff --git a/btrfs-scrub.sh b/btrfs-scrub.sh index 0f5bb75..557c87a 100755 --- a/btrfs-scrub.sh +++ b/btrfs-scrub.sh @@ -15,7 +15,7 @@ if [ -f /etc/default/btrfsmaintenance ] ; then fi if [ "$BTRFS_SCRUB_WAIT_AC_POWER" = "true" ]; then - wait_ac_power $BTRFS_AC_POWER_TIMEOUT "$BTRFS_AC_POWER_DEVICE" + wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE fi LOGIDENTIFIER='btrfs-scrub' diff --git a/btrfs-trim.sh b/btrfs-trim.sh index 04500ea..fe94d96 100755 --- a/btrfs-trim.sh +++ b/btrfs-trim.sh @@ -15,7 +15,7 @@ if [ -f /etc/default/btrfsmaintenance ] ; then fi if [ "$BTRFS_TRIM__WAIT_AC_POWER" = "true" ]; then - wait_ac_power $BTRFS_AC_POWER_TIMEOUT "$BTRFS_AC_POWER_DEVICE" + wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE fi LOGIDENTIFIER='btrfs-trim' diff --git a/btrfsmaintenance-functions b/btrfsmaintenance-functions index 529b183..96a3c94 100644 --- a/btrfsmaintenance-functions +++ b/btrfsmaintenance-functions @@ -77,28 +77,37 @@ is_btrfs() { return 1 } +# function: check_power_status +# parameter: paths to check the status +# +# Return true if at least one path is online +check_power_status() { + local status=$(cat "$@" 2>/dev/null) + [ -z "$status" ] && return 0 + case "$status" in + *1*) + return 0 + ;; + *) + return 1 + ;; + esac +} + # function: wait_ac_power # parameter: {timeout} {AC power online device} # # wait until ac power goes online wait_ac_power() { local timecount=0 - local timeout=0 - local ac_power_device=/sys/class/power_supply/AC/online + local timeout=$1 - [ -n "$1" ] && [[ $1 =~ ^[0-9]+$ ]] && timeout=$1 - [ -n "$2" ]&& ac_power_device="$2" + while ! check_power_status "$@"; do + # AC is not online + [ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1 + sleep 1s + timecount=$((timecount+1)) + done - echo timeout $timeout ac_power_device $ac_power_device - - if [ -f "${ac_power_device}" ]; then - while [ "$(cat ${ac_power_device})" -ne 1 ]; do - # AC is not online - [ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1 - sleep 1s - timecount=$((timecount+1)) - done - return 0 - fi return 0 } diff --git a/sysconfig.btrfsmaintenance b/sysconfig.btrfsmaintenance index 871ab4e..1d259ac 100644 --- a/sysconfig.btrfsmaintenance +++ b/sysconfig.btrfsmaintenance @@ -15,10 +15,10 @@ BTRFS_AC_POWER_TIMEOUT=3600 ## Path: System/File systems/btrfs ## Type: string -## Default: "/sys/class/power_supply/AC/online" +## Default: "/sys/class/power_supply/*/online" # # Path of AC status flag -BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/AC/online" +BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/*/online" ## Path: System/File systems/btrfs ## Type: string