Files
gists/tools/hashing/sparsecmp.sh
tke fd515742b5 Restructure repo layout and document conventions
Move legacy systemscripts into scripts/display and scripts/setup.
Rehome stray top-level tools into their domain folders.
Archive narrow experiments and outdated codegrab leftovers.
Remove empty legacy directories and stale root files.
Expand macOS metadata ignores and update the README with the refined repository structure.
2026-03-07 18:54:32 +01:00

51 lines
1.4 KiB
Bash

#!/bin/bash
# Function to get size of file or block device
get_size() {
if [ -b "$1" ]; then # Check if input is a block device
size=$(sudo blockdev --getsize64 "$1")
else # Assume input is a file
size=$(stat --format="%s" "$1")
fi
echo $size
}
# Check if at least two arguments are provided
if [ $# -lt 2 ]; then
echo "Usage: $0 <source> <target> [test count]"
exit 1
fi
source="$1"
target="$2"
count="${3:-100}" # Default to 100 tests if not specified
# Ensure the source and target exist
if [ ! -e "$source" ] || [ ! -e "$target" ]; then
echo "Error: Source or target does not exist."
exit 1
fi
size=$(get_size "$source") # Get size in bytes
if [ $? -ne 0 ] || [ -z "$size" ] || [ "$size" -eq 0 ]; then
echo "Error: Failed to get size of the source."
exit 1
fi
interval=$((size / count))
for ((i=0; i<count; i++)); do
offset=$((i * interval))
percent=$((i * 100 / count))
# Use printf to overwrite the current line with the offset and percentage
printf "\r%$(tput cols)s\rComparing at offset $offset... (${percent}%%)"
if ! cmp --bytes=1M "$source" "$target" --ignore-initial=$offset > /dev/null 2>&1; then
echo -e "\nMismatch or error at offset $offset"
exit 1 # Exit after the first mismatch
fi
done
# Clear the line after the loop finishes successfully and print success message
printf "\r%$(tput cols)s\r"
echo "No mismatches found."