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