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