+#
+# Return the history of all backups for a particular directory
+#
+sub dirHistory
+{
+ my($m, $share, $dir) = @_;
+ my($i, $level);
+ my $files = {};
+
+ $dir = "/$dir" if ( $dir !~ m{^/} );
+ $dir =~ s{/+$}{};
+
+ #
+ # merge backups, starting at the first one, and working
+ # forward.
+ #
+ for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
+ $level = $m->{backups}[$i]{level};
+ my $backupNum = $m->{backups}[$i]{num};
+ my $mangle = $m->{backups}[$i]{mangle};
+ my $compress = $m->{backups}[$i]{compress};
+ my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
+ my $legacyCharset = $m->{backups}[$i]{version} < 3.0;
+ my $sharePathM;
+ if ( $mangle ) {
+ $sharePathM = $m->{bpc}->fileNameEltMangle($share)
+ . $m->{bpc}->fileNameMangle($dir);
+ } else {
+ $sharePathM = $share . $dir;
+ }
+ $path .= $sharePathM;
+ #print(STDERR "Opening $path (share=$share)\n");
+
+ my $dirOpts = { %{$m->{dirOpts} || {} } };
+ my $attribOpts = { compress => $compress };
+ if ( $legacyCharset ) {
+ $dirOpts->{charsetLegacy}
+ = $attribOpts->{charsetLegacy}
+ = $m->{bpc}->{Conf}{ClientCharsetLegacy} || "iso-8859-1";
+ }
+
+ my $dirInfo = $m->{bpc}->dirRead($path, $dirOpts);
+ if ( !defined($dirInfo) ) {
+ #
+ # Oops, directory doesn't exist.
+ #
+ next;
+ }
+ my $attr;
+ if ( $mangle ) {
+ $attr = BackupPC::Attrib->new($attribOpts);
+ if ( !$attr->read($path) ) {
+ $m->{error} = "Can't read attribute file in $path";
+ $attr = undef;
+ }
+ }
+ foreach my $entry ( @$dirInfo ) {
+ my $file = $1 if ( $entry->{name} =~ /(.*)/s );
+ my $fileUM = $file;
+ $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
+ #print(STDERR "Doing $fileUM\n");
+ #
+ # skip special files
+ #
+ next if ( $file eq ".."
+ || $file eq "."
+ || $mangle && $file eq "attrib"
+ || defined($files->{$fileUM}[$i]) );
+
+ my $realPath = "$path/$file";
+ from_to($realPath, "utf8", $attribOpts->{charsetLegacy})
+ if ( $attribOpts->{charsetLegacy} ne "" );
+ my @s = stat($realPath);
+ if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
+ $files->{$fileUM}[$i] = $a;
+ $attr->set($fileUM, undef);
+ } else {
+ #
+ # Very expensive in the non-attribute case when compresseion
+ # is on. We have to stat the file and read compressed files
+ # to determine their size.
+ #
+ $files->{$fileUM}[$i] = {
+ type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
+ mode => $s[2],
+ uid => $s[4],
+ gid => $s[5],
+ size => -f _ ? $s[7] : 0,
+ mtime => $s[9],
+ };
+ if ( $compress && -f _ ) {
+ #
+ # Compute the correct size by reading the whole file
+ #
+ my $f = BackupPC::FileZIO->open("$realPath",
+ 0, $compress);
+ if ( !defined($f) ) {
+ $m->{error} = "Can't open $path/$file";
+ } else {
+ my($data, $size);
+ while ( $f->read(\$data, 65636 * 8) > 0 ) {
+ $size += length($data);
+ }
+ $f->close;
+ $files->{$fileUM}[$i]{size} = $size;
+ }
+ }
+ }
+ ($files->{$fileUM}[$i]{relPath} = "$dir/$fileUM") =~ s{//+}{/}g;
+ ($files->{$fileUM}[$i]{sharePathM} = "$sharePathM/$file")
+ =~ s{//+}{/}g;
+ ($files->{$fileUM}[$i]{fullPath} = "$path/$file") =~ s{//+}{/}g;
+ $files->{$fileUM}[$i]{backupNum} = $backupNum;
+ $files->{$fileUM}[$i]{compress} = $compress;
+ $files->{$fileUM}[$i]{nlink} = $entry->{nlink}
+ if ( $m->{dirOpts}{nlink} );
+ $files->{$fileUM}[$i]{inode} = $entry->{inode}
+ if ( $m->{dirOpts}{inode} );
+ }
+
+ #
+ # Flag deleted files
+ #
+ if ( defined($attr) ) {
+ my $a = $attr->get;
+ foreach my $fileUM ( keys(%$a) ) {
+ next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
+ $files->{$fileUM}[$i]{type} = BPC_FTYPE_DELETED;
+ }
+ }
+
+ #
+ # Merge old backups. Don't merge directories from old
+ # backups because every backup has an accurate directory
+ # tree.
+ #
+ for ( my $k = $i - 1 ; $level > 0 && $k >= 0 ; $k-- ) {
+ next if ( $m->{backups}[$k]{level} >= $level );
+ $level = $m->{backups}[$k]{level};
+ foreach my $fileUM ( keys(%$files) ) {
+ next if ( !defined($files->{$fileUM}[$k])
+ || defined($files->{$fileUM}[$i])
+ || $files->{$fileUM}[$k]{type} == BPC_FTYPE_DIR );
+ $files->{$fileUM}[$i] = $files->{$fileUM}[$k];
+ }
+ }
+ }
+
+ #
+ # Remove deleted files
+ #
+ for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
+ foreach my $fileUM ( keys(%$files) ) {
+ next if ( !defined($files->{$fileUM}[$i])
+ || $files->{$fileUM}[$i]{type} != BPC_FTYPE_DELETED );
+ $files->{$fileUM}[$i] = undef;
+ }
+ }
+
+ #print STDERR "Returning:\n", Dumper($files);
+ return $files;
+}
+
+