]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-item.cc
* merged with mvo
[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 the compression method
152 if(FileExists("/usr/bin/bzip2"))
153 CompressionExtension = ".bz2";
154 else
155 CompressionExtension = ".gz";
156 } else {
157 CompressionExtension = comprExt;
158 }
159 Desc.URI = URI + CompressionExtension;
160
161 Desc.Description = URIDesc;
162 Desc.Owner = this;
163 Desc.ShortDesc = ShortDesc;
164
165 QueueURI(Desc);
166 }
167 /*}}}*/
168 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
169 // ---------------------------------------------------------------------
170 /* The only header we use is the last-modified header. */
171 string pkgAcqIndex::Custom600Headers()
172 {
173 string Final = _config->FindDir("Dir::State::lists");
174 Final += URItoFileName(RealURI);
175
176 struct stat Buf;
177 if (stat(Final.c_str(),&Buf) != 0)
178 return "\nIndex-File: true";
179
180 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
181 }
182 /*}}}*/
183
184 void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
185 {
186 // no .bz2 found, retry with .gz
187 if(Desc.URI.substr(Desc.URI.size()-3) == "bz2") {
188 Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
189
190 // retry with a gzip one
191 new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
192 ExpectedMD5, string(".gz"));
193 Status = StatDone;
194 Complete = false;
195 Dequeue();
196 return;
197 }
198
199
200 Item::Failed(Message,Cnf);
201 }
202
203
204 // AcqIndex::Done - Finished a fetch /*{{{*/
205 // ---------------------------------------------------------------------
206 /* This goes through a number of states.. On the initial fetch the
207 method could possibly return an alternate filename which points
208 to the uncompressed version of the file. If this is so the file
209 is copied into the partial directory. In all other cases the file
210 is decompressed with a gzip uri. */
211 void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
212 pkgAcquire::MethodConfig *Cfg)
213 {
214 Item::Done(Message,Size,MD5,Cfg);
215
216 if (Decompression == true)
217 {
218 if (_config->FindB("Debug::pkgAcquire::Auth", false))
219 {
220 std::cerr << std::endl << RealURI << ": Computed MD5: " << MD5;
221 std::cerr << " Expected MD5: " << ExpectedMD5 << std::endl;
222 }
223
224 if (MD5.empty())
225 {
226 MD5Summation sum;
227 FileFd Fd(DestFile, FileFd::ReadOnly);
228 sum.AddFD(Fd.Fd(), Fd.Size());
229 Fd.Close();
230 MD5 = (string)sum.Result();
231 }
232
233 if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
234 {
235 Status = StatAuthError;
236 ErrorText = _("MD5Sum mismatch");
237 Rename(DestFile,DestFile + ".FAILED");
238 return;
239 }
240 // Done, move it into position
241 string FinalFile = _config->FindDir("Dir::State::lists");
242 FinalFile += URItoFileName(RealURI);
243 Rename(DestFile,FinalFile);
244 chmod(FinalFile.c_str(),0644);
245
246 /* We restore the original name to DestFile so that the clean operation
247 will work OK */
248 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
249 DestFile += URItoFileName(RealURI);
250
251 // Remove the compressed version.
252 if (Erase == true)
253 unlink(DestFile.c_str());
254 return;
255 }
256
257 Erase = false;
258 Complete = true;
259
260 // Handle the unzipd case
261 string FileName = LookupTag(Message,"Alt-Filename");
262 if (FileName.empty() == false)
263 {
264 // The files timestamp matches
265 if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
266 return;
267
268 Decompression = true;
269 Local = true;
270 DestFile += ".decomp";
271 Desc.URI = "copy:" + FileName;
272 QueueURI(Desc);
273 Mode = "copy";
274 return;
275 }
276
277 FileName = LookupTag(Message,"Filename");
278 if (FileName.empty() == true)
279 {
280 Status = StatError;
281 ErrorText = "Method gave a blank filename";
282 }
283
284 // The files timestamp matches
285 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
286 return;
287
288 if (FileName == DestFile)
289 Erase = true;
290 else
291 Local = true;
292
293 string compExt = Desc.URI.substr(Desc.URI.size()-3);
294 char *decompProg;
295 if(compExt == "bz2")
296 decompProg = "bzip2";
297 else if(compExt == ".gz")
298 decompProg = "gzip";
299 else {
300 _error->Error("Unsupported extension: %s", compExt.c_str());
301 return;
302 }
303
304 Decompression = true;
305 DestFile += ".decomp";
306 Desc.URI = string(decompProg) + ":" + FileName;
307 QueueURI(Desc);
308 Mode = decompProg;
309 }
310
311 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
312 string URI,string URIDesc,string ShortDesc,
313 string MetaIndexURI, string MetaIndexURIDesc,
314 string MetaIndexShortDesc,
315 const vector<IndexTarget*>* IndexTargets,
316 indexRecords* MetaIndexParser) :
317 Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
318 MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
319 MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets)
320 {
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), AuthPass(false),
433 MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets), IMSHit(false)
434 {
435 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
436 DestFile += URItoFileName(URI);
437
438 // Create the item
439 Desc.Description = URIDesc;
440 Desc.Owner = this;
441 Desc.ShortDesc = ShortDesc;
442 Desc.URI = URI;
443
444 QueueURI(Desc);
445 }
446
447 /*}}}*/
448 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
449 // ---------------------------------------------------------------------
450 /* The only header we use is the last-modified header. */
451 string pkgAcqMetaIndex::Custom600Headers()
452 {
453 string Final = _config->FindDir("Dir::State::lists");
454 Final += URItoFileName(RealURI);
455
456 struct stat Buf;
457 if (stat(Final.c_str(),&Buf) != 0)
458 return "\nIndex-File: true";
459
460 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
461 }
462
463 void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
464 pkgAcquire::MethodConfig *Cfg)
465 {
466 Item::Done(Message,Size,MD5,Cfg);
467
468 // MetaIndexes are done in two passes: one to download the
469 // metaindex with an appropriate method, and a second to verify it
470 // with the gpgv method
471
472 if (AuthPass == true)
473 {
474 AuthDone(Message);
475 }
476 else
477 {
478 RetrievalDone(Message);
479 if (!Complete)
480 // Still more retrieving to do
481 return;
482
483 if (SigFile == "")
484 {
485 // There was no signature file, so we are finished. Download
486 // the indexes without verification.
487 QueueIndexes(false);
488 }
489 else
490 {
491 // There was a signature file, so pass it to gpgv for
492 // verification
493
494 if (_config->FindB("Debug::pkgAcquire::Auth", false))
495 std::cerr << "Metaindex acquired, queueing gpg verification ("
496 << SigFile << "," << DestFile << ")\n";
497 AuthPass = true;
498 Desc.URI = "gpgv:" + SigFile;
499 QueueURI(Desc);
500 Mode = "gpgv";
501 }
502 }
503 }
504
505 void pkgAcqMetaIndex::RetrievalDone(string Message)
506 {
507 // We have just finished downloading a Release file (it is not
508 // verified yet)
509
510 string FileName = LookupTag(Message,"Filename");
511 if (FileName.empty() == true)
512 {
513 Status = StatError;
514 ErrorText = "Method gave a blank filename";
515 return;
516 }
517
518 if (FileName != DestFile)
519 {
520 Local = true;
521 Desc.URI = "copy:" + FileName;
522 QueueURI(Desc);
523 return;
524 }
525
526 // see if the download was a IMSHit
527 IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false);
528
529 Complete = true;
530
531 string FinalFile = _config->FindDir("Dir::State::lists");
532 FinalFile += URItoFileName(RealURI);
533
534 // The files timestamp matches
535 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
536 {
537 // Move it into position
538 Rename(DestFile,FinalFile);
539 }
540 DestFile = FinalFile;
541 }
542
543 void pkgAcqMetaIndex::AuthDone(string Message)
544 {
545 // At this point, the gpgv method has succeeded, so there is a
546 // valid signature from a key in the trusted keyring. We
547 // perform additional verification of its contents, and use them
548 // to verify the indexes we are about to download
549
550 if (!MetaIndexParser->Load(DestFile))
551 {
552 Status = StatAuthError;
553 ErrorText = MetaIndexParser->ErrorText;
554 return;
555 }
556
557 if (!VerifyVendor(Message))
558 {
559 return;
560 }
561
562 if (_config->FindB("Debug::pkgAcquire::Auth", false))
563 std::cerr << "Signature verification succeeded: "
564 << DestFile << std::endl;
565
566 // Download further indexes with verification
567 QueueIndexes(true);
568
569 // Done, move signature file into position
570
571 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
572 URItoFileName(RealURI) + ".gpg";
573 Rename(SigFile,VerifiedSigFile);
574 chmod(VerifiedSigFile.c_str(),0644);
575 }
576
577 void pkgAcqMetaIndex::QueueIndexes(bool verify)
578 {
579 for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
580 Target != IndexTargets->end();
581 Target++)
582 {
583 string ExpectedIndexMD5;
584 if (verify)
585 {
586 const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
587 if (!Record)
588 {
589 Status = StatAuthError;
590 ErrorText = "Unable to find expected entry "
591 + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
592 return;
593 }
594 ExpectedIndexMD5 = Record->MD5Hash;
595 if (_config->FindB("Debug::pkgAcquire::Auth", false))
596 {
597 std::cerr << "Queueing: " << (*Target)->URI << std::endl;
598 std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
599 }
600 if (ExpectedIndexMD5.empty())
601 {
602 Status = StatAuthError;
603 ErrorText = "Unable to find MD5 sum for "
604 + (*Target)->MetaKey + " in Meta-index file";
605 return;
606 }
607 }
608
609 // Queue Packages file
610 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
611 (*Target)->ShortDesc, ExpectedIndexMD5);
612 }
613 }
614
615 bool pkgAcqMetaIndex::VerifyVendor(string Message)
616 {
617 // // Maybe this should be made available from above so we don't have
618 // // to read and parse it every time?
619 // pkgVendorList List;
620 // List.ReadMainList();
621
622 // const Vendor* Vndr = NULL;
623 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
624 // {
625 // string::size_type pos = (*I).find("VALIDSIG ");
626 // if (_config->FindB("Debug::Vendor", false))
627 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
628 // << std::endl;
629 // if (pos != std::string::npos)
630 // {
631 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
632 // if (_config->FindB("Debug::Vendor", false))
633 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
634 // std::endl;
635 // Vndr = List.FindVendor(Fingerprint) != "";
636 // if (Vndr != NULL);
637 // break;
638 // }
639 // }
640 string::size_type pos;
641
642 // check for missing sigs (that where not fatal because otherwise we had
643 // bombed earlier)
644 string missingkeys;
645 string msg = _("There was no public key available for the "
646 "following key IDs:\n");
647 pos = Message.find("NO_PUBKEY ");
648 if (pos != std::string::npos)
649 {
650 string::size_type start = pos+strlen("NO_PUBKEY ");
651 string Fingerprint = Message.substr(start, Message.find("\n")-start);
652 missingkeys += (Fingerprint);
653 }
654 if(!missingkeys.empty())
655 _error->Warning("%s", string(msg+missingkeys).c_str());
656
657 string Transformed = MetaIndexParser->GetExpectedDist();
658
659 if (Transformed == "../project/experimental")
660 {
661 Transformed = "experimental";
662 }
663
664 pos = Transformed.rfind('/');
665 if (pos != string::npos)
666 {
667 Transformed = Transformed.substr(0, pos);
668 }
669
670 if (Transformed == ".")
671 {
672 Transformed = "";
673 }
674
675 if (_config->FindB("Debug::pkgAcquire::Auth", false))
676 {
677 std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
678 std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
679 std::cerr << "Transformed Dist: " << Transformed << std::endl;
680 }
681
682 if (MetaIndexParser->CheckDist(Transformed) == false)
683 {
684 // This might become fatal one day
685 // Status = StatAuthError;
686 // ErrorText = "Conflicting distribution; expected "
687 // + MetaIndexParser->GetExpectedDist() + " but got "
688 // + MetaIndexParser->GetDist();
689 // return false;
690 if (!Transformed.empty())
691 {
692 _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
693 Desc.Description.c_str(),
694 Transformed.c_str(),
695 MetaIndexParser->GetDist().c_str());
696 }
697 }
698
699 return true;
700 }
701 /*}}}*/
702 // pkgAcqMetaIndex::Failed - no Release file present or no signature
703 // file present /*{{{*/
704 // ---------------------------------------------------------------------
705 /* */
706 void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
707 {
708 if (AuthPass == true)
709 {
710 // if we fail the authentication but got the file via a IMS-Hit
711 // this means that the file wasn't downloaded and that it might be
712 // just stale (server problem, proxy etc). we delete what we have
713 // queue it again without i-m-s
714 // alternatively we could just unlink the file and let the user try again
715 if (IMSHit)
716 {
717 Complete = false;
718 Local = false;
719 AuthPass = false;
720 unlink(DestFile.c_str());
721
722 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
723 DestFile += URItoFileName(RealURI);
724 Desc.URI = RealURI;
725 QueueURI(Desc);
726 return;
727 }
728
729 // gpgv method failed
730 _error->Warning("GPG error: %s: %s",
731 Desc.Description.c_str(),
732 LookupTag(Message,"Message").c_str());
733
734 }
735
736 // No Release file was present, or verification failed, so fall
737 // back to queueing Packages files without verification
738 QueueIndexes(false);
739 }
740
741 /*}}}*/
742
743 // AcqArchive::AcqArchive - Constructor /*{{{*/
744 // ---------------------------------------------------------------------
745 /* This just sets up the initial fetch environment and queues the first
746 possibilitiy */
747 pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
748 pkgRecords *Recs,pkgCache::VerIterator const &Version,
749 string &StoreFilename) :
750 Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
751 StoreFilename(StoreFilename), Vf(Version.FileList()),
752 Trusted(false)
753 {
754 Retries = _config->FindI("Acquire::Retries",0);
755
756 if (Version.Arch() == 0)
757 {
758 _error->Error(_("I wasn't able to locate a file for the %s package. "
759 "This might mean you need to manually fix this package. "
760 "(due to missing arch)"),
761 Version.ParentPkg().Name());
762 return;
763 }
764
765 /* We need to find a filename to determine the extension. We make the
766 assumption here that all the available sources for this version share
767 the same extension.. */
768 // Skip not source sources, they do not have file fields.
769 for (; Vf.end() == false; Vf++)
770 {
771 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
772 continue;
773 break;
774 }
775
776 // Does not really matter here.. we are going to fail out below
777 if (Vf.end() != true)
778 {
779 // If this fails to get a file name we will bomb out below.
780 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
781 if (_error->PendingError() == true)
782 return;
783
784 // Generate the final file name as: package_version_arch.foo
785 StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
786 QuoteString(Version.VerStr(),"_:") + '_' +
787 QuoteString(Version.Arch(),"_:.") +
788 "." + flExtension(Parse.FileName());
789 }
790
791 // check if we have one trusted source for the package. if so, switch
792 // to "TrustedOnly" mode
793 for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
794 {
795 pkgIndexFile *Index;
796 if (Sources->FindIndex(i.File(),Index) == false)
797 continue;
798 if (_config->FindB("Debug::pkgAcquire::Auth", false))
799 {
800 std::cerr << "Checking index: " << Index->Describe()
801 << "(Trusted=" << Index->IsTrusted() << ")\n";
802 }
803 if (Index->IsTrusted()) {
804 Trusted = true;
805 break;
806 }
807 }
808
809 // "allow-unauthenticated" restores apts old fetching behaviour
810 // that means that e.g. unauthenticated file:// uris are higher
811 // priority than authenticated http:// uris
812 if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
813 Trusted = false;
814
815 // Select a source
816 if (QueueNext() == false && _error->PendingError() == false)
817 _error->Error(_("I wasn't able to locate file for the %s package. "
818 "This might mean you need to manually fix this package."),
819 Version.ParentPkg().Name());
820 }
821 /*}}}*/
822 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
823 // ---------------------------------------------------------------------
824 /* This queues the next available file version for download. It checks if
825 the archive is already available in the cache and stashs the MD5 for
826 checking later. */
827 bool pkgAcqArchive::QueueNext()
828 {
829 for (; Vf.end() == false; Vf++)
830 {
831 // Ignore not source sources
832 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
833 continue;
834
835 // Try to cross match against the source list
836 pkgIndexFile *Index;
837 if (Sources->FindIndex(Vf.File(),Index) == false)
838 continue;
839
840 // only try to get a trusted package from another source if that source
841 // is also trusted
842 if(Trusted && !Index->IsTrusted())
843 continue;
844
845 // Grab the text package record
846 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
847 if (_error->PendingError() == true)
848 return false;
849
850 string PkgFile = Parse.FileName();
851 MD5 = Parse.MD5Hash();
852 if (PkgFile.empty() == true)
853 return _error->Error(_("The package index files are corrupted. No Filename: "
854 "field for package %s."),
855 Version.ParentPkg().Name());
856
857 Desc.URI = Index->ArchiveURI(PkgFile);
858 Desc.Description = Index->ArchiveInfo(Version);
859 Desc.Owner = this;
860 Desc.ShortDesc = Version.ParentPkg().Name();
861
862 // See if we already have the file. (Legacy filenames)
863 FileSize = Version->Size;
864 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
865 struct stat Buf;
866 if (stat(FinalFile.c_str(),&Buf) == 0)
867 {
868 // Make sure the size matches
869 if ((unsigned)Buf.st_size == Version->Size)
870 {
871 Complete = true;
872 Local = true;
873 Status = StatDone;
874 StoreFilename = DestFile = FinalFile;
875 return true;
876 }
877
878 /* Hmm, we have a file and its size does not match, this means it is
879 an old style mismatched arch */
880 unlink(FinalFile.c_str());
881 }
882
883 // Check it again using the new style output filenames
884 FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
885 if (stat(FinalFile.c_str(),&Buf) == 0)
886 {
887 // Make sure the size matches
888 if ((unsigned)Buf.st_size == Version->Size)
889 {
890 Complete = true;
891 Local = true;
892 Status = StatDone;
893 StoreFilename = DestFile = FinalFile;
894 return true;
895 }
896
897 /* Hmm, we have a file and its size does not match, this shouldnt
898 happen.. */
899 unlink(FinalFile.c_str());
900 }
901
902 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
903
904 // Check the destination file
905 if (stat(DestFile.c_str(),&Buf) == 0)
906 {
907 // Hmm, the partial file is too big, erase it
908 if ((unsigned)Buf.st_size > Version->Size)
909 unlink(DestFile.c_str());
910 else
911 PartialSize = Buf.st_size;
912 }
913
914 // Create the item
915 Local = false;
916 Desc.URI = Index->ArchiveURI(PkgFile);
917 Desc.Description = Index->ArchiveInfo(Version);
918 Desc.Owner = this;
919 Desc.ShortDesc = Version.ParentPkg().Name();
920 QueueURI(Desc);
921
922 Vf++;
923 return true;
924 }
925 return false;
926 }
927 /*}}}*/
928 // AcqArchive::Done - Finished fetching /*{{{*/
929 // ---------------------------------------------------------------------
930 /* */
931 void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
932 pkgAcquire::MethodConfig *Cfg)
933 {
934 Item::Done(Message,Size,Md5Hash,Cfg);
935
936 // Check the size
937 if (Size != Version->Size)
938 {
939 Status = StatError;
940 ErrorText = _("Size mismatch");
941 return;
942 }
943
944 // Check the md5
945 if (Md5Hash.empty() == false && MD5.empty() == false)
946 {
947 if (Md5Hash != MD5)
948 {
949 Status = StatError;
950 ErrorText = _("MD5Sum mismatch");
951 if(FileExists(DestFile))
952 Rename(DestFile,DestFile + ".FAILED");
953 return;
954 }
955 }
956
957 // Grab the output filename
958 string FileName = LookupTag(Message,"Filename");
959 if (FileName.empty() == true)
960 {
961 Status = StatError;
962 ErrorText = "Method gave a blank filename";
963 return;
964 }
965
966 Complete = true;
967
968 // Reference filename
969 if (FileName != DestFile)
970 {
971 StoreFilename = DestFile = FileName;
972 Local = true;
973 return;
974 }
975
976 // Done, move it into position
977 string FinalFile = _config->FindDir("Dir::Cache::Archives");
978 FinalFile += flNotDir(StoreFilename);
979 Rename(DestFile,FinalFile);
980
981 StoreFilename = DestFile = FinalFile;
982 Complete = true;
983 }
984 /*}}}*/
985 // AcqArchive::Failed - Failure handler /*{{{*/
986 // ---------------------------------------------------------------------
987 /* Here we try other sources */
988 void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
989 {
990 ErrorText = LookupTag(Message,"Message");
991
992 /* We don't really want to retry on failed media swaps, this prevents
993 that. An interesting observation is that permanent failures are not
994 recorded. */
995 if (Cnf->Removable == true &&
996 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
997 {
998 // Vf = Version.FileList();
999 while (Vf.end() == false) Vf++;
1000 StoreFilename = string();
1001 Item::Failed(Message,Cnf);
1002 return;
1003 }
1004
1005 if (QueueNext() == false)
1006 {
1007 // This is the retry counter
1008 if (Retries != 0 &&
1009 Cnf->LocalOnly == false &&
1010 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1011 {
1012 Retries--;
1013 Vf = Version.FileList();
1014 if (QueueNext() == true)
1015 return;
1016 }
1017
1018 StoreFilename = string();
1019 Item::Failed(Message,Cnf);
1020 }
1021 }
1022 /*}}}*/
1023 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1024 // trusted source /*{{{*/
1025 // ---------------------------------------------------------------------
1026 bool pkgAcqArchive::IsTrusted()
1027 {
1028 return Trusted;
1029 }
1030
1031 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1032 // ---------------------------------------------------------------------
1033 /* */
1034 void pkgAcqArchive::Finished()
1035 {
1036 if (Status == pkgAcquire::Item::StatDone &&
1037 Complete == true)
1038 return;
1039 StoreFilename = string();
1040 }
1041 /*}}}*/
1042
1043 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1044 // ---------------------------------------------------------------------
1045 /* The file is added to the queue */
1046 pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
1047 unsigned long Size,string Dsc,string ShortDesc,
1048 const string &DestDir, const string &DestFilename) :
1049 Item(Owner), Md5Hash(MD5)
1050 {
1051 Retries = _config->FindI("Acquire::Retries",0);
1052
1053 if(!DestFilename.empty())
1054 DestFile = DestFilename;
1055 else if(!DestDir.empty())
1056 DestFile = DestDir + "/" + flNotDir(URI);
1057 else
1058 DestFile = flNotDir(URI);
1059
1060 // Create the item
1061 Desc.URI = URI;
1062 Desc.Description = Dsc;
1063 Desc.Owner = this;
1064
1065 // Set the short description to the archive component
1066 Desc.ShortDesc = ShortDesc;
1067
1068 // Get the transfer sizes
1069 FileSize = Size;
1070 struct stat Buf;
1071 if (stat(DestFile.c_str(),&Buf) == 0)
1072 {
1073 // Hmm, the partial file is too big, erase it
1074 if ((unsigned)Buf.st_size > Size)
1075 unlink(DestFile.c_str());
1076 else
1077 PartialSize = Buf.st_size;
1078 }
1079
1080 QueueURI(Desc);
1081 }
1082 /*}}}*/
1083 // AcqFile::Done - Item downloaded OK /*{{{*/
1084 // ---------------------------------------------------------------------
1085 /* */
1086 void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
1087 pkgAcquire::MethodConfig *Cnf)
1088 {
1089 // Check the md5
1090 if (Md5Hash.empty() == false && MD5.empty() == false)
1091 {
1092 if (Md5Hash != MD5)
1093 {
1094 Status = StatError;
1095 ErrorText = "MD5Sum mismatch";
1096 Rename(DestFile,DestFile + ".FAILED");
1097 return;
1098 }
1099 }
1100
1101 Item::Done(Message,Size,MD5,Cnf);
1102
1103 string FileName = LookupTag(Message,"Filename");
1104 if (FileName.empty() == true)
1105 {
1106 Status = StatError;
1107 ErrorText = "Method gave a blank filename";
1108 return;
1109 }
1110
1111 Complete = true;
1112
1113 // The files timestamp matches
1114 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
1115 return;
1116
1117 // We have to copy it into place
1118 if (FileName != DestFile)
1119 {
1120 Local = true;
1121 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
1122 Cnf->Removable == true)
1123 {
1124 Desc.URI = "copy:" + FileName;
1125 QueueURI(Desc);
1126 return;
1127 }
1128
1129 // Erase the file if it is a symlink so we can overwrite it
1130 struct stat St;
1131 if (lstat(DestFile.c_str(),&St) == 0)
1132 {
1133 if (S_ISLNK(St.st_mode) != 0)
1134 unlink(DestFile.c_str());
1135 }
1136
1137 // Symlink the file
1138 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
1139 {
1140 ErrorText = "Link to " + DestFile + " failure ";
1141 Status = StatError;
1142 Complete = false;
1143 }
1144 }
1145 }
1146 /*}}}*/
1147 // AcqFile::Failed - Failure handler /*{{{*/
1148 // ---------------------------------------------------------------------
1149 /* Here we try other sources */
1150 void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1151 {
1152 ErrorText = LookupTag(Message,"Message");
1153
1154 // This is the retry counter
1155 if (Retries != 0 &&
1156 Cnf->LocalOnly == false &&
1157 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1158 {
1159 Retries--;
1160 QueueURI(Desc);
1161 return;
1162 }
1163
1164 Item::Failed(Message,Cnf);
1165 }
1166 /*}}}*/