]> git.saurik.com Git - apt.git/blame - apt-pkg/acquire-item.cc
tag of michael.vogt@ubuntu.com--2005/apt--local-install--0--base-0
[apt.git] / apt-pkg / acquire-item.cc
CommitLineData
0118833a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 3// $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
0118833a
AL
4/* ######################################################################
5
6 Acquire Item - Item to acquire
7
8 Each item can download to exactly one file at a time. This means you
9 cannot create an item that fetches two uri's to two files at the same
10 time. The pkgAcqIndex class creates a second class upon instantiation
11 to fetch the other index files because of this.
b185acc2 12
0118833a
AL
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
16#ifdef __GNUG__
17#pragma implementation "apt-pkg/acquire-item.h"
18#endif
19#include <apt-pkg/acquire-item.h>
20#include <apt-pkg/configuration.h>
b2e465d6 21#include <apt-pkg/sourcelist.h>
b3d44315 22#include <apt-pkg/vendorlist.h>
03e39e59 23#include <apt-pkg/error.h>
cdcc6d34 24#include <apt-pkg/strutl.h>
36375005 25#include <apt-pkg/fileutl.h>
b3d44315 26#include <apt-pkg/md5.h>
0a8a80e5 27
b2e465d6
AL
28#include <apti18n.h>
29
0a8a80e5
AL
30#include <sys/stat.h>
31#include <unistd.h>
c88edf1d 32#include <errno.h>
5819a761 33#include <string>
c88edf1d 34#include <stdio.h>
0118833a
AL
35 /*}}}*/
36
b3d44315 37using namespace std;
5819a761 38
0118833a
AL
39// Acquire::Item::Item - Constructor /*{{{*/
40// ---------------------------------------------------------------------
41/* */
8267fe24 42pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
6b1ff003
AL
43 PartialSize(0), Mode(0), ID(0), Complete(false),
44 Local(false), QueueCounter(0)
0118833a
AL
45{
46 Owner->Add(this);
c88edf1d 47 Status = StatIdle;
0118833a
AL
48}
49 /*}}}*/
50// Acquire::Item::~Item - Destructor /*{{{*/
51// ---------------------------------------------------------------------
52/* */
53pkgAcquire::Item::~Item()
54{
55 Owner->Remove(this);
56}
57 /*}}}*/
c88edf1d
AL
58// Acquire::Item::Failed - Item failed to download /*{{{*/
59// ---------------------------------------------------------------------
93bf083d
AL
60/* We return to an idle state if there are still other queues that could
61 fetch this object */
7d8afa39 62void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
c88edf1d 63{
93bf083d 64 Status = StatIdle;
db890fdb 65 ErrorText = LookupTag(Message,"Message");
c88edf1d 66 if (QueueCounter <= 1)
93bf083d 67 {
a72ace20 68 /* This indicates that the file is not available right now but might
7d8afa39 69 be sometime later. If we do a retry cycle then this should be
17caf1b1 70 retried [CDROMs] */
7d8afa39
AL
71 if (Cnf->LocalOnly == true &&
72 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
a72ace20
AL
73 {
74 Status = StatIdle;
681d76d0 75 Dequeue();
a72ace20
AL
76 return;
77 }
78
93bf083d 79 Status = StatError;
681d76d0 80 Dequeue();
93bf083d 81 }
c88edf1d
AL
82}
83 /*}}}*/
8267fe24
AL
84// Acquire::Item::Start - Item has begun to download /*{{{*/
85// ---------------------------------------------------------------------
17caf1b1
AL
86/* Stash status and the file size. Note that setting Complete means
87 sub-phases of the acquire process such as decompresion are operating */
727f18af 88void pkgAcquire::Item::Start(string /*Message*/,unsigned long Size)
8267fe24
AL
89{
90 Status = StatFetching;
91 if (FileSize == 0 && Complete == false)
92 FileSize = Size;
93}
94 /*}}}*/
c88edf1d
AL
95// Acquire::Item::Done - Item downloaded OK /*{{{*/
96// ---------------------------------------------------------------------
97/* */
459681d3
AL
98void pkgAcquire::Item::Done(string Message,unsigned long Size,string,
99 pkgAcquire::MethodConfig *Cnf)
c88edf1d 100{
b98f2859
AL
101 // We just downloaded something..
102 string FileName = LookupTag(Message,"Filename");
103 if (Complete == false && FileName == DestFile)
104 {
105 if (Owner->Log != 0)
106 Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
107 }
aa0e1101
AL
108
109 if (FileSize == 0)
110 FileSize= Size;
b98f2859 111
c88edf1d
AL
112 Status = StatDone;
113 ErrorText = string();
114 Owner->Dequeue(this);
115}
116 /*}}}*/
8b89e57f
AL
117// Acquire::Item::Rename - Rename a file /*{{{*/
118// ---------------------------------------------------------------------
119/* This helper function is used by alot of item methods as thier final
120 step */
121void pkgAcquire::Item::Rename(string From,string To)
122{
123 if (rename(From.c_str(),To.c_str()) != 0)
124 {
125 char S[300];
0fcd01de 126 snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
8b89e57f
AL
127 From.c_str(),To.c_str());
128 Status = StatError;
129 ErrorText = S;
7a3c2ab0 130 }
8b89e57f
AL
131}
132 /*}}}*/
0118833a
AL
133
134// AcqIndex::AcqIndex - Constructor /*{{{*/
135// ---------------------------------------------------------------------
136/* The package file is added to the queue and a second class is
b2e465d6
AL
137 instantiated to fetch the revision file */
138pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
b3d44315
MV
139 string URI,string URIDesc,string ShortDesc,
140 string ExpectedMD5, string comprExt) :
141 Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5)
0118833a 142{
8b89e57f 143 Decompression = false;
bfd22fc0 144 Erase = false;
8b89e57f 145
0a8a80e5 146 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
b2e465d6 147 DestFile += URItoFileName(URI);
8267fe24 148
b3d44315
MV
149 if(comprExt.empty())
150 {
151 // autoselect
152 if(FileExists("/usr/bin/bzip2"))
153 Desc.URI = URI + ".bz2";
154 else
155 Desc.URI = URI + ".gz";
156 } else {
157 Desc.URI = URI + comprExt;
158 }
159
b2e465d6 160 Desc.Description = URIDesc;
8267fe24 161 Desc.Owner = this;
b2e465d6 162 Desc.ShortDesc = ShortDesc;
8267fe24
AL
163
164 QueueURI(Desc);
0118833a
AL
165}
166 /*}}}*/
0a8a80e5 167// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
0118833a 168// ---------------------------------------------------------------------
0a8a80e5
AL
169/* The only header we use is the last-modified header. */
170string pkgAcqIndex::Custom600Headers()
0118833a 171{
0a8a80e5 172 string Final = _config->FindDir("Dir::State::lists");
b2e465d6 173 Final += URItoFileName(RealURI);
0a8a80e5
AL
174
175 struct stat Buf;
176 if (stat(Final.c_str(),&Buf) != 0)
a72ace20 177 return "\nIndex-File: true";
0118833a 178
a72ace20 179 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
0118833a
AL
180}
181 /*}}}*/
debc84b2
MZ
182
183void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
184{
185 // no .bz2 found, retry with .gz
186 if(Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1) == "bz2") {
187 Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
b3d44315
MV
188
189 // retry with a gzip one
190 new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
191 ExpectedMD5, string(".gz"));
192 Status = StatDone;
193 Complete = false;
194 Dequeue();
debc84b2
MZ
195 return;
196 }
197
198
199 Item::Failed(Message,Cnf);
200}
201
202
8b89e57f
AL
203// AcqIndex::Done - Finished a fetch /*{{{*/
204// ---------------------------------------------------------------------
205/* This goes through a number of states.. On the initial fetch the
206 method could possibly return an alternate filename which points
207 to the uncompressed version of the file. If this is so the file
208 is copied into the partial directory. In all other cases the file
209 is decompressed with a gzip uri. */
459681d3
AL
210void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
211 pkgAcquire::MethodConfig *Cfg)
8b89e57f 212{
459681d3 213 Item::Done(Message,Size,MD5,Cfg);
8b89e57f
AL
214
215 if (Decompression == true)
216 {
b3d44315
MV
217 if (_config->FindB("Debug::pkgAcquire::Auth", false))
218 {
219 std::cerr << std::endl << RealURI << ": Computed MD5: " << MD5;
220 std::cerr << " Expected MD5: " << ExpectedMD5 << std::endl;
221 }
222
223 if (MD5.empty())
224 {
225 MD5Summation sum;
226 FileFd Fd(DestFile, FileFd::ReadOnly);
227 sum.AddFD(Fd.Fd(), Fd.Size());
228 Fd.Close();
229 MD5 = (string)sum.Result();
230 }
231
232 if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
233 {
234 Status = StatAuthError;
235 ErrorText = _("MD5Sum mismatch");
236 Rename(DestFile,DestFile + ".FAILED");
237 return;
238 }
8b89e57f
AL
239 // Done, move it into position
240 string FinalFile = _config->FindDir("Dir::State::lists");
b2e465d6 241 FinalFile += URItoFileName(RealURI);
8b89e57f 242 Rename(DestFile,FinalFile);
7a3c2ab0 243 chmod(FinalFile.c_str(),0644);
bfd22fc0 244
7a7fa5f0
AL
245 /* We restore the original name to DestFile so that the clean operation
246 will work OK */
247 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
b2e465d6 248 DestFile += URItoFileName(RealURI);
7a7fa5f0 249
bfd22fc0
AL
250 // Remove the compressed version.
251 if (Erase == true)
bfd22fc0 252 unlink(DestFile.c_str());
8b89e57f
AL
253 return;
254 }
bfd22fc0
AL
255
256 Erase = false;
8267fe24 257 Complete = true;
bfd22fc0 258
8b89e57f
AL
259 // Handle the unzipd case
260 string FileName = LookupTag(Message,"Alt-Filename");
261 if (FileName.empty() == false)
262 {
263 // The files timestamp matches
264 if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
265 return;
b3d44315 266
8b89e57f 267 Decompression = true;
a6568219 268 Local = true;
8b89e57f 269 DestFile += ".decomp";
8267fe24
AL
270 Desc.URI = "copy:" + FileName;
271 QueueURI(Desc);
b98f2859 272 Mode = "copy";
8b89e57f
AL
273 return;
274 }
275
276 FileName = LookupTag(Message,"Filename");
277 if (FileName.empty() == true)
278 {
279 Status = StatError;
280 ErrorText = "Method gave a blank filename";
281 }
282
283 // The files timestamp matches
284 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
285 return;
bfd22fc0
AL
286
287 if (FileName == DestFile)
288 Erase = true;
8267fe24 289 else
a6568219 290 Local = true;
8b89e57f 291
debc84b2
MZ
292 string compExt = Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1);
293 char *decompProg;
294 if(compExt == "bz2")
295 decompProg = "bzip2";
296 else if(compExt == ".gz")
297 decompProg = "gzip";
298 else {
299 _error->Error("Unsupported extension: %s", compExt.c_str());
300 return;
301 }
302
8b89e57f
AL
303 Decompression = true;
304 DestFile += ".decomp";
debc84b2 305 Desc.URI = string(decompProg) + ":" + FileName;
8267fe24 306 QueueURI(Desc);
debc84b2 307 Mode = decompProg;
8b89e57f 308}
8b89e57f 309
b3d44315
MV
310pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
311 string URI,string URIDesc,string ShortDesc,
312 string MetaIndexURI, string MetaIndexURIDesc,
313 string MetaIndexShortDesc,
314 const vector<IndexTarget*>* IndexTargets,
315 indexRecords* MetaIndexParser) :
316 Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
317 MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc)
0118833a 318{
b3d44315
MV
319 this->MetaIndexParser = MetaIndexParser;
320 this->IndexTargets = IndexTargets;
0a8a80e5 321 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
b2e465d6 322 DestFile += URItoFileName(URI);
b3d44315 323
f6237efd
MV
324 // remove any partial downloaded sig-file. it may confuse proxies
325 // and is too small to warrant a partial download anyway
326 unlink(DestFile.c_str());
327
8267fe24 328 // Create the item
b2e465d6 329 Desc.Description = URIDesc;
8267fe24 330 Desc.Owner = this;
b3d44315
MV
331 Desc.ShortDesc = ShortDesc;
332 Desc.URI = URI;
333
334
335 string Final = _config->FindDir("Dir::State::lists");
336 Final += URItoFileName(RealURI);
337 struct stat Buf;
338 if (stat(Final.c_str(),&Buf) == 0)
339 {
340 // File was already in place. It needs to be re-verified
341 // because Release might have changed, so Move it into partial
342 Rename(Final,DestFile);
343 }
8267fe24 344
8267fe24 345 QueueURI(Desc);
0118833a
AL
346}
347 /*}}}*/
b3d44315 348// pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
0118833a 349// ---------------------------------------------------------------------
0a8a80e5 350/* The only header we use is the last-modified header. */
b3d44315 351string pkgAcqMetaSig::Custom600Headers()
0118833a 352{
0a8a80e5 353 struct stat Buf;
2aab5956 354 if (stat(DestFile.c_str(),&Buf) != 0)
a72ace20 355 return "\nIndex-File: true";
a789b983 356
a72ace20 357 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
0118833a 358}
b3d44315
MV
359
360void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
361 pkgAcquire::MethodConfig *Cfg)
c88edf1d 362{
459681d3 363 Item::Done(Message,Size,MD5,Cfg);
c88edf1d
AL
364
365 string FileName = LookupTag(Message,"Filename");
366 if (FileName.empty() == true)
367 {
368 Status = StatError;
369 ErrorText = "Method gave a blank filename";
8b89e57f 370 return;
c88edf1d 371 }
8b89e57f 372
c88edf1d
AL
373 if (FileName != DestFile)
374 {
b3d44315 375 // We have to copy it into place
a6568219 376 Local = true;
8267fe24
AL
377 Desc.URI = "copy:" + FileName;
378 QueueURI(Desc);
c88edf1d
AL
379 return;
380 }
b3d44315
MV
381
382 Complete = true;
383
384 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
385 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
386 DestFile, IndexTargets, MetaIndexParser);
387
c88edf1d
AL
388}
389 /*}}}*/
b3d44315 390void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
681d76d0 391{
b3d44315
MV
392 // Delete any existing sigfile, so that this source isn't
393 // mistakenly trusted
394 string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
395 unlink(Final.c_str());
a789b983 396
b3d44315
MV
397 // queue a pkgAcqMetaIndex with no sigfile
398 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
399 "", IndexTargets, MetaIndexParser);
400
681d76d0
AL
401 if (Cnf->LocalOnly == true ||
402 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
403 {
2b154e53
AL
404 // Ignore this
405 Status = StatDone;
406 Complete = false;
681d76d0
AL
407 Dequeue();
408 return;
409 }
410
411 Item::Failed(Message,Cnf);
412}
b3d44315
MV
413
414pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
415 string URI,string URIDesc,string ShortDesc,
416 string SigFile,
417 const vector<struct IndexTarget*>* IndexTargets,
418 indexRecords* MetaIndexParser) :
419 Item(Owner), RealURI(URI), SigFile(SigFile)
420{
421 this->AuthPass = false;
422 this->MetaIndexParser = MetaIndexParser;
423 this->IndexTargets = IndexTargets;
424 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
425 DestFile += URItoFileName(URI);
426
427 // Create the item
428 Desc.Description = URIDesc;
429 Desc.Owner = this;
430 Desc.ShortDesc = ShortDesc;
431 Desc.URI = URI;
432
433 QueueURI(Desc);
434}
435
436 /*}}}*/
437// pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
438// ---------------------------------------------------------------------
439/* The only header we use is the last-modified header. */
440string pkgAcqMetaIndex::Custom600Headers()
441{
442 string Final = _config->FindDir("Dir::State::lists");
443 Final += URItoFileName(RealURI);
444
445 struct stat Buf;
446 if (stat(Final.c_str(),&Buf) != 0)
447 return "\nIndex-File: true";
448
449 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
450}
451
452void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
453 pkgAcquire::MethodConfig *Cfg)
454{
455 Item::Done(Message,Size,MD5,Cfg);
456
457 // MetaIndexes are done in two passes: one to download the
458 // metaindex with an appropriate method, and a second to verify it
459 // with the gpgv method
460
461 if (AuthPass == true)
462 {
463 AuthDone(Message);
464 }
465 else
466 {
467 RetrievalDone(Message);
468 if (!Complete)
469 // Still more retrieving to do
470 return;
471
472 if (SigFile == "")
473 {
474 // There was no signature file, so we are finished. Download
475 // the indexes without verification.
476 QueueIndexes(false);
477 }
478 else
479 {
480 // There was a signature file, so pass it to gpgv for
481 // verification
482
483 if (_config->FindB("Debug::pkgAcquire::Auth", false))
484 std::cerr << "Metaindex acquired, queueing gpg verification ("
485 << SigFile << "," << DestFile << ")\n";
486 AuthPass = true;
487 Desc.URI = "gpgv:" + SigFile;
488 QueueURI(Desc);
489 Mode = "gpgv";
490 }
491 }
492}
493
494void pkgAcqMetaIndex::RetrievalDone(string Message)
495{
496 // We have just finished downloading a Release file (it is not
497 // verified yet)
498
499 string FileName = LookupTag(Message,"Filename");
500 if (FileName.empty() == true)
501 {
502 Status = StatError;
503 ErrorText = "Method gave a blank filename";
504 return;
505 }
506
507 if (FileName != DestFile)
508 {
509 Local = true;
510 Desc.URI = "copy:" + FileName;
511 QueueURI(Desc);
512 return;
513 }
514
515 Complete = true;
516
517 string FinalFile = _config->FindDir("Dir::State::lists");
518 FinalFile += URItoFileName(RealURI);
519
520 // The files timestamp matches
521 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
522 {
523 // Move it into position
524 Rename(DestFile,FinalFile);
525 }
526 DestFile = FinalFile;
527}
528
529void pkgAcqMetaIndex::AuthDone(string Message)
530{
531 // At this point, the gpgv method has succeeded, so there is a
532 // valid signature from a key in the trusted keyring. We
533 // perform additional verification of its contents, and use them
534 // to verify the indexes we are about to download
535
536 if (!MetaIndexParser->Load(DestFile))
537 {
538 Status = StatAuthError;
539 ErrorText = MetaIndexParser->ErrorText;
540 return;
541 }
542
543 if (!VerifyVendor())
544 {
545 return;
546 }
547
548 if (_config->FindB("Debug::pkgAcquire::Auth", false))
549 std::cerr << "Signature verification succeeded: "
550 << DestFile << std::endl;
551
552 // Download further indexes with verification
553 QueueIndexes(true);
554
555 // Done, move signature file into position
556
557 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
558 URItoFileName(RealURI) + ".gpg";
559 Rename(SigFile,VerifiedSigFile);
560 chmod(VerifiedSigFile.c_str(),0644);
561}
562
563void pkgAcqMetaIndex::QueueIndexes(bool verify)
564{
565 for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
566 Target != IndexTargets->end();
567 Target++)
568 {
569 string ExpectedIndexMD5;
570 if (verify)
571 {
572 const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
573 if (!Record)
574 {
575 Status = StatAuthError;
576 ErrorText = "Unable to find expected entry "
577 + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
578 return;
579 }
580 ExpectedIndexMD5 = Record->MD5Hash;
581 if (_config->FindB("Debug::pkgAcquire::Auth", false))
582 {
583 std::cerr << "Queueing: " << (*Target)->URI << std::endl;
584 std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
585 }
586 if (ExpectedIndexMD5.empty())
587 {
588 Status = StatAuthError;
589 ErrorText = "Unable to find MD5 sum for "
590 + (*Target)->MetaKey + " in Meta-index file";
591 return;
592 }
593 }
594
595 // Queue Packages file
596 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
597 (*Target)->ShortDesc, ExpectedIndexMD5);
598 }
599}
600
601bool pkgAcqMetaIndex::VerifyVendor()
602{
603// // Maybe this should be made available from above so we don't have
604// // to read and parse it every time?
605// pkgVendorList List;
606// List.ReadMainList();
607
608// const Vendor* Vndr = NULL;
609// for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
610// {
611// string::size_type pos = (*I).find("VALIDSIG ");
612// if (_config->FindB("Debug::Vendor", false))
613// std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
614// << std::endl;
615// if (pos != std::string::npos)
616// {
617// string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
618// if (_config->FindB("Debug::Vendor", false))
619// std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
620// std::endl;
621// Vndr = List.FindVendor(Fingerprint) != "";
622// if (Vndr != NULL);
623// break;
624// }
625// }
626
627 string Transformed = MetaIndexParser->GetExpectedDist();
628
629 if (Transformed == "../project/experimental")
630 {
631 Transformed = "experimental";
632 }
633
634 string::size_type pos = Transformed.rfind('/');
635 if (pos != string::npos)
636 {
637 Transformed = Transformed.substr(0, pos);
638 }
639
640 if (Transformed == ".")
641 {
642 Transformed = "";
643 }
644
645 if (_config->FindB("Debug::pkgAcquire::Auth", false))
646 {
647 std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
648 std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
649 std::cerr << "Transformed Dist: " << Transformed << std::endl;
650 }
651
652 if (MetaIndexParser->CheckDist(Transformed) == false)
653 {
654 // This might become fatal one day
655// Status = StatAuthError;
656// ErrorText = "Conflicting distribution; expected "
657// + MetaIndexParser->GetExpectedDist() + " but got "
658// + MetaIndexParser->GetDist();
659// return false;
660 if (!Transformed.empty())
661 {
662 _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
663 Desc.Description.c_str(),
664 Transformed.c_str(),
665 MetaIndexParser->GetDist().c_str());
666 }
667 }
668
669 return true;
670}
671 /*}}}*/
672// pkgAcqMetaIndex::Failed - no Release file present or no signature
673// file present /*{{{*/
674// ---------------------------------------------------------------------
675/* */
676void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
677{
678 if (AuthPass == true)
679 {
680 // gpgv method failed
681 _error->Warning("GPG error: %s: %s",
682 Desc.Description.c_str(),
683 LookupTag(Message,"Message").c_str());
684 }
685
686 // No Release file was present, or verification failed, so fall
687 // back to queueing Packages files without verification
688 QueueIndexes(false);
689}
690
681d76d0 691 /*}}}*/
03e39e59
AL
692
693// AcqArchive::AcqArchive - Constructor /*{{{*/
694// ---------------------------------------------------------------------
17caf1b1
AL
695/* This just sets up the initial fetch environment and queues the first
696 possibilitiy */
03e39e59 697pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
30e1eab5
AL
698 pkgRecords *Recs,pkgCache::VerIterator const &Version,
699 string &StoreFilename) :
700 Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
b3d44315
MV
701 StoreFilename(StoreFilename), Vf(Version.FileList()),
702 Trusted(false)
03e39e59 703{
7d8afa39 704 Retries = _config->FindI("Acquire::Retries",0);
813c8eea
AL
705
706 if (Version.Arch() == 0)
bdae53f1 707 {
d1f1f6a8 708 _error->Error(_("I wasn't able to locate a file for the %s package. "
7a3c2ab0
AL
709 "This might mean you need to manually fix this package. "
710 "(due to missing arch)"),
813c8eea 711 Version.ParentPkg().Name());
bdae53f1
AL
712 return;
713 }
813c8eea 714
b2e465d6
AL
715 /* We need to find a filename to determine the extension. We make the
716 assumption here that all the available sources for this version share
717 the same extension.. */
718 // Skip not source sources, they do not have file fields.
719 for (; Vf.end() == false; Vf++)
720 {
721 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
722 continue;
723 break;
724 }
725
726 // Does not really matter here.. we are going to fail out below
727 if (Vf.end() != true)
728 {
729 // If this fails to get a file name we will bomb out below.
730 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
731 if (_error->PendingError() == true)
732 return;
733
734 // Generate the final file name as: package_version_arch.foo
735 StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
736 QuoteString(Version.VerStr(),"_:") + '_' +
737 QuoteString(Version.Arch(),"_:.") +
738 "." + flExtension(Parse.FileName());
739 }
b3d44315
MV
740
741 // check if we have one trusted source for the package. if so, switch
742 // to "TrustedOnly" mode
743 for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
744 {
745 pkgIndexFile *Index;
746 if (Sources->FindIndex(i.File(),Index) == false)
747 continue;
748 if (_config->FindB("Debug::pkgAcquire::Auth", false))
749 {
750 std::cerr << "Checking index: " << Index->Describe()
751 << "(Trusted=" << Index->IsTrusted() << ")\n";
752 }
753 if (Index->IsTrusted()) {
754 Trusted = true;
755 break;
756 }
757 }
758
03e39e59 759 // Select a source
b185acc2 760 if (QueueNext() == false && _error->PendingError() == false)
b2e465d6
AL
761 _error->Error(_("I wasn't able to locate file for the %s package. "
762 "This might mean you need to manually fix this package."),
b185acc2
AL
763 Version.ParentPkg().Name());
764}
765 /*}}}*/
766// AcqArchive::QueueNext - Queue the next file source /*{{{*/
767// ---------------------------------------------------------------------
17caf1b1
AL
768/* This queues the next available file version for download. It checks if
769 the archive is already available in the cache and stashs the MD5 for
770 checking later. */
b185acc2 771bool pkgAcqArchive::QueueNext()
b2e465d6 772{
03e39e59
AL
773 for (; Vf.end() == false; Vf++)
774 {
775 // Ignore not source sources
776 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
777 continue;
778
779 // Try to cross match against the source list
b2e465d6
AL
780 pkgIndexFile *Index;
781 if (Sources->FindIndex(Vf.File(),Index) == false)
782 continue;
03e39e59 783
b3d44315
MV
784 // only try to get a trusted package from another source if that source
785 // is also trusted
786 if(Trusted && !Index->IsTrusted())
787 continue;
788
03e39e59
AL
789 // Grab the text package record
790 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
791 if (_error->PendingError() == true)
b185acc2 792 return false;
03e39e59 793
b2e465d6 794 string PkgFile = Parse.FileName();
03e39e59
AL
795 MD5 = Parse.MD5Hash();
796 if (PkgFile.empty() == true)
b2e465d6
AL
797 return _error->Error(_("The package index files are corrupted. No Filename: "
798 "field for package %s."),
799 Version.ParentPkg().Name());
a6568219 800
b3d44315
MV
801 Desc.URI = Index->ArchiveURI(PkgFile);
802 Desc.Description = Index->ArchiveInfo(Version);
803 Desc.Owner = this;
804 Desc.ShortDesc = Version.ParentPkg().Name();
805
17caf1b1 806 // See if we already have the file. (Legacy filenames)
a6568219
AL
807 FileSize = Version->Size;
808 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
809 struct stat Buf;
810 if (stat(FinalFile.c_str(),&Buf) == 0)
811 {
812 // Make sure the size matches
813 if ((unsigned)Buf.st_size == Version->Size)
814 {
815 Complete = true;
816 Local = true;
817 Status = StatDone;
30e1eab5 818 StoreFilename = DestFile = FinalFile;
b185acc2 819 return true;
a6568219
AL
820 }
821
6b1ff003
AL
822 /* Hmm, we have a file and its size does not match, this means it is
823 an old style mismatched arch */
a6568219
AL
824 unlink(FinalFile.c_str());
825 }
17caf1b1
AL
826
827 // Check it again using the new style output filenames
828 FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
829 if (stat(FinalFile.c_str(),&Buf) == 0)
830 {
831 // Make sure the size matches
832 if ((unsigned)Buf.st_size == Version->Size)
833 {
834 Complete = true;
835 Local = true;
836 Status = StatDone;
837 StoreFilename = DestFile = FinalFile;
838 return true;
839 }
840
841 /* Hmm, we have a file and its size does not match, this shouldnt
842 happen.. */
843 unlink(FinalFile.c_str());
844 }
845
846 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
6b1ff003
AL
847
848 // Check the destination file
849 if (stat(DestFile.c_str(),&Buf) == 0)
850 {
851 // Hmm, the partial file is too big, erase it
852 if ((unsigned)Buf.st_size > Version->Size)
853 unlink(DestFile.c_str());
854 else
855 PartialSize = Buf.st_size;
856 }
857
03e39e59 858 // Create the item
b2e465d6
AL
859 Local = false;
860 Desc.URI = Index->ArchiveURI(PkgFile);
861 Desc.Description = Index->ArchiveInfo(Version);
03e39e59
AL
862 Desc.Owner = this;
863 Desc.ShortDesc = Version.ParentPkg().Name();
864 QueueURI(Desc);
b185acc2
AL
865
866 Vf++;
867 return true;
03e39e59 868 }
b185acc2
AL
869 return false;
870}
03e39e59
AL
871 /*}}}*/
872// AcqArchive::Done - Finished fetching /*{{{*/
873// ---------------------------------------------------------------------
874/* */
459681d3
AL
875void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
876 pkgAcquire::MethodConfig *Cfg)
03e39e59 877{
459681d3 878 Item::Done(Message,Size,Md5Hash,Cfg);
03e39e59
AL
879
880 // Check the size
881 if (Size != Version->Size)
882 {
bdae53f1 883 Status = StatError;
b2e465d6 884 ErrorText = _("Size mismatch");
03e39e59
AL
885 return;
886 }
887
888 // Check the md5
889 if (Md5Hash.empty() == false && MD5.empty() == false)
890 {
891 if (Md5Hash != MD5)
892 {
bdae53f1 893 Status = StatError;
b2e465d6 894 ErrorText = _("MD5Sum mismatch");
9978c7b0 895 Rename(DestFile,DestFile + ".FAILED");
03e39e59
AL
896 return;
897 }
898 }
a6568219
AL
899
900 // Grab the output filename
03e39e59
AL
901 string FileName = LookupTag(Message,"Filename");
902 if (FileName.empty() == true)
903 {
904 Status = StatError;
905 ErrorText = "Method gave a blank filename";
906 return;
907 }
a6568219
AL
908
909 Complete = true;
30e1eab5
AL
910
911 // Reference filename
a6568219
AL
912 if (FileName != DestFile)
913 {
30e1eab5 914 StoreFilename = DestFile = FileName;
a6568219
AL
915 Local = true;
916 return;
917 }
918
919 // Done, move it into position
920 string FinalFile = _config->FindDir("Dir::Cache::Archives");
17caf1b1 921 FinalFile += flNotDir(StoreFilename);
a6568219 922 Rename(DestFile,FinalFile);
03e39e59 923
30e1eab5 924 StoreFilename = DestFile = FinalFile;
03e39e59
AL
925 Complete = true;
926}
927 /*}}}*/
db890fdb
AL
928// AcqArchive::Failed - Failure handler /*{{{*/
929// ---------------------------------------------------------------------
930/* Here we try other sources */
7d8afa39 931void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
db890fdb
AL
932{
933 ErrorText = LookupTag(Message,"Message");
b2e465d6
AL
934
935 /* We don't really want to retry on failed media swaps, this prevents
936 that. An interesting observation is that permanent failures are not
937 recorded. */
938 if (Cnf->Removable == true &&
939 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
940 {
941 // Vf = Version.FileList();
942 while (Vf.end() == false) Vf++;
943 StoreFilename = string();
944 Item::Failed(Message,Cnf);
945 return;
946 }
947
db890fdb 948 if (QueueNext() == false)
7d8afa39
AL
949 {
950 // This is the retry counter
951 if (Retries != 0 &&
952 Cnf->LocalOnly == false &&
953 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
954 {
955 Retries--;
956 Vf = Version.FileList();
957 if (QueueNext() == true)
958 return;
959 }
960
9dbb421f 961 StoreFilename = string();
7d8afa39
AL
962 Item::Failed(Message,Cnf);
963 }
db890fdb
AL
964}
965 /*}}}*/
b3d44315
MV
966// AcqArchive::IsTrusted - Determine whether this archive comes from a
967// trusted source /*{{{*/
968// ---------------------------------------------------------------------
969bool pkgAcqArchive::IsTrusted()
970{
971 return Trusted;
972}
973
ab559b35
AL
974// AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
975// ---------------------------------------------------------------------
976/* */
977void pkgAcqArchive::Finished()
978{
979 if (Status == pkgAcquire::Item::StatDone &&
980 Complete == true)
981 return;
982 StoreFilename = string();
983}
984 /*}}}*/
36375005
AL
985
986// AcqFile::pkgAcqFile - Constructor /*{{{*/
987// ---------------------------------------------------------------------
988/* The file is added to the queue */
989pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
990 unsigned long Size,string Dsc,string ShortDesc) :
b3c39978 991 Item(Owner), Md5Hash(MD5)
36375005 992{
08cfc005
AL
993 Retries = _config->FindI("Acquire::Retries",0);
994
36375005
AL
995 DestFile = flNotDir(URI);
996
997 // Create the item
998 Desc.URI = URI;
999 Desc.Description = Dsc;
1000 Desc.Owner = this;
1001
1002 // Set the short description to the archive component
1003 Desc.ShortDesc = ShortDesc;
1004
1005 // Get the transfer sizes
1006 FileSize = Size;
1007 struct stat Buf;
1008 if (stat(DestFile.c_str(),&Buf) == 0)
1009 {
1010 // Hmm, the partial file is too big, erase it
1011 if ((unsigned)Buf.st_size > Size)
1012 unlink(DestFile.c_str());
1013 else
1014 PartialSize = Buf.st_size;
1015 }
1016
1017 QueueURI(Desc);
1018}
1019 /*}}}*/
1020// AcqFile::Done - Item downloaded OK /*{{{*/
1021// ---------------------------------------------------------------------
1022/* */
459681d3
AL
1023void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
1024 pkgAcquire::MethodConfig *Cnf)
36375005 1025{
b3c39978
AL
1026 // Check the md5
1027 if (Md5Hash.empty() == false && MD5.empty() == false)
1028 {
1029 if (Md5Hash != MD5)
1030 {
1031 Status = StatError;
1032 ErrorText = "MD5Sum mismatch";
1033 Rename(DestFile,DestFile + ".FAILED");
1034 return;
1035 }
1036 }
1037
459681d3 1038 Item::Done(Message,Size,MD5,Cnf);
36375005
AL
1039
1040 string FileName = LookupTag(Message,"Filename");
1041 if (FileName.empty() == true)
1042 {
1043 Status = StatError;
1044 ErrorText = "Method gave a blank filename";
1045 return;
1046 }
1047
1048 Complete = true;
1049
1050 // The files timestamp matches
1051 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
1052 return;
1053
1054 // We have to copy it into place
1055 if (FileName != DestFile)
1056 {
1057 Local = true;
459681d3
AL
1058 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
1059 Cnf->Removable == true)
917ae805
AL
1060 {
1061 Desc.URI = "copy:" + FileName;
1062 QueueURI(Desc);
1063 return;
1064 }
1065
83ab33fc
AL
1066 // Erase the file if it is a symlink so we can overwrite it
1067 struct stat St;
1068 if (lstat(DestFile.c_str(),&St) == 0)
1069 {
1070 if (S_ISLNK(St.st_mode) != 0)
1071 unlink(DestFile.c_str());
1072 }
1073
1074 // Symlink the file
917ae805
AL
1075 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
1076 {
83ab33fc 1077 ErrorText = "Link to " + DestFile + " failure ";
917ae805
AL
1078 Status = StatError;
1079 Complete = false;
1080 }
36375005
AL
1081 }
1082}
1083 /*}}}*/
08cfc005
AL
1084// AcqFile::Failed - Failure handler /*{{{*/
1085// ---------------------------------------------------------------------
1086/* Here we try other sources */
1087void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1088{
1089 ErrorText = LookupTag(Message,"Message");
1090
1091 // This is the retry counter
1092 if (Retries != 0 &&
1093 Cnf->LocalOnly == false &&
1094 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1095 {
1096 Retries--;
1097 QueueURI(Desc);
1098 return;
1099 }
1100
1101 Item::Failed(Message,Cnf);
1102}
1103 /*}}}*/