update 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/usr/bin/perl
  2. # -*-perl-*-
  3. #
  4. # Copyright © 1996 Andy Guy <awpguy@acs.ucalgary.ca>
  5. # Copyright © 1998 Martin Schulze <joey@infodrom.north.de>
  6. # Copyright © 1999, 2009 Raphaël Hertzog <hertzog@debian.org>
  7. #
  8. # This program has been distributed under the terms of the GNU GPL.
  9. use strict;
  10. use warnings;
  11. #use diagnostics;
  12. use lib '/usr/lib/perl5/Debian';
  13. use lib '/usr/share/perl5/Debian';
  14. eval 'use Net::FTP;';
  15. if ($@) {
  16. print STDERR "Please install the 'perl' package if you want to use the\n" .
  17. "FTP access method of dselect.\n\n";
  18. exit 1;
  19. }
  20. use Dselect::Ftp;
  21. # deal with arguments
  22. my $vardir = $ARGV[0];
  23. my $method = $ARGV[1];
  24. my $option = $ARGV[2];
  25. if ($option eq "manual") {
  26. print "Enter package file names or a blank line to finish\n";
  27. while(1) {
  28. print "Enter package file name:";
  29. my $fn = <STDIN>;
  30. chomp $fn;
  31. if ( $fn == "") {
  32. exit 0;
  33. }
  34. if ( -f $fn ) {
  35. system ("dpkg", "--merge-avail", $fn);
  36. } else {
  37. print "Could not find $fn, try again\n";
  38. }
  39. };
  40. };
  41. #print "vardir: $vardir, method: $method, option: $option\n";
  42. my $arch=`dpkg --print-architecture`;
  43. $arch='i386' if $?;
  44. chomp $arch;
  45. my $exit = 0;
  46. # get info from control file
  47. read_config("$vardir/methods/ftp/vars");
  48. chdir "$vardir/methods/ftp";
  49. print "Getting Packages files...(stop with ^C)\n\n";
  50. my @pkgfiles;
  51. my $ftp;
  52. my $packages_modified = 0;
  53. sub download {
  54. foreach (@{$config{'site'}}) {
  55. my $site = $_;
  56. $ftp = do_connect ($_->[0], # Ftp server
  57. $_->[4], # username
  58. $_->[5], # password
  59. $_->[1], # ftp dir
  60. $_->[3], # passive
  61. $config{'use_auth_proxy'},
  62. $config{'proxyhost'},
  63. $config{'proxylogname'},
  64. $config{'proxypassword'});
  65. my @dists = @{$_->[2]};
  66. my $dist;
  67. PACKAGE:
  68. foreach $dist (@dists) {
  69. my $dir = "$dist/binary-$arch";
  70. my $must_get = 0;
  71. my $newest_pack_date;
  72. # check existing Packages on remote site
  73. print "\nChecking for Packages file... ";
  74. $newest_pack_date = do_mdtm ($ftp, "$dir/Packages.gz");
  75. if (defined $newest_pack_date) {
  76. print "$dir/Packages.gz\n";
  77. } else {
  78. $dir = "$dist";
  79. $newest_pack_date = do_mdtm ($ftp, "$dir/Packages.gz");
  80. if (defined $newest_pack_date) {
  81. print "$dir/Packages.gz\n";
  82. } else {
  83. print "Couldn't find Packages.gz in $dist/binary-$arch or $dist; ignoring.\n";
  84. print "Your setup is probably wrong, check the distributions directories,\n";
  85. print "and try with passive mode enabled/disabled (if you use a proxy/firewall)\n";
  86. next PACKAGE;
  87. }
  88. }
  89. # we now have $dir set to point to an existing Packages.gz file
  90. # check if we already have a Packages file (and get its date)
  91. $dist =~ tr/\//_/;
  92. my $file = "Packages.$site->[0].$dist";
  93. # if not
  94. if (! -f $file) {
  95. # must get one
  96. # print "No Packages here; must get it.\n";
  97. $must_get = 1;
  98. } else {
  99. # else check last modification date
  100. my @pack_stat = stat($file);
  101. if($newest_pack_date > $pack_stat[9]) {
  102. # print "Packages has changed; must get it.\n";
  103. $must_get = 1;
  104. } elsif ($newest_pack_date < $pack_stat[9]) {
  105. print " Our file is newer than theirs; skipping.\n";
  106. } else {
  107. print " Already up-to-date; skipping.\n";
  108. }
  109. }
  110. if ($must_get) {
  111. -f "Packages.gz" and unlink "Packages.gz";
  112. -f "Packages" and unlink "Packages";
  113. my $size = 0;
  114. TRY_GET_PACKAGES:
  115. while (1) {
  116. if ($size) {
  117. print " Continuing ";
  118. } else {
  119. print " Getting ";
  120. }
  121. print "Packages file from $dir...\n";
  122. eval {
  123. if ($ftp->get("$dir/Packages.gz", "Packages.gz", $size)) {
  124. if (system("gunzip", "Packages.gz")) {
  125. print " Couldn't gunzip Packages.gz, stopped";
  126. die "error";
  127. }
  128. } else {
  129. print " Couldn't get Packages.gz from $dir !!! Stopped.";
  130. die "error";
  131. }
  132. };
  133. if ($@) {
  134. $size = -s "Packages.gz";
  135. if (ref($ftp)) {
  136. $ftp->abort();
  137. $ftp->quit();
  138. };
  139. if (yesno ("y", "Transfer failed at $size: retry at once")) {
  140. $ftp = do_connect ($site->[0], # Ftp server
  141. $site->[4], # username
  142. $site->[5], # password
  143. $site->[1], # ftp dir
  144. $site->[3], # passive
  145. $config{'use_auth_proxy'},
  146. $config{'proxyhost'},
  147. $config{'proxylogname'},
  148. $config{'proxypassword'});
  149. if ($newest_pack_date != do_mdtm ($ftp, "$dir/Packages.gz")) {
  150. print ("Packages file has changed !\n");
  151. $size = 0;
  152. }
  153. next TRY_GET_PACKAGES;
  154. } else {
  155. die "error";
  156. }
  157. }
  158. last TRY_GET_PACKAGES;
  159. }
  160. if(!rename "Packages", "Packages.$site->[0].$dist") {
  161. print " Couldn't rename Packages to Packages.$site->[0].$dist";
  162. die "error";
  163. } else {
  164. # set local Packages file to same date as the one it mirrors
  165. # to allow comparison to work.
  166. utime $newest_pack_date, $newest_pack_date, "Packages.$site->[0].$dist";
  167. $packages_modified = 1;
  168. }
  169. }
  170. push @pkgfiles, "Packages.$site->[0].$dist";
  171. }
  172. $ftp->quit();
  173. }
  174. }
  175. eval {
  176. local $SIG{INT} = sub {
  177. die "Interrupted!\n";
  178. };
  179. download();
  180. };
  181. if($@) {
  182. $ftp->quit() if (ref($ftp));
  183. if($@ =~ /timeout/i) {
  184. print "FTP TIMEOUT\n";
  185. } else {
  186. print "FTP ERROR - $@\n";
  187. }
  188. $exit = 1;
  189. };
  190. my $ans;
  191. if ($packages_modified) { # don't clear if nothing changed
  192. print <<EOM;
  193. It is a good idea to clear the available list of old packages.
  194. However if you have only downloaded a Package files from non-main
  195. distributions you might not want to do this.
  196. EOM
  197. if (yesno ("y", "Do you want to clear available list")) {
  198. print "Clearing...\n";
  199. if(system("dpkg", "--clear-avail")) {
  200. print "dpkg --clear-avail failed.";
  201. die "error";
  202. }
  203. }
  204. }
  205. if (!$packages_modified) {
  206. print "No Packages files was updated.\n";
  207. } else {
  208. my $file;
  209. foreach $file (@pkgfiles) {
  210. if(system ("dpkg", "--merge-avail", $file)) {
  211. print "Dpkg merge available failed on $file";
  212. $exit = 1;
  213. }
  214. }
  215. }
  216. exit $exit;