]>
git.saurik.com Git - apt.git/blob - methods/mirror.cc
00f7b780714535c172b2411686c14c9d8a489327
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>
35 * - send expected checksum to the mirror method so that
36 some checking/falling back can be done here already
37 * - keep the mirror file around in /var/lib/apt/mirrors
38 * can't be put into lists/ because of the listclearer
39 * cleanup by time (mtime relative to the other mtimes)
40 * - use a TTL time the mirror file is fetched again (6h?)
41 * - deal with runing as non-root because we can't write to the lists
42 dir then -> use the cached mirror file
43 * - better method to download than having a pkgAcquire interface here
44 * - magicmarker is (a bit) evil
48 MirrorMethod::MirrorMethod()
49 : HttpMethod(), HasMirrorFile(false)
53 BaseUri
="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
54 MirrorFile
="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
55 Mirror
="http://de.archive.ubuntu.com/ubuntu/";
59 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
60 // ---------------------------------------------------------------------
61 /* We stash the desired pipeline depth */
62 bool MirrorMethod::Configuration(string Message
)
64 if (pkgAcqMethod::Configuration(Message
) == false)
66 Debug
= _config
->FindB("Debug::Acquire::mirror",false);
72 // clean the mirrors dir based on ttl information
73 bool MirrorMethod::Clean(string Dir
)
75 vector
<metaIndex
*>::const_iterator I
;
78 clog
<< "MirrorMethod::Clean(): " << Dir
<< endl
;
84 DIR *D
= opendir(Dir
.c_str());
86 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
88 string StartDir
= SafeGetCWD();
89 if (chdir(Dir
.c_str()) != 0)
92 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
95 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
98 if (strcmp(Dir
->d_name
,"lock") == 0 ||
99 strcmp(Dir
->d_name
,"partial") == 0 ||
100 strcmp(Dir
->d_name
,".") == 0 ||
101 strcmp(Dir
->d_name
,"..") == 0)
104 // see if we have that uri
105 for(I
=list
.begin(); I
!= list
.end(); I
++)
107 string uri
= (*I
)->GetURI();
108 if(uri
.substr(0,strlen("mirror://")) != string("mirror://"))
110 string Marker
= _config
->Find("Acquire::Mirror::MagicMarker","///");
111 string BaseUri
= uri
.substr(0,uri
.find(Marker
));
112 if (URItoFileName(BaseUri
) == Dir
->d_name
)
115 // nothing found, nuke it
120 chdir(StartDir
.c_str());
126 bool MirrorMethod::GetMirrorFile(string uri
)
128 string Marker
= _config
->Find("Acquire::Mirror::MagicMarker","///");
129 BaseUri
= uri
.substr(0,uri
.find(Marker
));
131 string fetch
= BaseUri
;
132 fetch
.replace(0,strlen("mirror://"),"http://");
135 MirrorFile
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
139 cerr
<< "base-uri: " << BaseUri
<< endl
;
140 cerr
<< "mirror-file: " << MirrorFile
<< endl
;
143 // check the file, if it is not older than RefreshInterval just use it
144 // otherwise try to get a new one
145 if(FileExists(MirrorFile
))
148 time_t t
,now
,refresh
;
149 if(stat(MirrorFile
.c_str(), &buf
) != 0)
151 t
= std::max(buf
.st_mtime
, buf
.st_ctime
);
153 refresh
= 60*_config
->FindI("Acquire::Mirror::RefreshInterval",360);
154 if(t
+ refresh
> now
)
157 clog
<< "Mirror file is in RefreshInterval" << endl
;
158 HasMirrorFile
= true;
162 clog
<< "Mirror file " << MirrorFile
<< " older than " << refresh
<< "min, re-download it" << endl
;
165 // not that great to use pkgAcquire here, but we do not have
166 // any other way right now
168 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
169 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
171 HasMirrorFile
= true;
176 bool MirrorMethod::SelectMirror()
178 // FIXME: make the mirror selection more clever, do not
179 // just use the first one!
180 ifstream
in(MirrorFile
.c_str());
183 cerr
<< "Using mirror: " << Mirror
<< endl
;
187 // MirrorMethod::Fetch - Fetch an item /*{{{*/
188 // ---------------------------------------------------------------------
189 /* This adds an item to the pipeline. We keep the pipeline at a fixed
191 bool MirrorMethod::Fetch(FetchItem
*Itm
)
193 // select mirror only once per session
196 Clean(_config
->FindDir("Dir::State::mirrors"));
197 GetMirrorFile(Itm
->Uri
);
201 for (FetchItem
*I
= Queue
; I
!= 0; I
= I
->Next
)
203 if(I
->Uri
.find("mirror://") != string::npos
)
204 I
->Uri
.replace(0,BaseUri
.size(),Mirror
);
207 // now run the real fetcher
208 return HttpMethod::Fetch(Itm
);
211 void MirrorMethod::Fail(string Err
,bool Transient
)
213 if(Queue
->Uri
.find("http://") != string::npos
)
214 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
215 pkgAcqMethod::Fail(Err
, Transient
);
218 void MirrorMethod::URIStart(FetchResult
&Res
)
220 if(Queue
->Uri
.find("http://") != string::npos
)
221 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
222 pkgAcqMethod::URIStart(Res
);
225 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
227 if(Queue
->Uri
.find("http://") != string::npos
)
228 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
229 pkgAcqMethod::URIDone(Res
, Alt
);
235 setlocale(LC_ALL
, "");