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