Skip to content

Commit

Permalink
mss: binary: do a storage check before creating
Browse files Browse the repository at this point in the history
* The DiskFree binary on android is very neutered, and is missing some flags such as --ouput=avail
* Use awk to strip the "Available" and "G" from the string, and then finally round the number

Signed-off-by: chickendrop89 <[email protected]>
  • Loading branch information
chickendrop89 committed Jul 19, 2024
1 parent fd9ccd5 commit b9fa7cd
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions system/bin/magiskswap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ abort(){
exit 1
}

# Function to check available space in gigabytes
checkFreeSpace() {
REQUIRED_SPACE="$1"

# The DiskFree binary on android is very neutered, and is missing some flags such as --ouput=avail
# Use awk to strip the "Available" and "G" from the string, and then finally round the number
AVAILABLE_SPACE=$(df -h "$MODPATH" | awk 'NR==2 {gsub(/Available\n/,""); sub(/G$/,""); print int($4)}')

# Check if required space is less than or equal to available space
if ! [[ $REQUIRED_SPACE -le $AVAILABLE_SPACE ]];
then
abort "insufficient space ($AVAILABLE_SPACE GB available)"
fi
}

# Create swapfile and swaps-on
createSwapSpace(){
SIZE="$1";
Expand Down Expand Up @@ -64,6 +79,8 @@ createSwapSpace(){
rm -rf "$SWAPFILE" || abort "cannot remove swapfile!"
fi

checkFreeSpace "$SIZE"

cprint "creating $SIZE GB swapfile"
dd if=/dev/zero of="$SWAPFILE" bs=1024 count="$SIZE_KB" 1>/dev/null

Expand Down

0 comments on commit b9fa7cd

Please sign in to comment.