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