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