]> git.saurik.com Git - apt.git/blobdiff - methods/mirror.cc
undo the temporary progress reporting disabling which slipped into last commit
[apt.git] / methods / mirror.cc
index e8873d97b099f892c5d636d44982760ecd683d0d..e499b054b15edb0e36a62bf08417a6867e6aeba4 100644 (file)
 #include <apt-pkg/hashes.h>
 #include <apt-pkg/sourcelist.h>
 
+
+#include <algorithm>
 #include <fstream>
 #include <iostream>
+
 #include <stdarg.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/utsname.h>
 #include <dirent.h>
 
 using namespace std;
@@ -125,23 +129,66 @@ bool MirrorMethod::Clean(string Dir)
 
 bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str)
 {
-   if(Debug)
-      clog << "MirrorMethod::DownloadMirrorFile(): " << endl;
-
    // not that great to use pkgAcquire here, but we do not have 
    // any other way right now
    string fetch = BaseUri;
    fetch.replace(0,strlen("mirror://"),"http://");
 
+   if(Debug)
+      clog << "MirrorMethod::DownloadMirrorFile(): '" << fetch << "'"
+           << " to " << MirrorFile << endl;
+
    pkgAcquire Fetcher;
    new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
    bool res = (Fetcher.Run() == pkgAcquire::Continue);
    if(res)
       DownloadedMirrorFile = true;
    Fetcher.Shutdown();
+
+   if(Debug)
+      clog << "MirrorMethod::DownloadMirrorFile() success: " << res << endl;
+   
    return res;
 }
 
+// Randomizes the lines in the mirror file, this is used so that
+// we spread the load on the mirrors evenly
+bool MirrorMethod::RandomizeMirrorFile(string mirror_file)
+{
+   vector<string> content;
+   string line;
+
+   // read 
+   ifstream in(mirror_file.c_str());
+   while ( !in.eof() ) {
+      getline(in, line);
+      content.push_back(line);
+   }
+   
+   // we want the file to be random for each different machine, but also
+   // "stable" on the same machine. this is to avoid running into out-of-sync
+   // issues (i.e. Release/Release.gpg different on each mirror)
+   struct utsname buf;
+   int seed=1, i;
+   if(uname(&buf) == 0) {
+      for(i=0,seed=1; buf.nodename[i] != 0; i++) {
+         seed = seed * 31 + buf.nodename[i];
+      }
+   }
+   srand( seed );
+   random_shuffle(content.begin(), content.end());
+
+   // write
+   ofstream out(mirror_file.c_str());
+   while ( !content.empty()) {
+      line = content.back();
+      content.pop_back();
+      out << line << "\n";
+   }
+
+   return true;
+}
+
 /* convert a the Queue->Uri back to the mirror base uri and look
  * at all mirrors we have for this, this is needed as queue->uri
  * may point to different mirrors (if TryNextMirror() was run)
@@ -176,11 +223,15 @@ bool MirrorMethod::TryNextMirror()
         continue;
 
       vector<string>::const_iterator nextmirror = mirror + 1;
-      if (nextmirror != AllMirrors.end())
+      if (nextmirror == AllMirrors.end())
         break;
       Queue->Uri.replace(0, mirror->length(), *nextmirror);
       if (Debug)
         clog << "TryNextMirror: " << Queue->Uri << endl;
+
+      // inform parent
+      UsedMirror = *nextmirror;
+      Log("Switching mirror");
       return true;
    }
 
@@ -304,6 +355,7 @@ bool MirrorMethod::Fetch(FetchItem *Itm)
    {
       Clean(_config->FindDir("Dir::State::mirrors"));
       DownloadMirrorFile(Itm->Uri);
+      RandomizeMirrorFile(MirrorFile);
    }
 
    if(AllMirrors.empty()) {