Take hard links into account when computing disk usage based on dpkg-gencontrol.pl

Thanks: Guillem Jover <guillem@debian.org>, Sven Joachim <svenjoac@gmx.de>
This commit is contained in:
Johannes Schauer Marin Rodrigues 2024-02-01 05:53:51 +01:00
parent a7586e55d1
commit 35cd477fea
Signed by: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -4394,18 +4394,26 @@ sub approx_disk_usage {
# populated or not and we want consistent disk usage results independent
# of the mode.
my $installed_size = 0;
my %hardlink;
my $scan_installed_size = sub {
if ($File::Find::name eq "$directory/dev") {
# add all entries of @devfiles once
$installed_size += scalar @devfiles;
return;
} elsif ($File::Find::name =~ /^$directory\/dev\//) {
# ignore everything below /dev
} elsif (-l $File::Find::name) {
# -f follows symlinks, so we first check if we have a symlink
$installed_size += 1;
} elsif (-f $File::Find::name) {
return;
}
lstat or error "cannot stat $File::Find::name";
if (-f _ or -l _) {
my ($dev, $ino, $nlink) = (lstat _)[0, 1, 3];
return if exists $hardlink{"$dev:$ino"};
# Track hardlinks to avoid repeated additions.
$hardlink{"$dev:$ino"} = 1 if $nlink > 1;
# add file size in 1024 byte blocks, rounded up
$installed_size += int(((-s $File::Find::name) + 1024) / 1024);
$installed_size += int(((-s _) + 1024) / 1024);
} else {
# all other entries are assumed to only take up one block
$installed_size += 1;