]>
git.saurik.com Git - apt.git/blob - methods/mirror.cc
b64879bec19429929a403b5c98652c7da573d1fd
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"
38 * - use pkgAcqMethod::FailReason() to have a string representation
39 * of the failure that is also send to LP
42 * - deal with runing as non-root because we can't write to the lists
43 dir then -> use the cached mirror file
44 * - better method to download than having a pkgAcquire interface here
45 * - magicmarker is evil, maybe just use a similar approach as in
46 clean and read the sources.list and use the GetURI() method to find
48 * support more than http
52 MirrorMethod::MirrorMethod()
53 : HttpMethod(), HasMirrorFile(false)
57 BaseUri
="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
58 MirrorFile
="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
59 Mirror
="http://de.archive.ubuntu.com/ubuntu/";
63 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
64 // ---------------------------------------------------------------------
65 /* We stash the desired pipeline depth */
66 bool MirrorMethod::Configuration(string Message
)
68 if (pkgAcqMethod::Configuration(Message
) == false)
70 Debug
= _config
->FindB("Debug::Acquire::mirror",false);
76 // clean the mirrors dir based on ttl information
77 bool MirrorMethod::Clean(string Dir
)
79 vector
<metaIndex
*>::const_iterator I
;
82 clog
<< "MirrorMethod::Clean(): " << Dir
<< endl
;
88 DIR *D
= opendir(Dir
.c_str());
90 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
92 string StartDir
= SafeGetCWD();
93 if (chdir(Dir
.c_str()) != 0)
96 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
99 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
102 if (strcmp(Dir
->d_name
,"lock") == 0 ||
103 strcmp(Dir
->d_name
,"partial") == 0 ||
104 strcmp(Dir
->d_name
,".") == 0 ||
105 strcmp(Dir
->d_name
,"..") == 0)
108 // see if we have that uri
109 for(I
=list
.begin(); I
!= list
.end(); I
++)
111 string uri
= (*I
)->GetURI();
112 if(uri
.substr(0,strlen("mirror://")) != string("mirror://"))
114 string Marker
= _config
->Find("Acquire::Mirror::MagicMarker","///");
115 string BaseUri
= uri
.substr(0,uri
.find(Marker
));
116 if (URItoFileName(BaseUri
) == Dir
->d_name
)
119 // nothing found, nuke it
124 chdir(StartDir
.c_str());
130 bool MirrorMethod::GetMirrorFile(string uri
)
132 string Marker
= _config
->Find("Acquire::Mirror::MagicMarker","///");
133 BaseUri
= uri
.substr(0,uri
.find(Marker
));
135 string fetch
= BaseUri
;
136 fetch
.replace(0,strlen("mirror://"),"http://");
139 MirrorFile
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
143 cerr
<< "base-uri: " << BaseUri
<< endl
;
144 cerr
<< "mirror-file: " << MirrorFile
<< endl
;
147 // check the file, if it is not older than RefreshInterval just use it
148 // otherwise try to get a new one
149 if(FileExists(MirrorFile
))
152 time_t t
,now
,refresh
;
153 if(stat(MirrorFile
.c_str(), &buf
) != 0)
155 t
= std::max(buf
.st_mtime
, buf
.st_ctime
);
157 refresh
= 60*_config
->FindI("Acquire::Mirror::RefreshInterval",360);
158 if(t
+ refresh
> now
)
161 clog
<< "Mirror file is in RefreshInterval" << endl
;
162 HasMirrorFile
= true;
166 clog
<< "Mirror file " << MirrorFile
<< " older than " << refresh
<< "min, re-download it" << endl
;
169 // not that great to use pkgAcquire here, but we do not have
170 // any other way right now
172 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
173 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
175 HasMirrorFile
= true;
180 bool MirrorMethod::SelectMirror()
182 // FIXME: make the mirror selection more clever, do not
183 // just use the first one!
184 ifstream
in(MirrorFile
.c_str());
187 cerr
<< "Using mirror: " << Mirror
<< endl
;
193 // MirrorMethod::Fetch - Fetch an item /*{{{*/
194 // ---------------------------------------------------------------------
195 /* This adds an item to the pipeline. We keep the pipeline at a fixed
197 bool MirrorMethod::Fetch(FetchItem
*Itm
)
199 // select mirror only once per session
202 Clean(_config
->FindDir("Dir::State::mirrors"));
203 GetMirrorFile(Itm
->Uri
);
207 for (FetchItem
*I
= Queue
; I
!= 0; I
= I
->Next
)
209 if(I
->Uri
.find("mirror://") != string::npos
)
210 I
->Uri
.replace(0,BaseUri
.size(),Mirror
);
213 // now run the real fetcher
214 return HttpMethod::Fetch(Itm
);
217 void MirrorMethod::Fail(string Err
,bool Transient
)
219 if(Queue
->Uri
.find("http://") != string::npos
)
220 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
221 pkgAcqMethod::Fail(Err
, Transient
);
224 void MirrorMethod::URIStart(FetchResult
&Res
)
226 if(Queue
->Uri
.find("http://") != string::npos
)
227 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
228 pkgAcqMethod::URIStart(Res
);
231 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
233 if(Queue
->Uri
.find("http://") != string::npos
)
234 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
235 pkgAcqMethod::URIDone(Res
, Alt
);
241 setlocale(LC_ALL
, "");