]>
git.saurik.com Git - apt.git/blob - methods/mirror.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mirror.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
6 Mirror Aquire Method - This is the Mirror aquire method for APT.
8 ##################################################################### */
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 #include <apt-pkg/sourcelist.h>
23 #include <sys/types.h>
34 * - works with http only
35 * - always picks the first mirror from the list
36 * - call out to problem reporting script
37 * - supports "deb mirror://host/path/to/mirror-list/// dist component"
40 * what about gpgv failures? this should call-out to the problem reporting
41 script, but we need to know what mirror was used -> just run pkgAcquire::Item::ReportMirrorFailure()
42 * better standard format for errors to send back
43 * - implement failure reporting at the pkgAcquire::Item::Failed() level
44 but then we need to send back what uri exactly was failing
45 [mvo: the problem with this approach is ::Failed() is not really
46 called for all failures :/ e.g. md5sum mismatch in a archive
48 * - deal with runing as non-root because we can't write to the lists
49 dir then -> use the cached mirror file
50 * - better method to download than having a pkgAcquire interface here
51 * - magicmarker is evil, maybe just use a similar approach as in
52 clean and read the sources.list and use the GetURI() method to find
54 * support more than http
58 MirrorMethod::MirrorMethod()
59 : HttpMethod(), HasMirrorFile(false)
63 BaseUri
="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
64 MirrorFile
="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
65 Mirror
="http://de.archive.ubuntu.com/ubuntu/";
69 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
70 // ---------------------------------------------------------------------
71 /* We stash the desired pipeline depth */
72 bool MirrorMethod::Configuration(string Message
)
74 if (pkgAcqMethod::Configuration(Message
) == false)
76 Debug
= _config
->FindB("Debug::Acquire::mirror",false);
82 // clean the mirrors dir based on ttl information
83 bool MirrorMethod::Clean(string Dir
)
85 vector
<metaIndex
*>::const_iterator I
;
88 clog
<< "MirrorMethod::Clean(): " << Dir
<< endl
;
94 DIR *D
= opendir(Dir
.c_str());
96 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
98 string StartDir
= SafeGetCWD();
99 if (chdir(Dir
.c_str()) != 0)
102 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
105 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
108 if (strcmp(Dir
->d_name
,"lock") == 0 ||
109 strcmp(Dir
->d_name
,"partial") == 0 ||
110 strcmp(Dir
->d_name
,".") == 0 ||
111 strcmp(Dir
->d_name
,"..") == 0)
114 // see if we have that uri
115 for(I
=list
.begin(); I
!= list
.end(); I
++)
117 string uri
= (*I
)->GetURI();
118 if(uri
.substr(0,strlen("mirror://")) != string("mirror://"))
120 string Marker
= _config
->Find("Acquire::Mirror::MagicMarker","///");
121 string BaseUri
= uri
.substr(0,uri
.find(Marker
));
122 if (URItoFileName(BaseUri
) == Dir
->d_name
)
125 // nothing found, nuke it
130 chdir(StartDir
.c_str());
136 bool MirrorMethod::GetMirrorFile(string uri
)
138 string Marker
= _config
->Find("Acquire::Mirror::MagicMarker","///");
139 BaseUri
= uri
.substr(0,uri
.find(Marker
));
141 string fetch
= BaseUri
;
142 fetch
.replace(0,strlen("mirror://"),"http://");
145 MirrorFile
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
149 cerr
<< "base-uri: " << BaseUri
<< endl
;
150 cerr
<< "mirror-file: " << MirrorFile
<< endl
;
153 // check the file, if it is not older than RefreshInterval just use it
154 // otherwise try to get a new one
155 if(FileExists(MirrorFile
))
158 time_t t
,now
,refresh
;
159 if(stat(MirrorFile
.c_str(), &buf
) != 0)
161 t
= std::max(buf
.st_mtime
, buf
.st_ctime
);
163 refresh
= 60*_config
->FindI("Acquire::Mirror::RefreshInterval",360);
164 if(t
+ refresh
> now
)
167 clog
<< "Mirror file is in RefreshInterval" << endl
;
168 HasMirrorFile
= true;
172 clog
<< "Mirror file " << MirrorFile
<< " older than " << refresh
<< "min, re-download it" << endl
;
175 // not that great to use pkgAcquire here, but we do not have
176 // any other way right now
178 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
179 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
181 HasMirrorFile
= true;
186 bool MirrorMethod::SelectMirror()
188 // FIXME: make the mirror selection more clever, do not
189 // just use the first one!
190 ifstream
in(MirrorFile
.c_str());
193 cerr
<< "Using mirror: " << Mirror
<< endl
;
199 // MirrorMethod::Fetch - Fetch an item /*{{{*/
200 // ---------------------------------------------------------------------
201 /* This adds an item to the pipeline. We keep the pipeline at a fixed
203 bool MirrorMethod::Fetch(FetchItem
*Itm
)
205 // select mirror only once per session
208 Clean(_config
->FindDir("Dir::State::mirrors"));
209 GetMirrorFile(Itm
->Uri
);
213 for (FetchItem
*I
= Queue
; I
!= 0; I
= I
->Next
)
215 if(I
->Uri
.find("mirror://") != string::npos
)
216 I
->Uri
.replace(0,BaseUri
.size(),Mirror
);
219 // now run the real fetcher
220 return HttpMethod::Fetch(Itm
);
223 void MirrorMethod::Fail(string Err
,bool Transient
)
225 if(Queue
->Uri
.find("http://") != string::npos
)
226 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
227 pkgAcqMethod::Fail(Err
, Transient
);
230 void MirrorMethod::URIStart(FetchResult
&Res
)
232 if(Queue
->Uri
.find("http://") != string::npos
)
233 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
234 pkgAcqMethod::URIStart(Res
);
237 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
239 if(Queue
->Uri
.find("http://") != string::npos
)
240 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
241 pkgAcqMethod::URIDone(Res
, Alt
);
247 setlocale(LC_ALL
, "");