Skip to content

Commit

Permalink
freebsd and macos fixes (#44)
Browse files Browse the repository at this point in the history
freebsd and macos share stat and sed history with some differences from
gnu

tested on freebsd and linux, mac just verified with man pages and stack
overflow

Co-authored-by: mosshope <[email protected]>
  • Loading branch information
duskmoss and mosshope authored Oct 4, 2024
1 parent 9cea80a commit 4a6fb83
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions zfs-inplace-rebalancing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,29 @@ function rebalance () {
# this shouldn't be needed in the typical case of `find` only finding files with links == 1
# but this can run for a long time, so it's good to double check if something changed
if [[ "${skip_hardlinks_flag,,}" == "true"* ]]; then
hardlink_count=$(stat -c "%h" "${file_path}")

if [ "${hardlink_count}" -ge 2 ]; then
if [[ "${OSTYPE,,}" == "linux-gnu"* ]]; then
# Linux
#
# -c --format=FORMAT
# use the specified FORMAT instead of the default; output a
# newline after each use of FORMAT
# %h number of hard links

hardlink_count=$(stat -c "%h" "${file_path}")
elif [[ "${OSTYPE,,}" == "darwin"* ]] || [[ "${OSTYPE,,}" == "freebsd"* ]]; then
# Mac OS
# FreeBSD
# -f format
# Display information using the specified format
# l Number of hard links to file (st_nlink)

hardlink_count=$(stat -f %l "${file_path}")
else
echo "Unsupported OS type: $OSTYPE"
exit 1
fi

if [ "${hardlink_count}" -ge 2 ]; then
echo "Skipping hard-linked file: ${file_path}"
return
fi
Expand Down Expand Up @@ -180,7 +200,7 @@ function rebalance () {
else
rebalance_count_line_nr="$((line_nr + 1))"
rebalance_count="$((rebalance_count + 1))"
sed -i "${rebalance_count_line_nr}s/.*/${rebalance_count}/" "./${rebalance_db_file_name}"
sed -i '' "${rebalance_count_line_nr}s/.*/${rebalance_count}/" "./${rebalance_db_file_name}"
fi
fi
}
Expand Down

0 comments on commit 4a6fb83

Please sign in to comment.