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