| 1 | package Slackware::Slackget::Network::Connection::FILE; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | require Slackware::Slackget::Network::Connection ; | ||
| 7 | require Time::HiRes ; | ||
| 8 | require Slackware::Slackget::Status ; | ||
| 9 | use File::Copy ; | ||
| 10 | use Slackware::Slackget::File; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Slackware::Slackget::Network::Connection::FILE - This class is the file:// protocol driver for Slackware::Slackget::Network::Connection | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | Version 1.0.0 | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | our $VERSION = '0.6.0'; | ||
| 23 | # our @ISA = qw( Slackware::Slackget::Network::Connection ) ; | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | This class is the file:// protocol driver for Slackware::Slackget::Network::Connection. | ||
| 28 | |||
| 29 | You can't use this class without the Slackware::Slackget::Network::Connection one. | ||
| 30 | |||
| 31 | This class need the following extra CPAN modules : | ||
| 32 | |||
| 33 | - File::Copy | ||
| 34 | - Time::HiRes | ||
| 35 | |||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub new | ||
| 40 | { | ||
| 41 | my ($class,$url,$config) = @_ ; | ||
| 42 | my $self = {}; | ||
| 43 | # return undef if(!defined($config) && ref($config) ne 'HASH'); | ||
| 44 | return undef unless (is_url($self,$url)); | ||
| 45 | bless($self,$class); | ||
| 46 | $self->parse_url($url) ; | ||
| 47 | return $self; | ||
| 48 | } | ||
| 49 | |||
| 50 | =head1 CONSTRUCTOR | ||
| 51 | |||
| 52 | =head2 new | ||
| 53 | |||
| 54 | This class is not designed to be instanciate alone or used alone. You have to use the Slackware::Slackget::Network::Connection. | ||
| 55 | |||
| 56 | =head1 FUNCTIONS | ||
| 57 | |||
| 58 | =head2 __test_server | ||
| 59 | |||
| 60 | This method test the rapidity of the repository, by timing a copy of the FILELIST.TXT file. | ||
| 61 | |||
| 62 | my $time = $self->test_server() ; | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub __test_server { | ||
| 67 | my $self = shift ; | ||
| 68 | # print "[debug file] protocol : $self->{DATA}->{protocol}\n"; | ||
| 69 | # print "[debug file] host : $self->{DATA}->{host}\n"; | ||
| 70 | my $file = $self->{DATA}->{path}.'/'.'FILELIST.TXT'; | ||
| 71 | $file = $self->strip_slash($file); | ||
| 72 | return undef unless( -e $file); | ||
| 73 | my $start_time = Time::HiRes::time(); | ||
| 74 | my @head = copy($file,'/tmp/FILELIST.TXT') or return undef; | ||
| 75 | my $stop_time = Time::HiRes::time(); | ||
| 76 | unlink '/tmp/FILELIST.TXT'; | ||
| 77 | return ($stop_time - $start_time); | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 __get_file | ||
| 81 | |||
| 82 | Return the given file. | ||
| 83 | |||
| 84 | my $file = $connection->get_file('PACKAGES.TXT') ; | ||
| 85 | |||
| 86 | You can pass an extra argument (boolean) to mark the file as a binary one. | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub __get_file { | ||
| 91 | my ($self,$remote_file,$binary) = @_ ; | ||
| 92 | $remote_file = $self->file unless(defined($remote_file)) ; | ||
| 93 | my $file; | ||
| 94 | if($binary or $remote_file =~ /\.tgz$/) | ||
| 95 | { | ||
| 96 | $file = Slackware::Slackget::File->new($self->strip_slash($self->path().'/'.$remote_file), 'binary' => 1); | ||
| 97 | } | ||
| 98 | else | ||
| 99 | { | ||
| 100 | $file = Slackware::Slackget::File->new($self->strip_slash($self->path().'/'.$remote_file), 'binary' => 0); | ||
| 101 | } | ||
| 102 | return $file->Get_file ; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 __fetch_file | ||
| 106 | |||
| 107 | Copy a given file to a given location. | ||
| 108 | |||
| 109 | $connection->fetch_file() ; # download the file $connection->file and store it at $config->{common}->{'update-directory'}/$connection->file, this way is not recommended | ||
| 110 | or | ||
| 111 | $connection->fetch_file($remote_file) ; # download the file $remote_file and store it at $config->{common}->{'update-directory'}/$connection->file, this way is not recommended | ||
| 112 | or | ||
| 113 | $connection->fetch_file('PACKAGES.TXT',"$config->{common}->{'update-directory'}/".$current_specialfilecontainer_object->id."/PACKAGES.TXT") ; # This is the recommended way. | ||
| 114 | # This is equivalent to : $connection->fetch_file($remote_file,$local_file) ; | ||
| 115 | |||
| 116 | This method return a Slackware::Slackget::Status object with the following object declaration : | ||
| 117 | |||
| 118 | my $state = Slackware::Slackget::Status->new(codes => { | ||
| 119 | 0 => "All goes well. Server said: $ret_code - ".status_message( $ret_code ), | ||
| 120 | 1 => "Destination directory does not exist.\n", | ||
| 121 | 2 => "Destination directory is not writable.\n", | ||
| 122 | }); | ||
| 123 | |||
| 124 | This is the direct code of this method :) | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub __fetch_file { | ||
| 129 | my ($self,$remote_file,$local_file) = @_ ; | ||
| 130 | $remote_file = $self->file unless(defined($remote_file)); | ||
| 131 | unless(defined($local_file)){ | ||
| 132 | if(defined($self->{DATA}->{download_directory}) && -e $self->{DATA}->{download_directory}){ | ||
| 133 | $remote_file=~ /([^\/]*)$/; | ||
| 134 | $local_file = $self->{DATA}->{download_directory}.'/'.$1 ; | ||
| 135 | } | ||
| 136 | elsif(defined($self->{DATA}->{config})){ | ||
| 137 | $remote_file=~ /([^\/]*)$/; | ||
| 138 | $local_file = $self->{DATA}->{config}->{common}->{'update-directory'}.'/'.$1 ; | ||
| 139 | } | ||
| 140 | else{ | ||
| 141 | warn "[Slackware::Slackget::Network::Connection::FILE] unable to determine the path to save $remote_file.\n"; | ||
| 142 | return undef; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | my $url = $self->path().'/'.$remote_file; | ||
| 146 | $url = $self->strip_slash($url); | ||
| 147 | # print "[debug file] save the fetched file ($url) to $local_file\n"; | ||
| 148 | my $state = Slackware::Slackget::Status->new(codes => { | ||
| 149 | 0 => "All goes well.", | ||
| 150 | 1 => "Destination directory does not exist.", | ||
| 151 | 2 => "Destination directory is not writable.", | ||
| 152 | }); | ||
| 153 | if(copy($url,$local_file.'.part')){ | ||
| 154 | move($local_file.'.part',$local_file); | ||
| 155 | $state->current(0); | ||
| 156 | } | ||
| 157 | else | ||
| 158 | { | ||
| 159 | if(! -e $self->path()) | ||
| 160 | { | ||
| 161 | |||
| 162 | $state->current(1); | ||
| 163 | |||
| 164 | } | ||
| 165 | elsif(! -w $self->path()) | ||
| 166 | { | ||
| 167 | $state->current(2); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | return $state; | ||
| 171 | } | ||
| 172 | |||
| 173 | =head2 __fetch_all | ||
| 174 | |||
| 175 | This method fetch all files declare in the "files" parameter of the constructor. | ||
| 176 | |||
| 177 | $connection->fetch_all or die "Unable to fetch all files\n"; | ||
| 178 | |||
| 179 | This method save all files in the $config->{common}->{'update-directory'} directory (so you have to manage yourself the files deletion/replacement problems) | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub __fetch_all { | ||
| 184 | my $self = shift ; | ||
| 185 | foreach (@{$self->files}){ | ||
| 186 | $self->fetch_file($_) or return undef; | ||
| 187 | } | ||
| 188 | return 1 ; | ||
| 189 | } | ||
| 190 | |||
| 191 | =head1 AUTHOR | ||
| 192 | |||
| 193 | DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >> | ||
| 194 | |||
| 195 | =head1 BUGS | ||
| 196 | |||
| 197 | Please report any bugs or feature requests to | ||
| 198 | C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at | ||
| 199 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>. | ||
| 200 | I will be notified, and then you'll automatically be notified of progress on | ||
| 201 | your bug as I make changes. | ||
| 202 | |||
| 203 | =head1 SUPPORT | ||
| 204 | |||
| 205 | You can find documentation for this module with the perldoc command. | ||
| 206 | |||
| 207 | perldoc Slackware::Slackget | ||
| 208 | |||
| 209 | |||
| 210 | You can also look for information at: | ||
| 211 | |||
| 212 | =over 4 | ||
| 213 | |||
| 214 | =item * Infinity Perl website | ||
| 215 | |||
| 216 | L<http://www.infinityperl.org/category/slack-get> | ||
| 217 | |||
| 218 | =item * slack-get specific website | ||
| 219 | |||
| 220 | L<http://slackget.infinityperl.org> | ||
| 221 | |||
| 222 | =item * RT: CPAN's request tracker | ||
| 223 | |||
| 224 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget> | ||
| 225 | |||
| 226 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 227 | |||
| 228 | L<http://annocpan.org/dist/Slackware-Slackget> | ||
| 229 | |||
| 230 | =item * CPAN Ratings | ||
| 231 | |||
| 232 | L<http://cpanratings.perl.org/d/Slackware-Slackget> | ||
| 233 | |||
| 234 | =item * Search CPAN | ||
| 235 | |||
| 236 | L<http://search.cpan.org/dist/Slackware-Slackget> | ||
| 237 | |||
| 238 | =back | ||
| 239 | |||
| 240 | =head1 ACKNOWLEDGEMENTS | ||
| 241 | |||
| 242 | Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation. | ||
| 243 | |||
| 244 | =head1 COPYRIGHT & LICENSE | ||
| 245 | |||
| 246 | Copyright 2005 DUPUIS Arnaud, All Rights Reserved. | ||
| 247 | |||
| 248 | This program is free software; you can redistribute it and/or modify it | ||
| 249 | under the same terms as Perl itself. | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | 1; # End of Slackware::Slackget::Network::Connection::FILE |