1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#!/usr/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_ls: emulate a simple subset of ls to make it easy to
# navigate stored backups
#
# DESCRIPTION
#
# Usage: BackupPC_ls path
#
# AUTHOR
# Craig Barratt <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
# Copyright (C) 2005-2020 Craig Barratt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#========================================================================
#
# Version 4.4.0, released 20 Jun 2020.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================
use strict;
no utf8;
use lib "__INSTALLDIR__/lib";
use BackupPC::Lib;
use BackupPC::XS qw ( :all );
use BackupPC::View;
use Data::Dumper;
use Getopt::Std;
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
my $TopDir = $bpc->TopDir();
my $BinDir = $bpc->BinDir();
my %Conf = $bpc->Conf();
my($Host, $Num, $Share, $View, @Backups);
my %opts;
if ( !getopts("Rih:n:s:", \%opts) || @ARGV < 1 ) {
usageAndExit();
}
while ( my $dir = shift(@ARGV) ) {
my $i;
if ( $dir =~ m{^\Q$TopDir\E/pc/([^/]*)/(\d+)/(.*)} ) {
$Host = $1;
$Num = $2;
$dir = $3;
if ( $dir =~ m{(.*?)(/.*)} ) {
$Share = $1;
$dir = $2;
} else {
$Share = $dir;
$dir = "/";
}
$dir = $bpc->fileNameUnmangle($dir) if ( $dir !~ m{/[^f]} && -d "$TopDir/pc/$Host/$Num/$Share/$dir" );
$Share = $bpc->fileNameUnmangle($Share) if ( $Share =~ /^f/ && -d "$TopDir/pc/$Host/$Num/$Share" );
} else {
if ( !defined($opts{h}) || !defined($opts{n}) || !defined($opts{s}) ) {
print(STDERR "Can't determine host or backup number from path - need to specific -h, -n and -s\n");
usageAndExit();
}
$Host = $opts{h};
$Num = $opts{n};
$Share = $opts{s};
}
@Backups = $bpc->BackupInfoRead($Host);
$Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
for ( $i = 0 ; $i < @Backups ; $i++ ) {
last if ( $Backups[$i]{num} == $Num );
}
if ( $i >= @Backups ) {
print(STDERR "Backup #$Num doesn't exist for host $Host\n");
exit(1);
}
$View = BackupPC::View->new($bpc, $Host, \@Backups);
printDir($dir);
}
sub usageAndExit
{
print(STDERR "usage: $0 [-iR] [-h host] [-n bkupNum] [-s shareName] dirs/files...\n");
exit(1);
}
sub printDir
{
my($dir) = @_;
my($fileName, $info, $dirList);
$dir =~ s{/+$}{};
$dir =~ s{^/+}{/};
$dir = "/" if ( $dir eq "" );
my $info = $View->dirAttrib($Num, $Share, $dir);
#print("Num = $Num, Share = $Share, dir = $dir\n");
#print Dumper($info);
print("$dir:\n");
foreach my $file ( sort(keys(%$info)) ) {
my($str, $s, $fileData);
my @type2modeChar = ("-", "-", "l", "c", "b", "d", "p", "s", "?", "x");
my @type2suffixChar = ("", "", "@", "%", "%", "/", "|", "=", "", "");
my $attr = $info->{$file};
if (
(
$attr->{type} == BPC_FTYPE_SYMLINK
|| $attr->{type} == BPC_FTYPE_CHARDEV
|| $attr->{type} == BPC_FTYPE_BLOCKDEV
|| $attr->{type} == BPC_FTYPE_HARDLINK
)
&& $attr->{digest} ne ""
) {
#
# read the file
# TODO - handle v3 backups
#
my $f = $bpc->MD52Path($attr->{digest}, $attr->{compress});
if ( defined(my $fd = BackupPC::XS::FileZIO::open($f, 0, $attr->{compress})) ) {
my $target;
$fd->read(\$fileData, 65536);
$fd->close();
} else {
$fileData = "???";
}
}
$str = "";
if ( $opts{i} ) {
$str .= sprintf("%6d/%-3d ", $attr->{inode}, $attr->{nlinks} > 0 ? $attr->{nlinks} : 1);
}
if ( $attr->{type} == BPC_FTYPE_DELETED ) {
$str .= "*deleted--";
} else {
$str .= $type2modeChar[$attr->{type}] || "-";
for ( my $i = 6 ; $i >= 0 ; $i -= 3 ) {
$str .= ($attr->{mode} & (1 << ($i + 2))) ? "r" : '-';
$str .= ($attr->{mode} & (1 << ($i + 1))) ? "w" : '-';
$str .= ($attr->{mode} & (1 << ($i + 0))) ? "x" : '-';
}
}
$str .= defined($attr->{xattr}) ? "+" : " ";
$s = sprintf("%d/%d", $attr->{uid}, $attr->{gid});
$str .= sprintf(" %8s", $s);
if ( ($attr->{type} == BPC_FTYPE_CHARDEV || $attr->{type} == BPC_FTYPE_BLOCKDEV) && $fileData ne "" ) {
$str .= sprintf(" %10s", $fileData);
} else {
$str .= sprintf(" %10llu", $attr->{size});
}
$str .= " " . $bpc->timeStamp($attr->{mtime});
my $nameStr = "$dir/$attr->{name}";
$nameStr =~ s{//+}{/}g;
$str .= " " . $nameStr;
$str .= $type2suffixChar[$attr->{type}];
if ( $attr->{type} == BPC_FTYPE_SYMLINK && $fileData ne "" ) {
$str .= " -> " . $fileData;
}
if ( $attr->{type} == BPC_FTYPE_HARDLINK && $fileData =~ m{(.*)/(.*)} ) {
#
# Update digest to the hardlink target file
#
my $d = $1;
my $f = $2;
my $v = $View->dirAttrib($Num, $Share, $d);
$attr->{digest} = $v->{$f}{digest} if ( $v->{$f} && length($v->{$f}{digest}) );
}
if ( length($attr->{digest}) ) {
$str .= " (" . unpack("H*", $attr->{digest}) . ")";
}
print($str, "\n");
push(@$dirList, $file) if ( $attr->{type} == BPC_FTYPE_DIR );
}
if ( $opts{R} ) {
foreach my $file ( @$dirList ) {
printDir("$dir/$file");
}
}
$bpc->flushXSLibMesgs();
}
|