]>
git.saurik.com Git - apt.git/blob - methods/mirror.cc
425a2f7f57d919b05ea0b18ba879b45a7cf6e620
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>
22 #include <sys/types.h>
34 * - send expected checksum to the mirror method so that
35 some checking/falling back can be done here already
36 * - keep the mirror file around in /var/lib/apt/mirrors
37 * can't be put into lists/ because of the listclearer
38 * cleanup by time (mtime relative to the other mtimes)
39 * - use a TTL time the mirror file is fetched again (6h?)
40 * - deal with runing as non-root because we can't write to the lists
41 dir then -> use the cached mirror file
42 * - better method to download than having a pkgAcquire interface here
43 * - magicmarker is (a bit) evil
47 MirrorMethod::MirrorMethod()
48 : HttpMethod(), HasMirrorFile(false)
52 BaseUri
="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
53 MirrorFile
="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
54 Mirror
="http://de.archive.ubuntu.com/ubuntu/";
58 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
59 // ---------------------------------------------------------------------
60 /* We stash the desired pipeline depth */
61 bool MirrorMethod::Configuration(string Message
)
63 if (pkgAcqMethod::Configuration(Message
) == false)
65 Debug
= _config
->FindB("Debug::Acquire::mirror",false);
71 // clean the mirrors dir based on ttl information
72 bool MirrorMethod::Clean(string Dir
)
74 // FIXME: it would better to have a global idea of the mirrors
75 // in the sources.list and use this instead of this time
76 // based approach. currently apt does not support this :/
78 DIR *D
= opendir(Dir
.c_str());
80 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
82 string StartDir
= SafeGetCWD();
83 if (chdir(Dir
.c_str()) != 0)
86 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
89 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
92 if (strcmp(Dir
->d_name
,"lock") == 0 ||
93 strcmp(Dir
->d_name
,"partial") == 0 ||
94 strcmp(Dir
->d_name
,".") == 0 ||
95 strcmp(Dir
->d_name
,"..") == 0)
98 // Del everything not touched for MaxAge days
101 if(stat(Dir
->d_name
, &buf
) != 0)
103 cerr
<< "Can't stat '" << Dir
->d_name
<< "'" << endl
;
106 t
= std::max(buf
.st_mtime
, buf
.st_ctime
);
108 max
= 24*60*60*_config
->FindI("Acquire::Mirror::MaxAge",90);
112 clog
<< "Mirror file is older than MaxAge days, deleting" << endl
;
117 chdir(StartDir
.c_str());
123 bool MirrorMethod::GetMirrorFile(string uri
)
125 string Marker
= _config
->Find("Acquire::Mirror::MagicMarker","///");
126 BaseUri
= uri
.substr(0,uri
.find(Marker
));
128 string fetch
= BaseUri
;
129 fetch
.replace(0,strlen("mirror://"),"http://");
132 MirrorFile
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
136 cerr
<< "base-uri: " << BaseUri
<< endl
;
137 cerr
<< "mirror-file: " << MirrorFile
<< endl
;
140 // check the file, if it is not older than RefreshInterval just use it
141 // otherwise try to get a new one
142 if(FileExists(MirrorFile
))
145 time_t t
,now
,refresh
;
146 if(stat(MirrorFile
.c_str(), &buf
) != 0)
148 t
= std::max(buf
.st_mtime
, buf
.st_ctime
);
150 refresh
= 60*_config
->FindI("Acquire::Mirror::RefreshInterval",360);
151 if(t
+ refresh
> now
)
154 clog
<< "Mirror file is in RefreshInterval" << endl
;
155 HasMirrorFile
= true;
159 clog
<< "Mirror file " << MirrorFile
<< " older than " << refresh
<< "min, re-download it" << endl
;
162 // not that great to use pkgAcquire here, but we do not have
163 // any other way right now
165 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
166 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
168 HasMirrorFile
= true;
173 bool MirrorMethod::SelectMirror()
175 // FIXME: make the mirror selection more clever, do not
176 // just use the first one!
177 ifstream
in(MirrorFile
.c_str());
180 cerr
<< "Using mirror: " << Mirror
<< endl
;
184 // MirrorMethod::Fetch - Fetch an item /*{{{*/
185 // ---------------------------------------------------------------------
186 /* This adds an item to the pipeline. We keep the pipeline at a fixed
188 bool MirrorMethod::Fetch(FetchItem
*Itm
)
190 // select mirror only once per session
193 Clean(_config
->FindDir("Dir::State::mirrors"));
194 GetMirrorFile(Itm
->Uri
);
198 for (FetchItem
*I
= Queue
; I
!= 0; I
= I
->Next
)
200 if(I
->Uri
.find("mirror://") != string::npos
)
201 I
->Uri
.replace(0,BaseUri
.size(),Mirror
);
204 // now run the real fetcher
205 return HttpMethod::Fetch(Itm
);
208 void MirrorMethod::Fail(string Err
,bool Transient
)
210 if(Queue
->Uri
.find("http://") != string::npos
)
211 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
212 pkgAcqMethod::Fail(Err
, Transient
);
215 void MirrorMethod::URIStart(FetchResult
&Res
)
217 if(Queue
->Uri
.find("http://") != string::npos
)
218 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
219 pkgAcqMethod::URIStart(Res
);
222 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
224 if(Queue
->Uri
.find("http://") != string::npos
)
225 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
226 pkgAcqMethod::URIDone(Res
, Alt
);
232 setlocale(LC_ALL
, "");