update 6.5 KB

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