]> git.saurik.com Git - apt.git/blob - methods/mirror.cc
* methods/mirror.cc:
[apt.git] / methods / mirror.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: mirror.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
5
6 Mirror Aquire Method - This is the Mirror aquire method for APT.
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #include <apt-pkg/fileutl.h>
12 #include <apt-pkg/acquire-method.h>
13 #include <apt-pkg/acquire-item.h>
14 #include <apt-pkg/acquire.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/hashes.h>
17
18 #include <fstream>
19 #include <iostream>
20 #include <stdarg.h>
21
22 using namespace std;
23
24 #include "mirror.h"
25 #include "http.h"
26
27 /*}}}*/
28
29 /*
30 * TODO:
31 * - better method to download than having a pkgAcquire interface here
32 * - support keeping the mirror file around (evil listclearer strikes again)
33 * -> /var/lib/apt/mirrors dir? how to cleanup? by time?
34 * - provide some TTL time until the mirror file is get again (1h? 6h?)
35 * - deal with runing as non-root (we can't write to the lists dir then)
36 * - testing :)
37 */
38
39 MirrorMethod::MirrorMethod()
40 : HttpMethod(), HasMirrorFile(false)
41 {
42 #if 0
43 HasMirrorFile=true;
44 BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
45 MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
46 Mirror="http://de.archive.ubuntu.com/ubuntu/";
47 #endif
48 };
49
50 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
51 // ---------------------------------------------------------------------
52 /* We stash the desired pipeline depth */
53 bool MirrorMethod::Configuration(string Message)
54 {
55 if (pkgAcqMethod::Configuration(Message) == false)
56 return false;
57 Debug = _config->FindB("Debug::Acquire::mirror",false);
58
59 return true;
60 }
61 /*}}}*/
62
63
64 bool MirrorMethod::GetMirrorFile(string uri)
65 {
66 string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
67 BaseUri = uri.substr(0,uri.find(Marker));
68
69 string fetch = BaseUri;
70 fetch.replace(0,strlen("mirror://"),"http://");
71
72 MirrorFile = _config->FindDir("Dir::State::lists") + URItoFileName(BaseUri);
73
74 if(Debug)
75 {
76 cerr << "base-uri: " << BaseUri << endl;
77 cerr << "mirror-file: " << MirrorFile << endl;
78 }
79
80 // FIXME: fetch it with curl
81 pkgAcquire Fetcher;
82 new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
83 bool res = (Fetcher.Run() == pkgAcquire::Continue);
84
85 if(res)
86 HasMirrorFile = true;
87 Fetcher.Shutdown();
88 return true;
89 }
90
91 bool MirrorMethod::SelectMirror()
92 {
93 ifstream in(MirrorFile.c_str());
94 getline(in, Mirror);
95 if(Debug)
96 cerr << "Using mirror: " << Mirror << endl;
97 return true;
98 }
99
100 // MirrorMethod::Fetch - Fetch an item /*{{{*/
101 // ---------------------------------------------------------------------
102 /* This adds an item to the pipeline. We keep the pipeline at a fixed
103 depth. */
104 bool MirrorMethod::Fetch(FetchItem *Itm)
105 {
106 // get mirror information
107 if(!HasMirrorFile)
108 {
109 GetMirrorFile(Itm->Uri);
110 SelectMirror();
111 }
112
113 for (FetchItem *I = Queue; I != 0; I = I->Next)
114 {
115 if(I->Uri.find("mirror://") != string::npos)
116 I->Uri.replace(0,BaseUri.size(),Mirror);
117 }
118
119 // now run the real fetcher
120 return HttpMethod::Fetch(Itm);
121 };
122
123 void MirrorMethod::Fail(string Err,bool Transient)
124 {
125 if(Queue->Uri.find("http://") != string::npos)
126 Queue->Uri.replace(0,Mirror.size(), BaseUri);
127 pkgAcqMethod::Fail(Err, Transient);
128 }
129
130 void MirrorMethod::URIStart(FetchResult &Res)
131 {
132 if(Queue->Uri.find("http://") != string::npos)
133 Queue->Uri.replace(0,Mirror.size(), BaseUri);
134 pkgAcqMethod::URIStart(Res);
135 }
136
137 void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
138 {
139 if(Queue->Uri.find("http://") != string::npos)
140 Queue->Uri.replace(0,Mirror.size(), BaseUri);
141 pkgAcqMethod::URIDone(Res, Alt);
142 }
143
144
145 int main()
146 {
147 setlocale(LC_ALL, "");
148
149 MirrorMethod Mth;
150
151 return Mth.Loop();
152 }
153
154