* use the new cool rred method for the patchting
[ntk/apt.git] / apt-pkg / acquire-item.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
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.
12
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>
21 #include <apt-pkg/sourcelist.h>
22 #include <apt-pkg/vendorlist.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/strutl.h>
25 #include <apt-pkg/fileutl.h>
26 #include <apt-pkg/md5.h>
27 #include <apt-pkg/sha1.h>
28 #include <apt-pkg/tagfile.h>
29
30 #include <apti18n.h>
31
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <string>
36 #include <sstream>
37 #include <stdio.h>
38 /*}}}*/
39
40 using namespace std;
41
42 // Acquire::Item::Item - Constructor /*{{{*/
43 // ---------------------------------------------------------------------
44 /* */
45 pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
46 PartialSize(0), Mode(0), ID(0), Complete(false),
47 Local(false), QueueCounter(0)
48 {
49 Owner->Add(this);
50 Status = StatIdle;
51 }
52 /*}}}*/
53 // Acquire::Item::~Item - Destructor /*{{{*/
54 // ---------------------------------------------------------------------
55 /* */
56 pkgAcquire::Item::~Item()
57 {
58 Owner->Remove(this);
59 }
60 /*}}}*/
61 // Acquire::Item::Failed - Item failed to download /*{{{*/
62 // ---------------------------------------------------------------------
63 /* We return to an idle state if there are still other queues that could
64 fetch this object */
65 void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
66 {
67 Status = StatIdle;
68 ErrorText = LookupTag(Message,"Message");
69 if (QueueCounter <= 1)
70 {
71 /* This indicates that the file is not available right now but might
72 be sometime later. If we do a retry cycle then this should be
73 retried [CDROMs] */
74 if (Cnf->LocalOnly == true &&
75 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
76 {
77 Status = StatIdle;
78 Dequeue();
79 return;
80 }
81
82 Status = StatError;
83 Dequeue();
84 }
85 }
86 /*}}}*/
87 // Acquire::Item::Start - Item has begun to download /*{{{*/
88 // ---------------------------------------------------------------------
89 /* Stash status and the file size. Note that setting Complete means
90 sub-phases of the acquire process such as decompresion are operating */
91 void pkgAcquire::Item::Start(string /*Message*/,unsigned long Size)
92 {
93 Status = StatFetching;
94 if (FileSize == 0 && Complete == false)
95 FileSize = Size;
96 }
97 /*}}}*/
98 // Acquire::Item::Done - Item downloaded OK /*{{{*/
99 // ---------------------------------------------------------------------
100 /* */
101 void pkgAcquire::Item::Done(string Message,unsigned long Size,string,
102 pkgAcquire::MethodConfig *Cnf)
103 {
104 // We just downloaded something..
105 string FileName = LookupTag(Message,"Filename");
106 if (Complete == false && FileName == DestFile)
107 {
108 if (Owner->Log != 0)
109 Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
110 }
111
112 if (FileSize == 0)
113 FileSize= Size;
114
115 Status = StatDone;
116 ErrorText = string();
117 Owner->Dequeue(this);
118 }
119 /*}}}*/
120 // Acquire::Item::Rename - Rename a file /*{{{*/
121 // ---------------------------------------------------------------------
122 /* This helper function is used by alot of item methods as thier final
123 step */
124 void pkgAcquire::Item::Rename(string From,string To)
125 {
126 if (rename(From.c_str(),To.c_str()) != 0)
127 {
128 char S[300];
129 snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
130 From.c_str(),To.c_str());
131 Status = StatError;
132 ErrorText = S;
133 }
134 }
135 /*}}}*/
136
137 // AcqIndexDiffs::AcqIndexDiffs - Constructor
138 // ---------------------------------------------------------------------
139 /* The package diff is added to the queue. one object is constructed
140 * for each diff and the index
141 */
142 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
143 string URI,string URIDesc,string ShortDesc,
144 string ExpectedMD5, vector<DiffInfo> diffs)
145 : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5),
146 available_patches(diffs)
147 {
148
149 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
150 DestFile += URItoFileName(URI);
151
152 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
153
154 Desc.Description = URIDesc;
155 Desc.Owner = this;
156 Desc.ShortDesc = ShortDesc;
157
158 CurrentPackagesFile = _config->FindDir("Dir::State::lists");
159 CurrentPackagesFile += URItoFileName(RealURI);
160
161 if(Debug) {
162 std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
163 << CurrentPackagesFile << std::endl;
164 }
165
166 if(!FileExists(CurrentPackagesFile) ||
167 !_config->FindB("Acquire::Diffs",true)) {
168 // we don't have a pkg file or we don't want to queue
169 if(Debug)
170 std::clog << "No index file or canceld by user" << std::endl;
171 Failed("", NULL);
172 return;
173 }
174
175 if(available_patches.size() == 0) {
176 State = StateFetchIndex;
177 QueueDiffIndex(URI);
178 } else {
179 State = StateFetchDiff;
180 QueueNextDiff();
181 }
182 }
183
184 void pkgAcqIndexDiffs::QueueDiffIndex(string URI)
185 {
186 Desc.URI = URI + ".diff/Index";
187 Desc.Description = Description + "IndexDiff";
188 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
189 DestFile += URItoFileName(URI) + string(".IndexDiff");
190
191 if(Debug)
192 std::clog << "QueueDiffIndex: " << Desc.URI << std::endl;
193
194 QueueURI(Desc);
195 }
196
197 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
198 // ---------------------------------------------------------------------
199 /* The only header we use is the last-modified header. */
200 string pkgAcqIndexDiffs::Custom600Headers()
201 {
202 // we only care for the IndexDiff file
203 if(State != StateFetchIndex)
204 return string("");
205
206 string Final = _config->FindDir("Dir::State::lists");
207 Final += URItoFileName(RealURI) + string(".IndexDiff");
208
209 if(Debug)
210 std::clog << "Custom600Header-IMS: " << Final << std::endl;
211
212 struct stat Buf;
213 if (stat(Final.c_str(),&Buf) != 0)
214 return "\nIndex-File: true";
215
216 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
217 }
218
219 void pkgAcqIndexDiffs::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
220 {
221 if(Debug)
222 std::clog << "Failed(): " << Desc.URI << std::endl
223 << "Falling back to big package file" << std::endl;
224 new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
225 ExpectedMD5);
226 Finish();
227 }
228
229
230 // helper that cleans the item out of the fetcher queue
231 void pkgAcqIndexDiffs::Finish(bool allDone)
232 {
233 // we restore the original name, this is required, otherwise
234 // the file will be cleaned
235 if(allDone) {
236 // this is for the "real" finish
237 DestFile = _config->FindDir("Dir::State::lists");
238 DestFile += URItoFileName(RealURI);
239 Complete = true;
240 Dequeue();
241 if(Debug)
242 std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
243 return;
244
245 }
246
247 if(Debug)
248 std::clog << "Finishing: " << Desc.URI << std::endl;
249 Complete = false;
250 Status = StatDone;
251 Dequeue();
252 return;
253 }
254
255
256
257 bool pkgAcqIndexDiffs::QueueNextDiff()
258 {
259 // calc sha1 of the just patched file
260 string FinalFile = _config->FindDir("Dir::State::lists");
261 FinalFile += URItoFileName(RealURI);
262
263 FileFd fd(FinalFile, FileFd::ReadOnly);
264 SHA1Summation SHA1;
265 SHA1.AddFD(fd.Fd(), fd.Size());
266 string local_sha1 = string(SHA1.Result());
267
268 // remove all patches until the next matching patch is found
269 // this requires the Index file to be ordered
270 for(vector<DiffInfo>::iterator I=available_patches.begin();
271 I != available_patches.end() && (*I).sha1 != local_sha1;
272 I++)
273 available_patches.erase(I);
274
275 // error checking and falling back if no patch was found
276 if(available_patches.size() == 0) {
277 Failed("", NULL);
278 return false;
279 }
280
281 // queue the right diff
282 Desc.URI = string(RealURI) + string(".diff/") + available_patches[0].file + string(".gz");
283 Desc.Description = available_patches[0].file + string(".pdiff");
284
285 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
286 DestFile += URItoFileName(RealURI + string(".diff/") + available_patches[0].file);
287
288 if(Debug)
289 std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
290
291 QueueURI(Desc);
292
293 return true;
294 }
295
296 bool pkgAcqIndexDiffs::ParseIndexDiff(string IndexDiffFile)
297 {
298 if(Debug)
299 std::clog << "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
300 << std::endl;
301
302 FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
303 pkgTagFile TF(&Fd);
304 if (_error->PendingError() == true)
305 return false;
306 pkgTagSection Tags;
307 if(TF.Step(Tags) == true)
308 {
309 string local_sha1;
310 string tmp = Tags.FindS("SHA1-Current");
311 std::stringstream ss(tmp);
312 ss >> ServerSha1;
313
314 FileFd fd(CurrentPackagesFile, FileFd::ReadOnly);
315 SHA1Summation SHA1;
316 SHA1.AddFD(fd.Fd(), fd.Size());
317 local_sha1 = string(SHA1.Result());
318
319 if(local_sha1 == ServerSha1) {
320 if(Debug)
321 std::clog << "Package file is up-to-date" << std::endl;
322 Finish(true);
323 return true;
324 }
325 if(Debug)
326 std::clog << "SHA1-Current: " << ServerSha1 << std::endl;
327
328 // check the historie and see what patches we need
329 string history = Tags.FindS("SHA1-History");
330 std::stringstream hist(history);
331 DiffInfo d;
332 string size;
333 bool found = false;
334 while(hist >> d.sha1 >> size >> d.file) {
335 d.size = atoi(size.c_str());
336 // read until the first match is found
337 if(d.sha1 == local_sha1)
338 found=true;
339 // from that point on, we probably need all diffs
340 if(found) {
341 if(Debug)
342 std::clog << "Need to get diff: " << d.file << std::endl;
343 available_patches.push_back(d);
344 }
345 }
346
347 // no information how to get the patches, bail out
348 if(!found) {
349 if(Debug)
350 std::clog << "Can't find a patch in the index file" << std::endl;
351 // Failed will queue a big package file
352 Failed("", NULL);
353 } else {
354 // queue the diffs
355 new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
356 ExpectedMD5, available_patches);
357 Finish();
358 return true;
359 }
360 }
361
362 return false;
363 }
364
365
366 void pkgAcqIndexDiffs::Done(string Message,unsigned long Size,string Md5Hash,
367 pkgAcquire::MethodConfig *Cnf)
368 {
369 if(Debug)
370 std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl;
371
372 Item::Done(Message,Size,Md5Hash,Cnf);
373
374 string FinalFile;
375 FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
376
377 // sucess in downloading the index
378 if(State == StateFetchIndex)
379 {
380 // rename the index
381 FinalFile += string(".IndexDiff");
382 if(Debug)
383 std::clog << "Renaming: " << DestFile << " -> " << FinalFile
384 << std::endl;
385 Rename(DestFile,FinalFile);
386 chmod(FinalFile.c_str(),0644);
387 DestFile = FinalFile;
388
389 if(!ParseIndexDiff(DestFile))
390 return Failed("", NULL);
391 else
392 return Finish();
393 }
394
395 // sucess in downloading a diff, enter ApplyDiff state
396 if(State == StateFetchDiff)
397 {
398
399 if(Debug)
400 std::clog << "Sending to gzip method: " << FinalFile << std::endl;
401
402 string FileName = LookupTag(Message,"Filename");
403 State = StateUnzipDiff;
404 Desc.URI = "gzip:" + FileName;
405 DestFile += ".decomp";
406 QueueURI(Desc);
407 Mode = "gzip";
408 return;
409 }
410
411 // sucess in downloading a diff, enter ApplyDiff state
412 if(State == StateUnzipDiff)
413 {
414
415 // rred excepts the patch as $FinalFile.ed
416 Rename(DestFile,FinalFile+".ed");
417
418 if(Debug)
419 std::clog << "Sending to rred method: " << FinalFile << std::endl;
420
421 State = StateApplyDiff;
422 Desc.URI = "rred:" + FinalFile;
423 QueueURI(Desc);
424 Mode = "rred";
425 return;
426 }
427
428
429 // success in download/apply a diff, queue next (if needed)
430 if(State == StateApplyDiff)
431 {
432 // remove the just applied patch
433 available_patches.erase(available_patches.begin());
434
435 // move into place
436 if(Debug)
437 std::clog << "Moving patched file in place: " << std::endl
438 << DestFile << " -> " << FinalFile << std::endl;
439 Rename(DestFile,FinalFile);
440
441 // see if there is more to download
442 if(available_patches.size() > 0) {
443 new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
444 ExpectedMD5, available_patches);
445 return Finish();
446 } else
447 return Finish(true);
448 }
449 }
450
451
452 // AcqIndex::AcqIndex - Constructor /*{{{*/
453 // ---------------------------------------------------------------------
454 /* The package file is added to the queue and a second class is
455 instantiated to fetch the revision file */
456 pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
457 string URI,string URIDesc,string ShortDesc,
458 string ExpectedMD5, string comprExt)
459 : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5)
460 {
461 Decompression = false;
462 Erase = false;
463
464 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
465 DestFile += URItoFileName(URI);
466
467 if(comprExt.empty())
468 {
469 // autoselect
470 if(FileExists("/usr/bin/bzip2"))
471 Desc.URI = URI + ".bz2";
472 else
473 Desc.URI = URI + ".gz";
474 } else {
475 Desc.URI = URI + comprExt;
476 }
477
478 Desc.Description = URIDesc;
479 Desc.Owner = this;
480 Desc.ShortDesc = ShortDesc;
481
482 QueueURI(Desc);
483 }
484 /*}}}*/
485 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
486 // ---------------------------------------------------------------------
487 /* The only header we use is the last-modified header. */
488 string pkgAcqIndex::Custom600Headers()
489 {
490 string Final = _config->FindDir("Dir::State::lists");
491 Final += URItoFileName(RealURI);
492
493 struct stat Buf;
494 if (stat(Final.c_str(),&Buf) != 0)
495 return "\nIndex-File: true";
496
497 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
498 }
499 /*}}}*/
500
501 void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
502 {
503 // no .bz2 found, retry with .gz
504 if(Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1) == "bz2") {
505 Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
506
507 // retry with a gzip one
508 new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
509 ExpectedMD5, string(".gz"));
510 Status = StatDone;
511 Complete = false;
512 Dequeue();
513 return;
514 }
515
516
517 Item::Failed(Message,Cnf);
518 }
519
520
521 // AcqIndex::Done - Finished a fetch /*{{{*/
522 // ---------------------------------------------------------------------
523 /* This goes through a number of states.. On the initial fetch the
524 method could possibly return an alternate filename which points
525 to the uncompressed version of the file. If this is so the file
526 is copied into the partial directory. In all other cases the file
527 is decompressed with a gzip uri. */
528 void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
529 pkgAcquire::MethodConfig *Cfg)
530 {
531 Item::Done(Message,Size,MD5,Cfg);
532
533 if (Decompression == true)
534 {
535 if (_config->FindB("Debug::pkgAcquire::Auth", false))
536 {
537 std::cerr << std::endl << RealURI << ": Computed MD5: " << MD5;
538 std::cerr << " Expected MD5: " << ExpectedMD5 << std::endl;
539 }
540
541 if (MD5.empty())
542 {
543 MD5Summation sum;
544 FileFd Fd(DestFile, FileFd::ReadOnly);
545 sum.AddFD(Fd.Fd(), Fd.Size());
546 Fd.Close();
547 MD5 = (string)sum.Result();
548 }
549
550 if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
551 {
552 Status = StatAuthError;
553 ErrorText = _("MD5Sum mismatch");
554 Rename(DestFile,DestFile + ".FAILED");
555 return;
556 }
557 // Done, move it into position
558 string FinalFile = _config->FindDir("Dir::State::lists");
559 FinalFile += URItoFileName(RealURI);
560 Rename(DestFile,FinalFile);
561 chmod(FinalFile.c_str(),0644);
562
563 /* We restore the original name to DestFile so that the clean operation
564 will work OK */
565 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
566 DestFile += URItoFileName(RealURI);
567
568 // Remove the compressed version.
569 if (Erase == true)
570 unlink(DestFile.c_str());
571 return;
572 }
573
574 Erase = false;
575 Complete = true;
576
577 // Handle the unzipd case
578 string FileName = LookupTag(Message,"Alt-Filename");
579 if (FileName.empty() == false)
580 {
581 // The files timestamp matches
582 if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
583 return;
584
585 Decompression = true;
586 Local = true;
587 DestFile += ".decomp";
588 Desc.URI = "copy:" + FileName;
589 QueueURI(Desc);
590 Mode = "copy";
591 return;
592 }
593
594 FileName = LookupTag(Message,"Filename");
595 if (FileName.empty() == true)
596 {
597 Status = StatError;
598 ErrorText = "Method gave a blank filename";
599 }
600
601 // The files timestamp matches
602 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
603 return;
604
605 if (FileName == DestFile)
606 Erase = true;
607 else
608 Local = true;
609
610 string compExt = Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1);
611 char *decompProg;
612 if(compExt == "bz2")
613 decompProg = "bzip2";
614 else if(compExt == ".gz")
615 decompProg = "gzip";
616 else {
617 _error->Error("Unsupported extension: %s", compExt.c_str());
618 return;
619 }
620
621 Decompression = true;
622 DestFile += ".decomp";
623 Desc.URI = string(decompProg) + ":" + FileName;
624 QueueURI(Desc);
625 Mode = decompProg;
626 }
627
628 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
629 string URI,string URIDesc,string ShortDesc,
630 string MetaIndexURI, string MetaIndexURIDesc,
631 string MetaIndexShortDesc,
632 const vector<IndexTarget*>* IndexTargets,
633 indexRecords* MetaIndexParser) :
634 Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
635 MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc)
636 {
637 this->MetaIndexParser = MetaIndexParser;
638 this->IndexTargets = IndexTargets;
639 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
640 DestFile += URItoFileName(URI);
641
642 // remove any partial downloaded sig-file. it may confuse proxies
643 // and is too small to warrant a partial download anyway
644 unlink(DestFile.c_str());
645
646 // Create the item
647 Desc.Description = URIDesc;
648 Desc.Owner = this;
649 Desc.ShortDesc = ShortDesc;
650 Desc.URI = URI;
651
652
653 string Final = _config->FindDir("Dir::State::lists");
654 Final += URItoFileName(RealURI);
655 struct stat Buf;
656 if (stat(Final.c_str(),&Buf) == 0)
657 {
658 // File was already in place. It needs to be re-verified
659 // because Release might have changed, so Move it into partial
660 Rename(Final,DestFile);
661 // unlink the file and do not try to use I-M-S and Last-Modified
662 // if the users proxy is broken
663 if(_config->FindB("Acquire::BrokenProxy", false) == true) {
664 std::cerr << "forcing re-get of the signature file as requested" << std::endl;
665 unlink(DestFile.c_str());
666 }
667 }
668
669 QueueURI(Desc);
670 }
671 /*}}}*/
672 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
673 // ---------------------------------------------------------------------
674 /* The only header we use is the last-modified header. */
675 string pkgAcqMetaSig::Custom600Headers()
676 {
677 struct stat Buf;
678 if (stat(DestFile.c_str(),&Buf) != 0)
679 return "\nIndex-File: true";
680
681 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
682 }
683
684 void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
685 pkgAcquire::MethodConfig *Cfg)
686 {
687 Item::Done(Message,Size,MD5,Cfg);
688
689 string FileName = LookupTag(Message,"Filename");
690 if (FileName.empty() == true)
691 {
692 Status = StatError;
693 ErrorText = "Method gave a blank filename";
694 return;
695 }
696
697 if (FileName != DestFile)
698 {
699 // We have to copy it into place
700 Local = true;
701 Desc.URI = "copy:" + FileName;
702 QueueURI(Desc);
703 return;
704 }
705
706 Complete = true;
707
708 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
709 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
710 DestFile, IndexTargets, MetaIndexParser);
711
712 }
713 /*}}}*/
714 void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
715 {
716 // Delete any existing sigfile, so that this source isn't
717 // mistakenly trusted
718 string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
719 unlink(Final.c_str());
720
721 // queue a pkgAcqMetaIndex with no sigfile
722 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
723 "", IndexTargets, MetaIndexParser);
724
725 if (Cnf->LocalOnly == true ||
726 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
727 {
728 // Ignore this
729 Status = StatDone;
730 Complete = false;
731 Dequeue();
732 return;
733 }
734
735 Item::Failed(Message,Cnf);
736 }
737
738 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
739 string URI,string URIDesc,string ShortDesc,
740 string SigFile,
741 const vector<struct IndexTarget*>* IndexTargets,
742 indexRecords* MetaIndexParser) :
743 Item(Owner), RealURI(URI), SigFile(SigFile)
744 {
745 this->AuthPass = false;
746 this->MetaIndexParser = MetaIndexParser;
747 this->IndexTargets = IndexTargets;
748 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
749 DestFile += URItoFileName(URI);
750
751 // Create the item
752 Desc.Description = URIDesc;
753 Desc.Owner = this;
754 Desc.ShortDesc = ShortDesc;
755 Desc.URI = URI;
756
757 QueueURI(Desc);
758 }
759
760 /*}}}*/
761 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
762 // ---------------------------------------------------------------------
763 /* The only header we use is the last-modified header. */
764 string pkgAcqMetaIndex::Custom600Headers()
765 {
766 string Final = _config->FindDir("Dir::State::lists");
767 Final += URItoFileName(RealURI);
768
769 struct stat Buf;
770 if (stat(Final.c_str(),&Buf) != 0)
771 return "\nIndex-File: true";
772
773 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
774 }
775
776 void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
777 pkgAcquire::MethodConfig *Cfg)
778 {
779 Item::Done(Message,Size,MD5,Cfg);
780
781 // MetaIndexes are done in two passes: one to download the
782 // metaindex with an appropriate method, and a second to verify it
783 // with the gpgv method
784
785 if (AuthPass == true)
786 {
787 AuthDone(Message);
788 }
789 else
790 {
791 RetrievalDone(Message);
792 if (!Complete)
793 // Still more retrieving to do
794 return;
795
796 if (SigFile == "")
797 {
798 // There was no signature file, so we are finished. Download
799 // the indexes without verification.
800 QueueIndexes(false);
801 }
802 else
803 {
804 // There was a signature file, so pass it to gpgv for
805 // verification
806
807 if (_config->FindB("Debug::pkgAcquire::Auth", false))
808 std::cerr << "Metaindex acquired, queueing gpg verification ("
809 << SigFile << "," << DestFile << ")\n";
810 AuthPass = true;
811 Desc.URI = "gpgv:" + SigFile;
812 QueueURI(Desc);
813 Mode = "gpgv";
814 }
815 }
816 }
817
818 void pkgAcqMetaIndex::RetrievalDone(string Message)
819 {
820 // We have just finished downloading a Release file (it is not
821 // verified yet)
822
823 string FileName = LookupTag(Message,"Filename");
824 if (FileName.empty() == true)
825 {
826 Status = StatError;
827 ErrorText = "Method gave a blank filename";
828 return;
829 }
830
831 if (FileName != DestFile)
832 {
833 Local = true;
834 Desc.URI = "copy:" + FileName;
835 QueueURI(Desc);
836 return;
837 }
838
839 Complete = true;
840
841 string FinalFile = _config->FindDir("Dir::State::lists");
842 FinalFile += URItoFileName(RealURI);
843
844 // The files timestamp matches
845 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
846 {
847 // Move it into position
848 Rename(DestFile,FinalFile);
849 }
850 DestFile = FinalFile;
851 }
852
853 void pkgAcqMetaIndex::AuthDone(string Message)
854 {
855 // At this point, the gpgv method has succeeded, so there is a
856 // valid signature from a key in the trusted keyring. We
857 // perform additional verification of its contents, and use them
858 // to verify the indexes we are about to download
859
860 if (!MetaIndexParser->Load(DestFile))
861 {
862 Status = StatAuthError;
863 ErrorText = MetaIndexParser->ErrorText;
864 return;
865 }
866
867 if (!VerifyVendor())
868 {
869 return;
870 }
871
872 if (_config->FindB("Debug::pkgAcquire::Auth", false))
873 std::cerr << "Signature verification succeeded: "
874 << DestFile << std::endl;
875
876 // Download further indexes with verification
877 QueueIndexes(true);
878
879 // Done, move signature file into position
880
881 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
882 URItoFileName(RealURI) + ".gpg";
883 Rename(SigFile,VerifiedSigFile);
884 chmod(VerifiedSigFile.c_str(),0644);
885 }
886
887 void pkgAcqMetaIndex::QueueIndexes(bool verify)
888 {
889 for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
890 Target != IndexTargets->end();
891 Target++)
892 {
893 string ExpectedIndexMD5;
894 if (verify)
895 {
896 const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
897 if (!Record)
898 {
899 Status = StatAuthError;
900 ErrorText = "Unable to find expected entry "
901 + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
902 return;
903 }
904 ExpectedIndexMD5 = Record->MD5Hash;
905 if (_config->FindB("Debug::pkgAcquire::Auth", false))
906 {
907 std::cerr << "Queueing: " << (*Target)->URI << std::endl;
908 std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
909 }
910 if (ExpectedIndexMD5.empty())
911 {
912 Status = StatAuthError;
913 ErrorText = "Unable to find MD5 sum for "
914 + (*Target)->MetaKey + " in Meta-index file";
915 return;
916 }
917 }
918
919 // Queue Packages file
920 new pkgAcqIndexDiffs(Owner, (*Target)->URI, (*Target)->Description,
921 (*Target)->ShortDesc, ExpectedIndexMD5);
922 }
923 }
924
925 bool pkgAcqMetaIndex::VerifyVendor()
926 {
927 // // Maybe this should be made available from above so we don't have
928 // // to read and parse it every time?
929 // pkgVendorList List;
930 // List.ReadMainList();
931
932 // const Vendor* Vndr = NULL;
933 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
934 // {
935 // string::size_type pos = (*I).find("VALIDSIG ");
936 // if (_config->FindB("Debug::Vendor", false))
937 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
938 // << std::endl;
939 // if (pos != std::string::npos)
940 // {
941 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
942 // if (_config->FindB("Debug::Vendor", false))
943 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
944 // std::endl;
945 // Vndr = List.FindVendor(Fingerprint) != "";
946 // if (Vndr != NULL);
947 // break;
948 // }
949 // }
950
951 string Transformed = MetaIndexParser->GetExpectedDist();
952
953 if (Transformed == "../project/experimental")
954 {
955 Transformed = "experimental";
956 }
957
958 string::size_type pos = Transformed.rfind('/');
959 if (pos != string::npos)
960 {
961 Transformed = Transformed.substr(0, pos);
962 }
963
964 if (Transformed == ".")
965 {
966 Transformed = "";
967 }
968
969 if (_config->FindB("Debug::pkgAcquire::Auth", false))
970 {
971 std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
972 std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
973 std::cerr << "Transformed Dist: " << Transformed << std::endl;
974 }
975
976 if (MetaIndexParser->CheckDist(Transformed) == false)
977 {
978 // This might become fatal one day
979 // Status = StatAuthError;
980 // ErrorText = "Conflicting distribution; expected "
981 // + MetaIndexParser->GetExpectedDist() + " but got "
982 // + MetaIndexParser->GetDist();
983 // return false;
984 if (!Transformed.empty())
985 {
986 _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
987 Desc.Description.c_str(),
988 Transformed.c_str(),
989 MetaIndexParser->GetDist().c_str());
990 }
991 }
992
993 return true;
994 }
995 /*}}}*/
996 // pkgAcqMetaIndex::Failed - no Release file present or no signature
997 // file present /*{{{*/
998 // ---------------------------------------------------------------------
999 /* */
1000 void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1001 {
1002 if (AuthPass == true)
1003 {
1004 // gpgv method failed
1005 _error->Warning("GPG error: %s: %s",
1006 Desc.Description.c_str(),
1007 LookupTag(Message,"Message").c_str());
1008 }
1009
1010 // No Release file was present, or verification failed, so fall
1011 // back to queueing Packages files without verification
1012 QueueIndexes(false);
1013 }
1014
1015 /*}}}*/
1016
1017 // AcqArchive::AcqArchive - Constructor /*{{{*/
1018 // ---------------------------------------------------------------------
1019 /* This just sets up the initial fetch environment and queues the first
1020 possibilitiy */
1021 pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
1022 pkgRecords *Recs,pkgCache::VerIterator const &Version,
1023 string &StoreFilename) :
1024 Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
1025 StoreFilename(StoreFilename), Vf(Version.FileList()),
1026 Trusted(false)
1027 {
1028 Retries = _config->FindI("Acquire::Retries",0);
1029
1030 if (Version.Arch() == 0)
1031 {
1032 _error->Error(_("I wasn't able to locate a file for the %s package. "
1033 "This might mean you need to manually fix this package. "
1034 "(due to missing arch)"),
1035 Version.ParentPkg().Name());
1036 return;
1037 }
1038
1039 /* We need to find a filename to determine the extension. We make the
1040 assumption here that all the available sources for this version share
1041 the same extension.. */
1042 // Skip not source sources, they do not have file fields.
1043 for (; Vf.end() == false; Vf++)
1044 {
1045 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1046 continue;
1047 break;
1048 }
1049
1050 // Does not really matter here.. we are going to fail out below
1051 if (Vf.end() != true)
1052 {
1053 // If this fails to get a file name we will bomb out below.
1054 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
1055 if (_error->PendingError() == true)
1056 return;
1057
1058 // Generate the final file name as: package_version_arch.foo
1059 StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
1060 QuoteString(Version.VerStr(),"_:") + '_' +
1061 QuoteString(Version.Arch(),"_:.") +
1062 "." + flExtension(Parse.FileName());
1063 }
1064
1065 // check if we have one trusted source for the package. if so, switch
1066 // to "TrustedOnly" mode
1067 for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
1068 {
1069 pkgIndexFile *Index;
1070 if (Sources->FindIndex(i.File(),Index) == false)
1071 continue;
1072 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1073 {
1074 std::cerr << "Checking index: " << Index->Describe()
1075 << "(Trusted=" << Index->IsTrusted() << ")\n";
1076 }
1077 if (Index->IsTrusted()) {
1078 Trusted = true;
1079 break;
1080 }
1081 }
1082
1083 // Select a source
1084 if (QueueNext() == false && _error->PendingError() == false)
1085 _error->Error(_("I wasn't able to locate file for the %s package. "
1086 "This might mean you need to manually fix this package."),
1087 Version.ParentPkg().Name());
1088 }
1089 /*}}}*/
1090 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1091 // ---------------------------------------------------------------------
1092 /* This queues the next available file version for download. It checks if
1093 the archive is already available in the cache and stashs the MD5 for
1094 checking later. */
1095 bool pkgAcqArchive::QueueNext()
1096 {
1097 for (; Vf.end() == false; Vf++)
1098 {
1099 // Ignore not source sources
1100 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1101 continue;
1102
1103 // Try to cross match against the source list
1104 pkgIndexFile *Index;
1105 if (Sources->FindIndex(Vf.File(),Index) == false)
1106 continue;
1107
1108 // only try to get a trusted package from another source if that source
1109 // is also trusted
1110 if(Trusted && !Index->IsTrusted())
1111 continue;
1112
1113 // Grab the text package record
1114 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
1115 if (_error->PendingError() == true)
1116 return false;
1117
1118 string PkgFile = Parse.FileName();
1119 MD5 = Parse.MD5Hash();
1120 if (PkgFile.empty() == true)
1121 return _error->Error(_("The package index files are corrupted. No Filename: "
1122 "field for package %s."),
1123 Version.ParentPkg().Name());
1124
1125 Desc.URI = Index->ArchiveURI(PkgFile);
1126 Desc.Description = Index->ArchiveInfo(Version);
1127 Desc.Owner = this;
1128 Desc.ShortDesc = Version.ParentPkg().Name();
1129
1130 // See if we already have the file. (Legacy filenames)
1131 FileSize = Version->Size;
1132 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
1133 struct stat Buf;
1134 if (stat(FinalFile.c_str(),&Buf) == 0)
1135 {
1136 // Make sure the size matches
1137 if ((unsigned)Buf.st_size == Version->Size)
1138 {
1139 Complete = true;
1140 Local = true;
1141 Status = StatDone;
1142 StoreFilename = DestFile = FinalFile;
1143 return true;
1144 }
1145
1146 /* Hmm, we have a file and its size does not match, this means it is
1147 an old style mismatched arch */
1148 unlink(FinalFile.c_str());
1149 }
1150
1151 // Check it again using the new style output filenames
1152 FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
1153 if (stat(FinalFile.c_str(),&Buf) == 0)
1154 {
1155 // Make sure the size matches
1156 if ((unsigned)Buf.st_size == Version->Size)
1157 {
1158 Complete = true;
1159 Local = true;
1160 Status = StatDone;
1161 StoreFilename = DestFile = FinalFile;
1162 return true;
1163 }
1164
1165 /* Hmm, we have a file and its size does not match, this shouldnt
1166 happen.. */
1167 unlink(FinalFile.c_str());
1168 }
1169
1170 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
1171
1172 // Check the destination file
1173 if (stat(DestFile.c_str(),&Buf) == 0)
1174 {
1175 // Hmm, the partial file is too big, erase it
1176 if ((unsigned)Buf.st_size > Version->Size)
1177 unlink(DestFile.c_str());
1178 else
1179 PartialSize = Buf.st_size;
1180 }
1181
1182 // Create the item
1183 Local = false;
1184 Desc.URI = Index->ArchiveURI(PkgFile);
1185 Desc.Description = Index->ArchiveInfo(Version);
1186 Desc.Owner = this;
1187 Desc.ShortDesc = Version.ParentPkg().Name();
1188 QueueURI(Desc);
1189
1190 Vf++;
1191 return true;
1192 }
1193 return false;
1194 }
1195 /*}}}*/
1196 // AcqArchive::Done - Finished fetching /*{{{*/
1197 // ---------------------------------------------------------------------
1198 /* */
1199 void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
1200 pkgAcquire::MethodConfig *Cfg)
1201 {
1202 Item::Done(Message,Size,Md5Hash,Cfg);
1203
1204 // Check the size
1205 if (Size != Version->Size)
1206 {
1207 Status = StatError;
1208 ErrorText = _("Size mismatch");
1209 return;
1210 }
1211
1212 // Check the md5
1213 if (Md5Hash.empty() == false && MD5.empty() == false)
1214 {
1215 if (Md5Hash != MD5)
1216 {
1217 Status = StatError;
1218 ErrorText = _("MD5Sum mismatch");
1219 Rename(DestFile,DestFile + ".FAILED");
1220 return;
1221 }
1222 }
1223
1224 // Grab the output filename
1225 string FileName = LookupTag(Message,"Filename");
1226 if (FileName.empty() == true)
1227 {
1228 Status = StatError;
1229 ErrorText = "Method gave a blank filename";
1230 return;
1231 }
1232
1233 Complete = true;
1234
1235 // Reference filename
1236 if (FileName != DestFile)
1237 {
1238 StoreFilename = DestFile = FileName;
1239 Local = true;
1240 return;
1241 }
1242
1243 // Done, move it into position
1244 string FinalFile = _config->FindDir("Dir::Cache::Archives");
1245 FinalFile += flNotDir(StoreFilename);
1246 Rename(DestFile,FinalFile);
1247
1248 StoreFilename = DestFile = FinalFile;
1249 Complete = true;
1250 }
1251 /*}}}*/
1252 // AcqArchive::Failed - Failure handler /*{{{*/
1253 // ---------------------------------------------------------------------
1254 /* Here we try other sources */
1255 void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1256 {
1257 ErrorText = LookupTag(Message,"Message");
1258
1259 /* We don't really want to retry on failed media swaps, this prevents
1260 that. An interesting observation is that permanent failures are not
1261 recorded. */
1262 if (Cnf->Removable == true &&
1263 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1264 {
1265 // Vf = Version.FileList();
1266 while (Vf.end() == false) Vf++;
1267 StoreFilename = string();
1268 Item::Failed(Message,Cnf);
1269 return;
1270 }
1271
1272 if (QueueNext() == false)
1273 {
1274 // This is the retry counter
1275 if (Retries != 0 &&
1276 Cnf->LocalOnly == false &&
1277 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1278 {
1279 Retries--;
1280 Vf = Version.FileList();
1281 if (QueueNext() == true)
1282 return;
1283 }
1284
1285 StoreFilename = string();
1286 Item::Failed(Message,Cnf);
1287 }
1288 }
1289 /*}}}*/
1290 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1291 // trusted source /*{{{*/
1292 // ---------------------------------------------------------------------
1293 bool pkgAcqArchive::IsTrusted()
1294 {
1295 return Trusted;
1296 }
1297
1298 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1299 // ---------------------------------------------------------------------
1300 /* */
1301 void pkgAcqArchive::Finished()
1302 {
1303 if (Status == pkgAcquire::Item::StatDone &&
1304 Complete == true)
1305 return;
1306 StoreFilename = string();
1307 }
1308 /*}}}*/
1309
1310 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1311 // ---------------------------------------------------------------------
1312 /* The file is added to the queue */
1313 pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
1314 unsigned long Size,string Dsc,string ShortDesc) :
1315 Item(Owner), Md5Hash(MD5)
1316 {
1317 Retries = _config->FindI("Acquire::Retries",0);
1318
1319 DestFile = flNotDir(URI);
1320
1321 // Create the item
1322 Desc.URI = URI;
1323 Desc.Description = Dsc;
1324 Desc.Owner = this;
1325
1326 // Set the short description to the archive component
1327 Desc.ShortDesc = ShortDesc;
1328
1329 // Get the transfer sizes
1330 FileSize = Size;
1331 struct stat Buf;
1332 if (stat(DestFile.c_str(),&Buf) == 0)
1333 {
1334 // Hmm, the partial file is too big, erase it
1335 if ((unsigned)Buf.st_size > Size)
1336 unlink(DestFile.c_str());
1337 else
1338 PartialSize = Buf.st_size;
1339 }
1340
1341 QueueURI(Desc);
1342 }
1343 /*}}}*/
1344 // AcqFile::Done - Item downloaded OK /*{{{*/
1345 // ---------------------------------------------------------------------
1346 /* */
1347 void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
1348 pkgAcquire::MethodConfig *Cnf)
1349 {
1350 // Check the md5
1351 if (Md5Hash.empty() == false && MD5.empty() == false)
1352 {
1353 if (Md5Hash != MD5)
1354 {
1355 Status = StatError;
1356 ErrorText = "MD5Sum mismatch";
1357 Rename(DestFile,DestFile + ".FAILED");
1358 return;
1359 }
1360 }
1361
1362 Item::Done(Message,Size,MD5,Cnf);
1363
1364 string FileName = LookupTag(Message,"Filename");
1365 if (FileName.empty() == true)
1366 {
1367 Status = StatError;
1368 ErrorText = "Method gave a blank filename";
1369 return;
1370 }
1371
1372 Complete = true;
1373
1374 // The files timestamp matches
1375 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
1376 return;
1377
1378 // We have to copy it into place
1379 if (FileName != DestFile)
1380 {
1381 Local = true;
1382 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
1383 Cnf->Removable == true)
1384 {
1385 Desc.URI = "copy:" + FileName;
1386 QueueURI(Desc);
1387 return;
1388 }
1389
1390 // Erase the file if it is a symlink so we can overwrite it
1391 struct stat St;
1392 if (lstat(DestFile.c_str(),&St) == 0)
1393 {
1394 if (S_ISLNK(St.st_mode) != 0)
1395 unlink(DestFile.c_str());
1396 }
1397
1398 // Symlink the file
1399 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
1400 {
1401 ErrorText = "Link to " + DestFile + " failure ";
1402 Status = StatError;
1403 Complete = false;
1404 }
1405 }
1406 }
1407 /*}}}*/
1408 // AcqFile::Failed - Failure handler /*{{{*/
1409 // ---------------------------------------------------------------------
1410 /* Here we try other sources */
1411 void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1412 {
1413 ErrorText = LookupTag(Message,"Message");
1414
1415 // This is the retry counter
1416 if (Retries != 0 &&
1417 Cnf->LocalOnly == false &&
1418 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1419 {
1420 Retries--;
1421 QueueURI(Desc);
1422 return;
1423 }
1424
1425 Item::Failed(Message,Cnf);
1426 }
1427 /*}}}*/