From d13917104528fba28cd110373f130b3149002782 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Wed, 18 Apr 2012 18:47:08 -0400 Subject: lsinitcpio: show file sizes in analyze output Always display the file size on disk, but also display the uncompressed size for compressed images. Borrows my own size_to_human function from paccache and elsewhere. Before: ==> Compressed with: gzip -> Compression ratio: .377 -> Estimated decompression time: 0.058s After: ==> Size: 2.67 MiB ==> Compressed with: gzip -> Uncompressed size: 7.07 MiB (.377 ratio) -> Estimated decompression time: 0.059s Signed-off-by: Dave Reisner --- lsinitcpio | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'lsinitcpio') diff --git a/lsinitcpio b/lsinitcpio index aa97df7..55ce1e8 100755 --- a/lsinitcpio +++ b/lsinitcpio @@ -39,6 +39,28 @@ die() { exit 1 } +size_to_human() { + awk -v size="$1" ' + BEGIN { + suffix[1] = "B" + suffix[2] = "KiB" + suffix[3] = "MiB" + suffix[4] = "GiB" + suffix[5] = "TiB" + count = 1 + + while (size > 1024) { + size /= 1024 + count++ + } + + sizestr = sprintf("%.2f", size) + sub(/\.?0+$/, "", sizestr) + printf("%s %s", sizestr, suffix[count]) + }' +} + + OPT_SHORT='ahnvx' OPT_LONG=('analyze' 'help' 'nocolor' 'verbose' 'extract') @@ -105,11 +127,13 @@ if (( analyze )); then declare -a binaries explicitmod modules foundhooks hooks declare kernver ratio columns=$(tput cols) + zsize=$(stat -c %s "$image") + # calculate compression ratio if [[ $compress ]]; then TIMEFORMAT=%R decomptime=$({ time decomp "$image" >/dev/null; } 2>&1 ) - ratio=.$(( $(stat -c %s "$image") * 1000 / - $(decomp "$image" | bsdtar xOf - | wc -c) % 1000 )) + fullsize=$(decomp "$image" | bsdtar xOf - | wc -c) + ratio=.$(( zsize * 1000 / fullsize % 1000 )) fi # read contents of image @@ -141,10 +165,11 @@ if (( analyze )); then msg 'Image: %s %s' "$imagename" [[ $version ]] && msg 'Created with mkinitcpio %s' "$version" msg 'Kernel: %s' "${kernver:-unknown}" + msg 'Size: %s' "$(size_to_human "$zsize")" if [[ $compress ]]; then msg 'Compressed with: %s' "$compress" - msg2 'Compression ratio: %s' "$ratio" + msg2 'Uncompressed size: %s (%s ratio)' "$(size_to_human "$fullsize")" "$ratio" msg2 'Estimated decompression time: %ss' "$decomptime" fi printf '\n' -- cgit v1.2.3-24-g4f1b