~n3npq/lsb/distribution-checker

« back to all changes in this revision

Viewing changes to utils/Ftp.pm

  • Committer: biga
  • Date: 2009-04-24 14:16:44 UTC
  • Revision ID: biga@spidey.linux-foundation.org-20090424141644-7evzd6mjocix7e68
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# LSB Test Execution Framework
 
2
# FTP Routines Module (Ftp.pm)
 
3
#
 
4
# Copyright (C) 2007-2009 The Linux Foundation. All rights reserved.
 
5
#
 
6
# This program has been developed by ISP RAS for LF.
 
7
# The ptyshell tool is originally written by Jiri Dluhos <jdluhos@suse.cz>
 
8
# Copyright (C) 2005-2007 SuSE Linux Products GmbH
 
9
#
 
10
# This program is free software; you can redistribute it and/or
 
11
# modify it under the terms of the GNU General Public License
 
12
# version 2 as published by the Free Software Foundation.
 
13
#
 
14
# This program is distributed in the hope that it will be useful,
 
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
# GNU General Public License for more details.
 
18
#
 
19
# You should have received a copy of the GNU General Public License
 
20
# along with this program; if not, write to the Free Software
 
21
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
22
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 
23
 
 
24
package Ftp;
 
25
use strict;
 
26
 
 
27
use POSIX qw/mktime/;
 
28
 
 
29
my $ftp;
 
30
our $list_refresh = 0;
 
31
#----------------------------------------------------------------------
 
32
 
 
33
sub login_ftp {
 
34
        my ($srv) = @_;
 
35
        
 
36
        # Check FTP perl module
 
37
        if( system ("perl -MNet::FTP -e 1 >/dev/null 2>&1") != 0) {
 
38
                        die "Perl module Net::FTP is required.";
 
39
        }
 
40
        
 
41
        # Open ftp
 
42
        require Net::FTP;
 
43
        
 
44
        print STDERR "FTP init... $srv\n";
 
45
        $ftp = Net::FTP->new($srv, Debug => 0, Passive => 1)
 
46
                or die "FTP init failed. Message: $@";
 
47
        
 
48
        # Log in
 
49
        print STDERR "FTP log in... $srv\n";
 
50
        $ftp->login()
 
51
                or die "FTP log in (anonymous) failed. Message: ".$ftp->message; 
 
52
        
 
53
        return;
 
54
}
 
55
#------------------------------------------------------------------------
 
56
 
 
57
# Reads FTP listing from a file.
 
58
sub read_listing_from_file {
 
59
        my ($filename) = @_;
 
60
        
 
61
        my @listing = (); #result; 
 
62
        
 
63
        open FILE, $filename
 
64
                or die "Can't open file '$filename': $!";
 
65
        
 
66
        while (<FILE>) {
 
67
                chomp;
 
68
                if ( /^\s*#/ ) {
 
69
                        if ( /^#TIMESTAMP:(\d{2})-(\d{2})-(\d{4})\s+(\d{2}):(\d{2})$/ ) {
 
70
                                # Timestamp helps to determine when the cache expires.
 
71
                                my $timestamp = mktime(0, $5, $4, $1, $2-1, $3-1900);
 
72
                                # Not used
 
73
                        }
 
74
                        elsif ( /^#PWD:(.*)$/ ) {
 
75
                                my $path = $1;
 
76
                                # One could check the path here
 
77
                        }
 
78
                        else {
 
79
                                print STDERR "Wrong syntax in file $filename\n";
 
80
                        }
 
81
                }
 
82
                else {
 
83
                        # It is a regular DIR line
 
84
                        push (@listing, $_);
 
85
                }
 
86
        }
 
87
        close FILE;
 
88
        
 
89
        return \@listing;
 
90
}
 
91
#------------------------------------------------------------------------
 
92
 
 
93
# Returns file listing of a given dir on the ftp server.
 
94
sub list_ftp {
 
95
        my ($srv, $dir, $cache_dir) = @_;
 
96
        # $srv  - e.g. "ftp.linuxfoundation.org"
 
97
        # $dir - what directory should be listed
 
98
        
 
99
        my @ftp_files = (); # results: @ftp_files[] -> ($filename, $filesize)
 
100
        
 
101
        my $listing = ();
 
102
        
 
103
        unless ( -d $cache_dir ) {
 
104
                (system("mkdir -p '$cache_dir'") == 0 )
 
105
                        or die "Failed to create the cache directory: '$cache_dir'";
 
106
        }
 
107
        
 
108
        print STDERR "> SRC $dir\n";
 
109
        
 
110
        my $ftp_list_file = "$cache_dir/ftp_list";
 
111
        unless( $list_refresh ) {
 
112
                # Read cached listing
 
113
                if ( -f $ftp_list_file ) {
 
114
                        $listing = read_listing_from_file( $ftp_list_file );
 
115
                }
 
116
        } 
 
117
        if( !$listing ) { 
 
118
                # Refresh the listing.
 
119
                ( $ftp ) or login_ftp( $srv );
 
120
                
 
121
                $listing = $ftp->dir($dir);
 
122
        }
 
123
        
 
124
        unless( $listing ) { 
 
125
                print STDERR "WRONG DIR\n";
 
126
                # Return empty list
 
127
                return @ftp_files;
 
128
        }
 
129
        
 
130
        # Save listing
 
131
        if ( $list_refresh || !-f $ftp_list_file ) {
 
132
                my ($sec, $min, $hour, $mday, $mon, $year) = localtime ();
 
133
                open LISTFILE, "> $ftp_list_file"
 
134
                        or die "Failed to open file '$ftp_list_file' for writing: $!";
 
135
                print LISTFILE
 
136
                        "#TIMESTAMP:"
 
137
                        .sprintf ("%02d-%02d-%04d %02d:%02d\n", $mday, ($mon + 1), 
 
138
                                        ($year + 1900), $hour, $min )
 
139
                        ."#PWD:".$dir."\n"
 
140
                        .(join "\n", @$listing)
 
141
                        ;
 
142
                close LISTFILE;
 
143
        }
 
144
        
 
145
        unless(@$listing) { 
 
146
                print STDERR "< Empty\n";
 
147
                # Return empty list
 
148
                return @ftp_files;
 
149
        }
 
150
 
 
151
        # Parse ftp files listing
 
152
        foreach my $line (@$listing) {
 
153
                # -rwxr-xr-x 1 user group size date time filename.ext
 
154
                if($line =~ m/^(\S+)\s+\S+\s+\S+\s+\S+\s+(\S+)\s+\S+\s+\S+\s+\S+\s+(\S+)/) {
 
155
                        my $attrs = $1;
 
156
                        my $filesize = $2;
 
157
                        my $filename = $3;
 
158
                        
 
159
                        # If it is an ordinary file or a symlink
 
160
                        if($attrs =~ /^[-l]/ ) {
 
161
                                $filesize = 0  if $attrs =~ /^l/; # Set the size undefined if this is a symlink
 
162
                                my @line = ($filename, $filesize);
 
163
                                push @ftp_files, \@line;
 
164
                        }
 
165
                }
 
166
                else { 
 
167
                        print STDERR "Wrong syntax in ftp listing: $line\n";
 
168
                }
 
169
        }
 
170
        print STDERR "< Ok\n";
 
171
 
 
172
        return @ftp_files;
 
173
}
 
174
 
 
175
 
 
176
#----------------------------------------------------------------------------
 
177
1; # Returned value