Skip to content

Commit

Permalink
Make generic the AC power online check.
Browse files Browse the repository at this point in the history
  • Loading branch information
comio committed Jan 31, 2018
1 parent 1ee5d67 commit 7c89cf0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion btrfs-balance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion btrfs-defrag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion btrfs-scrub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion btrfs-trim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
39 changes: 24 additions & 15 deletions btrfsmaintenance-functions
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,37 @@ check_balance_running() {
return 0
}

# 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
}
4 changes: 2 additions & 2 deletions sysconfig.btrfsmaintenance
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7c89cf0

Please sign in to comment.