Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(readability): use a function for disk usage check #1615

Merged
merged 5 commits into from
Oct 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 17 additions & 36 deletions nodebuilder
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,21 @@ ensure_xargs_dependency()
fi
}

get_directory_size_in_mib()
{
if [ $# -ne 1 ]; then
throw_error 'get_directory_size_in_mib() requires 1 argument.'
fi

readonly directory_to_check="$1"

if [ -d "${directory_to_check}/" ]; then
BLOCKSIZE=1M du -d0 "${directory_to_check}" | cut -f1
else
printf '0\n'
fi
}

get_free_space_in_mib()
{
BLOCKSIZE=1M df "${BITCOIN_DATA_DIRECTORY}" | awk 'NR==2 {print $4}'
Expand Down Expand Up @@ -1688,42 +1703,8 @@ if [ ! -f "${BITCOIN_CORE_CONFIG_FILE}" ]; then
set_bitcoin_core_option 'server' '1'
fi

# Synced data and prune checks
readonly BITCOIN_CORE_BLOCKS_DIRECTORY="${BITCOIN_DATA_DIRECTORY}/blocks"
if [ -d "${BITCOIN_CORE_BLOCKS_DIRECTORY}"/ ]; then
case "${TARGET_KERNEL}" in
Darwin | FreeBSD | NetBSD)
INITIAL_BLOCKS_SIZE_IN_MIB="$(du -d0 -m "${BITCOIN_CORE_BLOCKS_DIRECTORY}" | cut -f1)"
;;
OpenBSD)
INITIAL_BLOCKS_SIZE_IN_MIB="$(BLOCKSIZE=1M du -d0 "${BITCOIN_CORE_BLOCKS_DIRECTORY}" | cut -f1)"
;;
*)
INITIAL_BLOCKS_SIZE_IN_MIB="$(du -d0 --block-size='1MiB' "${BITCOIN_CORE_BLOCKS_DIRECTORY}" | cut -f1)"
;;
esac
else
INITIAL_BLOCKS_SIZE_IN_MIB=0
fi
readonly INITIAL_BLOCKS_SIZE_IN_MIB
readonly BITCOIN_CORE_CHAINSTATE_DIRECTORY="${BITCOIN_DATA_DIRECTORY}/chainstate"
if [ -d "${BITCOIN_CORE_CHAINSTATE_DIRECTORY}"/ ]; then
case "${TARGET_KERNEL}" in
Darwin | FreeBSD | NetBSD)
INITIAL_CHAINSTATE_SIZE_IN_MIB="$(du -d0 -m "${BITCOIN_CORE_CHAINSTATE_DIRECTORY}" | cut -f1)"
;;
OpenBSD)
INITIAL_CHAINSTATE_SIZE_IN_MIB="$(BLOCKSIZE=1M du -d0 "${BITCOIN_CORE_CHAINSTATE_DIRECTORY}" | cut -f1)"
;;
*)
INITIAL_CHAINSTATE_SIZE_IN_MIB="$(du -d0 --block-size='1MiB' "${BITCOIN_CORE_CHAINSTATE_DIRECTORY}" | cut -f1)"
;;
esac
else
INITIAL_CHAINSTATE_SIZE_IN_MIB=0
fi
readonly INITIAL_CHAINSTATE_SIZE_IN_MIB
readonly INITIAL_DATA_ALREADY_SYNCED_IN_MIB=$((INITIAL_BLOCKS_SIZE_IN_MIB + INITIAL_CHAINSTATE_SIZE_IN_MIB))
INITIAL_DATA_ALREADY_SYNCED_IN_MIB="$(($(get_directory_size_in_mib "${BITCOIN_DATA_DIRECTORY}/blocks") + $(get_directory_size_in_mib "${BITCOIN_DATA_DIRECTORY}/chainstate")))"
readonly INITIAL_DATA_ALREADY_SYNCED_IN_MIB
log_info "Found data already synced... $((INITIAL_DATA_ALREADY_SYNCED_IN_MIB / MIB_TO_GIB)) GiB."

free_space_in_mib="$(get_free_space_in_mib)"
Expand Down
Loading