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