Fix space handling in paths

This commit is contained in:
undaunt 2024-11-06 13:37:42 -08:00 committed by GitHub
parent 46c14fae53
commit 3ae15e565a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -37,9 +37,9 @@ function color_echo () {
} }
function get_rebalance_count () { function get_rebalance_count () {
file_path=$1 file_path="$1"
line_nr=$(grep -xF -n "${file_path}" "./${rebalance_db_file_name}" | head -n 1 | cut -d: -f1) line_nr=$(grep -xF -n -e "${file_path}" "./${rebalance_db_file_name}" | head -n 1 | cut -d: -f1)
if [ -z "${line_nr}" ]; then if [ -z "${line_nr}" ]; then
echo "0" echo "0"
return return
@ -176,7 +176,7 @@ function process_inode_group() {
if [ "${passes_flag}" -ge 1 ]; then if [ "${passes_flag}" -ge 1 ]; then
# Update rebalance "database" for all files # Update rebalance "database" for all files
for path in "${paths[@]}"; do for path in "${paths[@]}"; do
line_nr=$(grep -xF -n "${path}" "./${rebalance_db_file_name}" | head -n 1 | cut -d: -f1) line_nr=$(grep -xF -n -e "${path}" "./${rebalance_db_file_name}" | head -n 1 | cut -d: -f1)
if [ -z "${line_nr}" ]; then if [ -z "${line_nr}" ]; then
rebalance_count=1 rebalance_count=1
echo "${path}" >> "./${rebalance_db_file_name}" echo "${path}" >> "./${rebalance_db_file_name}"
@ -268,23 +268,19 @@ if [ "$debug_flag" = true ]; then
cat sorted_files_list.txt cat sorted_files_list.txt
fi fi
# Use awk to group paths by inode key # Use awk to group paths by inode key and handle spaces in paths
awk -F'|' '{ awk -F'|' '{
key = $1 key = $1
path = $2 path = $2
if (key == prev_key) { if (key == prev_key) {
paths = paths " " path print "\t" path
} else { } else {
if (NR > 1) { if (NR > 1) {
print prev_key "|" paths # Do nothing
} }
print key
print "\t" path
prev_key = key prev_key = key
paths = path
}
}
END {
if (NR > 0) {
print prev_key "|" paths
} }
}' sorted_files_list.txt > grouped_inodes.txt }' sorted_files_list.txt > grouped_inodes.txt
@ -294,7 +290,7 @@ if [ "$debug_flag" = true ]; then
fi fi
# Count number of inode groups # Count number of inode groups
file_count=$(wc -l < grouped_inodes.txt | tr -d ' ') file_count=$(grep -cvP '^\t' grouped_inodes.txt)
color_echo "$Cyan" " Number of files to process: ${file_count}" color_echo "$Cyan" " Number of files to process: ${file_count}"
@ -306,17 +302,31 @@ if [ "${passes_flag}" -ge 1 ]; then
touch "./${rebalance_db_file_name}" touch "./${rebalance_db_file_name}"
fi fi
# Read grouped_inodes.txt and process each group key=""
while IFS='|' read -r key paths; do paths=()
if [ "$debug_flag" = true ]; then
echo "Detected inode group: key=${key}" # Read grouped_inodes.txt line by line
echo "Paths:${paths}" while IFS= read -r line; do
if [[ "$line" == $'\t'* ]]; then
# This is a path line
path="${line#$'\t'}"
paths+=("$path")
else
# This is a new inode key
if [[ "${#paths[@]}" -gt 0 ]]; then
# Process the previous group
process_inode_group "${paths[@]}"
fi
key="$line"
paths=()
fi fi
# Split the paths into an array
read -a path_array <<< "${paths}"
process_inode_group "${path_array[@]}"
done < grouped_inodes.txt done < grouped_inodes.txt
# Process the last group after the loop ends
if [[ "${#paths[@]}" -gt 0 ]]; then
process_inode_group "${paths[@]}"
fi
# Clean up temporary files # Clean up temporary files
rm files_list.txt sorted_files_list.txt grouped_inodes.txt rm files_list.txt sorted_files_list.txt grouped_inodes.txt