X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=lib%2FBackupPC%2FLib.pm;h=cb66a106ff6900381a961c9ac486a30c3f8fb0ae;hp=3a8cf184f08484054b91fa260c3f2d1bd8fb3387;hb=ca593f66fd6c35764bd8997c6338b781330f019c;hpb=82ffaa1c4130a34812fb241c2ea5cd3d0608bdab diff --git a/lib/BackupPC/Lib.pm b/lib/BackupPC/Lib.pm index 3a8cf18..cb66a10 100644 --- a/lib/BackupPC/Lib.pm +++ b/lib/BackupPC/Lib.pm @@ -11,7 +11,7 @@ # Craig Barratt # # COPYRIGHT -# Copyright (C) 2001-2007 Craig Barratt +# Copyright (C) 2001-2009 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 @@ -29,7 +29,7 @@ # #======================================================================== # -# Version 3.1.0beta0, released 3 Sep 2007. +# Version 3.2.0beta0, released 5 April 2009. # # See http://backuppc.sourceforge.net. # @@ -51,7 +51,7 @@ use Digest::MD5; use Config; use Encode qw/from_to encode_utf8/; -use vars qw( $IODirentOk ); +use vars qw( $IODirentOk $IODirentLoaded ); use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); require Exporter; @@ -72,7 +72,7 @@ require DynaLoader; BEGIN { eval "use IO::Dirent qw( readdirent DT_DIR );"; - $IODirentOk = 1 if ( !$@ ); + $IODirentLoaded = 1 if ( !$@ ); }; # @@ -102,7 +102,7 @@ sub new # # Set defaults for $topDir and $installDir. # - $topDir = '/tera0/backup/BackupPC' if ( $topDir eq "" ); + $topDir = '/data/BackupPC' if ( $topDir eq "" ); $installDir = '/usr/local/BackupPC' if ( $installDir eq "" ); # @@ -115,7 +115,7 @@ sub new useFHS => $useFHS, TopDir => $topDir, InstallDir => $installDir, - ConfDir => $confDir eq "" ? '/tera0/backup/BackupPC/conf' : $confDir, + ConfDir => $confDir eq "" ? '/data/BackupPC/conf' : $confDir, LogDir => '/var/log/BackupPC', }; } else { @@ -130,7 +130,7 @@ sub new my $bpc = bless { %$paths, - Version => '3.1.0beta0', + Version => '3.2.0beta0', }, $class; $bpc->{storage} = BackupPC::Storage->new($paths); @@ -139,8 +139,6 @@ sub new # Clean up %ENV and setup other variables. # delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; - $bpc->{PoolDir} = "$bpc->{TopDir}/pool"; - $bpc->{CPoolDir} = "$bpc->{TopDir}/cpool"; if ( defined(my $error = $bpc->ConfigRead()) ) { print(STDERR $error, "\n"); return; @@ -154,6 +152,8 @@ sub new $paths->{$dir} = $bpc->{$dir} = $bpc->{Conf}{$dir}; } $bpc->{storage}->setPaths($paths); + $bpc->{PoolDir} = "$bpc->{TopDir}/pool"; + $bpc->{CPoolDir} = "$bpc->{TopDir}/cpool"; # # Verify we are running as the correct user @@ -364,9 +364,9 @@ sub ConfigRead # Read host config file # if ( $host ne "" ) { - ($mesg, $config) = $bpc->{storage}->ConfigDataRead($host); + ($mesg, $config) = $bpc->{storage}->ConfigDataRead($host, $config); return $mesg if ( defined($mesg) ); - $bpc->{Conf} = { %{$bpc->{Conf}}, %$config }; + $bpc->{Conf} = $config; } # @@ -396,6 +396,12 @@ sub ConfigRead return $mesg; } $bpc->{Lang} = \%Lang; + + # + # Make sure IncrLevels is defined + # + $bpc->{Conf}{IncrLevels} = [1] if ( !defined($bpc->{Conf}{IncrLevels}) ); + return; } @@ -467,6 +473,26 @@ sub dirRead from_to($path, "utf8", $need->{charsetLegacy}) if ( $need->{charsetLegacy} ne "" ); return if ( !opendir(my $fh, $path) ); + if ( $IODirentLoaded && !$IODirentOk ) { + # + # Make sure the IO::Dirent really works - some installs + # on certain file systems (eg: XFS) don't return a valid type. + # + if ( opendir(my $fh, $bpc->{TopDir}) ) { + my $dt_dir = eval("DT_DIR"); + foreach my $e ( readdirent($fh) ) { + if ( $e->{name} eq "." && $e->{type} == $dt_dir ) { + $IODirentOk = 1; + last; + } + } + closedir($fh); + } + # + # if it isn't ok then don't check again. + # + $IODirentLoaded = 0 if ( !$IODirentOk ); + } if ( $IODirentOk ) { @entries = sort({ $a->{inode} <=> $b->{inode} } readdirent($fh)); map { $_->{type} = 0 + $_->{type} } @entries; # make type numeric @@ -598,7 +624,15 @@ sub RmTreeDefer my($i, $f); return if ( !-e $file ); - mkpath($trashDir, 0, 0777) if ( !-d $trashDir ); + if ( !-d $trashDir ) { + eval { mkpath($trashDir, 0, 0777) }; + if ( $@ ) { + # + # There's no good place to send this error - use stderr + # + print(STDERR "RmTreeDefer: can't create directory $trashDir"); + } + } for ( $i = 0 ; $i < 1000 ; $i++ ) { $f = sprintf("%s/%d_%d_%d", $trashDir, time, $$, $i); next if ( -e $f ); @@ -877,7 +911,10 @@ sub MakeFileLink } elsif ( $newFile && -f $name && (stat($name))[3] == 1 ) { my($newDir); ($newDir = $rawFile) =~ s{(.*)/.*}{$1}; - mkpath($newDir, 0, 0777) if ( !-d $newDir ); + if ( !-d $newDir ) { + eval { mkpath($newDir, 0, 0777) }; + return -5 if ( $@ ); + } return -4 if ( !link($name, $rawFile) ); return 2; } else { @@ -1009,6 +1046,10 @@ sub NetBiosInfoGet }; $nmbCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{NmbLookupCmd}, $args); foreach ( split(/[\n\r]+/, $bpc->cmdSystemOrEval($nmbCmd, undef, $args)) ) { + # + # skip and other non entries + # + next if ( /<\w{2}> - /i ); next if ( !/^\s*([\w\s-]+?)\s*<(\w{2})\> - .*/i ); $netBiosHostName ||= $1 if ( $2 eq "00" ); # host is first 00 $netBiosUserName = $1 if ( $2 eq "03" ); # user is last 03 @@ -1192,22 +1233,27 @@ sub cmdVarSubstitute } } # - # Merge variables into @tarClientCmd + # Merge variables into @cmd # foreach my $arg ( @$template ) { + # + # Replace $VAR with ${VAR} so that both types of variable + # substitution are supported + # + $arg =~ s[\$(\w+)]{\${$1}}g; # # Replace scalar variables first # - $arg =~ s{\$(\w+)(\+?)}{ + $arg =~ s[\${(\w+)}(\+?)]{ exists($vars->{$1}) && ref($vars->{$1}) ne "ARRAY" ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1}) - : "\$$1$2" + : "\${$1}$2" }eg; # # Now replicate any array arguments; this just works for just one # array var in each argument. # - if ( $arg =~ m{(.*)\$(\w+)(\+?)(.*)} && ref($vars->{$2}) eq "ARRAY" ) { + if ( $arg =~ m[(.*)\${(\w+)}(\+?)(.*)] && ref($vars->{$2}) eq "ARRAY" ) { my $pre = $1; my $var = $2; my $esc = $3; @@ -1444,7 +1490,40 @@ sub sortedPCLogFiles } closedir(DIR); } - return sort(compareLOGName @files); + return sort compareLOGName @files; +} + +# +# converts a glob-style pattern into a perl regular expression. +# +sub glob2re +{ + my ( $bpc, $glob ) = @_; + my ( $char, $subst ); + + # $escapeChars escapes characters with no special glob meaning but + # have meaning in regexps. + my $escapeChars = [ '.', '/', ]; + + # $charMap is where we implement the special meaning of glob + # patterns and translate them to regexps. + my $charMap = { + '?' => '[^/]', + '*' => '[^/]*', }; + + # multiple forward slashes are equivalent to one slash. We should + # never have to use this. + $glob =~ s/\/+/\//; + + foreach $char (@$escapeChars) { + $glob =~ s/\Q$char\E/\\$char/g; + } + + while ( ( $char, $subst ) = each(%$charMap) ) { + $glob =~ s/(?