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