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