source: MondoRescue/branches/3.2/MondoRescue/lib/MondoRescue/File.pm@ 3226

Last change on this file since 3226 was 3224, checked in by Bruno Cornec, 10 years ago
  • mr_file_read_all_link seems to be working correctly now.
  • test file for the latest mr functions (mr-process-ldd and mr-read-all-link)
File size: 3.8 KB
Line 
1#!/usr/bin/perl -w
2#
3# File subroutines brought by the MondoRescue project
4#
5# $Id$
6#
7# Copyright B. Cornec 2008-2014
8# Provided under the GPL v2
9
10package MondoRescue::File;
11
12use strict 'vars';
13use Data::Dumper;
14use English;
15#use File::Basename;
16use lib qw (lib);
17
18# Inherit from the "Exporter" module which handles exporting functions.
19
20use Exporter;
21
22# Export, by default, all the functions into the namespace of
23# any code which uses this module.
24
25our @ISA = qw(Exporter);
26our @EXPORT = qw(mr_file_read_all_link mr_file_process_ldd);
27
28=pod
29
30=head1 NAME
31
32MondoRescue::File, part of the mondorescue.org
33
34=head1 DESCRIPTION
35
36This modules provides low level and generic functions for the Mondorescue project
37
38=head1 USAGE
39
40=over 4
41
42=item B<mr_file_read_all_link>
43
44This function returns all the links found for a given file passed as parameter
45Example: mr_file_read_all_link(/lib64) returns (/lib64,/usr/lib64) on a system having a link from /lib64 to /usr/lib64
46
47=cut
48
49sub mr_file_read_all_link {
50
51# TODO: Can be parallelized
52my %files;
53
54foreach 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;
59 my @fullpath = split(/\//,$f);
60 my $curdir = "";
61 while (@fullpath) {
62 my $dir = shift @fullpath;
63 print "curdir is now: $curdir** and dir: $dir**\n";
64 next if (($dir eq ".") || ($dir eq ""));
65 my $link = readlink("$curdir/$dir");
66 if (defined $link) {
67 # 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 }
73 if (substr($link,0,1) eq "/") {
74 $curdir = $link;
75 } else {
76 my $dn = "";
77 $dn = $curdir if ($curdir ne "");
78 $curdir = "$dn/$link";
79 }
80 if ((! -d $curdir) || (-l $curdir)) {
81 my $h = mr_file_read_all_link($curdir);
82 print "File: $curdir - Return:\n".Dumper($h)."\n";
83 foreach my $k (keys %$h) {
84 $files{$f} .= $h->{$k}."|";
85 }
86 }
87 } else {
88 $curdir .= "/$dir";
89 }
90 }
91 $files{$f} .= "$curdir";
92}
93return(\%files);
94}
95
96=over 4
97
98=item B<mr_file_process_ldd>
99
100This function keeps track of the dependencies of a binary with its dynamic libraries by analyzing the return of ldd on that binary and recursing on all the dependencies to provide the complete list of files needed to have this binary run in a mindi or chroot context
101It takes a list of parameters which are the path of the binaries to analyze and for which we want its dependencies to be included
102
103=cut
104
105sub mr_file_process_ldd {
106
107my %files;
108
109foreach my $f (@_) {
110 open(CMD,"ldd $f 2> /dev/null |") || die "You need ldd for mindi support";
111 # Format is something like this:
112 # linux-vdso.so.1 (0x00007fff7c0ee000)
113 # libtermcap.so.2 => /lib64/libtermcap.so.2 (0x00007ff1f0b1b000)
114 # libdl.so.2 => /lib64/libdl.so.2 (0x00007ff1f0917000)
115 # libc.so.6 => /lib64/libc.so.6 (0x00007ff1f0564000)
116 # /lib64/ld-linux-x86-64.so.2 (0x00007ff1f0d1f000)
117 my $answer = undef;
118 while (<CMD>) {
119 my ($empty,$orig,$symb,$dest,$hexa) = split(/\s+/);
120 # print "**$orig**$symb**$dest**$hexa\n";
121 $answer = $orig if ($orig =~ /^\//);
122 $answer = $dest if ((defined $dest) && ($dest =~ /^\//));
123 $files{$answer} = $answer if (defined $answer);
124 }
125}
126close(CMD);
127
128return(\%files);
129}
130
131=back
132
133=head1 WEB SITES
134
135The main Web site of the project is available at L<http://www.mondorescue.org>. Bug reports should be filled using the trac instance of the project at L<http://trac.mondorescue.org/>.
136
137=head1 USER MAILING LIST
138
139For community exchanges around MondoRescue please use the list L<http://sourceforge.net/mailarchive/forum.php?forum_name=mondo-devel>
140
141=head1 AUTHORS
142
143The MondoRescue team lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
144
145=head1 COPYRIGHT
146
147MondoRescue is distributed under the GPL v2.0 license or later,
148described in the file C<COPYING> included with the distribution.
149
150=cut
151
152
Note: See TracBrowser for help on using the repository browser.