]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoDownload.pm
Move common functions for template use to Packages::Template
[deb/packages.git] / lib / Packages / DoDownload.pm
1 package Packages::DoDownload;
2
3 use strict;
4 use warnings;
5
6 use POSIX;
7 use CGI ();
8 use DB_File;
9 use Benchmark ':hireswallclock';
10 use Exporter;
11
12 use Deb::Versions;
13 use Packages::I18N::Locale;
14 use Packages::Search qw( :all );
15 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES @SECTIONS @ARCHITECTURES );
16 use Packages::CGI;
17 use Packages::DB;
18
19 our @ISA = qw( Exporter );
20 our @EXPORT = qw( do_download );
21
22
23 sub do_download {
24     my ($params, $opts, $page_content) = @_;
25
26     if ($params->{errors}{package}) {
27         fatal_error( _g( "package not valid or not specified" ) );
28     }
29     if ($params->{errors}{suite}) {
30         fatal_error( _g( "suite not valid or not specified" ) );
31     }
32     if ($params->{errors}{arch}) {
33         fatal_error( _g( "architecture not valid or not specified" ) );
34     }
35     if (@{$opts->{suite}} > 1) {
36         fatal_error( sprintf( _g( "more than one suite specified for download (%s)" ), "@{$opts->{suite}}" ) );
37     }
38     if (@{$opts->{arch}} > 1) {
39         fatal_error( sprintf( _g( "more than one architecture specified for download (%s)" ), "@{$opts->{arch}}" ) );
40     }
41
42     $opts->{h_sections} = { map { $_ => 1 } @SECTIONS };
43     my $pkg = $opts->{package};
44     my $suite = $opts->{suite}[0];
45     my $arch = $opts->{arch}[0] ||'';
46
47     our (%packages_all);
48     my (@results);
49     my ($final_result, $filename, $directory, @file_components, $archive) = ("")x5;
50
51     my $st0 = new Benchmark;
52     unless (@Packages::CGI::fatal_errors) {
53         tie %packages_all, 'DB_File', "$DBDIR/packages_all_$suite.db",
54         O_RDONLY, 0666, $DB_BTREE
55             or die "couldn't tie DB $DBDIR/packages_all_$suite.db: $!";
56         
57         read_entry( \%packages, $pkg, \@results, $opts );
58
59         @results = grep { $_->[7] ne 'v' } @results;
60         unless (@results) {
61 #           fatal_error( _g( "No such package." )."<br>".
62 #                        sprintf( _g( '<a href="%s">Search for the package</a>' ), "$SEARCH_URL/$pkg" ) );
63         } else {
64             my $final_result = shift @results;
65             foreach (@results) {
66                 if (version_cmp( $_->[7], $final_result->[7] ) > 0) {
67                     $final_result = $_;
68                 }
69             }
70             
71             debug( "final_result=@$final_result", 1 );
72             $archive = $final_result->[1];
73             my %data = split /\000/, $packages_all{"$pkg $arch $final_result->[7]"};
74             if (!%data && $arch ne 'all' && $final_result->[3] eq 'all') {
75                 %data = split /\000/, $packages_all{"$pkg all $final_result->[7]"};
76                 $arch = 'all';
77                 debug( "choosing arch 'all' instead of requested arch $arch", 1 );
78 #               fatal_error( _g( "No such package." )."<br>".
79 #                            sprintf( _g( '<a href="%s">Search for the package</a>' ), "$SEARCH_URL/$pkg" ) ) unless %data;
80             }
81             @file_components = split('/', $data{filename});
82             $filename = pop(@file_components);
83             $directory = join( '/', @file_components).'/';
84
85             $page_content->{archive} = $archive;
86             $page_content->{suite} = $suite;
87             $page_content->{pkg} = $pkg;
88             my $pkgsize = floor(($data{size}/102.4)+0.5)/10;
89             if ($pkgsize < 1024) {
90                 $page_content->{pkgsize} = sprintf( '%.1f', $pkgsize );
91                 $page_content->{pkgsize_unit} = _g( 'kByte' );
92             } else {
93                 $page_content->{pkgsize} = sprintf( '%.1f', floor(($data{size}/(102.4*102.4))+0.5)/100 );
94                 $page_content->{pkgsize_unit} = _g( 'MByte' );
95             }
96             $page_content->{architecture} = $arch;
97             foreach (keys %data) {
98                 $page_content->{$_} = $data{$_};
99             }
100             $page_content->{filename} = { file => $filename,
101                                           directory => $directory,
102                                           full => $data{filename},
103                                       };
104
105         }
106     }
107         
108 }
109
110 1;