Subject: | There should be a "block count" option to be more like du(1). |
Normal du(1) counts blocks in use rather than the length of the file.
This patch adds an option "blocks" which can make Filesys::DiskUsage to
do the same thing. This makes the results closer to the output of
du(1), which is what I was expecting.
Subject: | filesys-diskusage-blocks.patch |
diff -ruN Filesys-DiskUsage-0.04/Changes Filesys-DiskUsage-0.04-dom/Changes
--- Filesys-DiskUsage-0.04/Changes 2005-11-07 10:32:14.000000000 +0000
+++ Filesys-DiskUsage-0.04-dom/Changes 2006-08-04 15:43:00.499068318 +0100
@@ -1,5 +1,8 @@
Revision history for Filesys::DiskUsage
+0.05 Fri Aug 4 15:42:30 2006
+ - added blocks support as well as file length.
+
0.04 Mon Nov 05 10:31:00 2005
- added sector-size and symlink-size patch, thanks to Darin McBride
- fixed a couple of things on fdu
diff -ruN Filesys-DiskUsage-0.04/lib/Filesys/DiskUsage.pm Filesys-DiskUsage-0.04-dom/lib/Filesys/DiskUsage.pm
--- Filesys-DiskUsage-0.04/lib/Filesys/DiskUsage.pm 2005-11-07 10:27:28.000000000 +0000
+++ Filesys-DiskUsage-0.04-dom/lib/Filesys/DiskUsage.pm 2006-08-04 15:59:41.019523206 +0100
@@ -5,6 +5,8 @@
use File::Basename;
+use constant BYTES_PER_BLOCK => 512;
+
=head1 NAME
Filesys::DiskUsage - Estimate file space usage (similar to `du`)
@@ -73,6 +75,17 @@
=over 6
+=item blocks
+
+Return the size based upon the number of blocks that the file occupies,
+rather than the length of the file. The two values might be different
+if the file is sparse.
+
+This value should match more closely the value returned by the du(1)
+command.
+
+ $total = du( { blocks => 1 } , $dir );
+
=item dereference
Follow symbolic links. Default is 0. Overrides C<symlink-size>.
@@ -165,6 +178,7 @@
sub du {
# options
my %config = (
+ 'blocks' => 0,
'dereference' => 0,
'exclude' => undef,
'human-readable' => 0,
@@ -192,6 +206,7 @@
$sizes{$_} = du( { 'recursive' => $config{'recursive'},
'exclude' => $config{'exclude'},
'sector-size' => $config{'sector-size'},
+ 'blocks' => $config{'blocks'},
}, readlink($_));
}
else {
@@ -200,8 +215,13 @@
}
}
elsif (-f) { # is a file
- $sizes{$_} = $config{'sector-size'} - 1 + -s;
- $sizes{$_} -= $sizes{$_} % $config{'sector-size'};
+ if ($config{blocks}) {
+ $sizes{$_} = (stat(_))[12] * BYTES_PER_BLOCK;
+ }
+ else {
+ $sizes{$_} = $config{'sector-size'} - 1 + -s;
+ $sizes{$_} -= $sizes{$_} % $config{'sector-size'};
+ }
}
elsif (-d) { # is a directory
if ($config{recursive} && $config{'max-depth'}) {
@@ -211,6 +231,7 @@
'max-depth' => $config{'max-depth'} -1,
'exclude' => $config{'exclude'},
'sector-size' => $config{'sector-size'},
+ 'blocks' => $config{'blocks'},
}, map {"$dir/$_"} grep {! /^\.\.?$/} readdir DIR );
}
}