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