mirror.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/perl
  2. #
  3. # Author: Daniel Stenberg <daniel@haxx.se>
  4. # Version: 0.1
  5. # Date: October 10, 2000
  6. #
  7. # This is public domain. Feel free to do whatever you please with this script.
  8. # There are no warranties whatsoever! It might work, it might ruin your hard
  9. # disk. Use this on your own risk.
  10. #
  11. # PURPOSE
  12. #
  13. # This script uses a local directory to maintain a "mirror" of the curl
  14. # packages listed in the remote curl web sites package list. Files present in
  15. # the local directory that aren't present in the remote list will be removed.
  16. # Files that are present in the remote list but not in the local directory
  17. # will be downloaded and put there. Files present at both places will not
  18. # be touched.
  19. #
  20. # WARNING: don't put other files in the mirror directory, they will be removed
  21. # when this script runs if they don't exist in the remote package list!
  22. #
  23. # this is the directory to keep all the mirrored curl files in:
  24. $some_dir = $ARGV[0];
  25. if( ! -d $some_dir ) {
  26. print "$some_dir is not a dir!\n";
  27. exit;
  28. }
  29. # path to the curl binary
  30. $curl = "/home/danste/bin/curl";
  31. # this is the remote file list
  32. $filelist = "http://curl.haxx.se/download/curldist.txt";
  33. # prepend URL:
  34. $prepend = "http://curl.haxx.se/download";
  35. opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
  36. @existing = grep { /^[^\.]/ } readdir(DIR);
  37. closedir DIR;
  38. $LOCAL_FILE = 1;
  39. $REMOTE_FILE = 2;
  40. # create a hash array
  41. for(@existing) {
  42. $allfiles{$_} |= $LOCAL_FILE;
  43. }
  44. # get remote file list
  45. print "Getting file list from $filelist\n";
  46. @remotefiles=`$curl -s $filelist`;
  47. # fill in the hash array
  48. for(@remotefiles) {
  49. chomp;
  50. $allfiles{$_} |= $REMOTE_FILE;
  51. $remote++;
  52. }
  53. if($remote < 10) {
  54. print "There's something wrong. The remote file list seems too smallish!\n";
  55. exit;
  56. }
  57. @sfiles = sort { $a cmp $b } keys %allfiles;
  58. $leftalone = $downloaded = $removed = 0;
  59. for(@sfiles) {
  60. $file = $_;
  61. $info = $allfiles{$file};
  62. if($info == ($REMOTE_FILE|$LOCAL_FILE)) {
  63. print "$file is LOCAL and REMOTE, left alone\n";
  64. $leftalone++;
  65. }
  66. elsif($info == $REMOTE_FILE) {
  67. print "$file is only REMOTE, getting it...\n";
  68. system("$curl $prepend/$file -o $some_dir/$file");
  69. $downloaded++;
  70. }
  71. elsif($info == $LOCAL_FILE) {
  72. print "$file is only LOCAL, removing it...\n";
  73. system("rm $some_dir/$file");
  74. $removed++;
  75. }
  76. else {
  77. print "Problem, file $file was marked $info\n";
  78. }
  79. $loops++;
  80. }
  81. if(!$loops) {
  82. print "No remote or local files were found!\n";
  83. exit;
  84. }
  85. print "$leftalone files were already present\n",
  86. "$downloaded files were added\n",
  87. "$removed files were removed\n";