update 6.1 KB

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