Changeset 3228


Ignore:
Timestamp:
Dec 30, 2013, 10:46:39 AM (5 years ago)
Author:
bruno
Message:
  • Add a test file for MondoRescue::File for the mr_read_all_link function which is a critical one
  • Function now work as expected including normalization
Location:
branches/3.2/MondoRescue
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/MondoRescue/bin/mr-read-all-link

    r3226 r3228  
    88#
    99use strict;
     10use Data::Dumper;
    1011use MondoRescue::File;
    1112
     
    5657my $file = mr_file_read_all_link(@ARGV) if (defined $ARGV[0]);
    5758
     59#print Dumper($file);
     60
    5861foreach my $f (sort keys %$file) {
    59     print "$f: $file->{$f}\n";
     62    print "$f\n";
     63    print "--\n";
     64    #print Dumper($f);
     65    foreach my $l (@{$file->{$f}}) {
     66        print "$l\n";
     67    }
     68    print "--\n";
    6069}
  • branches/3.2/MondoRescue/lib/MondoRescue/File.pm

    r3224 r3228  
    1313use Data::Dumper;
    1414use English;
    15 #use File::Basename;
     15use Cwd;
     16use File::Basename;
     17use ProjectBuilder::Base;
    1618use lib qw (lib);
    1719
     
    2426
    2527our @ISA = qw(Exporter);
    26 our @EXPORT = qw(mr_file_read_all_link mr_file_process_ldd);
     28our @EXPORT = qw(mr_file_read_all_link mr_file_process_ldd mr_file_normalize);
    2729
    2830=pod
     
    3739
    3840=head1 USAGE
     41
     42=over 4
     43
     44=item B<mr_file_normalize>
     45
     46This function normalize a path by removing .., . or // in paths given as parameters
     47
     48=cut
     49
     50$pbdebug = 2;
     51
     52sub mr_file_normalize {
     53
     54my $f = shift;
     55my $dir = shift || undef;
     56
     57# We modify based on the current dir,
     58# except when we're asked to use another one (case of a target link)
     59my $newpath = cwd;
     60$newpath = $dir if (defined $dir);
     61
     62return($f) if (not defined $f);
     63
     64pb_log(0,"mr_file_normalize file: **$f**\n");
     65pb_log(0,"mr_file_normalize dir : **$dir**\n") if (defined $dir);
     66
     67# Handle case of . at the start in path
     68if ($f =~ /^\.\//) {
     69    $f =~ s/^\.\//$newpath\//;
     70}
     71# Handle case of .. at the start in path
     72if ($f =~ /^\.\.\//) {
     73    my $dn = dirname(dirname($newpath));
     74    $f =~ s/^\.\.\//$dn\//;
     75}
     76# Now handles .. and . in the middle
     77$f =~ s|([^/]*)/([^/]+)/\.\./([^/]+)|$1/$3|g;
     78$f =~ s|([^/]*)/([^/]+)/\./([^/]+)|$1/$2/$3|g;
     79# Handle double /
     80$f =~ s|//|/|g;
     81
     82return($f);
     83}
    3984
    4085=over 4
     
    4489This function returns all the links found for a given file passed as parameter
    4590Example: mr_file_read_all_link(/lib64) returns (/lib64,/usr/lib64) on a system having a link from /lib64 to /usr/lib64
     91The return value is a hash of all input files pointing to arrays of links
    4692
    4793=cut
     
    5096
    5197# TODO: Can be parallelized
    52 my %files;
     98my $files;
    5399
    54100foreach my $f (@_) {
    55     print "Processing $f**\n";
    56     # Normalize the path if with .. in it or //
    57     $f =~ s|([^/]*)/([^/]+)/\.\./([^/]+)|$1/$3|g;
    58     $f =~ s|//|/|g;
     101    pb_log(2,"mr_file_read_all_link: **$f**\n");
     102    # Normalize the path if with .. or . or // in it
     103    $f = mr_file_normalize($f);
     104
    59105    my @fullpath = split(/\//,$f);
    60106    my $curdir = "";
    61107    while (@fullpath) {
    62108        my $dir = shift @fullpath;
    63         print "curdir is now: $curdir** and dir: $dir**\n";
    64         next if (($dir eq ".") || ($dir eq ""));
     109        pb_log(2,"curdir is now: $curdir** and dir: $dir**\n");
     110        next if ($dir eq "");
     111       
    65112        my $link = readlink("$curdir/$dir");
    66113        if (defined $link) {
     114            $link = mr_file_normalize($link,"$curdir/$dir");
    67115            # It's a real symlink so handle it
    68             if (defined $files{$f}) {
    69                 $files{$f} .= "$curdir/$dir|";
    70             } else {
    71                 $files{$f} = "$curdir/$dir|";
    72             }
     116            push @{$files->{$f}},"$curdir/$dir";
    73117            if (substr($link,0,1) eq "/") {
    74118                $curdir = $link;
     
    78122                $curdir = "$dn/$link";
    79123            }
    80             if ((! -d $curdir) || (-l $curdir)) {
     124            if ((-e $curdir) && ((! -d $curdir) || (-l $curdir))) {
    81125                my $h = mr_file_read_all_link($curdir);
    82                 print "File: $curdir - Return:\n".Dumper($h)."\n";
     126                pb_log(2,"File: $curdir - Return:\n".Dumper($h)."\n");
    83127                foreach my $k (keys %$h) {
    84                     $files{$f} .= $h->{$k}."|";
     128                    # At that point there is only one key
     129                    # as there was one param passed to the function.
     130                    foreach my $l (keys %$k) {
     131                        push @{$files->{$f}},$k->{$l};
     132                    }
    85133                }
    86134            }
     
    89137        }
    90138    }
    91     $files{$f} .= "$curdir";
    92 }
    93 return(\%files);
     139    pb_log(2,"curdir is now: $curdir**\n");
     140    push @{$files->{$f}},$curdir if (-e $curdir);
     141}
     142return($files);
    94143}
    95144
     
    105154sub mr_file_process_ldd {
    106155
    107 my %files;
     156my $files;
    108157
    109158foreach my $f (@_) {
     159    pb_log(2,"mr_file_process_ldd: **$f**\n");
    110160    open(CMD,"ldd $f 2> /dev/null |") || die "You need ldd for mindi support";
    111161    # Format is something like this:
     
    121171        $answer = $orig if ($orig =~ /^\//);
    122172        $answer = $dest if ((defined $dest) && ($dest =~ /^\//));
    123         $files{$answer} = $answer if (defined $answer);
     173        $files->{$answer} = $answer if (defined $answer);
    124174    }
    125175}
    126176close(CMD);
    127177
    128 return(\%files);
     178return($files);
    129179}
    130180
Note: See TracChangeset for help on using the changeset viewer.