update 6.7 KB

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