]> git.saurik.com Git - apt.git/blobdiff - methods/mirror.cc
* cmdline/apt-get.cc:
[apt.git] / methods / mirror.cc
index 425a2f7f57d919b05ea0b18ba879b45a7cf6e620..9fe64fd7031c0a05c932ef44b7f12922fd20721f 100644 (file)
@@ -14,6 +14,7 @@
 #include <apt-pkg/acquire.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/hashes.h>
+#include <apt-pkg/sourcelist.h>
 
 #include <fstream>
 #include <iostream>
@@ -29,30 +30,26 @@ using namespace std;
 #include "apti18n.h"
                                                                        /*}}}*/
 
-/* 
+/* Done:
+ * - works with http (only!)
+ * - always picks the first mirror from the list
+ * - call out to problem reporting script
+ * - supports "deb mirror://host/path/to/mirror-list/// dist component"
+ * - uses pkgAcqMethod::FailReason() to have a string representation
+ *   of the failure that is also send to LP
+ * 
  * TODO: 
- * - send expected checksum to the mirror method so that 
-     some checking/falling back can be done here already
- * - keep the mirror file around in /var/lib/apt/mirrors
-     * can't be put into lists/ because of the listclearer
-     * cleanup by time (mtime relative to the other mtimes)
- * - use a TTL time the mirror file is fetched again (6h?)
  * - deal with runing as non-root because we can't write to the lists 
      dir then -> use the cached mirror file
  * - better method to download than having a pkgAcquire interface here
- * - magicmarker is (a bit) evil
+ *   and better error handling there!
+ * - support more than http
  * - testing :)
  */
 
 MirrorMethod::MirrorMethod()
    : HttpMethod(), HasMirrorFile(false)
 {
-#if 0
-   HasMirrorFile=true;
-   BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
-   MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
-   Mirror="http://de.archive.ubuntu.com/ubuntu/";
-#endif
 };
 
 // HttpMethod::Configuration - Handle a configuration message          /*{{{*/
@@ -71,9 +68,14 @@ bool MirrorMethod::Configuration(string Message)
 // clean the mirrors dir based on ttl information
 bool MirrorMethod::Clean(string Dir)
 {
-   // FIXME: it would better to have a global idea of the mirrors
-   //        in the sources.list and use this instead of this time
-   //        based approach. currently apt does not support this :/
+   vector<metaIndex *>::const_iterator I;
+
+   if(Debug)
+      clog << "MirrorMethod::Clean(): " << Dir << endl;
+
+   // read sources.list
+   pkgSourceList list;
+   list.ReadMainList();
 
    DIR *D = opendir(Dir.c_str());   
    if (D == 0)
@@ -94,24 +96,20 @@ bool MirrorMethod::Clean(string Dir)
          strcmp(Dir->d_name,".") == 0 ||
          strcmp(Dir->d_name,"..") == 0)
         continue;
-      
-      // Del everything not touched for MaxAge days
-      time_t t,now,max;
-      struct stat buf;
-      if(stat(Dir->d_name, &buf) != 0) 
+
+      // see if we have that uri
+      for(I=list.begin(); I != list.end(); I++)
       {
-        cerr << "Can't stat '" << Dir->d_name << "'" << endl;
-        continue;
+        string uri = (*I)->GetURI();
+        if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
+           continue;
+        string BaseUri = uri.substr(0,uri.size()-1);
+        if (URItoFileName(BaseUri) == Dir->d_name)
+           break;
       }
-      t = std::max(buf.st_mtime, buf.st_ctime);
-      now = time(NULL);
-      max = 24*60*60*_config->FindI("Acquire::Mirror::MaxAge",90);
-      if(t + max < now)
-      {
-        if(Debug)
-           clog << "Mirror file is older than MaxAge days, deleting" << endl;
+      // nothing found, nuke it
+      if (I == list.end())
         unlink(Dir->d_name);
-      }
    };
    
    chdir(StartDir.c_str());
@@ -120,11 +118,54 @@ bool MirrorMethod::Clean(string Dir)
 }
 
 
-bool MirrorMethod::GetMirrorFile(string uri)
+bool MirrorMethod::GetMirrorFile(string mirror_uri_str)
 {
-   string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
-   BaseUri = uri.substr(0,uri.find(Marker));
+   /* 
+    - a mirror_uri_str looks like this:
+    mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
+   
+    - the matching source.list entry
+    deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
+   
+    - we actually want to go after:
+    http://people.ubuntu.com/~mvo/apt/mirror/mirrors
+
+    And we need to save the BaseUri for later:
+    - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
+
+   FIXME: what if we have two similar prefixes?
+     mirror://people.ubuntu.com/~mvo/mirror
+     mirror://people.ubuntu.com/~mvo/mirror2
+   then mirror_uri_str looks like:
+     mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
+     mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
+   we search sources.list and find:
+     mirror://people.ubuntu.com/~mvo/apt/mirror
+   in both cases! So we need to apply some domain knowledge here :( and
+   check for /dists/ or /Release.gpg as suffixes
+   */
+   if(Debug)
+      std::cerr << "GetMirrorFile: " << mirror_uri_str << std::endl;
 
+   // read sources.list and find match
+   vector<metaIndex *>::const_iterator I;
+   pkgSourceList list;
+   list.ReadMainList();
+   for(I=list.begin(); I != list.end(); I++)
+   {
+      string uristr = (*I)->GetURI();
+      if(Debug)
+        std::cerr << "Checking: " << uristr << std::endl;
+      if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
+        continue;
+      // find matching uri in sources.list
+      if(mirror_uri_str.substr(0,uristr.size()) == uristr)
+      {
+        if(Debug)
+           std::cerr << "found BaseURI: " << uristr << std::endl;
+        BaseUri = uristr.substr(0,uristr.size()-1);
+      }
+   }
    string fetch = BaseUri;
    fetch.replace(0,strlen("mirror://"),"http://");
 
@@ -178,6 +219,8 @@ bool MirrorMethod::SelectMirror()
    getline(in, Mirror);
    if(Debug)
       cerr << "Using mirror: " << Mirror << endl;
+
+   UsedMirror = Mirror;
    return true;
 }