]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-item.cc
* hack around local file:/ uri problem
[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 aquire" << 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,Desc.URI.size()-1) == "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,Desc.URI.size()-1);
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 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
715 string URI,string URIDesc,string ShortDesc,
716 string MetaIndexURI, string MetaIndexURIDesc,
717 string MetaIndexShortDesc,
718 const vector<IndexTarget*>* IndexTargets,
719 indexRecords* MetaIndexParser) :
720 Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
721 MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc)
722 {
723 this->MetaIndexParser = MetaIndexParser;
724 this->IndexTargets = IndexTargets;
725 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
726 DestFile += URItoFileName(URI);
727
728 // remove any partial downloaded sig-file. it may confuse proxies
729 // and is too small to warrant a partial download anyway
730 unlink(DestFile.c_str());
731
732 // Create the item
733 Desc.Description = URIDesc;
734 Desc.Owner = this;
735 Desc.ShortDesc = ShortDesc;
736 Desc.URI = URI;
737
738
739 string Final = _config->FindDir("Dir::State::lists");
740 Final += URItoFileName(RealURI);
741 struct stat Buf;
742 if (stat(Final.c_str(),&Buf) == 0)
743 {
744 // File was already in place. It needs to be re-verified
745 // because Release might have changed, so Move it into partial
746 Rename(Final,DestFile);
747 // unlink the file and do not try to use I-M-S and Last-Modified
748 // if the users proxy is broken
749 if(_config->FindB("Acquire::BrokenProxy", false) == true) {
750 std::cerr << "forcing re-get of the signature file as requested" << std::endl;
751 unlink(DestFile.c_str());
752 }
753 }
754
755 QueueURI(Desc);
756 }
757 /*}}}*/
758 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
759 // ---------------------------------------------------------------------
760 /* The only header we use is the last-modified header. */
761 string pkgAcqMetaSig::Custom600Headers()
762 {
763 struct stat Buf;
764 if (stat(DestFile.c_str(),&Buf) != 0)
765 return "\nIndex-File: true";
766
767 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
768 }
769
770 void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
771 pkgAcquire::MethodConfig *Cfg)
772 {
773 Item::Done(Message,Size,MD5,Cfg);
774
775 string FileName = LookupTag(Message,"Filename");
776 if (FileName.empty() == true)
777 {
778 Status = StatError;
779 ErrorText = "Method gave a blank filename";
780 return;
781 }
782
783 if (FileName != DestFile)
784 {
785 // We have to copy it into place
786 Local = true;
787 Desc.URI = "copy:" + FileName;
788 QueueURI(Desc);
789 return;
790 }
791
792 Complete = true;
793
794 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
795 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
796 DestFile, IndexTargets, MetaIndexParser);
797
798 }
799 /*}}}*/
800 void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
801 {
802 // Delete any existing sigfile, so that this source isn't
803 // mistakenly trusted
804 string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
805 unlink(Final.c_str());
806
807 // if we get a timeout if fail
808 if(LookupTag(Message,"FailReason") == "Timeout" ||
809 LookupTag(Message,"FailReason") == "TmpResolveFailure") {
810 Item::Failed(Message,Cnf);
811 return;
812 }
813
814 // queue a pkgAcqMetaIndex with no sigfile
815 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
816 "", IndexTargets, MetaIndexParser);
817
818 if (Cnf->LocalOnly == true ||
819 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
820 {
821 // Ignore this
822 Status = StatDone;
823 Complete = false;
824 Dequeue();
825 return;
826 }
827
828 Item::Failed(Message,Cnf);
829 }
830
831 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
832 string URI,string URIDesc,string ShortDesc,
833 string SigFile,
834 const vector<struct IndexTarget*>* IndexTargets,
835 indexRecords* MetaIndexParser) :
836 Item(Owner), RealURI(URI), SigFile(SigFile)
837 {
838 this->AuthPass = false;
839 this->MetaIndexParser = MetaIndexParser;
840 this->IndexTargets = IndexTargets;
841 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
842 DestFile += URItoFileName(URI);
843
844 // Create the item
845 Desc.Description = URIDesc;
846 Desc.Owner = this;
847 Desc.ShortDesc = ShortDesc;
848 Desc.URI = URI;
849
850 QueueURI(Desc);
851 }
852
853 /*}}}*/
854 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
855 // ---------------------------------------------------------------------
856 /* The only header we use is the last-modified header. */
857 string pkgAcqMetaIndex::Custom600Headers()
858 {
859 string Final = _config->FindDir("Dir::State::lists");
860 Final += URItoFileName(RealURI);
861
862 struct stat Buf;
863 if (stat(Final.c_str(),&Buf) != 0)
864 return "\nIndex-File: true";
865
866 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
867 }
868
869 void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
870 pkgAcquire::MethodConfig *Cfg)
871 {
872 Item::Done(Message,Size,MD5,Cfg);
873
874 // MetaIndexes are done in two passes: one to download the
875 // metaindex with an appropriate method, and a second to verify it
876 // with the gpgv method
877
878 if (AuthPass == true)
879 {
880 AuthDone(Message);
881 }
882 else
883 {
884 RetrievalDone(Message);
885 if (!Complete)
886 // Still more retrieving to do
887 return;
888
889 if (SigFile == "")
890 {
891 // There was no signature file, so we are finished. Download
892 // the indexes without verification.
893 QueueIndexes(false);
894 }
895 else
896 {
897 // There was a signature file, so pass it to gpgv for
898 // verification
899
900 if (_config->FindB("Debug::pkgAcquire::Auth", false))
901 std::cerr << "Metaindex acquired, queueing gpg verification ("
902 << SigFile << "," << DestFile << ")\n";
903 AuthPass = true;
904 Desc.URI = "gpgv:" + SigFile;
905 QueueURI(Desc);
906 Mode = "gpgv";
907 }
908 }
909 }
910
911 void pkgAcqMetaIndex::RetrievalDone(string Message)
912 {
913 // We have just finished downloading a Release file (it is not
914 // verified yet)
915
916 string FileName = LookupTag(Message,"Filename");
917 if (FileName.empty() == true)
918 {
919 Status = StatError;
920 ErrorText = "Method gave a blank filename";
921 return;
922 }
923
924 if (FileName != DestFile)
925 {
926 Local = true;
927 Desc.URI = "copy:" + FileName;
928 QueueURI(Desc);
929 return;
930 }
931
932 Complete = true;
933
934 string FinalFile = _config->FindDir("Dir::State::lists");
935 FinalFile += URItoFileName(RealURI);
936
937 // The files timestamp matches
938 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
939 {
940 // Move it into position
941 Rename(DestFile,FinalFile);
942 }
943 DestFile = FinalFile;
944 }
945
946 void pkgAcqMetaIndex::AuthDone(string Message)
947 {
948 // At this point, the gpgv method has succeeded, so there is a
949 // valid signature from a key in the trusted keyring. We
950 // perform additional verification of its contents, and use them
951 // to verify the indexes we are about to download
952
953 if (!MetaIndexParser->Load(DestFile))
954 {
955 Status = StatAuthError;
956 ErrorText = MetaIndexParser->ErrorText;
957 return;
958 }
959
960 if (!VerifyVendor())
961 {
962 return;
963 }
964
965 if (_config->FindB("Debug::pkgAcquire::Auth", false))
966 std::cerr << "Signature verification succeeded: "
967 << DestFile << std::endl;
968
969 // Download further indexes with verification
970 QueueIndexes(true);
971
972 // Done, move signature file into position
973
974 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
975 URItoFileName(RealURI) + ".gpg";
976 Rename(SigFile,VerifiedSigFile);
977 chmod(VerifiedSigFile.c_str(),0644);
978 }
979
980 void pkgAcqMetaIndex::QueueIndexes(bool verify)
981 {
982 for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
983 Target != IndexTargets->end();
984 Target++)
985 {
986 string ExpectedIndexMD5;
987 if (verify)
988 {
989 const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
990 if (!Record)
991 {
992 Status = StatAuthError;
993 ErrorText = "Unable to find expected entry "
994 + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
995 return;
996 }
997 ExpectedIndexMD5 = Record->MD5Hash;
998 if (_config->FindB("Debug::pkgAcquire::Auth", false))
999 {
1000 std::cerr << "Queueing: " << (*Target)->URI << std::endl;
1001 std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
1002 }
1003 if (ExpectedIndexMD5.empty())
1004 {
1005 Status = StatAuthError;
1006 ErrorText = "Unable to find MD5 sum for "
1007 + (*Target)->MetaKey + " in Meta-index file";
1008 return;
1009 }
1010 }
1011
1012 // Queue Packages file
1013 new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
1014 (*Target)->ShortDesc, ExpectedIndexMD5);
1015 }
1016 }
1017
1018 bool pkgAcqMetaIndex::VerifyVendor()
1019 {
1020 // // Maybe this should be made available from above so we don't have
1021 // // to read and parse it every time?
1022 // pkgVendorList List;
1023 // List.ReadMainList();
1024
1025 // const Vendor* Vndr = NULL;
1026 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1027 // {
1028 // string::size_type pos = (*I).find("VALIDSIG ");
1029 // if (_config->FindB("Debug::Vendor", false))
1030 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1031 // << std::endl;
1032 // if (pos != std::string::npos)
1033 // {
1034 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1035 // if (_config->FindB("Debug::Vendor", false))
1036 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1037 // std::endl;
1038 // Vndr = List.FindVendor(Fingerprint) != "";
1039 // if (Vndr != NULL);
1040 // break;
1041 // }
1042 // }
1043
1044 string Transformed = MetaIndexParser->GetExpectedDist();
1045
1046 if (Transformed == "../project/experimental")
1047 {
1048 Transformed = "experimental";
1049 }
1050
1051 string::size_type pos = Transformed.rfind('/');
1052 if (pos != string::npos)
1053 {
1054 Transformed = Transformed.substr(0, pos);
1055 }
1056
1057 if (Transformed == ".")
1058 {
1059 Transformed = "";
1060 }
1061
1062 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1063 {
1064 std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
1065 std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
1066 std::cerr << "Transformed Dist: " << Transformed << std::endl;
1067 }
1068
1069 if (MetaIndexParser->CheckDist(Transformed) == false)
1070 {
1071 // This might become fatal one day
1072 // Status = StatAuthError;
1073 // ErrorText = "Conflicting distribution; expected "
1074 // + MetaIndexParser->GetExpectedDist() + " but got "
1075 // + MetaIndexParser->GetDist();
1076 // return false;
1077 if (!Transformed.empty())
1078 {
1079 _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
1080 Desc.Description.c_str(),
1081 Transformed.c_str(),
1082 MetaIndexParser->GetDist().c_str());
1083 }
1084 }
1085
1086 return true;
1087 }
1088 /*}}}*/
1089 // pkgAcqMetaIndex::Failed - no Release file present or no signature
1090 // file present /*{{{*/
1091 // ---------------------------------------------------------------------
1092 /* */
1093 void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1094 {
1095 if (AuthPass == true)
1096 {
1097 // gpgv method failed
1098 _error->Warning("GPG error: %s: %s",
1099 Desc.Description.c_str(),
1100 LookupTag(Message,"Message").c_str());
1101 }
1102
1103 // No Release file was present, or verification failed, so fall
1104 // back to queueing Packages files without verification
1105 QueueIndexes(false);
1106 }
1107
1108 /*}}}*/
1109
1110 // AcqArchive::AcqArchive - Constructor /*{{{*/
1111 // ---------------------------------------------------------------------
1112 /* This just sets up the initial fetch environment and queues the first
1113 possibilitiy */
1114 pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
1115 pkgRecords *Recs,pkgCache::VerIterator const &Version,
1116 string &StoreFilename) :
1117 Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
1118 StoreFilename(StoreFilename), Vf(Version.FileList()),
1119 Trusted(false)
1120 {
1121 Retries = _config->FindI("Acquire::Retries",0);
1122
1123 if (Version.Arch() == 0)
1124 {
1125 _error->Error(_("I wasn't able to locate a file for the %s package. "
1126 "This might mean you need to manually fix this package. "
1127 "(due to missing arch)"),
1128 Version.ParentPkg().Name());
1129 return;
1130 }
1131
1132 /* We need to find a filename to determine the extension. We make the
1133 assumption here that all the available sources for this version share
1134 the same extension.. */
1135 // Skip not source sources, they do not have file fields.
1136 for (; Vf.end() == false; Vf++)
1137 {
1138 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1139 continue;
1140 break;
1141 }
1142
1143 // Does not really matter here.. we are going to fail out below
1144 if (Vf.end() != true)
1145 {
1146 // If this fails to get a file name we will bomb out below.
1147 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
1148 if (_error->PendingError() == true)
1149 return;
1150
1151 // Generate the final file name as: package_version_arch.foo
1152 StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
1153 QuoteString(Version.VerStr(),"_:") + '_' +
1154 QuoteString(Version.Arch(),"_:.") +
1155 "." + flExtension(Parse.FileName());
1156 }
1157
1158 // check if we have one trusted source for the package. if so, switch
1159 // to "TrustedOnly" mode
1160 for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
1161 {
1162 pkgIndexFile *Index;
1163 if (Sources->FindIndex(i.File(),Index) == false)
1164 continue;
1165 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1166 {
1167 std::cerr << "Checking index: " << Index->Describe()
1168 << "(Trusted=" << Index->IsTrusted() << ")\n";
1169 }
1170 if (Index->IsTrusted()) {
1171 Trusted = true;
1172 break;
1173 }
1174 }
1175
1176 // Select a source
1177 if (QueueNext() == false && _error->PendingError() == false)
1178 _error->Error(_("I wasn't able to locate file for the %s package. "
1179 "This might mean you need to manually fix this package."),
1180 Version.ParentPkg().Name());
1181 }
1182 /*}}}*/
1183 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1184 // ---------------------------------------------------------------------
1185 /* This queues the next available file version for download. It checks if
1186 the archive is already available in the cache and stashs the MD5 for
1187 checking later. */
1188 bool pkgAcqArchive::QueueNext()
1189 {
1190 for (; Vf.end() == false; Vf++)
1191 {
1192 // Ignore not source sources
1193 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1194 continue;
1195
1196 // Try to cross match against the source list
1197 pkgIndexFile *Index;
1198 if (Sources->FindIndex(Vf.File(),Index) == false)
1199 continue;
1200
1201 // only try to get a trusted package from another source if that source
1202 // is also trusted
1203 if(Trusted && !Index->IsTrusted())
1204 continue;
1205
1206 // Grab the text package record
1207 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
1208 if (_error->PendingError() == true)
1209 return false;
1210
1211 string PkgFile = Parse.FileName();
1212 MD5 = Parse.MD5Hash();
1213 if (PkgFile.empty() == true)
1214 return _error->Error(_("The package index files are corrupted. No Filename: "
1215 "field for package %s."),
1216 Version.ParentPkg().Name());
1217
1218 Desc.URI = Index->ArchiveURI(PkgFile);
1219 Desc.Description = Index->ArchiveInfo(Version);
1220 Desc.Owner = this;
1221 Desc.ShortDesc = Version.ParentPkg().Name();
1222
1223 // See if we already have the file. (Legacy filenames)
1224 FileSize = Version->Size;
1225 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
1226 struct stat Buf;
1227 if (stat(FinalFile.c_str(),&Buf) == 0)
1228 {
1229 // Make sure the size matches
1230 if ((unsigned)Buf.st_size == Version->Size)
1231 {
1232 Complete = true;
1233 Local = true;
1234 Status = StatDone;
1235 StoreFilename = DestFile = FinalFile;
1236 return true;
1237 }
1238
1239 /* Hmm, we have a file and its size does not match, this means it is
1240 an old style mismatched arch */
1241 unlink(FinalFile.c_str());
1242 }
1243
1244 // Check it again using the new style output filenames
1245 FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
1246 if (stat(FinalFile.c_str(),&Buf) == 0)
1247 {
1248 // Make sure the size matches
1249 if ((unsigned)Buf.st_size == Version->Size)
1250 {
1251 Complete = true;
1252 Local = true;
1253 Status = StatDone;
1254 StoreFilename = DestFile = FinalFile;
1255 return true;
1256 }
1257
1258 /* Hmm, we have a file and its size does not match, this shouldnt
1259 happen.. */
1260 unlink(FinalFile.c_str());
1261 }
1262
1263 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
1264
1265 // Check the destination file
1266 if (stat(DestFile.c_str(),&Buf) == 0)
1267 {
1268 // Hmm, the partial file is too big, erase it
1269 if ((unsigned)Buf.st_size > Version->Size)
1270 unlink(DestFile.c_str());
1271 else
1272 PartialSize = Buf.st_size;
1273 }
1274
1275 // Create the item
1276 Local = false;
1277 Desc.URI = Index->ArchiveURI(PkgFile);
1278 Desc.Description = Index->ArchiveInfo(Version);
1279 Desc.Owner = this;
1280 Desc.ShortDesc = Version.ParentPkg().Name();
1281 QueueURI(Desc);
1282
1283 Vf++;
1284 return true;
1285 }
1286 return false;
1287 }
1288 /*}}}*/
1289 // AcqArchive::Done - Finished fetching /*{{{*/
1290 // ---------------------------------------------------------------------
1291 /* */
1292 void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
1293 pkgAcquire::MethodConfig *Cfg)
1294 {
1295 Item::Done(Message,Size,Md5Hash,Cfg);
1296
1297 // Check the size
1298 if (Size != Version->Size)
1299 {
1300 Status = StatError;
1301 ErrorText = _("Size mismatch");
1302 return;
1303 }
1304
1305 // Check the md5
1306 if (Md5Hash.empty() == false && MD5.empty() == false)
1307 {
1308 if (Md5Hash != MD5)
1309 {
1310 Status = StatError;
1311 ErrorText = _("MD5Sum mismatch");
1312 if(FileExists(DestFile))
1313 Rename(DestFile,DestFile + ".FAILED");
1314 return;
1315 }
1316 }
1317
1318 // Grab the output filename
1319 string FileName = LookupTag(Message,"Filename");
1320 if (FileName.empty() == true)
1321 {
1322 Status = StatError;
1323 ErrorText = "Method gave a blank filename";
1324 return;
1325 }
1326
1327 Complete = true;
1328
1329 // Reference filename
1330 if (FileName != DestFile)
1331 {
1332 StoreFilename = DestFile = FileName;
1333 Local = true;
1334 return;
1335 }
1336
1337 // Done, move it into position
1338 string FinalFile = _config->FindDir("Dir::Cache::Archives");
1339 FinalFile += flNotDir(StoreFilename);
1340 Rename(DestFile,FinalFile);
1341
1342 StoreFilename = DestFile = FinalFile;
1343 Complete = true;
1344 }
1345 /*}}}*/
1346 // AcqArchive::Failed - Failure handler /*{{{*/
1347 // ---------------------------------------------------------------------
1348 /* Here we try other sources */
1349 void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1350 {
1351 ErrorText = LookupTag(Message,"Message");
1352
1353 /* We don't really want to retry on failed media swaps, this prevents
1354 that. An interesting observation is that permanent failures are not
1355 recorded. */
1356 if (Cnf->Removable == true &&
1357 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1358 {
1359 // Vf = Version.FileList();
1360 while (Vf.end() == false) Vf++;
1361 StoreFilename = string();
1362 Item::Failed(Message,Cnf);
1363 return;
1364 }
1365
1366 if (QueueNext() == false)
1367 {
1368 // This is the retry counter
1369 if (Retries != 0 &&
1370 Cnf->LocalOnly == false &&
1371 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1372 {
1373 Retries--;
1374 Vf = Version.FileList();
1375 if (QueueNext() == true)
1376 return;
1377 }
1378
1379 StoreFilename = string();
1380 Item::Failed(Message,Cnf);
1381 }
1382 }
1383 /*}}}*/
1384 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1385 // trusted source /*{{{*/
1386 // ---------------------------------------------------------------------
1387 bool pkgAcqArchive::IsTrusted()
1388 {
1389 return Trusted;
1390 }
1391
1392 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1393 // ---------------------------------------------------------------------
1394 /* */
1395 void pkgAcqArchive::Finished()
1396 {
1397 if (Status == pkgAcquire::Item::StatDone &&
1398 Complete == true)
1399 return;
1400 StoreFilename = string();
1401 }
1402 /*}}}*/
1403
1404 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1405 // ---------------------------------------------------------------------
1406 /* The file is added to the queue */
1407 pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
1408 unsigned long Size,string Dsc,string ShortDesc) :
1409 Item(Owner), Md5Hash(MD5)
1410 {
1411 Retries = _config->FindI("Acquire::Retries",0);
1412
1413 DestFile = flNotDir(URI);
1414
1415 // Create the item
1416 Desc.URI = URI;
1417 Desc.Description = Dsc;
1418 Desc.Owner = this;
1419
1420 // Set the short description to the archive component
1421 Desc.ShortDesc = ShortDesc;
1422
1423 // Get the transfer sizes
1424 FileSize = Size;
1425 struct stat Buf;
1426 if (stat(DestFile.c_str(),&Buf) == 0)
1427 {
1428 // Hmm, the partial file is too big, erase it
1429 if ((unsigned)Buf.st_size > Size)
1430 unlink(DestFile.c_str());
1431 else
1432 PartialSize = Buf.st_size;
1433 }
1434
1435 QueueURI(Desc);
1436 }
1437 /*}}}*/
1438 // AcqFile::Done - Item downloaded OK /*{{{*/
1439 // ---------------------------------------------------------------------
1440 /* */
1441 void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
1442 pkgAcquire::MethodConfig *Cnf)
1443 {
1444 // Check the md5
1445 if (Md5Hash.empty() == false && MD5.empty() == false)
1446 {
1447 if (Md5Hash != MD5)
1448 {
1449 Status = StatError;
1450 ErrorText = "MD5Sum mismatch";
1451 Rename(DestFile,DestFile + ".FAILED");
1452 return;
1453 }
1454 }
1455
1456 Item::Done(Message,Size,MD5,Cnf);
1457
1458 string FileName = LookupTag(Message,"Filename");
1459 if (FileName.empty() == true)
1460 {
1461 Status = StatError;
1462 ErrorText = "Method gave a blank filename";
1463 return;
1464 }
1465
1466 Complete = true;
1467
1468 // The files timestamp matches
1469 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
1470 return;
1471
1472 // We have to copy it into place
1473 if (FileName != DestFile)
1474 {
1475 Local = true;
1476 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
1477 Cnf->Removable == true)
1478 {
1479 Desc.URI = "copy:" + FileName;
1480 QueueURI(Desc);
1481 return;
1482 }
1483
1484 // Erase the file if it is a symlink so we can overwrite it
1485 struct stat St;
1486 if (lstat(DestFile.c_str(),&St) == 0)
1487 {
1488 if (S_ISLNK(St.st_mode) != 0)
1489 unlink(DestFile.c_str());
1490 }
1491
1492 // Symlink the file
1493 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
1494 {
1495 ErrorText = "Link to " + DestFile + " failure ";
1496 Status = StatError;
1497 Complete = false;
1498 }
1499 }
1500 }
1501 /*}}}*/
1502 // AcqFile::Failed - Failure handler /*{{{*/
1503 // ---------------------------------------------------------------------
1504 /* Here we try other sources */
1505 void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1506 {
1507 ErrorText = LookupTag(Message,"Message");
1508
1509 // This is the retry counter
1510 if (Retries != 0 &&
1511 Cnf->LocalOnly == false &&
1512 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1513 {
1514 Retries--;
1515 QueueURI(Desc);
1516 return;
1517 }
1518
1519 Item::Failed(Message,Cnf);
1520 }
1521 /*}}}*/