1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 17:55:08 +00:00

fix: use Base-10 sizes and mdls logical size to match macOS Finder

- Switch bytes_to_human (shell) and humanizeBytes (Go) from Base-2
  (1024) to Base-10 (1000) to match Apple's storage calculation
  standard since Snow Leopard
- Add proper decimal rounding instead of truncation
- Use mdls kMDItemLogicalSize for .app bundles to avoid APFS clone
  file undercounting by du

Fixes #511
This commit is contained in:
tw93
2026-02-28 10:02:34 +08:00
parent 646ff72a96
commit 1be71edc9d
4 changed files with 36 additions and 20 deletions

View File

@@ -451,6 +451,7 @@ ensure_user_file() {
# ============================================================================
# Convert bytes to human-readable format (e.g., 1.5GB)
# macOS (since Snow Leopard) uses Base-10 calculation (1 KB = 1000 bytes)
bytes_to_human() {
local bytes="$1"
[[ "$bytes" =~ ^[0-9]+$ ]] || {
@@ -458,15 +459,17 @@ bytes_to_human() {
return 1
}
# GB: >= 1073741824 bytes
if ((bytes >= 1073741824)); then
printf "%d.%02dGB\n" $((bytes / 1073741824)) $(((bytes % 1073741824) * 100 / 1073741824))
# MB: >= 1048576 bytes
elif ((bytes >= 1048576)); then
printf "%d.%01dMB\n" $((bytes / 1048576)) $(((bytes % 1048576) * 10 / 1048576))
# KB: >= 1024 bytes (round up)
elif ((bytes >= 1024)); then
printf "%dKB\n" $(((bytes + 512) / 1024))
# GB: >= 1,000,000,000 bytes
if ((bytes >= 1000000000)); then
local scaled=$(( (bytes * 100 + 500000000) / 1000000000 ))
printf "%d.%02dGB\n" $((scaled / 100)) $((scaled % 100))
# MB: >= 1,000,000 bytes
elif ((bytes >= 1000000)); then
local scaled=$(( (bytes * 10 + 500000) / 1000000 ))
printf "%d.%01dMB\n" $((scaled / 10)) $((scaled % 10))
# KB: >= 1,000 bytes (round up to nearest KB instead of decimal)
elif ((bytes >= 1000)); then
printf "%dKB\n" $(((bytes + 500) / 1000))
else
printf "%dB\n" "$bytes"
fi

View File

@@ -503,6 +503,19 @@ get_path_size_kb() {
echo "0"
return
}
# For .app bundles, prefer mdls logical size as it matches Finder
# (APFS clone/sparse files make 'du' severely underreport apps like Xcode)
if [[ "$path" == *.app || "$path" == *.app/ ]]; then
local mdls_size
mdls_size=$(mdls -name kMDItemLogicalSize -raw "$path" 2> /dev/null || true)
if [[ "$mdls_size" =~ ^[0-9]+$ && "$mdls_size" -gt 0 ]]; then
# Return in KB
echo "$((mdls_size / 1024))"
return
fi
fi
local size
size=$(command du -skP "$path" 2> /dev/null | awk 'NR==1 {print $1; exit}' || true)