| 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 | #include <apt-pkg/sourcelist.h> |
| 18 | |
| 19 | #include <fstream> |
| 20 | #include <iostream> |
| 21 | #include <stdarg.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <dirent.h> |
| 25 | |
| 26 | using namespace std; |
| 27 | |
| 28 | #include "mirror.h" |
| 29 | #include "http.h" |
| 30 | #include "apti18n.h" |
| 31 | /*}}}*/ |
| 32 | |
| 33 | /* Done: |
| 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 | * - uses pkgAcqMethod::FailReason() to have a string representation |
| 39 | * of the failure that is also send to LP |
| 40 | * |
| 41 | * TODO: |
| 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 | * and better error handling there! |
| 46 | * - support more than http |
| 47 | * - testing :) |
| 48 | */ |
| 49 | |
| 50 | MirrorMethod::MirrorMethod() |
| 51 | : HttpMethod(), DownloadedMirrorFile(false) |
| 52 | { |
| 53 | }; |
| 54 | |
| 55 | // HttpMethod::Configuration - Handle a configuration message /*{{{*/ |
| 56 | // --------------------------------------------------------------------- |
| 57 | /* We stash the desired pipeline depth */ |
| 58 | bool MirrorMethod::Configuration(string Message) |
| 59 | { |
| 60 | if (pkgAcqMethod::Configuration(Message) == false) |
| 61 | return false; |
| 62 | Debug = _config->FindB("Debug::Acquire::mirror",false); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | /*}}}*/ |
| 67 | |
| 68 | // clean the mirrors dir based on ttl information |
| 69 | bool MirrorMethod::Clean(string Dir) |
| 70 | { |
| 71 | vector<metaIndex *>::const_iterator I; |
| 72 | |
| 73 | if(Debug) |
| 74 | clog << "MirrorMethod::Clean(): " << Dir << endl; |
| 75 | |
| 76 | if(Dir == "/") |
| 77 | return _error->Error("will not clean: '/'"); |
| 78 | |
| 79 | // read sources.list |
| 80 | pkgSourceList list; |
| 81 | list.ReadMainList(); |
| 82 | |
| 83 | DIR *D = opendir(Dir.c_str()); |
| 84 | if (D == 0) |
| 85 | return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); |
| 86 | |
| 87 | string StartDir = SafeGetCWD(); |
| 88 | if (chdir(Dir.c_str()) != 0) |
| 89 | { |
| 90 | closedir(D); |
| 91 | return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str()); |
| 92 | } |
| 93 | |
| 94 | for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D)) |
| 95 | { |
| 96 | // Skip some files.. |
| 97 | if (strcmp(Dir->d_name,"lock") == 0 || |
| 98 | strcmp(Dir->d_name,"partial") == 0 || |
| 99 | strcmp(Dir->d_name,".") == 0 || |
| 100 | strcmp(Dir->d_name,"..") == 0) |
| 101 | continue; |
| 102 | |
| 103 | // see if we have that uri |
| 104 | for(I=list.begin(); I != list.end(); I++) |
| 105 | { |
| 106 | string uri = (*I)->GetURI(); |
| 107 | if(uri.substr(0,strlen("mirror://")) != string("mirror://")) |
| 108 | continue; |
| 109 | string BaseUri = uri.substr(0,uri.size()-1); |
| 110 | if (URItoFileName(BaseUri) == Dir->d_name) |
| 111 | break; |
| 112 | } |
| 113 | // nothing found, nuke it |
| 114 | if (I == list.end()) |
| 115 | unlink(Dir->d_name); |
| 116 | }; |
| 117 | |
| 118 | chdir(StartDir.c_str()); |
| 119 | closedir(D); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str) |
| 125 | { |
| 126 | if(Debug) |
| 127 | clog << "MirrorMethod::DownloadMirrorFile(): " << endl; |
| 128 | |
| 129 | // check the file, if it is not older than RefreshInterval just use it |
| 130 | // otherwise try to get a new one |
| 131 | if(FileExists(MirrorFile)) |
| 132 | { |
| 133 | struct stat buf; |
| 134 | time_t t,now,refresh; |
| 135 | if(stat(MirrorFile.c_str(), &buf) != 0) |
| 136 | return false; |
| 137 | t = std::max(buf.st_mtime, buf.st_ctime); |
| 138 | now = time(NULL); |
| 139 | refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360); |
| 140 | if(t + refresh > now) |
| 141 | { |
| 142 | if(Debug) |
| 143 | clog << "Mirror file is in RefreshInterval" << endl; |
| 144 | DownloadedMirrorFile = true; |
| 145 | return true; |
| 146 | } |
| 147 | if(Debug) |
| 148 | clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl; |
| 149 | } |
| 150 | |
| 151 | // not that great to use pkgAcquire here, but we do not have |
| 152 | // any other way right now |
| 153 | string fetch = BaseUri; |
| 154 | fetch.replace(0,strlen("mirror://"),"http://"); |
| 155 | |
| 156 | pkgAcquire Fetcher; |
| 157 | new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile); |
| 158 | bool res = (Fetcher.Run() == pkgAcquire::Continue); |
| 159 | if(res) |
| 160 | DownloadedMirrorFile = true; |
| 161 | Fetcher.Shutdown(); |
| 162 | return res; |
| 163 | } |
| 164 | |
| 165 | bool MirrorMethod::SelectNextMirror() |
| 166 | { |
| 167 | if (AllMirrors.size() < 1) |
| 168 | return false; |
| 169 | |
| 170 | Mirror = AllMirrors[0]; |
| 171 | AllMirrors.erase(AllMirrors.begin()); |
| 172 | if(Debug) |
| 173 | cerr << "using mirror: " << Mirror << endl; |
| 174 | |
| 175 | UsedMirror = Mirror; |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | bool MirrorMethod::InitMirrors() |
| 180 | { |
| 181 | // if we do not have a MirrorFile, fallback |
| 182 | if(!FileExists(MirrorFile)) |
| 183 | { |
| 184 | // FIXME: fallback to a default mirror here instead |
| 185 | // and provide a config option to define that default |
| 186 | return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str()); |
| 187 | } |
| 188 | |
| 189 | // FIXME: make the mirror selection more clever, do not |
| 190 | // just use the first one! |
| 191 | // BUT: we can not make this random, the mirror has to be |
| 192 | // stable accross session, because otherwise we can |
| 193 | // get into sync issues (got indexfiles from mirror A, |
| 194 | // but packages from mirror B - one might be out of date etc) |
| 195 | ifstream in(MirrorFile.c_str()); |
| 196 | string s; |
| 197 | while (!in.eof()) |
| 198 | { |
| 199 | getline(in, s); |
| 200 | AllMirrors.push_back(s); |
| 201 | } |
| 202 | SelectNextMirror(); |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | string MirrorMethod::GetMirrorFileName(string mirror_uri_str) |
| 207 | { |
| 208 | /* |
| 209 | - a mirror_uri_str looks like this: |
| 210 | mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg |
| 211 | |
| 212 | - the matching source.list entry |
| 213 | deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main |
| 214 | |
| 215 | - we actually want to go after: |
| 216 | http://people.ubuntu.com/~mvo/apt/mirror/mirrors |
| 217 | |
| 218 | And we need to save the BaseUri for later: |
| 219 | - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors |
| 220 | |
| 221 | FIXME: what if we have two similar prefixes? |
| 222 | mirror://people.ubuntu.com/~mvo/mirror |
| 223 | mirror://people.ubuntu.com/~mvo/mirror2 |
| 224 | then mirror_uri_str looks like: |
| 225 | mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg |
| 226 | mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg |
| 227 | we search sources.list and find: |
| 228 | mirror://people.ubuntu.com/~mvo/apt/mirror |
| 229 | in both cases! So we need to apply some domain knowledge here :( and |
| 230 | check for /dists/ or /Release.gpg as suffixes |
| 231 | */ |
| 232 | string name; |
| 233 | if(Debug) |
| 234 | std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl; |
| 235 | |
| 236 | // read sources.list and find match |
| 237 | vector<metaIndex *>::const_iterator I; |
| 238 | pkgSourceList list; |
| 239 | list.ReadMainList(); |
| 240 | for(I=list.begin(); I != list.end(); I++) |
| 241 | { |
| 242 | string uristr = (*I)->GetURI(); |
| 243 | if(Debug) |
| 244 | std::cerr << "Checking: " << uristr << std::endl; |
| 245 | if(uristr.substr(0,strlen("mirror://")) != string("mirror://")) |
| 246 | continue; |
| 247 | // find matching uri in sources.list |
| 248 | if(mirror_uri_str.substr(0,uristr.size()) == uristr) |
| 249 | { |
| 250 | if(Debug) |
| 251 | std::cerr << "found BaseURI: " << uristr << std::endl; |
| 252 | BaseUri = uristr.substr(0,uristr.size()-1); |
| 253 | } |
| 254 | } |
| 255 | // get new file |
| 256 | name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri); |
| 257 | |
| 258 | if(Debug) |
| 259 | { |
| 260 | cerr << "base-uri: " << BaseUri << endl; |
| 261 | cerr << "mirror-file: " << name << endl; |
| 262 | } |
| 263 | return name; |
| 264 | } |
| 265 | |
| 266 | // MirrorMethod::Fetch - Fetch an item /*{{{*/ |
| 267 | // --------------------------------------------------------------------- |
| 268 | /* This adds an item to the pipeline. We keep the pipeline at a fixed |
| 269 | depth. */ |
| 270 | bool MirrorMethod::Fetch(FetchItem *Itm) |
| 271 | { |
| 272 | if(Debug) |
| 273 | clog << "MirrorMethod::Fetch()" << endl; |
| 274 | |
| 275 | // the http method uses Fetch(0) as a way to update the pipeline, |
| 276 | // just let it do its work in this case - Fetch() with a valid |
| 277 | // Itm will always run before the first Fetch(0) |
| 278 | if(Itm == NULL) |
| 279 | return HttpMethod::Fetch(Itm); |
| 280 | |
| 281 | // if we don't have the name of the mirror file on disk yet, |
| 282 | // calculate it now (can be derived from the uri) |
| 283 | if(MirrorFile.empty()) |
| 284 | MirrorFile = GetMirrorFileName(Itm->Uri); |
| 285 | |
| 286 | // download mirror file once (if we are after index files) |
| 287 | if(Itm->IndexFile && !DownloadedMirrorFile) |
| 288 | { |
| 289 | Clean(_config->FindDir("Dir::State::mirrors")); |
| 290 | DownloadMirrorFile(Itm->Uri); |
| 291 | } |
| 292 | |
| 293 | if(Mirror.empty()) { |
| 294 | if(!InitMirrors()) { |
| 295 | // no valid mirror selected, something went wrong downloading |
| 296 | // from the master mirror site most likely and there is |
| 297 | // no old mirror file availalbe |
| 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | if(Debug) |
| 302 | clog << "selected mirror: " << Mirror << endl; |
| 303 | |
| 304 | |
| 305 | for (FetchItem *I = Queue; I != 0; I = I->Next) |
| 306 | { |
| 307 | if(I->Uri.find("mirror://") != string::npos) |
| 308 | I->Uri.replace(0,BaseUri.size(), Mirror); |
| 309 | } |
| 310 | |
| 311 | // now run the real fetcher |
| 312 | return HttpMethod::Fetch(Itm); |
| 313 | }; |
| 314 | |
| 315 | void MirrorMethod::Fail(string Err,bool Transient) |
| 316 | { |
| 317 | if(Queue->Uri.find("http://") != string::npos) |
| 318 | Queue->Uri.replace(0,Mirror.size(), BaseUri); |
| 319 | pkgAcqMethod::Fail(Err, Transient); |
| 320 | } |
| 321 | |
| 322 | void MirrorMethod::URIStart(FetchResult &Res) |
| 323 | { |
| 324 | if(Queue->Uri.find("http://") != string::npos) |
| 325 | Queue->Uri.replace(0,Mirror.size(), BaseUri); |
| 326 | pkgAcqMethod::URIStart(Res); |
| 327 | } |
| 328 | |
| 329 | void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt) |
| 330 | { |
| 331 | if(Queue->Uri.find("http://") != string::npos) |
| 332 | Queue->Uri.replace(0,Mirror.size(), BaseUri); |
| 333 | pkgAcqMethod::URIDone(Res, Alt); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | int main() |
| 338 | { |
| 339 | setlocale(LC_ALL, ""); |
| 340 | |
| 341 | MirrorMethod Mth; |
| 342 | |
| 343 | return Mth.Loop(); |
| 344 | } |
| 345 | |
| 346 | |