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