tag of apt@packages.debian.org/apt--main--0--patch-87
[ntk/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);
284c8bbc
MV
343 // unlink the file and do not try to use I-M-S and Last-Modified
344 // if the users proxy is broken
345 if(_config->FindB("Acquire::BrokenProxy", false) == true) {
346 std::cerr << "forcing re-get of the signature file as requested" << std::endl;
347 unlink(DestFile.c_str());
348 }
b3d44315 349 }
8267fe24 350
8267fe24 351 QueueURI(Desc);
0118833a
AL
352}
353 /*}}}*/
b3d44315 354// pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
0118833a 355// ---------------------------------------------------------------------
0a8a80e5 356/* The only header we use is the last-modified header. */
b3d44315 357string pkgAcqMetaSig::Custom600Headers()
0118833a 358{
0a8a80e5 359 struct stat Buf;
2aab5956 360 if (stat(DestFile.c_str(),&Buf) != 0)
a72ace20 361 return "\nIndex-File: true";
a789b983 362
a72ace20 363 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
0118833a 364}
b3d44315
MV
365
366void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
367 pkgAcquire::MethodConfig *Cfg)
c88edf1d 368{
459681d3 369 Item::Done(Message,Size,MD5,Cfg);
c88edf1d
AL
370
371 string FileName = LookupTag(Message,"Filename");
372 if (FileName.empty() == true)
373 {
374 Status = StatError;
375 ErrorText = "Method gave a blank filename";
8b89e57f 376 return;
c88edf1d 377 }
8b89e57f 378
c88edf1d
AL
379 if (FileName != DestFile)
380 {
b3d44315 381 // We have to copy it into place
a6568219 382 Local = true;
8267fe24
AL
383 Desc.URI = "copy:" + FileName;
384 QueueURI(Desc);
c88edf1d
AL
385 return;
386 }
b3d44315
MV
387
388 Complete = true;
389
390 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
391 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
392 DestFile, IndexTargets, MetaIndexParser);
393
c88edf1d
AL
394}
395 /*}}}*/
b3d44315 396void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
681d76d0 397{
b3d44315
MV
398 // Delete any existing sigfile, so that this source isn't
399 // mistakenly trusted
400 string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
401 unlink(Final.c_str());
a789b983 402
b3d44315
MV
403 // queue a pkgAcqMetaIndex with no sigfile
404 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
405 "", IndexTargets, MetaIndexParser);
406
681d76d0
AL
407 if (Cnf->LocalOnly == true ||
408 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
409 {
2b154e53
AL
410 // Ignore this
411 Status = StatDone;
412 Complete = false;
681d76d0
AL
413 Dequeue();
414 return;
415 }
416
417 Item::Failed(Message,Cnf);
418}
b3d44315
MV
419
420pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
421 string URI,string URIDesc,string ShortDesc,
422 string SigFile,
423 const vector<struct IndexTarget*>* IndexTargets,
424 indexRecords* MetaIndexParser) :
425 Item(Owner), RealURI(URI), SigFile(SigFile)
426{
427 this->AuthPass = false;
428 this->MetaIndexParser = MetaIndexParser;
429 this->IndexTargets = IndexTargets;
430 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
431 DestFile += URItoFileName(URI);
432
433 // Create the item
434 Desc.Description = URIDesc;
435 Desc.Owner = this;
436 Desc.ShortDesc = ShortDesc;
437 Desc.URI = URI;
438
439 QueueURI(Desc);
440}
441
442 /*}}}*/
443// pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
444// ---------------------------------------------------------------------
445/* The only header we use is the last-modified header. */
446string pkgAcqMetaIndex::Custom600Headers()
447{
448 string Final = _config->FindDir("Dir::State::lists");
449 Final += URItoFileName(RealURI);
450
451 struct stat Buf;
452 if (stat(Final.c_str(),&Buf) != 0)
453 return "\nIndex-File: true";
454
455 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
456}
457
458void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
459 pkgAcquire::MethodConfig *Cfg)
460{
461 Item::Done(Message,Size,MD5,Cfg);
462
463 // MetaIndexes are done in two passes: one to download the
464 // metaindex with an appropriate method, and a second to verify it
465 // with the gpgv method
466
467 if (AuthPass == true)
468 {
469 AuthDone(Message);
470 }
471 else
472 {
473 RetrievalDone(Message);
474 if (!Complete)
475 // Still more retrieving to do
476 return;
477
478 if (SigFile == "")
479 {
480 // There was no signature file, so we are finished. Download
481 // the indexes without verification.
482 QueueIndexes(false);
483 }
484 else
485 {
486 // There was a signature file, so pass it to gpgv for
487 // verification
488
489 if (_config->FindB("Debug::pkgAcquire::Auth", false))
490 std::cerr << "Metaindex acquired, queueing gpg verification ("
491 << SigFile << "," << DestFile << ")\n";
492 AuthPass = true;
493 Desc.URI = "gpgv:" + SigFile;
494 QueueURI(Desc);
495 Mode = "gpgv";
496 }
497 }
498}
499
500void pkgAcqMetaIndex::RetrievalDone(string Message)
501{
502 // We have just finished downloading a Release file (it is not
503 // verified yet)
504
505 string FileName = LookupTag(Message,"Filename");
506 if (FileName.empty() == true)
507 {
508 Status = StatError;
509 ErrorText = "Method gave a blank filename";
510 return;
511 }
512
513 if (FileName != DestFile)
514 {
515 Local = true;
516 Desc.URI = "copy:" + FileName;
517 QueueURI(Desc);
518 return;
519 }
520
521 Complete = true;
522
523 string FinalFile = _config->FindDir("Dir::State::lists");
524 FinalFile += URItoFileName(RealURI);
525
526 // The files timestamp matches
527 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
528 {
529 // Move it into position
530 Rename(DestFile,FinalFile);
531 }
532 DestFile = FinalFile;
533}
534
535void pkgAcqMetaIndex::AuthDone(string Message)
536{
537 // At this point, the gpgv method has succeeded, so there is a
538 // valid signature from a key in the trusted keyring. We
539 // perform additional verification of its contents, and use them
540 // to verify the indexes we are about to download
541
542 if (!MetaIndexParser->Load(DestFile))
543 {
544 Status = StatAuthError;
545 ErrorText = MetaIndexParser->ErrorText;
546 return;
547 }
548
549 if (!VerifyVendor())
550 {
551 return;
552 }
553
554 if (_config->FindB("Debug::pkgAcquire::Auth", false))
555 std::cerr << "Signature verification succeeded: "
556 << DestFile << std::endl;
557
558 // Download further indexes with verification
559 QueueIndexes(true);
560
561 // Done, move signature file into position
562
563 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
564 URItoFileName(RealURI) + ".gpg";
565 Rename(SigFile,VerifiedSigFile);
566 chmod(VerifiedSigFile.c_str(),0644);
567}
568
569void pkgAcqMetaIndex::QueueIndexes(bool verify)
570{
571 for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
572 Target != IndexTargets->end();
573 Target++)
574 {
575 string ExpectedIndexMD5;
576 if (verify)
577 {
578 const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
579 if (!Record)
580 {
581 Status = StatAuthError;
582 ErrorText = "Unable to find expected entry "
583 + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
584 return;
585 }
586 ExpectedIndexMD5 = Record->MD5Hash;
587 if (_config->FindB("Debug::pkgAcquire::Auth", false))
588 {
589 std::cerr << "Queueing: " << (*Target)->URI << std::endl;
590 std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
591 }
592 if (ExpectedIndexMD5.empty())
593 {
594 Status = StatAuthError;
595 ErrorText = "Unable to find MD5 sum for "
596 + (*Target)->MetaKey + " in Meta-index file";
597 return;
598 }
599 }
600
601 // Queue Packages file
602 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
603 (*Target)->ShortDesc, ExpectedIndexMD5);
604 }
605}
606
607bool pkgAcqMetaIndex::VerifyVendor()
608{
609// // Maybe this should be made available from above so we don't have
610// // to read and parse it every time?
611// pkgVendorList List;
612// List.ReadMainList();
613
614// const Vendor* Vndr = NULL;
615// for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
616// {
617// string::size_type pos = (*I).find("VALIDSIG ");
618// if (_config->FindB("Debug::Vendor", false))
619// std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
620// << std::endl;
621// if (pos != std::string::npos)
622// {
623// string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
624// if (_config->FindB("Debug::Vendor", false))
625// std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
626// std::endl;
627// Vndr = List.FindVendor(Fingerprint) != "";
628// if (Vndr != NULL);
629// break;
630// }
631// }
632
633 string Transformed = MetaIndexParser->GetExpectedDist();
634
635 if (Transformed == "../project/experimental")
636 {
637 Transformed = "experimental";
638 }
639
640 string::size_type pos = Transformed.rfind('/');
641 if (pos != string::npos)
642 {
643 Transformed = Transformed.substr(0, pos);
644 }
645
646 if (Transformed == ".")
647 {
648 Transformed = "";
649 }
650
651 if (_config->FindB("Debug::pkgAcquire::Auth", false))
652 {
653 std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
654 std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
655 std::cerr << "Transformed Dist: " << Transformed << std::endl;
656 }
657
658 if (MetaIndexParser->CheckDist(Transformed) == false)
659 {
660 // This might become fatal one day
661// Status = StatAuthError;
662// ErrorText = "Conflicting distribution; expected "
663// + MetaIndexParser->GetExpectedDist() + " but got "
664// + MetaIndexParser->GetDist();
665// return false;
666 if (!Transformed.empty())
667 {
668 _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
669 Desc.Description.c_str(),
670 Transformed.c_str(),
671 MetaIndexParser->GetDist().c_str());
672 }
673 }
674
675 return true;
676}
677 /*}}}*/
678// pkgAcqMetaIndex::Failed - no Release file present or no signature
679// file present /*{{{*/
680// ---------------------------------------------------------------------
681/* */
682void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
683{
684 if (AuthPass == true)
685 {
686 // gpgv method failed
687 _error->Warning("GPG error: %s: %s",
688 Desc.Description.c_str(),
689 LookupTag(Message,"Message").c_str());
690 }
691
692 // No Release file was present, or verification failed, so fall
693 // back to queueing Packages files without verification
694 QueueIndexes(false);
695}
696
681d76d0 697 /*}}}*/
03e39e59
AL
698
699// AcqArchive::AcqArchive - Constructor /*{{{*/
700// ---------------------------------------------------------------------
17caf1b1
AL
701/* This just sets up the initial fetch environment and queues the first
702 possibilitiy */
03e39e59 703pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
30e1eab5
AL
704 pkgRecords *Recs,pkgCache::VerIterator const &Version,
705 string &StoreFilename) :
706 Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
b3d44315
MV
707 StoreFilename(StoreFilename), Vf(Version.FileList()),
708 Trusted(false)
03e39e59 709{
7d8afa39 710 Retries = _config->FindI("Acquire::Retries",0);
813c8eea
AL
711
712 if (Version.Arch() == 0)
bdae53f1 713 {
d1f1f6a8 714 _error->Error(_("I wasn't able to locate a file for the %s package. "
7a3c2ab0
AL
715 "This might mean you need to manually fix this package. "
716 "(due to missing arch)"),
813c8eea 717 Version.ParentPkg().Name());
bdae53f1
AL
718 return;
719 }
813c8eea 720
b2e465d6
AL
721 /* We need to find a filename to determine the extension. We make the
722 assumption here that all the available sources for this version share
723 the same extension.. */
724 // Skip not source sources, they do not have file fields.
725 for (; Vf.end() == false; Vf++)
726 {
727 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
728 continue;
729 break;
730 }
731
732 // Does not really matter here.. we are going to fail out below
733 if (Vf.end() != true)
734 {
735 // If this fails to get a file name we will bomb out below.
736 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
737 if (_error->PendingError() == true)
738 return;
739
740 // Generate the final file name as: package_version_arch.foo
741 StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
742 QuoteString(Version.VerStr(),"_:") + '_' +
743 QuoteString(Version.Arch(),"_:.") +
744 "." + flExtension(Parse.FileName());
745 }
b3d44315
MV
746
747 // check if we have one trusted source for the package. if so, switch
748 // to "TrustedOnly" mode
749 for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
750 {
751 pkgIndexFile *Index;
752 if (Sources->FindIndex(i.File(),Index) == false)
753 continue;
754 if (_config->FindB("Debug::pkgAcquire::Auth", false))
755 {
756 std::cerr << "Checking index: " << Index->Describe()
757 << "(Trusted=" << Index->IsTrusted() << ")\n";
758 }
759 if (Index->IsTrusted()) {
760 Trusted = true;
761 break;
762 }
763 }
764
03e39e59 765 // Select a source
b185acc2 766 if (QueueNext() == false && _error->PendingError() == false)
b2e465d6
AL
767 _error->Error(_("I wasn't able to locate file for the %s package. "
768 "This might mean you need to manually fix this package."),
b185acc2
AL
769 Version.ParentPkg().Name());
770}
771 /*}}}*/
772// AcqArchive::QueueNext - Queue the next file source /*{{{*/
773// ---------------------------------------------------------------------
17caf1b1
AL
774/* This queues the next available file version for download. It checks if
775 the archive is already available in the cache and stashs the MD5 for
776 checking later. */
b185acc2 777bool pkgAcqArchive::QueueNext()
b2e465d6 778{
03e39e59
AL
779 for (; Vf.end() == false; Vf++)
780 {
781 // Ignore not source sources
782 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
783 continue;
784
785 // Try to cross match against the source list
b2e465d6
AL
786 pkgIndexFile *Index;
787 if (Sources->FindIndex(Vf.File(),Index) == false)
788 continue;
03e39e59 789
b3d44315
MV
790 // only try to get a trusted package from another source if that source
791 // is also trusted
792 if(Trusted && !Index->IsTrusted())
793 continue;
794
03e39e59
AL
795 // Grab the text package record
796 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
797 if (_error->PendingError() == true)
b185acc2 798 return false;
03e39e59 799
b2e465d6 800 string PkgFile = Parse.FileName();
03e39e59
AL
801 MD5 = Parse.MD5Hash();
802 if (PkgFile.empty() == true)
b2e465d6
AL
803 return _error->Error(_("The package index files are corrupted. No Filename: "
804 "field for package %s."),
805 Version.ParentPkg().Name());
a6568219 806
b3d44315
MV
807 Desc.URI = Index->ArchiveURI(PkgFile);
808 Desc.Description = Index->ArchiveInfo(Version);
809 Desc.Owner = this;
810 Desc.ShortDesc = Version.ParentPkg().Name();
811
17caf1b1 812 // See if we already have the file. (Legacy filenames)
a6568219
AL
813 FileSize = Version->Size;
814 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
815 struct stat Buf;
816 if (stat(FinalFile.c_str(),&Buf) == 0)
817 {
818 // Make sure the size matches
819 if ((unsigned)Buf.st_size == Version->Size)
820 {
821 Complete = true;
822 Local = true;
823 Status = StatDone;
30e1eab5 824 StoreFilename = DestFile = FinalFile;
b185acc2 825 return true;
a6568219
AL
826 }
827
6b1ff003
AL
828 /* Hmm, we have a file and its size does not match, this means it is
829 an old style mismatched arch */
a6568219
AL
830 unlink(FinalFile.c_str());
831 }
17caf1b1
AL
832
833 // Check it again using the new style output filenames
834 FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
835 if (stat(FinalFile.c_str(),&Buf) == 0)
836 {
837 // Make sure the size matches
838 if ((unsigned)Buf.st_size == Version->Size)
839 {
840 Complete = true;
841 Local = true;
842 Status = StatDone;
843 StoreFilename = DestFile = FinalFile;
844 return true;
845 }
846
847 /* Hmm, we have a file and its size does not match, this shouldnt
848 happen.. */
849 unlink(FinalFile.c_str());
850 }
851
852 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
6b1ff003
AL
853
854 // Check the destination file
855 if (stat(DestFile.c_str(),&Buf) == 0)
856 {
857 // Hmm, the partial file is too big, erase it
858 if ((unsigned)Buf.st_size > Version->Size)
859 unlink(DestFile.c_str());
860 else
861 PartialSize = Buf.st_size;
862 }
863
03e39e59 864 // Create the item
b2e465d6
AL
865 Local = false;
866 Desc.URI = Index->ArchiveURI(PkgFile);
867 Desc.Description = Index->ArchiveInfo(Version);
03e39e59
AL
868 Desc.Owner = this;
869 Desc.ShortDesc = Version.ParentPkg().Name();
870 QueueURI(Desc);
b185acc2
AL
871
872 Vf++;
873 return true;
03e39e59 874 }
b185acc2
AL
875 return false;
876}
03e39e59
AL
877 /*}}}*/
878// AcqArchive::Done - Finished fetching /*{{{*/
879// ---------------------------------------------------------------------
880/* */
459681d3
AL
881void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
882 pkgAcquire::MethodConfig *Cfg)
03e39e59 883{
459681d3 884 Item::Done(Message,Size,Md5Hash,Cfg);
03e39e59
AL
885
886 // Check the size
887 if (Size != Version->Size)
888 {
bdae53f1 889 Status = StatError;
b2e465d6 890 ErrorText = _("Size mismatch");
03e39e59
AL
891 return;
892 }
893
894 // Check the md5
895 if (Md5Hash.empty() == false && MD5.empty() == false)
896 {
897 if (Md5Hash != MD5)
898 {
bdae53f1 899 Status = StatError;
b2e465d6 900 ErrorText = _("MD5Sum mismatch");
9978c7b0 901 Rename(DestFile,DestFile + ".FAILED");
03e39e59
AL
902 return;
903 }
904 }
a6568219
AL
905
906 // Grab the output filename
03e39e59
AL
907 string FileName = LookupTag(Message,"Filename");
908 if (FileName.empty() == true)
909 {
910 Status = StatError;
911 ErrorText = "Method gave a blank filename";
912 return;
913 }
a6568219
AL
914
915 Complete = true;
30e1eab5
AL
916
917 // Reference filename
a6568219
AL
918 if (FileName != DestFile)
919 {
30e1eab5 920 StoreFilename = DestFile = FileName;
a6568219
AL
921 Local = true;
922 return;
923 }
924
925 // Done, move it into position
926 string FinalFile = _config->FindDir("Dir::Cache::Archives");
17caf1b1 927 FinalFile += flNotDir(StoreFilename);
a6568219 928 Rename(DestFile,FinalFile);
03e39e59 929
30e1eab5 930 StoreFilename = DestFile = FinalFile;
03e39e59
AL
931 Complete = true;
932}
933 /*}}}*/
db890fdb
AL
934// AcqArchive::Failed - Failure handler /*{{{*/
935// ---------------------------------------------------------------------
936/* Here we try other sources */
7d8afa39 937void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
db890fdb
AL
938{
939 ErrorText = LookupTag(Message,"Message");
b2e465d6
AL
940
941 /* We don't really want to retry on failed media swaps, this prevents
942 that. An interesting observation is that permanent failures are not
943 recorded. */
944 if (Cnf->Removable == true &&
945 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
946 {
947 // Vf = Version.FileList();
948 while (Vf.end() == false) Vf++;
949 StoreFilename = string();
950 Item::Failed(Message,Cnf);
951 return;
952 }
953
db890fdb 954 if (QueueNext() == false)
7d8afa39
AL
955 {
956 // This is the retry counter
957 if (Retries != 0 &&
958 Cnf->LocalOnly == false &&
959 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
960 {
961 Retries--;
962 Vf = Version.FileList();
963 if (QueueNext() == true)
964 return;
965 }
966
9dbb421f 967 StoreFilename = string();
7d8afa39
AL
968 Item::Failed(Message,Cnf);
969 }
db890fdb
AL
970}
971 /*}}}*/
b3d44315
MV
972// AcqArchive::IsTrusted - Determine whether this archive comes from a
973// trusted source /*{{{*/
974// ---------------------------------------------------------------------
975bool pkgAcqArchive::IsTrusted()
976{
977 return Trusted;
978}
979
ab559b35
AL
980// AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
981// ---------------------------------------------------------------------
982/* */
983void pkgAcqArchive::Finished()
984{
985 if (Status == pkgAcquire::Item::StatDone &&
986 Complete == true)
987 return;
988 StoreFilename = string();
989}
990 /*}}}*/
36375005
AL
991
992// AcqFile::pkgAcqFile - Constructor /*{{{*/
993// ---------------------------------------------------------------------
994/* The file is added to the queue */
995pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
996 unsigned long Size,string Dsc,string ShortDesc) :
b3c39978 997 Item(Owner), Md5Hash(MD5)
36375005 998{
08cfc005
AL
999 Retries = _config->FindI("Acquire::Retries",0);
1000
36375005
AL
1001 DestFile = flNotDir(URI);
1002
1003 // Create the item
1004 Desc.URI = URI;
1005 Desc.Description = Dsc;
1006 Desc.Owner = this;
1007
1008 // Set the short description to the archive component
1009 Desc.ShortDesc = ShortDesc;
1010
1011 // Get the transfer sizes
1012 FileSize = Size;
1013 struct stat Buf;
1014 if (stat(DestFile.c_str(),&Buf) == 0)
1015 {
1016 // Hmm, the partial file is too big, erase it
1017 if ((unsigned)Buf.st_size > Size)
1018 unlink(DestFile.c_str());
1019 else
1020 PartialSize = Buf.st_size;
1021 }
1022
1023 QueueURI(Desc);
1024}
1025 /*}}}*/
1026// AcqFile::Done - Item downloaded OK /*{{{*/
1027// ---------------------------------------------------------------------
1028/* */
459681d3
AL
1029void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
1030 pkgAcquire::MethodConfig *Cnf)
36375005 1031{
b3c39978
AL
1032 // Check the md5
1033 if (Md5Hash.empty() == false && MD5.empty() == false)
1034 {
1035 if (Md5Hash != MD5)
1036 {
1037 Status = StatError;
1038 ErrorText = "MD5Sum mismatch";
1039 Rename(DestFile,DestFile + ".FAILED");
1040 return;
1041 }
1042 }
1043
459681d3 1044 Item::Done(Message,Size,MD5,Cnf);
36375005
AL
1045
1046 string FileName = LookupTag(Message,"Filename");
1047 if (FileName.empty() == true)
1048 {
1049 Status = StatError;
1050 ErrorText = "Method gave a blank filename";
1051 return;
1052 }
1053
1054 Complete = true;
1055
1056 // The files timestamp matches
1057 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
1058 return;
1059
1060 // We have to copy it into place
1061 if (FileName != DestFile)
1062 {
1063 Local = true;
459681d3
AL
1064 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
1065 Cnf->Removable == true)
917ae805
AL
1066 {
1067 Desc.URI = "copy:" + FileName;
1068 QueueURI(Desc);
1069 return;
1070 }
1071
83ab33fc
AL
1072 // Erase the file if it is a symlink so we can overwrite it
1073 struct stat St;
1074 if (lstat(DestFile.c_str(),&St) == 0)
1075 {
1076 if (S_ISLNK(St.st_mode) != 0)
1077 unlink(DestFile.c_str());
1078 }
1079
1080 // Symlink the file
917ae805
AL
1081 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
1082 {
83ab33fc 1083 ErrorText = "Link to " + DestFile + " failure ";
917ae805
AL
1084 Status = StatError;
1085 Complete = false;
1086 }
36375005
AL
1087 }
1088}
1089 /*}}}*/
08cfc005
AL
1090// AcqFile::Failed - Failure handler /*{{{*/
1091// ---------------------------------------------------------------------
1092/* Here we try other sources */
1093void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1094{
1095 ErrorText = LookupTag(Message,"Message");
1096
1097 // This is the retry counter
1098 if (Retries != 0 &&
1099 Cnf->LocalOnly == false &&
1100 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1101 {
1102 Retries--;
1103 QueueURI(Desc);
1104 return;
1105 }
1106
1107 Item::Failed(Message,Cnf);
1108}
1109 /*}}}*/