some more coverity fixes
[ntk/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 <config.h>
17
18 #include <apt-pkg/acquire-item.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/aptconfiguration.h>
21 #include <apt-pkg/sourcelist.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/strutl.h>
24 #include <apt-pkg/fileutl.h>
25 #include <apt-pkg/md5.h>
26 #include <apt-pkg/sha1.h>
27 #include <apt-pkg/tagfile.h>
28 #include <apt-pkg/indexrecords.h>
29 #include <apt-pkg/metaindex.h>
30
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string>
35 #include <sstream>
36 #include <stdio.h>
37 #include <ctime>
38
39 #include <apti18n.h>
40 /*}}}*/
41
42 using namespace std;
43
44 // Acquire::Item::Item - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
46 /* */
47 pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
48 PartialSize(0), Mode(0), ID(0), Complete(false),
49 Local(false), QueueCounter(0)
50 {
51 Owner->Add(this);
52 Status = StatIdle;
53 }
54 /*}}}*/
55 // Acquire::Item::~Item - Destructor /*{{{*/
56 // ---------------------------------------------------------------------
57 /* */
58 pkgAcquire::Item::~Item()
59 {
60 Owner->Remove(this);
61 }
62 /*}}}*/
63 // Acquire::Item::Failed - Item failed to download /*{{{*/
64 // ---------------------------------------------------------------------
65 /* We return to an idle state if there are still other queues that could
66 fetch this object */
67 void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
68 {
69 Status = StatIdle;
70 ErrorText = LookupTag(Message,"Message");
71 UsedMirror = LookupTag(Message,"UsedMirror");
72 if (QueueCounter <= 1)
73 {
74 /* This indicates that the file is not available right now but might
75 be sometime later. If we do a retry cycle then this should be
76 retried [CDROMs] */
77 if (Cnf->LocalOnly == true &&
78 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
79 {
80 Status = StatIdle;
81 Dequeue();
82 return;
83 }
84
85 Status = StatError;
86 Dequeue();
87 }
88
89 // report mirror failure back to LP if we actually use a mirror
90 string FailReason = LookupTag(Message, "FailReason");
91 if(FailReason.size() != 0)
92 ReportMirrorFailure(FailReason);
93 else
94 ReportMirrorFailure(ErrorText);
95 }
96 /*}}}*/
97 // Acquire::Item::Start - Item has begun to download /*{{{*/
98 // ---------------------------------------------------------------------
99 /* Stash status and the file size. Note that setting Complete means
100 sub-phases of the acquire process such as decompresion are operating */
101 void pkgAcquire::Item::Start(string /*Message*/,unsigned long long Size)
102 {
103 Status = StatFetching;
104 if (FileSize == 0 && Complete == false)
105 FileSize = Size;
106 }
107 /*}}}*/
108 // Acquire::Item::Done - Item downloaded OK /*{{{*/
109 // ---------------------------------------------------------------------
110 /* */
111 void pkgAcquire::Item::Done(string Message,unsigned long long Size,string Hash,
112 pkgAcquire::MethodConfig *Cnf)
113 {
114 // We just downloaded something..
115 string FileName = LookupTag(Message,"Filename");
116 UsedMirror = LookupTag(Message,"UsedMirror");
117 if (Complete == false && !Local && FileName == DestFile)
118 {
119 if (Owner->Log != 0)
120 Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
121 }
122
123 if (FileSize == 0)
124 FileSize= Size;
125 Status = StatDone;
126 ErrorText = string();
127 Owner->Dequeue(this);
128 }
129 /*}}}*/
130 // Acquire::Item::Rename - Rename a file /*{{{*/
131 // ---------------------------------------------------------------------
132 /* This helper function is used by alot of item methods as thier final
133 step */
134 void pkgAcquire::Item::Rename(string From,string To)
135 {
136 if (rename(From.c_str(),To.c_str()) != 0)
137 {
138 char S[300];
139 snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
140 From.c_str(),To.c_str());
141 Status = StatError;
142 ErrorText = S;
143 }
144 }
145 /*}}}*/
146 // Acquire::Item::ReportMirrorFailure /*{{{*/
147 // ---------------------------------------------------------------------
148 void pkgAcquire::Item::ReportMirrorFailure(string FailCode)
149 {
150 // we only act if a mirror was used at all
151 if(UsedMirror.empty())
152 return;
153 #if 0
154 std::cerr << "\nReportMirrorFailure: "
155 << UsedMirror
156 << " Uri: " << DescURI()
157 << " FailCode: "
158 << FailCode << std::endl;
159 #endif
160 const char *Args[40];
161 unsigned int i = 0;
162 string report = _config->Find("Methods::Mirror::ProblemReporting",
163 "/usr/lib/apt/apt-report-mirror-failure");
164 if(!FileExists(report))
165 return;
166 Args[i++] = report.c_str();
167 Args[i++] = UsedMirror.c_str();
168 Args[i++] = DescURI().c_str();
169 Args[i++] = FailCode.c_str();
170 Args[i++] = NULL;
171 pid_t pid = ExecFork();
172 if(pid < 0)
173 {
174 _error->Error("ReportMirrorFailure Fork failed");
175 return;
176 }
177 else if(pid == 0)
178 {
179 execvp(Args[0], (char**)Args);
180 std::cerr << "Could not exec " << Args[0] << std::endl;
181 _exit(100);
182 }
183 if(!ExecWait(pid, "report-mirror-failure"))
184 {
185 _error->Warning("Couldn't report problem to '%s'",
186 _config->Find("Methods::Mirror::ProblemReporting").c_str());
187 }
188 }
189 /*}}}*/
190 // AcqSubIndex::AcqSubIndex - Constructor /*{{{*/
191 // ---------------------------------------------------------------------
192 /* Get a sub-index file based on checksums from a 'master' file and
193 possibly query additional files */
194 pkgAcqSubIndex::pkgAcqSubIndex(pkgAcquire *Owner, string const &URI,
195 string const &URIDesc, string const &ShortDesc,
196 HashString const &ExpectedHash)
197 : Item(Owner), ExpectedHash(ExpectedHash)
198 {
199 /* XXX: Beware: Currently this class does nothing (of value) anymore ! */
200 Debug = _config->FindB("Debug::pkgAcquire::SubIndex",false);
201
202 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
203 DestFile += URItoFileName(URI);
204
205 Desc.URI = URI;
206 Desc.Description = URIDesc;
207 Desc.Owner = this;
208 Desc.ShortDesc = ShortDesc;
209
210 QueueURI(Desc);
211
212 if(Debug)
213 std::clog << "pkgAcqSubIndex: " << Desc.URI << std::endl;
214 }
215 /*}}}*/
216 // AcqSubIndex::Custom600Headers - Insert custom request headers /*{{{*/
217 // ---------------------------------------------------------------------
218 /* The only header we use is the last-modified header. */
219 string pkgAcqSubIndex::Custom600Headers()
220 {
221 string Final = _config->FindDir("Dir::State::lists");
222 Final += URItoFileName(Desc.URI);
223
224 struct stat Buf;
225 if (stat(Final.c_str(),&Buf) != 0)
226 return "\nIndex-File: true\nFail-Ignore: true\n";
227 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
228 }
229 /*}}}*/
230 void pkgAcqSubIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
231 {
232 if(Debug)
233 std::clog << "pkgAcqSubIndex failed: " << Desc.URI << std::endl;
234
235 Complete = false;
236 Status = StatDone;
237 Dequeue();
238
239 // No good Index is provided
240 }
241 /*}}}*/
242 void pkgAcqSubIndex::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
243 pkgAcquire::MethodConfig *Cnf)
244 {
245 if(Debug)
246 std::clog << "pkgAcqSubIndex::Done(): " << Desc.URI << std::endl;
247
248 string FileName = LookupTag(Message,"Filename");
249 if (FileName.empty() == true)
250 {
251 Status = StatError;
252 ErrorText = "Method gave a blank filename";
253 return;
254 }
255
256 if (FileName != DestFile)
257 {
258 Local = true;
259 Desc.URI = "copy:" + FileName;
260 QueueURI(Desc);
261 return;
262 }
263
264 Item::Done(Message,Size,Md5Hash,Cnf);
265
266 string FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(Desc.URI);
267
268 /* Downloaded invalid transindex => Error (LP: #346386) (Closes: #627642) */
269 indexRecords SubIndexParser;
270 if (FileExists(DestFile) == true && !SubIndexParser.Load(DestFile)) {
271 Status = StatError;
272 ErrorText = SubIndexParser.ErrorText;
273 return;
274 }
275
276 // sucess in downloading the index
277 // rename the index
278 if(Debug)
279 std::clog << "Renaming: " << DestFile << " -> " << FinalFile << std::endl;
280 Rename(DestFile,FinalFile);
281 chmod(FinalFile.c_str(),0644);
282 DestFile = FinalFile;
283
284 if(ParseIndex(DestFile) == false)
285 return Failed("", NULL);
286
287 Complete = true;
288 Status = StatDone;
289 Dequeue();
290 return;
291 }
292 /*}}}*/
293 bool pkgAcqSubIndex::ParseIndex(string const &IndexFile) /*{{{*/
294 {
295 indexRecords SubIndexParser;
296 if (FileExists(IndexFile) == false || SubIndexParser.Load(IndexFile) == false)
297 return false;
298 // so something with the downloaded index
299 return true;
300 }
301 /*}}}*/
302 // AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
303 // ---------------------------------------------------------------------
304 /* Get the DiffIndex file first and see if there are patches availabe
305 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
306 * patches. If anything goes wrong in that process, it will fall back to
307 * the original packages file
308 */
309 pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
310 string URI,string URIDesc,string ShortDesc,
311 HashString ExpectedHash)
312 : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash),
313 Description(URIDesc)
314 {
315
316 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
317
318 Desc.Description = URIDesc + "/DiffIndex";
319 Desc.Owner = this;
320 Desc.ShortDesc = ShortDesc;
321 Desc.URI = URI + ".diff/Index";
322
323 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
324 DestFile += URItoFileName(URI) + string(".DiffIndex");
325
326 if(Debug)
327 std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
328
329 // look for the current package file
330 CurrentPackagesFile = _config->FindDir("Dir::State::lists");
331 CurrentPackagesFile += URItoFileName(RealURI);
332
333 // FIXME: this file:/ check is a hack to prevent fetching
334 // from local sources. this is really silly, and
335 // should be fixed cleanly as soon as possible
336 if(!FileExists(CurrentPackagesFile) ||
337 Desc.URI.substr(0,strlen("file:/")) == "file:/")
338 {
339 // we don't have a pkg file or we don't want to queue
340 if(Debug)
341 std::clog << "No index file, local or canceld by user" << std::endl;
342 Failed("", NULL);
343 return;
344 }
345
346 if(Debug)
347 std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
348 << CurrentPackagesFile << std::endl;
349
350 QueueURI(Desc);
351
352 }
353 /*}}}*/
354 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
355 // ---------------------------------------------------------------------
356 /* The only header we use is the last-modified header. */
357 string pkgAcqDiffIndex::Custom600Headers()
358 {
359 string Final = _config->FindDir("Dir::State::lists");
360 Final += URItoFileName(RealURI) + string(".IndexDiff");
361
362 if(Debug)
363 std::clog << "Custom600Header-IMS: " << Final << std::endl;
364
365 struct stat Buf;
366 if (stat(Final.c_str(),&Buf) != 0)
367 return "\nIndex-File: true";
368
369 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
370 }
371 /*}}}*/
372 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile) /*{{{*/
373 {
374 if(Debug)
375 std::clog << "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
376 << std::endl;
377
378 pkgTagSection Tags;
379 string ServerSha1;
380 vector<DiffInfo> available_patches;
381
382 FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
383 pkgTagFile TF(&Fd);
384 if (_error->PendingError() == true)
385 return false;
386
387 if(TF.Step(Tags) == true)
388 {
389 bool found = false;
390 DiffInfo d;
391 string size;
392
393 string const tmp = Tags.FindS("SHA1-Current");
394 std::stringstream ss(tmp);
395 ss >> ServerSha1 >> size;
396 unsigned long const ServerSize = atol(size.c_str());
397
398 FileFd fd(CurrentPackagesFile, FileFd::ReadOnly);
399 SHA1Summation SHA1;
400 SHA1.AddFD(fd);
401 string const local_sha1 = SHA1.Result();
402
403 if(local_sha1 == ServerSha1)
404 {
405 // we have the same sha1 as the server
406 if(Debug)
407 std::clog << "Package file is up-to-date" << std::endl;
408 // set found to true, this will queue a pkgAcqIndexDiffs with
409 // a empty availabe_patches
410 found = true;
411 }
412 else
413 {
414 if(Debug)
415 std::clog << "SHA1-Current: " << ServerSha1 << " and we start at "<< fd.Name() << " " << fd.Size() << " " << local_sha1 << std::endl;
416
417 // check the historie and see what patches we need
418 string const history = Tags.FindS("SHA1-History");
419 std::stringstream hist(history);
420 while(hist >> d.sha1 >> size >> d.file)
421 {
422 // read until the first match is found
423 // from that point on, we probably need all diffs
424 if(d.sha1 == local_sha1)
425 found=true;
426 else if (found == false)
427 continue;
428
429 if(Debug)
430 std::clog << "Need to get diff: " << d.file << std::endl;
431 available_patches.push_back(d);
432 }
433
434 if (available_patches.empty() == false)
435 {
436 // patching with too many files is rather slow compared to a fast download
437 unsigned long const fileLimit = _config->FindI("Acquire::PDiffs::FileLimit", 0);
438 if (fileLimit != 0 && fileLimit < available_patches.size())
439 {
440 if (Debug)
441 std::clog << "Need " << available_patches.size() << " diffs (Limit is " << fileLimit
442 << ") so fallback to complete download" << std::endl;
443 return false;
444 }
445
446 // see if the patches are too big
447 found = false; // it was true and it will be true again at the end
448 d = *available_patches.begin();
449 string const firstPatch = d.file;
450 unsigned long patchesSize = 0;
451 std::stringstream patches(Tags.FindS("SHA1-Patches"));
452 while(patches >> d.sha1 >> size >> d.file)
453 {
454 if (firstPatch == d.file)
455 found = true;
456 else if (found == false)
457 continue;
458
459 patchesSize += atol(size.c_str());
460 }
461 unsigned long const sizeLimit = ServerSize * _config->FindI("Acquire::PDiffs::SizeLimit", 100);
462 if (sizeLimit > 0 && (sizeLimit/100) < patchesSize)
463 {
464 if (Debug)
465 std::clog << "Need " << patchesSize << " bytes (Limit is " << sizeLimit/100
466 << ") so fallback to complete download" << std::endl;
467 return false;
468 }
469 }
470 }
471
472 // we have something, queue the next diff
473 if(found)
474 {
475 // queue the diffs
476 string::size_type const last_space = Description.rfind(" ");
477 if(last_space != string::npos)
478 Description.erase(last_space, Description.size()-last_space);
479 new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
480 ExpectedHash, ServerSha1, available_patches);
481 Complete = false;
482 Status = StatDone;
483 Dequeue();
484 return true;
485 }
486 }
487
488 // Nothing found, report and return false
489 // Failing here is ok, if we return false later, the full
490 // IndexFile is queued
491 if(Debug)
492 std::clog << "Can't find a patch in the index file" << std::endl;
493 return false;
494 }
495 /*}}}*/
496 void pkgAcqDiffIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
497 {
498 if(Debug)
499 std::clog << "pkgAcqDiffIndex failed: " << Desc.URI << std::endl
500 << "Falling back to normal index file aquire" << std::endl;
501
502 new pkgAcqIndex(Owner, RealURI, Description, Desc.ShortDesc,
503 ExpectedHash);
504
505 Complete = false;
506 Status = StatDone;
507 Dequeue();
508 }
509 /*}}}*/
510 void pkgAcqDiffIndex::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
511 pkgAcquire::MethodConfig *Cnf)
512 {
513 if(Debug)
514 std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl;
515
516 Item::Done(Message,Size,Md5Hash,Cnf);
517
518 string FinalFile;
519 FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
520
521 // sucess in downloading the index
522 // rename the index
523 FinalFile += string(".IndexDiff");
524 if(Debug)
525 std::clog << "Renaming: " << DestFile << " -> " << FinalFile
526 << std::endl;
527 Rename(DestFile,FinalFile);
528 chmod(FinalFile.c_str(),0644);
529 DestFile = FinalFile;
530
531 if(!ParseDiffIndex(DestFile))
532 return Failed("", NULL);
533
534 Complete = true;
535 Status = StatDone;
536 Dequeue();
537 return;
538 }
539 /*}}}*/
540 // AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
541 // ---------------------------------------------------------------------
542 /* The package diff is added to the queue. one object is constructed
543 * for each diff and the index
544 */
545 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
546 string URI,string URIDesc,string ShortDesc,
547 HashString ExpectedHash,
548 string ServerSha1,
549 vector<DiffInfo> diffs)
550 : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash),
551 available_patches(diffs), ServerSha1(ServerSha1)
552 {
553
554 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
555 DestFile += URItoFileName(URI);
556
557 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
558
559 Description = URIDesc;
560 Desc.Owner = this;
561 Desc.ShortDesc = ShortDesc;
562
563 if(available_patches.empty() == true)
564 {
565 // we are done (yeah!)
566 Finish(true);
567 }
568 else
569 {
570 // get the next diff
571 State = StateFetchDiff;
572 QueueNextDiff();
573 }
574 }
575 /*}}}*/
576 void pkgAcqIndexDiffs::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
577 {
578 if(Debug)
579 std::clog << "pkgAcqIndexDiffs failed: " << Desc.URI << std::endl
580 << "Falling back to normal index file aquire" << std::endl;
581 new pkgAcqIndex(Owner, RealURI, Description,Desc.ShortDesc,
582 ExpectedHash);
583 Finish();
584 }
585 /*}}}*/
586 // Finish - helper that cleans the item out of the fetcher queue /*{{{*/
587 void pkgAcqIndexDiffs::Finish(bool allDone)
588 {
589 // we restore the original name, this is required, otherwise
590 // the file will be cleaned
591 if(allDone)
592 {
593 DestFile = _config->FindDir("Dir::State::lists");
594 DestFile += URItoFileName(RealURI);
595
596 if(!ExpectedHash.empty() && !ExpectedHash.VerifyFile(DestFile))
597 {
598 Status = StatAuthError;
599 ErrorText = _("MD5Sum mismatch");
600 Rename(DestFile,DestFile + ".FAILED");
601 Dequeue();
602 return;
603 }
604
605 // this is for the "real" finish
606 Complete = true;
607 Status = StatDone;
608 Dequeue();
609 if(Debug)
610 std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
611 return;
612 }
613
614 if(Debug)
615 std::clog << "Finishing: " << Desc.URI << std::endl;
616 Complete = false;
617 Status = StatDone;
618 Dequeue();
619 return;
620 }
621 /*}}}*/
622 bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
623 {
624
625 // calc sha1 of the just patched file
626 string FinalFile = _config->FindDir("Dir::State::lists");
627 FinalFile += URItoFileName(RealURI);
628
629 FileFd fd(FinalFile, FileFd::ReadOnly);
630 SHA1Summation SHA1;
631 SHA1.AddFD(fd);
632 string local_sha1 = string(SHA1.Result());
633 if(Debug)
634 std::clog << "QueueNextDiff: "
635 << FinalFile << " (" << local_sha1 << ")"<<std::endl;
636
637 // final file reached before all patches are applied
638 if(local_sha1 == ServerSha1)
639 {
640 Finish(true);
641 return true;
642 }
643
644 // remove all patches until the next matching patch is found
645 // this requires the Index file to be ordered
646 for(vector<DiffInfo>::iterator I=available_patches.begin();
647 available_patches.empty() == false &&
648 I != available_patches.end() &&
649 I->sha1 != local_sha1;
650 ++I)
651 {
652 available_patches.erase(I);
653 }
654
655 // error checking and falling back if no patch was found
656 if(available_patches.empty() == true)
657 {
658 Failed("", NULL);
659 return false;
660 }
661
662 // queue the right diff
663 Desc.URI = string(RealURI) + ".diff/" + available_patches[0].file + ".gz";
664 Desc.Description = Description + " " + available_patches[0].file + string(".pdiff");
665 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
666 DestFile += URItoFileName(RealURI + ".diff/" + available_patches[0].file);
667
668 if(Debug)
669 std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
670
671 QueueURI(Desc);
672
673 return true;
674 }
675 /*}}}*/
676 void pkgAcqIndexDiffs::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
677 pkgAcquire::MethodConfig *Cnf)
678 {
679 if(Debug)
680 std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl;
681
682 Item::Done(Message,Size,Md5Hash,Cnf);
683
684 string FinalFile;
685 FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
686
687 // sucess in downloading a diff, enter ApplyDiff state
688 if(State == StateFetchDiff)
689 {
690
691 // rred excepts the patch as $FinalFile.ed
692 Rename(DestFile,FinalFile+".ed");
693
694 if(Debug)
695 std::clog << "Sending to rred method: " << FinalFile << std::endl;
696
697 State = StateApplyDiff;
698 Local = true;
699 Desc.URI = "rred:" + FinalFile;
700 QueueURI(Desc);
701 Mode = "rred";
702 return;
703 }
704
705
706 // success in download/apply a diff, queue next (if needed)
707 if(State == StateApplyDiff)
708 {
709 // remove the just applied patch
710 available_patches.erase(available_patches.begin());
711
712 // move into place
713 if(Debug)
714 {
715 std::clog << "Moving patched file in place: " << std::endl
716 << DestFile << " -> " << FinalFile << std::endl;
717 }
718 Rename(DestFile,FinalFile);
719 chmod(FinalFile.c_str(),0644);
720
721 // see if there is more to download
722 if(available_patches.empty() == false) {
723 new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
724 ExpectedHash, ServerSha1, available_patches);
725 return Finish();
726 } else
727 return Finish(true);
728 }
729 }
730 /*}}}*/
731 // AcqIndex::AcqIndex - Constructor /*{{{*/
732 // ---------------------------------------------------------------------
733 /* The package file is added to the queue and a second class is
734 instantiated to fetch the revision file */
735 pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
736 string URI,string URIDesc,string ShortDesc,
737 HashString ExpectedHash, string comprExt)
738 : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash)
739 {
740 if(comprExt.empty() == true)
741 {
742 // autoselect the compression method
743 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
744 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
745 comprExt.append(*t).append(" ");
746 if (comprExt.empty() == false)
747 comprExt.erase(comprExt.end()-1);
748 }
749 CompressionExtension = comprExt;
750
751 Init(URI, URIDesc, ShortDesc);
752 }
753 pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner, IndexTarget const *Target,
754 HashString const &ExpectedHash, indexRecords const *MetaIndexParser)
755 : Item(Owner), RealURI(Target->URI), ExpectedHash(ExpectedHash)
756 {
757 // autoselect the compression method
758 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
759 CompressionExtension = "";
760 if (ExpectedHash.empty() == false)
761 {
762 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
763 if (*t == "uncompressed" || MetaIndexParser->Exists(string(Target->MetaKey).append(".").append(*t)) == true)
764 CompressionExtension.append(*t).append(" ");
765 }
766 else
767 {
768 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
769 CompressionExtension.append(*t).append(" ");
770 }
771 if (CompressionExtension.empty() == false)
772 CompressionExtension.erase(CompressionExtension.end()-1);
773
774 // only verify non-optional targets, see acquire-item.h for a FIXME
775 // to make this more flexible
776 if (Target->IsOptional())
777 Verify = false;
778 else
779 Verify = true;
780
781 Init(Target->URI, Target->Description, Target->ShortDesc);
782 }
783 /*}}}*/
784 // AcqIndex::Init - defered Constructor /*{{{*/
785 void pkgAcqIndex::Init(string const &URI, string const &URIDesc, string const &ShortDesc) {
786 Decompression = false;
787 Erase = false;
788
789 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
790 DestFile += URItoFileName(URI);
791
792 std::string const comprExt = CompressionExtension.substr(0, CompressionExtension.find(' '));
793 if (comprExt == "uncompressed")
794 Desc.URI = URI;
795 else
796 Desc.URI = URI + '.' + comprExt;
797
798 Desc.Description = URIDesc;
799 Desc.Owner = this;
800 Desc.ShortDesc = ShortDesc;
801
802 QueueURI(Desc);
803 }
804 /*}}}*/
805 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
806 // ---------------------------------------------------------------------
807 /* The only header we use is the last-modified header. */
808 string pkgAcqIndex::Custom600Headers()
809 {
810 string Final = _config->FindDir("Dir::State::lists");
811 Final += URItoFileName(RealURI);
812 if (_config->FindB("Acquire::GzipIndexes",false))
813 Final += ".gz";
814
815 string msg = "\nIndex-File: true";
816 // FIXME: this really should use "IndexTarget::IsOptional()" but that
817 // seems to be difficult without breaking ABI
818 if (ShortDesc().find("Translation") != 0)
819 msg += "\nFail-Ignore: true";
820 struct stat Buf;
821 if (stat(Final.c_str(),&Buf) == 0)
822 msg += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
823
824 return msg;
825 }
826 /*}}}*/
827 void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
828 {
829 size_t const nextExt = CompressionExtension.find(' ');
830 if (nextExt != std::string::npos)
831 {
832 CompressionExtension = CompressionExtension.substr(nextExt+1);
833 Init(RealURI, Desc.Description, Desc.ShortDesc);
834 return;
835 }
836
837 // on decompression failure, remove bad versions in partial/
838 if (Decompression && Erase) {
839 string s = _config->FindDir("Dir::State::lists") + "partial/";
840 s.append(URItoFileName(RealURI));
841 unlink(s.c_str());
842 }
843
844 Item::Failed(Message,Cnf);
845 }
846 /*}}}*/
847 // AcqIndex::Done - Finished a fetch /*{{{*/
848 // ---------------------------------------------------------------------
849 /* This goes through a number of states.. On the initial fetch the
850 method could possibly return an alternate filename which points
851 to the uncompressed version of the file. If this is so the file
852 is copied into the partial directory. In all other cases the file
853 is decompressed with a gzip uri. */
854 void pkgAcqIndex::Done(string Message,unsigned long long Size,string Hash,
855 pkgAcquire::MethodConfig *Cfg)
856 {
857 Item::Done(Message,Size,Hash,Cfg);
858
859 if (Decompression == true)
860 {
861 if (_config->FindB("Debug::pkgAcquire::Auth", false))
862 {
863 std::cerr << std::endl << RealURI << ": Computed Hash: " << Hash;
864 std::cerr << " Expected Hash: " << ExpectedHash.toStr() << std::endl;
865 }
866
867 if (!ExpectedHash.empty() && ExpectedHash.toStr() != Hash)
868 {
869 Status = StatAuthError;
870 ErrorText = _("Hash Sum mismatch");
871 Rename(DestFile,DestFile + ".FAILED");
872 ReportMirrorFailure("HashChecksumFailure");
873 return;
874 }
875
876 /* Verify the index file for correctness (all indexes must
877 * have a Package field) (LP: #346386) (Closes: #627642) */
878 if (Verify == true)
879 {
880 FileFd fd(DestFile, FileFd::ReadOnly);
881 pkgTagSection sec;
882 pkgTagFile tag(&fd);
883
884 // Only test for correctness if the file is not empty (empty is ok)
885 if (fd.Size() > 0) {
886 if (_error->PendingError() || !tag.Step(sec)) {
887 Status = StatError;
888 _error->DumpErrors();
889 Rename(DestFile,DestFile + ".FAILED");
890 return;
891 } else if (!sec.Exists("Package")) {
892 Status = StatError;
893 ErrorText = ("Encountered a section with no Package: header");
894 Rename(DestFile,DestFile + ".FAILED");
895 return;
896 }
897 }
898 }
899
900 // Done, move it into position
901 string FinalFile = _config->FindDir("Dir::State::lists");
902 FinalFile += URItoFileName(RealURI);
903 Rename(DestFile,FinalFile);
904 chmod(FinalFile.c_str(),0644);
905
906 /* We restore the original name to DestFile so that the clean operation
907 will work OK */
908 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
909 DestFile += URItoFileName(RealURI);
910
911 // Remove the compressed version.
912 if (Erase == true)
913 unlink(DestFile.c_str());
914 return;
915 }
916
917 Erase = false;
918 Complete = true;
919
920 // Handle the unzipd case
921 string FileName = LookupTag(Message,"Alt-Filename");
922 if (FileName.empty() == false)
923 {
924 // The files timestamp matches
925 if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
926 return;
927 Decompression = true;
928 Local = true;
929 DestFile += ".decomp";
930 Desc.URI = "copy:" + FileName;
931 QueueURI(Desc);
932 Mode = "copy";
933 return;
934 }
935
936 FileName = LookupTag(Message,"Filename");
937 if (FileName.empty() == true)
938 {
939 Status = StatError;
940 ErrorText = "Method gave a blank filename";
941 }
942
943 std::string const compExt = CompressionExtension.substr(0, CompressionExtension.find(' '));
944
945 // The files timestamp matches
946 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true) {
947 if (_config->FindB("Acquire::GzipIndexes",false) && compExt == "gz")
948 // Update DestFile for .gz suffix so that the clean operation keeps it
949 DestFile += ".gz";
950 return;
951 }
952
953 if (FileName == DestFile)
954 Erase = true;
955 else
956 Local = true;
957
958 string decompProg;
959
960 // If we enable compressed indexes and already have gzip, keep it
961 if (_config->FindB("Acquire::GzipIndexes",false) && compExt == "gz" && !Local) {
962 string FinalFile = _config->FindDir("Dir::State::lists");
963 FinalFile += URItoFileName(RealURI) + ".gz";
964 Rename(DestFile,FinalFile);
965 chmod(FinalFile.c_str(),0644);
966
967 // Update DestFile for .gz suffix so that the clean operation keeps it
968 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
969 DestFile += URItoFileName(RealURI) + ".gz";
970 return;
971 }
972
973 // get the binary name for your used compression type
974 decompProg = _config->Find(string("Acquire::CompressionTypes::").append(compExt),"");
975 if(decompProg.empty() == false);
976 else if(compExt == "uncompressed")
977 decompProg = "copy";
978 else {
979 _error->Error("Unsupported extension: %s", compExt.c_str());
980 return;
981 }
982
983 Decompression = true;
984 DestFile += ".decomp";
985 Desc.URI = decompProg + ":" + FileName;
986 QueueURI(Desc);
987
988 // FIXME: this points to a c++ string that goes out of scope
989 Mode = decompProg.c_str();
990 }
991 /*}}}*/
992 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
993 // ---------------------------------------------------------------------
994 /* The Translation file is added to the queue */
995 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner,
996 string URI,string URIDesc,string ShortDesc)
997 : pkgAcqIndex(Owner, URI, URIDesc, ShortDesc, HashString(), "")
998 {
999 }
1000 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner, IndexTarget const *Target,
1001 HashString const &ExpectedHash, indexRecords const *MetaIndexParser)
1002 : pkgAcqIndex(Owner, Target, ExpectedHash, MetaIndexParser)
1003 {
1004 }
1005 /*}}}*/
1006 // AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
1007 // ---------------------------------------------------------------------
1008 string pkgAcqIndexTrans::Custom600Headers()
1009 {
1010 string Final = _config->FindDir("Dir::State::lists");
1011 Final += URItoFileName(RealURI);
1012
1013 struct stat Buf;
1014 if (stat(Final.c_str(),&Buf) != 0)
1015 return "\nFail-Ignore: true\nIndex-File: true";
1016 return "\nFail-Ignore: true\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
1017 }
1018 /*}}}*/
1019 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
1020 // ---------------------------------------------------------------------
1021 /* */
1022 void pkgAcqIndexTrans::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1023 {
1024 size_t const nextExt = CompressionExtension.find(' ');
1025 if (nextExt != std::string::npos)
1026 {
1027 CompressionExtension = CompressionExtension.substr(nextExt+1);
1028 Init(RealURI, Desc.Description, Desc.ShortDesc);
1029 Status = StatIdle;
1030 return;
1031 }
1032
1033 if (Cnf->LocalOnly == true ||
1034 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
1035 {
1036 // Ignore this
1037 Status = StatDone;
1038 Complete = false;
1039 Dequeue();
1040 return;
1041 }
1042
1043 Item::Failed(Message,Cnf);
1044 }
1045 /*}}}*/
1046 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner, /*{{{*/
1047 string URI,string URIDesc,string ShortDesc,
1048 string MetaIndexURI, string MetaIndexURIDesc,
1049 string MetaIndexShortDesc,
1050 const vector<IndexTarget*>* IndexTargets,
1051 indexRecords* MetaIndexParser) :
1052 Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
1053 MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
1054 MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets)
1055 {
1056 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
1057 DestFile += URItoFileName(URI);
1058
1059 // remove any partial downloaded sig-file in partial/.
1060 // it may confuse proxies and is too small to warrant a
1061 // partial download anyway
1062 unlink(DestFile.c_str());
1063
1064 // Create the item
1065 Desc.Description = URIDesc;
1066 Desc.Owner = this;
1067 Desc.ShortDesc = ShortDesc;
1068 Desc.URI = URI;
1069
1070 string Final = _config->FindDir("Dir::State::lists");
1071 Final += URItoFileName(RealURI);
1072 struct stat Buf;
1073 if (stat(Final.c_str(),&Buf) == 0)
1074 {
1075 // File was already in place. It needs to be re-downloaded/verified
1076 // because Release might have changed, we do give it a differnt
1077 // name than DestFile because otherwise the http method will
1078 // send If-Range requests and there are too many broken servers
1079 // out there that do not understand them
1080 LastGoodSig = DestFile+".reverify";
1081 Rename(Final,LastGoodSig);
1082 }
1083
1084 QueueURI(Desc);
1085 }
1086 /*}}}*/
1087 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
1088 // ---------------------------------------------------------------------
1089 /* The only header we use is the last-modified header. */
1090 string pkgAcqMetaSig::Custom600Headers()
1091 {
1092 struct stat Buf;
1093 if (stat(LastGoodSig.c_str(),&Buf) != 0)
1094 return "\nIndex-File: true";
1095
1096 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
1097 }
1098
1099 void pkgAcqMetaSig::Done(string Message,unsigned long long Size,string MD5,
1100 pkgAcquire::MethodConfig *Cfg)
1101 {
1102 Item::Done(Message,Size,MD5,Cfg);
1103
1104 string FileName = LookupTag(Message,"Filename");
1105 if (FileName.empty() == true)
1106 {
1107 Status = StatError;
1108 ErrorText = "Method gave a blank filename";
1109 return;
1110 }
1111
1112 if (FileName != DestFile)
1113 {
1114 // We have to copy it into place
1115 Local = true;
1116 Desc.URI = "copy:" + FileName;
1117 QueueURI(Desc);
1118 return;
1119 }
1120
1121 Complete = true;
1122
1123 // put the last known good file back on i-m-s hit (it will
1124 // be re-verified again)
1125 // Else do nothing, we have the new file in DestFile then
1126 if(StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
1127 Rename(LastGoodSig, DestFile);
1128
1129 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
1130 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc,
1131 MetaIndexShortDesc, DestFile, IndexTargets,
1132 MetaIndexParser);
1133
1134 }
1135 /*}}}*/
1136 void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)/*{{{*/
1137 {
1138 string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
1139
1140 // if we get a network error we fail gracefully
1141 if(Status == StatTransientNetworkError)
1142 {
1143 Item::Failed(Message,Cnf);
1144 // move the sigfile back on transient network failures
1145 if(FileExists(LastGoodSig))
1146 Rename(LastGoodSig,Final);
1147
1148 // set the status back to , Item::Failed likes to reset it
1149 Status = pkgAcquire::Item::StatTransientNetworkError;
1150 return;
1151 }
1152
1153 // Delete any existing sigfile when the acquire failed
1154 unlink(Final.c_str());
1155
1156 // queue a pkgAcqMetaIndex with no sigfile
1157 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
1158 "", IndexTargets, MetaIndexParser);
1159
1160 if (Cnf->LocalOnly == true ||
1161 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
1162 {
1163 // Ignore this
1164 Status = StatDone;
1165 Complete = false;
1166 Dequeue();
1167 return;
1168 }
1169
1170 Item::Failed(Message,Cnf);
1171 }
1172 /*}}}*/
1173 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner, /*{{{*/
1174 string URI,string URIDesc,string ShortDesc,
1175 string SigFile,
1176 const vector<struct IndexTarget*>* IndexTargets,
1177 indexRecords* MetaIndexParser) :
1178 Item(Owner), RealURI(URI), SigFile(SigFile), IndexTargets(IndexTargets),
1179 MetaIndexParser(MetaIndexParser), AuthPass(false), IMSHit(false)
1180 {
1181 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
1182 DestFile += URItoFileName(URI);
1183
1184 // Create the item
1185 Desc.Description = URIDesc;
1186 Desc.Owner = this;
1187 Desc.ShortDesc = ShortDesc;
1188 Desc.URI = URI;
1189
1190 QueueURI(Desc);
1191 }
1192 /*}}}*/
1193 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
1194 // ---------------------------------------------------------------------
1195 /* The only header we use is the last-modified header. */
1196 string pkgAcqMetaIndex::Custom600Headers()
1197 {
1198 string Final = _config->FindDir("Dir::State::lists");
1199 Final += URItoFileName(RealURI);
1200
1201 struct stat Buf;
1202 if (stat(Final.c_str(),&Buf) != 0)
1203 return "\nIndex-File: true";
1204
1205 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
1206 }
1207 /*}}}*/
1208 void pkgAcqMetaIndex::Done(string Message,unsigned long long Size,string Hash, /*{{{*/
1209 pkgAcquire::MethodConfig *Cfg)
1210 {
1211 Item::Done(Message,Size,Hash,Cfg);
1212
1213 // MetaIndexes are done in two passes: one to download the
1214 // metaindex with an appropriate method, and a second to verify it
1215 // with the gpgv method
1216
1217 if (AuthPass == true)
1218 {
1219 AuthDone(Message);
1220
1221 // all cool, move Release file into place
1222 Complete = true;
1223 }
1224 else
1225 {
1226 RetrievalDone(Message);
1227 if (!Complete)
1228 // Still more retrieving to do
1229 return;
1230
1231 if (SigFile == "")
1232 {
1233 // There was no signature file, so we are finished. Download
1234 // the indexes and do only hashsum verification if possible
1235 MetaIndexParser->Load(DestFile);
1236 QueueIndexes(false);
1237 }
1238 else
1239 {
1240 // There was a signature file, so pass it to gpgv for
1241 // verification
1242
1243 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1244 std::cerr << "Metaindex acquired, queueing gpg verification ("
1245 << SigFile << "," << DestFile << ")\n";
1246 AuthPass = true;
1247 Desc.URI = "gpgv:" + SigFile;
1248 QueueURI(Desc);
1249 Mode = "gpgv";
1250 return;
1251 }
1252 }
1253
1254 if (Complete == true)
1255 {
1256 string FinalFile = _config->FindDir("Dir::State::lists");
1257 FinalFile += URItoFileName(RealURI);
1258 if (SigFile == DestFile)
1259 SigFile = FinalFile;
1260 Rename(DestFile,FinalFile);
1261 chmod(FinalFile.c_str(),0644);
1262 DestFile = FinalFile;
1263 }
1264 }
1265 /*}}}*/
1266 void pkgAcqMetaIndex::RetrievalDone(string Message) /*{{{*/
1267 {
1268 // We have just finished downloading a Release file (it is not
1269 // verified yet)
1270
1271 string FileName = LookupTag(Message,"Filename");
1272 if (FileName.empty() == true)
1273 {
1274 Status = StatError;
1275 ErrorText = "Method gave a blank filename";
1276 return;
1277 }
1278
1279 if (FileName != DestFile)
1280 {
1281 Local = true;
1282 Desc.URI = "copy:" + FileName;
1283 QueueURI(Desc);
1284 return;
1285 }
1286
1287 // make sure to verify against the right file on I-M-S hit
1288 IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false);
1289 if(IMSHit)
1290 {
1291 string FinalFile = _config->FindDir("Dir::State::lists");
1292 FinalFile += URItoFileName(RealURI);
1293 if (SigFile == DestFile)
1294 {
1295 SigFile = FinalFile;
1296 // constructor of pkgAcqMetaClearSig moved it out of the way,
1297 // now move it back in on IMS hit for the 'old' file
1298 string const OldClearSig = DestFile + ".reverify";
1299 if (RealFileExists(OldClearSig) == true)
1300 Rename(OldClearSig, FinalFile);
1301 }
1302 DestFile = FinalFile;
1303 }
1304 Complete = true;
1305 }
1306 /*}}}*/
1307 void pkgAcqMetaIndex::AuthDone(string Message) /*{{{*/
1308 {
1309 // At this point, the gpgv method has succeeded, so there is a
1310 // valid signature from a key in the trusted keyring. We
1311 // perform additional verification of its contents, and use them
1312 // to verify the indexes we are about to download
1313
1314 if (!MetaIndexParser->Load(DestFile))
1315 {
1316 Status = StatAuthError;
1317 ErrorText = MetaIndexParser->ErrorText;
1318 return;
1319 }
1320
1321 if (!VerifyVendor(Message))
1322 {
1323 return;
1324 }
1325
1326 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1327 std::cerr << "Signature verification succeeded: "
1328 << DestFile << std::endl;
1329
1330 // Download further indexes with verification
1331 QueueIndexes(true);
1332
1333 // is it a clearsigned MetaIndex file?
1334 if (DestFile == SigFile)
1335 return;
1336
1337 // Done, move signature file into position
1338 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
1339 URItoFileName(RealURI) + ".gpg";
1340 Rename(SigFile,VerifiedSigFile);
1341 chmod(VerifiedSigFile.c_str(),0644);
1342 }
1343 /*}}}*/
1344 void pkgAcqMetaIndex::QueueIndexes(bool verify) /*{{{*/
1345 {
1346 #if 0
1347 /* Reject invalid, existing Release files (LP: #346386) (Closes: #627642)
1348 * FIXME: Disabled; it breaks unsigned repositories without hashes */
1349 if (!verify && FileExists(DestFile) && !MetaIndexParser->Load(DestFile))
1350 {
1351 Status = StatError;
1352 ErrorText = MetaIndexParser->ErrorText;
1353 return;
1354 }
1355 #endif
1356 bool transInRelease = false;
1357 {
1358 std::vector<std::string> const keys = MetaIndexParser->MetaKeys();
1359 for (std::vector<std::string>::const_iterator k = keys.begin(); k != keys.end(); ++k)
1360 // FIXME: Feels wrong to check for hardcoded string here, but what should we do else…
1361 if (k->find("Translation-") != std::string::npos)
1362 {
1363 transInRelease = true;
1364 break;
1365 }
1366 }
1367
1368 for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
1369 Target != IndexTargets->end();
1370 ++Target)
1371 {
1372 HashString ExpectedIndexHash;
1373 const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
1374 bool compressedAvailable = false;
1375 if (Record == NULL)
1376 {
1377 if ((*Target)->IsOptional() == true)
1378 {
1379 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
1380 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
1381 if (MetaIndexParser->Exists(string((*Target)->MetaKey).append(".").append(*t)) == true)
1382 {
1383 compressedAvailable = true;
1384 break;
1385 }
1386 }
1387 else if (verify == true)
1388 {
1389 Status = StatAuthError;
1390 strprintf(ErrorText, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), (*Target)->MetaKey.c_str());
1391 return;
1392 }
1393 }
1394 else
1395 {
1396 ExpectedIndexHash = Record->Hash;
1397 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1398 {
1399 std::cerr << "Queueing: " << (*Target)->URI << std::endl;
1400 std::cerr << "Expected Hash: " << ExpectedIndexHash.toStr() << std::endl;
1401 std::cerr << "For: " << Record->MetaKeyFilename << std::endl;
1402 }
1403 if (verify == true && ExpectedIndexHash.empty() == true && (*Target)->IsOptional() == false)
1404 {
1405 Status = StatAuthError;
1406 strprintf(ErrorText, _("Unable to find hash sum for '%s' in Release file"), (*Target)->MetaKey.c_str());
1407 return;
1408 }
1409 }
1410
1411 if ((*Target)->IsOptional() == true)
1412 {
1413 if ((*Target)->IsSubIndex() == true)
1414 new pkgAcqSubIndex(Owner, (*Target)->URI, (*Target)->Description,
1415 (*Target)->ShortDesc, ExpectedIndexHash);
1416 else if (transInRelease == false || Record != NULL || compressedAvailable == true)
1417 {
1418 if (_config->FindB("Acquire::PDiffs",true) == true && transInRelease == true &&
1419 MetaIndexParser->Exists(string((*Target)->MetaKey).append(".diff/Index")) == true)
1420 new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
1421 (*Target)->ShortDesc, ExpectedIndexHash);
1422 else
1423 new pkgAcqIndexTrans(Owner, *Target, ExpectedIndexHash, MetaIndexParser);
1424 }
1425 continue;
1426 }
1427
1428 /* Queue Packages file (either diff or full packages files, depending
1429 on the users option) - we also check if the PDiff Index file is listed
1430 in the Meta-Index file. Ideal would be if pkgAcqDiffIndex would test this
1431 instead, but passing the required info to it is to much hassle */
1432 if(_config->FindB("Acquire::PDiffs",true) == true && (verify == false ||
1433 MetaIndexParser->Exists(string((*Target)->MetaKey).append(".diff/Index")) == true))
1434 new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
1435 (*Target)->ShortDesc, ExpectedIndexHash);
1436 else
1437 new pkgAcqIndex(Owner, *Target, ExpectedIndexHash, MetaIndexParser);
1438 }
1439 }
1440 /*}}}*/
1441 bool pkgAcqMetaIndex::VerifyVendor(string Message) /*{{{*/
1442 {
1443 string::size_type pos;
1444
1445 // check for missing sigs (that where not fatal because otherwise we had
1446 // bombed earlier)
1447 string missingkeys;
1448 string msg = _("There is no public key available for the "
1449 "following key IDs:\n");
1450 pos = Message.find("NO_PUBKEY ");
1451 if (pos != std::string::npos)
1452 {
1453 string::size_type start = pos+strlen("NO_PUBKEY ");
1454 string Fingerprint = Message.substr(start, Message.find("\n")-start);
1455 missingkeys += (Fingerprint);
1456 }
1457 if(!missingkeys.empty())
1458 _error->Warning("%s", string(msg+missingkeys).c_str());
1459
1460 string Transformed = MetaIndexParser->GetExpectedDist();
1461
1462 if (Transformed == "../project/experimental")
1463 {
1464 Transformed = "experimental";
1465 }
1466
1467 pos = Transformed.rfind('/');
1468 if (pos != string::npos)
1469 {
1470 Transformed = Transformed.substr(0, pos);
1471 }
1472
1473 if (Transformed == ".")
1474 {
1475 Transformed = "";
1476 }
1477
1478 if (_config->FindB("Acquire::Check-Valid-Until", true) == true &&
1479 MetaIndexParser->GetValidUntil() > 0) {
1480 time_t const invalid_since = time(NULL) - MetaIndexParser->GetValidUntil();
1481 if (invalid_since > 0)
1482 // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
1483 // the time since then the file is invalid - formated in the same way as in
1484 // the download progress display (e.g. 7d 3h 42min 1s)
1485 return _error->Error(
1486 _("Release file for %s is expired (invalid since %s). "
1487 "Updates for this repository will not be applied."),
1488 RealURI.c_str(), TimeToStr(invalid_since).c_str());
1489 }
1490
1491 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1492 {
1493 std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
1494 std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
1495 std::cerr << "Transformed Dist: " << Transformed << std::endl;
1496 }
1497
1498 if (MetaIndexParser->CheckDist(Transformed) == false)
1499 {
1500 // This might become fatal one day
1501 // Status = StatAuthError;
1502 // ErrorText = "Conflicting distribution; expected "
1503 // + MetaIndexParser->GetExpectedDist() + " but got "
1504 // + MetaIndexParser->GetDist();
1505 // return false;
1506 if (!Transformed.empty())
1507 {
1508 _error->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
1509 Desc.Description.c_str(),
1510 Transformed.c_str(),
1511 MetaIndexParser->GetDist().c_str());
1512 }
1513 }
1514
1515 return true;
1516 }
1517 /*}}}*/
1518 // pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
1519 // ---------------------------------------------------------------------
1520 /* */
1521 void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1522 {
1523 if (AuthPass == true)
1524 {
1525 // gpgv method failed, if we have a good signature
1526 string LastGoodSigFile = _config->FindDir("Dir::State::lists").append("partial/").append(URItoFileName(RealURI));
1527 if (DestFile != SigFile)
1528 LastGoodSigFile.append(".gpg");
1529 LastGoodSigFile.append(".reverify");
1530
1531 if(FileExists(LastGoodSigFile))
1532 {
1533 string VerifiedSigFile = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
1534 if (DestFile != SigFile)
1535 VerifiedSigFile.append(".gpg");
1536 Rename(LastGoodSigFile, VerifiedSigFile);
1537 Status = StatTransientNetworkError;
1538 _error->Warning(_("An error occurred during the signature "
1539 "verification. The repository is not updated "
1540 "and the previous index files will be used. "
1541 "GPG error: %s: %s\n"),
1542 Desc.Description.c_str(),
1543 LookupTag(Message,"Message").c_str());
1544 RunScripts("APT::Update::Auth-Failure");
1545 return;
1546 } else if (LookupTag(Message,"Message").find("NODATA") != string::npos) {
1547 /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */
1548 _error->Error(_("GPG error: %s: %s"),
1549 Desc.Description.c_str(),
1550 LookupTag(Message,"Message").c_str());
1551 return;
1552 } else {
1553 _error->Warning(_("GPG error: %s: %s"),
1554 Desc.Description.c_str(),
1555 LookupTag(Message,"Message").c_str());
1556 }
1557 // gpgv method failed
1558 ReportMirrorFailure("GPGFailure");
1559 }
1560
1561 /* Always move the meta index, even if gpgv failed. This ensures
1562 * that PackageFile objects are correctly filled in */
1563 if (FileExists(DestFile)) {
1564 string FinalFile = _config->FindDir("Dir::State::lists");
1565 FinalFile += URItoFileName(RealURI);
1566 /* InRelease files become Release files, otherwise
1567 * they would be considered as trusted later on */
1568 if (SigFile == DestFile) {
1569 RealURI = RealURI.replace(RealURI.rfind("InRelease"), 9,
1570 "Release");
1571 FinalFile = FinalFile.replace(FinalFile.rfind("InRelease"), 9,
1572 "Release");
1573 SigFile = FinalFile;
1574 }
1575 Rename(DestFile,FinalFile);
1576 chmod(FinalFile.c_str(),0644);
1577
1578 DestFile = FinalFile;
1579 }
1580
1581 // No Release file was present, or verification failed, so fall
1582 // back to queueing Packages files without verification
1583 QueueIndexes(false);
1584 }
1585 /*}}}*/
1586 pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire *Owner, /*{{{*/
1587 string const &URI, string const &URIDesc, string const &ShortDesc,
1588 string const &MetaIndexURI, string const &MetaIndexURIDesc, string const &MetaIndexShortDesc,
1589 string const &MetaSigURI, string const &MetaSigURIDesc, string const &MetaSigShortDesc,
1590 const vector<struct IndexTarget*>* IndexTargets,
1591 indexRecords* MetaIndexParser) :
1592 pkgAcqMetaIndex(Owner, URI, URIDesc, ShortDesc, "", IndexTargets, MetaIndexParser),
1593 MetaIndexURI(MetaIndexURI), MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
1594 MetaSigURI(MetaSigURI), MetaSigURIDesc(MetaSigURIDesc), MetaSigShortDesc(MetaSigShortDesc)
1595 {
1596 SigFile = DestFile;
1597
1598 // keep the old InRelease around in case of transistent network errors
1599 string const Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
1600 struct stat Buf;
1601 if (stat(Final.c_str(),&Buf) == 0)
1602 {
1603 string const LastGoodSig = DestFile + ".reverify";
1604 Rename(Final,LastGoodSig);
1605 }
1606 }
1607 /*}}}*/
1608 // pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
1609 // ---------------------------------------------------------------------
1610 // FIXME: this can go away once the InRelease file is used widely
1611 string pkgAcqMetaClearSig::Custom600Headers()
1612 {
1613 string Final = _config->FindDir("Dir::State::lists");
1614 Final += URItoFileName(RealURI);
1615
1616 struct stat Buf;
1617 if (stat(Final.c_str(),&Buf) != 0)
1618 {
1619 Final = DestFile + ".reverify";
1620 if (stat(Final.c_str(),&Buf) != 0)
1621 return "\nIndex-File: true\nFail-Ignore: true\n";
1622 }
1623
1624 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
1625 }
1626 /*}}}*/
1627 void pkgAcqMetaClearSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
1628 {
1629 if (AuthPass == false)
1630 {
1631 // Remove the 'old' InRelease file if we try Release.gpg now as otherwise
1632 // the file will stay around and gives a false-auth impression (CVE-2012-0214)
1633 string FinalFile = _config->FindDir("Dir::State::lists");
1634 FinalFile.append(URItoFileName(RealURI));
1635 if (FileExists(FinalFile))
1636 unlink(FinalFile.c_str());
1637
1638 new pkgAcqMetaSig(Owner,
1639 MetaSigURI, MetaSigURIDesc, MetaSigShortDesc,
1640 MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
1641 IndexTargets, MetaIndexParser);
1642 if (Cnf->LocalOnly == true ||
1643 StringToBool(LookupTag(Message, "Transient-Failure"), false) == false)
1644 Dequeue();
1645 }
1646 else
1647 pkgAcqMetaIndex::Failed(Message, Cnf);
1648 }
1649 /*}}}*/
1650 // AcqArchive::AcqArchive - Constructor /*{{{*/
1651 // ---------------------------------------------------------------------
1652 /* This just sets up the initial fetch environment and queues the first
1653 possibilitiy */
1654 pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
1655 pkgRecords *Recs,pkgCache::VerIterator const &Version,
1656 string &StoreFilename) :
1657 Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
1658 StoreFilename(StoreFilename), Vf(Version.FileList()),
1659 Trusted(false)
1660 {
1661 Retries = _config->FindI("Acquire::Retries",0);
1662
1663 if (Version.Arch() == 0)
1664 {
1665 _error->Error(_("I wasn't able to locate a file for the %s package. "
1666 "This might mean you need to manually fix this package. "
1667 "(due to missing arch)"),
1668 Version.ParentPkg().Name());
1669 return;
1670 }
1671
1672 /* We need to find a filename to determine the extension. We make the
1673 assumption here that all the available sources for this version share
1674 the same extension.. */
1675 // Skip not source sources, they do not have file fields.
1676 for (; Vf.end() == false; ++Vf)
1677 {
1678 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1679 continue;
1680 break;
1681 }
1682
1683 // Does not really matter here.. we are going to fail out below
1684 if (Vf.end() != true)
1685 {
1686 // If this fails to get a file name we will bomb out below.
1687 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
1688 if (_error->PendingError() == true)
1689 return;
1690
1691 // Generate the final file name as: package_version_arch.foo
1692 StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
1693 QuoteString(Version.VerStr(),"_:") + '_' +
1694 QuoteString(Version.Arch(),"_:.") +
1695 "." + flExtension(Parse.FileName());
1696 }
1697
1698 // check if we have one trusted source for the package. if so, switch
1699 // to "TrustedOnly" mode
1700 for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; ++i)
1701 {
1702 pkgIndexFile *Index;
1703 if (Sources->FindIndex(i.File(),Index) == false)
1704 continue;
1705 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1706 {
1707 std::cerr << "Checking index: " << Index->Describe()
1708 << "(Trusted=" << Index->IsTrusted() << ")\n";
1709 }
1710 if (Index->IsTrusted()) {
1711 Trusted = true;
1712 break;
1713 }
1714 }
1715
1716 // "allow-unauthenticated" restores apts old fetching behaviour
1717 // that means that e.g. unauthenticated file:// uris are higher
1718 // priority than authenticated http:// uris
1719 if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
1720 Trusted = false;
1721
1722 // Select a source
1723 if (QueueNext() == false && _error->PendingError() == false)
1724 _error->Error(_("I wasn't able to locate a file for the %s package. "
1725 "This might mean you need to manually fix this package."),
1726 Version.ParentPkg().Name());
1727 }
1728 /*}}}*/
1729 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1730 // ---------------------------------------------------------------------
1731 /* This queues the next available file version for download. It checks if
1732 the archive is already available in the cache and stashs the MD5 for
1733 checking later. */
1734 bool pkgAcqArchive::QueueNext()
1735 {
1736 string const ForceHash = _config->Find("Acquire::ForceHash");
1737 for (; Vf.end() == false; ++Vf)
1738 {
1739 // Ignore not source sources
1740 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1741 continue;
1742
1743 // Try to cross match against the source list
1744 pkgIndexFile *Index;
1745 if (Sources->FindIndex(Vf.File(),Index) == false)
1746 continue;
1747
1748 // only try to get a trusted package from another source if that source
1749 // is also trusted
1750 if(Trusted && !Index->IsTrusted())
1751 continue;
1752
1753 // Grab the text package record
1754 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
1755 if (_error->PendingError() == true)
1756 return false;
1757
1758 string PkgFile = Parse.FileName();
1759 if (ForceHash.empty() == false)
1760 {
1761 if(stringcasecmp(ForceHash, "sha512") == 0)
1762 ExpectedHash = HashString("SHA512", Parse.SHA512Hash());
1763 else if(stringcasecmp(ForceHash, "sha256") == 0)
1764 ExpectedHash = HashString("SHA256", Parse.SHA256Hash());
1765 else if (stringcasecmp(ForceHash, "sha1") == 0)
1766 ExpectedHash = HashString("SHA1", Parse.SHA1Hash());
1767 else
1768 ExpectedHash = HashString("MD5Sum", Parse.MD5Hash());
1769 }
1770 else
1771 {
1772 string Hash;
1773 if ((Hash = Parse.SHA512Hash()).empty() == false)
1774 ExpectedHash = HashString("SHA512", Hash);
1775 else if ((Hash = Parse.SHA256Hash()).empty() == false)
1776 ExpectedHash = HashString("SHA256", Hash);
1777 else if ((Hash = Parse.SHA1Hash()).empty() == false)
1778 ExpectedHash = HashString("SHA1", Hash);
1779 else
1780 ExpectedHash = HashString("MD5Sum", Parse.MD5Hash());
1781 }
1782 if (PkgFile.empty() == true)
1783 return _error->Error(_("The package index files are corrupted. No Filename: "
1784 "field for package %s."),
1785 Version.ParentPkg().Name());
1786
1787 Desc.URI = Index->ArchiveURI(PkgFile);
1788 Desc.Description = Index->ArchiveInfo(Version);
1789 Desc.Owner = this;
1790 Desc.ShortDesc = Version.ParentPkg().Name();
1791
1792 // See if we already have the file. (Legacy filenames)
1793 FileSize = Version->Size;
1794 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
1795 struct stat Buf;
1796 if (stat(FinalFile.c_str(),&Buf) == 0)
1797 {
1798 // Make sure the size matches
1799 if ((unsigned long long)Buf.st_size == Version->Size)
1800 {
1801 Complete = true;
1802 Local = true;
1803 Status = StatDone;
1804 StoreFilename = DestFile = FinalFile;
1805 return true;
1806 }
1807
1808 /* Hmm, we have a file and its size does not match, this means it is
1809 an old style mismatched arch */
1810 unlink(FinalFile.c_str());
1811 }
1812
1813 // Check it again using the new style output filenames
1814 FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
1815 if (stat(FinalFile.c_str(),&Buf) == 0)
1816 {
1817 // Make sure the size matches
1818 if ((unsigned long long)Buf.st_size == Version->Size)
1819 {
1820 Complete = true;
1821 Local = true;
1822 Status = StatDone;
1823 StoreFilename = DestFile = FinalFile;
1824 return true;
1825 }
1826
1827 /* Hmm, we have a file and its size does not match, this shouldnt
1828 happen.. */
1829 unlink(FinalFile.c_str());
1830 }
1831
1832 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
1833
1834 // Check the destination file
1835 if (stat(DestFile.c_str(),&Buf) == 0)
1836 {
1837 // Hmm, the partial file is too big, erase it
1838 if ((unsigned long long)Buf.st_size > Version->Size)
1839 unlink(DestFile.c_str());
1840 else
1841 PartialSize = Buf.st_size;
1842 }
1843
1844 // Disables download of archives - useful if no real installation follows,
1845 // e.g. if we are just interested in proposed installation order
1846 if (_config->FindB("Debug::pkgAcqArchive::NoQueue", false) == true)
1847 {
1848 Complete = true;
1849 Local = true;
1850 Status = StatDone;
1851 StoreFilename = DestFile = FinalFile;
1852 return true;
1853 }
1854
1855 // Create the item
1856 Local = false;
1857 Desc.URI = Index->ArchiveURI(PkgFile);
1858 Desc.Description = Index->ArchiveInfo(Version);
1859 Desc.Owner = this;
1860 Desc.ShortDesc = Version.ParentPkg().Name();
1861 QueueURI(Desc);
1862
1863 ++Vf;
1864 return true;
1865 }
1866 return false;
1867 }
1868 /*}}}*/
1869 // AcqArchive::Done - Finished fetching /*{{{*/
1870 // ---------------------------------------------------------------------
1871 /* */
1872 void pkgAcqArchive::Done(string Message,unsigned long long Size,string CalcHash,
1873 pkgAcquire::MethodConfig *Cfg)
1874 {
1875 Item::Done(Message,Size,CalcHash,Cfg);
1876
1877 // Check the size
1878 if (Size != Version->Size)
1879 {
1880 Status = StatError;
1881 ErrorText = _("Size mismatch");
1882 return;
1883 }
1884
1885 // Check the hash
1886 if(ExpectedHash.toStr() != CalcHash)
1887 {
1888 Status = StatError;
1889 ErrorText = _("Hash Sum mismatch");
1890 if(FileExists(DestFile))
1891 Rename(DestFile,DestFile + ".FAILED");
1892 return;
1893 }
1894
1895 // Grab the output filename
1896 string FileName = LookupTag(Message,"Filename");
1897 if (FileName.empty() == true)
1898 {
1899 Status = StatError;
1900 ErrorText = "Method gave a blank filename";
1901 return;
1902 }
1903
1904 Complete = true;
1905
1906 // Reference filename
1907 if (FileName != DestFile)
1908 {
1909 StoreFilename = DestFile = FileName;
1910 Local = true;
1911 return;
1912 }
1913
1914 // Done, move it into position
1915 string FinalFile = _config->FindDir("Dir::Cache::Archives");
1916 FinalFile += flNotDir(StoreFilename);
1917 Rename(DestFile,FinalFile);
1918
1919 StoreFilename = DestFile = FinalFile;
1920 Complete = true;
1921 }
1922 /*}}}*/
1923 // AcqArchive::Failed - Failure handler /*{{{*/
1924 // ---------------------------------------------------------------------
1925 /* Here we try other sources */
1926 void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1927 {
1928 ErrorText = LookupTag(Message,"Message");
1929
1930 /* We don't really want to retry on failed media swaps, this prevents
1931 that. An interesting observation is that permanent failures are not
1932 recorded. */
1933 if (Cnf->Removable == true &&
1934 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1935 {
1936 // Vf = Version.FileList();
1937 while (Vf.end() == false) ++Vf;
1938 StoreFilename = string();
1939 Item::Failed(Message,Cnf);
1940 return;
1941 }
1942
1943 if (QueueNext() == false)
1944 {
1945 // This is the retry counter
1946 if (Retries != 0 &&
1947 Cnf->LocalOnly == false &&
1948 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1949 {
1950 Retries--;
1951 Vf = Version.FileList();
1952 if (QueueNext() == true)
1953 return;
1954 }
1955
1956 StoreFilename = string();
1957 Item::Failed(Message,Cnf);
1958 }
1959 }
1960 /*}}}*/
1961 // AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
1962 // ---------------------------------------------------------------------
1963 bool pkgAcqArchive::IsTrusted()
1964 {
1965 return Trusted;
1966 }
1967 /*}}}*/
1968 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1969 // ---------------------------------------------------------------------
1970 /* */
1971 void pkgAcqArchive::Finished()
1972 {
1973 if (Status == pkgAcquire::Item::StatDone &&
1974 Complete == true)
1975 return;
1976 StoreFilename = string();
1977 }
1978 /*}}}*/
1979 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1980 // ---------------------------------------------------------------------
1981 /* The file is added to the queue */
1982 pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string Hash,
1983 unsigned long long Size,string Dsc,string ShortDesc,
1984 const string &DestDir, const string &DestFilename,
1985 bool IsIndexFile) :
1986 Item(Owner), ExpectedHash(Hash), IsIndexFile(IsIndexFile)
1987 {
1988 Retries = _config->FindI("Acquire::Retries",0);
1989
1990 if(!DestFilename.empty())
1991 DestFile = DestFilename;
1992 else if(!DestDir.empty())
1993 DestFile = DestDir + "/" + flNotDir(URI);
1994 else
1995 DestFile = flNotDir(URI);
1996
1997 // Create the item
1998 Desc.URI = URI;
1999 Desc.Description = Dsc;
2000 Desc.Owner = this;
2001
2002 // Set the short description to the archive component
2003 Desc.ShortDesc = ShortDesc;
2004
2005 // Get the transfer sizes
2006 FileSize = Size;
2007 struct stat Buf;
2008 if (stat(DestFile.c_str(),&Buf) == 0)
2009 {
2010 // Hmm, the partial file is too big, erase it
2011 if ((unsigned long long)Buf.st_size > Size)
2012 unlink(DestFile.c_str());
2013 else
2014 PartialSize = Buf.st_size;
2015 }
2016
2017 QueueURI(Desc);
2018 }
2019 /*}}}*/
2020 // AcqFile::Done - Item downloaded OK /*{{{*/
2021 // ---------------------------------------------------------------------
2022 /* */
2023 void pkgAcqFile::Done(string Message,unsigned long long Size,string CalcHash,
2024 pkgAcquire::MethodConfig *Cnf)
2025 {
2026 Item::Done(Message,Size,CalcHash,Cnf);
2027
2028 // Check the hash
2029 if(!ExpectedHash.empty() && ExpectedHash.toStr() != CalcHash)
2030 {
2031 Status = StatError;
2032 ErrorText = _("Hash Sum mismatch");
2033 Rename(DestFile,DestFile + ".FAILED");
2034 return;
2035 }
2036
2037 string FileName = LookupTag(Message,"Filename");
2038 if (FileName.empty() == true)
2039 {
2040 Status = StatError;
2041 ErrorText = "Method gave a blank filename";
2042 return;
2043 }
2044
2045 Complete = true;
2046
2047 // The files timestamp matches
2048 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
2049 return;
2050
2051 // We have to copy it into place
2052 if (FileName != DestFile)
2053 {
2054 Local = true;
2055 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
2056 Cnf->Removable == true)
2057 {
2058 Desc.URI = "copy:" + FileName;
2059 QueueURI(Desc);
2060 return;
2061 }
2062
2063 // Erase the file if it is a symlink so we can overwrite it
2064 struct stat St;
2065 if (lstat(DestFile.c_str(),&St) == 0)
2066 {
2067 if (S_ISLNK(St.st_mode) != 0)
2068 unlink(DestFile.c_str());
2069 }
2070
2071 // Symlink the file
2072 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
2073 {
2074 ErrorText = "Link to " + DestFile + " failure ";
2075 Status = StatError;
2076 Complete = false;
2077 }
2078 }
2079 }
2080 /*}}}*/
2081 // AcqFile::Failed - Failure handler /*{{{*/
2082 // ---------------------------------------------------------------------
2083 /* Here we try other sources */
2084 void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
2085 {
2086 ErrorText = LookupTag(Message,"Message");
2087
2088 // This is the retry counter
2089 if (Retries != 0 &&
2090 Cnf->LocalOnly == false &&
2091 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
2092 {
2093 Retries--;
2094 QueueURI(Desc);
2095 return;
2096 }
2097
2098 Item::Failed(Message,Cnf);
2099 }
2100 /*}}}*/
2101 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
2102 // ---------------------------------------------------------------------
2103 /* The only header we use is the last-modified header. */
2104 string pkgAcqFile::Custom600Headers()
2105 {
2106 if (IsIndexFile)
2107 return "\nIndex-File: true";
2108 return "";
2109 }
2110 /*}}}*/