update 6.6 KB

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