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