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