From 35cd477fea7b31a721e257bebdadc75245706435 Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Thu, 1 Feb 2024 05:53:51 +0100 Subject: [PATCH] Take hard links into account when computing disk usage based on dpkg-gencontrol.pl Thanks: Guillem Jover , Sven Joachim --- mmdebstrap | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/mmdebstrap b/mmdebstrap index a862a51..2da5c3a 100755 --- a/mmdebstrap +++ b/mmdebstrap @@ -4393,19 +4393,27 @@ sub approx_disk_usage { # We ignore /dev because depending on the mode, the directory might be # populated or not and we want consistent disk usage results independent # of the mode. - my $installed_size = 0; + 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;