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