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