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