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