]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-item.cc
c5b701fdcb30613ab539176bb035301621db031b
[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/tagfile.h>
26 #include <apt-pkg/metaindex.h>
27 #include <apt-pkg/acquire.h>
28 #include <apt-pkg/hashes.h>
29 #include <apt-pkg/indexfile.h>
30 #include <apt-pkg/pkgcache.h>
31 #include <apt-pkg/cacheiterators.h>
32 #include <apt-pkg/pkgrecords.h>
33 #include <apt-pkg/gpgv.h>
34
35 #include <algorithm>
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <iostream>
40 #include <vector>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <string>
45 #include <stdio.h>
46 #include <ctime>
47 #include <sstream>
48
49 #include <apti18n.h>
50 /*}}}*/
51
52 using namespace std;
53
54 static void printHashSumComparision(std::string const &URI, HashStringList const &Expected, HashStringList const &Actual) /*{{{*/
55 {
56 if (_config->FindB("Debug::Acquire::HashSumMismatch", false) == false)
57 return;
58 std::cerr << std::endl << URI << ":" << std::endl << " Expected Hash: " << std::endl;
59 for (HashStringList::const_iterator hs = Expected.begin(); hs != Expected.end(); ++hs)
60 std::cerr << "\t- " << hs->toStr() << std::endl;
61 std::cerr << " Actual Hash: " << std::endl;
62 for (HashStringList::const_iterator hs = Actual.begin(); hs != Actual.end(); ++hs)
63 std::cerr << "\t- " << hs->toStr() << std::endl;
64 }
65 /*}}}*/
66 static std::string GetPartialFileName(std::string const &file) /*{{{*/
67 {
68 std::string DestFile = _config->FindDir("Dir::State::lists") + "partial/";
69 DestFile += file;
70 return DestFile;
71 }
72 /*}}}*/
73 static std::string GetPartialFileNameFromURI(std::string const &uri) /*{{{*/
74 {
75 return GetPartialFileName(URItoFileName(uri));
76 }
77 /*}}}*/
78 static std::string GetFinalFileNameFromURI(std::string const &uri) /*{{{*/
79 {
80 return _config->FindDir("Dir::State::lists") + URItoFileName(uri);
81 }
82 /*}}}*/
83 static std::string GetKeepCompressedFileName(std::string file, IndexTarget const &Target)/*{{{*/
84 {
85 if (Target.KeepCompressed == false)
86 return file;
87
88 std::string const CompressionTypes = Target.Option(IndexTarget::COMPRESSIONTYPES);
89 if (CompressionTypes.empty() == false)
90 {
91 std::string const ext = CompressionTypes.substr(0, CompressionTypes.find(' '));
92 if (ext != "uncompressed")
93 file.append(".").append(ext);
94 }
95 return file;
96 }
97 /*}}}*/
98 static std::string GetCompressedFileName(IndexTarget const &Target, std::string const &Name, std::string const &Ext) /*{{{*/
99 {
100 if (Ext.empty() || Ext == "uncompressed")
101 return Name;
102
103 // do not reverify cdrom sources as apt-cdrom may rewrite the Packages
104 // file when its doing the indexcopy
105 if (Target.URI.substr(0,6) == "cdrom:")
106 return Name;
107
108 // adjust DestFile if its compressed on disk
109 if (Target.KeepCompressed == true)
110 return Name + '.' + Ext;
111 return Name;
112 }
113 /*}}}*/
114 static std::string GetMergeDiffsPatchFileName(std::string const &Final, std::string const &Patch)/*{{{*/
115 {
116 // rred expects the patch as $FinalFile.ed.$patchname.gz
117 return Final + ".ed." + Patch + ".gz";
118 }
119 /*}}}*/
120 static std::string GetDiffsPatchFileName(std::string const &Final) /*{{{*/
121 {
122 // rred expects the patch as $FinalFile.ed
123 return Final + ".ed";
124 }
125 /*}}}*/
126 static bool BootstrapPDiffWith(std::string const &PartialFile, std::string const &FinalFile, IndexTarget const &Target)/*{{{*/
127 {
128 // patching needs to be bootstrapped with the 'old' version
129 std::vector<std::string> types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' ');
130 auto typeItr = types.cbegin();
131 for (; typeItr != types.cend(); ++typeItr)
132 {
133 std::string Final = FinalFile;
134 if (*typeItr != "uncompressed")
135 Final.append(".").append(*typeItr);
136 if (RealFileExists(Final) == false)
137 continue;
138 std::string Partial = PartialFile;
139 if (*typeItr != "uncompressed")
140 Partial.append(".").append(*typeItr);
141 if (FileExists(Partial.c_str()) == true)
142 return true;
143 if (symlink(Final.c_str(), Partial.c_str()) != 0)
144 return false;
145 break;
146 }
147 return typeItr != types.cend();
148 }
149 /*}}}*/
150
151 static bool AllowInsecureRepositories(metaIndex const * const MetaIndexParser, pkgAcqMetaClearSig * const TransactionManager, pkgAcquire::Item * const I) /*{{{*/
152 {
153 if(MetaIndexParser->GetTrusted() == metaIndex::TRI_YES || _config->FindB("Acquire::AllowInsecureRepositories") == true)
154 return true;
155
156 _error->Error(_("Use --allow-insecure-repositories to force the update"));
157 TransactionManager->AbortTransaction();
158 I->Status = pkgAcquire::Item::StatError;
159 return false;
160 }
161 /*}}}*/
162 static HashStringList GetExpectedHashesFromFor(metaIndex * const Parser, std::string const &MetaKey)/*{{{*/
163 {
164 if (Parser == NULL)
165 return HashStringList();
166 metaIndex::checkSum * const R = Parser->Lookup(MetaKey);
167 if (R == NULL)
168 return HashStringList();
169 return R->Hashes;
170 }
171 /*}}}*/
172
173 // all ::HashesRequired and ::GetExpectedHashes implementations /*{{{*/
174 /* ::GetExpectedHashes is abstract and has to be implemented by all subclasses.
175 It is best to implement it as broadly as possible, while ::HashesRequired defaults
176 to true and should be as restrictive as possible for false cases. Note that if
177 a hash is returned by ::GetExpectedHashes it must match. Only if it doesn't
178 ::HashesRequired is called to evaluate if its okay to have no hashes. */
179 APT_CONST bool pkgAcqTransactionItem::HashesRequired() const
180 {
181 /* signed repositories obviously have a parser and good hashes.
182 unsigned repositories, too, as even if we can't trust them for security,
183 we can at least trust them for integrity of the download itself.
184 Only repositories without a Release file can (obviously) not have
185 hashes – and they are very uncommon and strongly discouraged */
186 return TransactionManager->MetaIndexParser != NULL &&
187 TransactionManager->MetaIndexParser->GetLoadedSuccessfully() != metaIndex::TRI_UNSET;
188 }
189 HashStringList pkgAcqTransactionItem::GetExpectedHashes() const
190 {
191 return GetExpectedHashesFor(GetMetaKey());
192 }
193
194 APT_CONST bool pkgAcqMetaBase::HashesRequired() const
195 {
196 // Release and co have no hashes 'by design'.
197 return false;
198 }
199 HashStringList pkgAcqMetaBase::GetExpectedHashes() const
200 {
201 return HashStringList();
202 }
203
204 APT_CONST bool pkgAcqIndexDiffs::HashesRequired() const
205 {
206 /* We don't always have the diff of the downloaded pdiff file.
207 What we have for sure is hashes for the uncompressed file,
208 but rred uncompresses them on the fly while parsing, so not handled here.
209 Hashes are (also) checked while searching for (next) patch to apply. */
210 if (State == StateFetchDiff)
211 return available_patches[0].download_hashes.empty() == false;
212 return false;
213 }
214 HashStringList pkgAcqIndexDiffs::GetExpectedHashes() const
215 {
216 if (State == StateFetchDiff)
217 return available_patches[0].download_hashes;
218 return HashStringList();
219 }
220
221 APT_CONST bool pkgAcqIndexMergeDiffs::HashesRequired() const
222 {
223 /* @see #pkgAcqIndexDiffs::HashesRequired, with the difference that
224 we can check the rred result after all patches are applied as
225 we know the expected result rather than potentially apply more patches */
226 if (State == StateFetchDiff)
227 return patch.download_hashes.empty() == false;
228 return State == StateApplyDiff;
229 }
230 HashStringList pkgAcqIndexMergeDiffs::GetExpectedHashes() const
231 {
232 if (State == StateFetchDiff)
233 return patch.download_hashes;
234 else if (State == StateApplyDiff)
235 return GetExpectedHashesFor(Target.MetaKey);
236 return HashStringList();
237 }
238
239 APT_CONST bool pkgAcqArchive::HashesRequired() const
240 {
241 return LocalSource == false;
242 }
243 HashStringList pkgAcqArchive::GetExpectedHashes() const
244 {
245 // figured out while parsing the records
246 return ExpectedHashes;
247 }
248
249 APT_CONST bool pkgAcqFile::HashesRequired() const
250 {
251 // supplied as parameter at creation time, so the caller decides
252 return ExpectedHashes.usable();
253 }
254 HashStringList pkgAcqFile::GetExpectedHashes() const
255 {
256 return ExpectedHashes;
257 }
258 /*}}}*/
259 // Acquire::Item::QueueURI and specialisations from child classes /*{{{*/
260 bool pkgAcquire::Item::QueueURI(pkgAcquire::ItemDesc &Item)
261 {
262 Owner->Enqueue(Item);
263 return true;
264 }
265 /* The idea here is that an item isn't queued if it exists on disk and the
266 transition manager was a hit as this means that the files it contains
267 the checksums for can't be updated either (or they are and we are asking
268 for a hashsum mismatch to happen which helps nobody) */
269 bool pkgAcqTransactionItem::QueueURI(pkgAcquire::ItemDesc &Item)
270 {
271 std::string const FinalFile = GetFinalFilename();
272 if (TransactionManager != NULL && TransactionManager->IMSHit == true &&
273 FileExists(FinalFile) == true)
274 {
275 PartialFile = DestFile = FinalFile;
276 Status = StatDone;
277 return false;
278 }
279 return pkgAcquire::Item::QueueURI(Item);
280 }
281 /* The transition manager InRelease itself (or its older sisters-in-law
282 Release & Release.gpg) is always queued as this allows us to rerun gpgv
283 on it to verify that we aren't stalled with old files */
284 bool pkgAcqMetaBase::QueueURI(pkgAcquire::ItemDesc &Item)
285 {
286 return pkgAcquire::Item::QueueURI(Item);
287 }
288 /* the Diff/Index needs to queue also the up-to-date complete index file
289 to ensure that the list cleaner isn't eating it */
290 bool pkgAcqDiffIndex::QueueURI(pkgAcquire::ItemDesc &Item)
291 {
292 if (pkgAcqTransactionItem::QueueURI(Item) == true)
293 return true;
294 QueueOnIMSHit();
295 return false;
296 }
297 /*}}}*/
298 // Acquire::Item::GetFinalFilename and specialisations for child classes /*{{{*/
299 std::string pkgAcquire::Item::GetFinalFilename() const
300 {
301 return GetFinalFileNameFromURI(Desc.URI);
302 }
303 std::string pkgAcqDiffIndex::GetFinalFilename() const
304 {
305 // the logic we inherent from pkgAcqBaseIndex isn't what we need here
306 return pkgAcquire::Item::GetFinalFilename();
307 }
308 std::string pkgAcqIndex::GetFinalFilename() const
309 {
310 std::string const FinalFile = GetFinalFileNameFromURI(Target.URI);
311 return GetCompressedFileName(Target, FinalFile, CurrentCompressionExtension);
312 }
313 std::string pkgAcqMetaSig::GetFinalFilename() const
314 {
315 return GetFinalFileNameFromURI(Target.URI);
316 }
317 std::string pkgAcqBaseIndex::GetFinalFilename() const
318 {
319 return GetFinalFileNameFromURI(Target.URI);
320 }
321 std::string pkgAcqMetaBase::GetFinalFilename() const
322 {
323 return GetFinalFileNameFromURI(Target.URI);
324 }
325 std::string pkgAcqArchive::GetFinalFilename() const
326 {
327 return _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
328 }
329 /*}}}*/
330 // pkgAcqTransactionItem::GetMetaKey and specialisations for child classes /*{{{*/
331 std::string pkgAcqTransactionItem::GetMetaKey() const
332 {
333 return Target.MetaKey;
334 }
335 std::string pkgAcqIndex::GetMetaKey() const
336 {
337 if (Stage == STAGE_DECOMPRESS_AND_VERIFY || CurrentCompressionExtension == "uncompressed")
338 return Target.MetaKey;
339 return Target.MetaKey + "." + CurrentCompressionExtension;
340 }
341 std::string pkgAcqDiffIndex::GetMetaKey() const
342 {
343 return Target.MetaKey + ".diff/Index";
344 }
345 /*}}}*/
346 //pkgAcqTransactionItem::TransactionState and specialisations for child classes /*{{{*/
347 bool pkgAcqTransactionItem::TransactionState(TransactionStates const state)
348 {
349 bool const Debug = _config->FindB("Debug::Acquire::Transaction", false);
350 switch(state)
351 {
352 case TransactionAbort:
353 if(Debug == true)
354 std::clog << " Cancel: " << DestFile << std::endl;
355 if (Status == pkgAcquire::Item::StatIdle)
356 {
357 Status = pkgAcquire::Item::StatDone;
358 Dequeue();
359 }
360 break;
361 case TransactionCommit:
362 if(PartialFile != "")
363 {
364 if(Debug == true)
365 std::clog << "mv " << PartialFile << " -> "<< DestFile << " # " << DescURI() << std::endl;
366
367 Rename(PartialFile, DestFile);
368 } else {
369 if(Debug == true)
370 std::clog << "rm " << DestFile << " # " << DescURI() << std::endl;
371 unlink(DestFile.c_str());
372 }
373 break;
374 }
375 return true;
376 }
377 bool pkgAcqMetaBase::TransactionState(TransactionStates const state)
378 {
379 // Do not remove InRelease on IMSHit of Release.gpg [yes, this is very edgecasey]
380 if (TransactionManager->IMSHit == false)
381 return pkgAcqTransactionItem::TransactionState(state);
382 return true;
383 }
384 bool pkgAcqIndex::TransactionState(TransactionStates const state)
385 {
386 if (pkgAcqTransactionItem::TransactionState(state) == false)
387 return false;
388
389 switch (state)
390 {
391 case TransactionAbort:
392 if (Stage == STAGE_DECOMPRESS_AND_VERIFY)
393 {
394 // keep the compressed file, but drop the decompressed
395 EraseFileName.clear();
396 if (PartialFile.empty() == false && flExtension(PartialFile) == "decomp")
397 unlink(PartialFile.c_str());
398 }
399 break;
400 case TransactionCommit:
401 if (EraseFileName.empty() == false)
402 unlink(EraseFileName.c_str());
403 break;
404 }
405 return true;
406 }
407 bool pkgAcqDiffIndex::TransactionState(TransactionStates const state)
408 {
409 if (pkgAcqTransactionItem::TransactionState(state) == false)
410 return false;
411
412 switch (state)
413 {
414 case TransactionCommit:
415 break;
416 case TransactionAbort:
417 std::string const Partial = GetPartialFileNameFromURI(Target.URI);
418 unlink(Partial.c_str());
419 break;
420 }
421
422 return true;
423 }
424 /*}}}*/
425
426 class APT_HIDDEN NoActionItem : public pkgAcquire::Item /*{{{*/
427 /* The sole purpose of this class is having an item which does nothing to
428 reach its done state to prevent cleanup deleting the mentioned file.
429 Handy in cases in which we know we have the file already, like IMS-Hits. */
430 {
431 IndexTarget const Target;
432 public:
433 virtual std::string DescURI() const APT_OVERRIDE {return Target.URI;};
434 virtual HashStringList GetExpectedHashes() const APT_OVERRIDE {return HashStringList();};
435
436 NoActionItem(pkgAcquire * const Owner, IndexTarget const &Target) :
437 pkgAcquire::Item(Owner), Target(Target)
438 {
439 Status = StatDone;
440 DestFile = GetFinalFileNameFromURI(Target.URI);
441 }
442 NoActionItem(pkgAcquire * const Owner, IndexTarget const &Target, std::string const &FinalFile) :
443 pkgAcquire::Item(Owner), Target(Target)
444 {
445 Status = StatDone;
446 DestFile = FinalFile;
447 }
448 };
449 /*}}}*/
450
451 // Acquire::Item::Item - Constructor /*{{{*/
452 APT_IGNORE_DEPRECATED_PUSH
453 pkgAcquire::Item::Item(pkgAcquire * const owner) :
454 FileSize(0), PartialSize(0), Mode(0), ID(0), Complete(false), Local(false),
455 QueueCounter(0), ExpectedAdditionalItems(0), Owner(owner), d(NULL)
456 {
457 Owner->Add(this);
458 Status = StatIdle;
459 }
460 APT_IGNORE_DEPRECATED_POP
461 /*}}}*/
462 // Acquire::Item::~Item - Destructor /*{{{*/
463 pkgAcquire::Item::~Item()
464 {
465 Owner->Remove(this);
466 }
467 /*}}}*/
468 std::string pkgAcquire::Item::Custom600Headers() const /*{{{*/
469 {
470 return std::string();
471 }
472 /*}}}*/
473 std::string pkgAcquire::Item::ShortDesc() const /*{{{*/
474 {
475 return DescURI();
476 }
477 /*}}}*/
478 APT_CONST void pkgAcquire::Item::Finished() /*{{{*/
479 {
480 }
481 /*}}}*/
482 APT_PURE pkgAcquire * pkgAcquire::Item::GetOwner() const /*{{{*/
483 {
484 return Owner;
485 }
486 /*}}}*/
487 APT_CONST pkgAcquire::ItemDesc &pkgAcquire::Item::GetItemDesc() /*{{{*/
488 {
489 return Desc;
490 }
491 /*}}}*/
492 APT_CONST bool pkgAcquire::Item::IsTrusted() const /*{{{*/
493 {
494 return false;
495 }
496 /*}}}*/
497 // Acquire::Item::Failed - Item failed to download /*{{{*/
498 // ---------------------------------------------------------------------
499 /* We return to an idle state if there are still other queues that could
500 fetch this object */
501 void pkgAcquire::Item::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)
502 {
503 if(ErrorText.empty())
504 ErrorText = LookupTag(Message,"Message");
505 if (QueueCounter <= 1)
506 {
507 /* This indicates that the file is not available right now but might
508 be sometime later. If we do a retry cycle then this should be
509 retried [CDROMs] */
510 if (Cnf != NULL && Cnf->LocalOnly == true &&
511 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
512 {
513 Status = StatIdle;
514 Dequeue();
515 return;
516 }
517
518 switch (Status)
519 {
520 case StatIdle:
521 case StatFetching:
522 case StatDone:
523 Status = StatError;
524 break;
525 case StatAuthError:
526 case StatError:
527 case StatTransientNetworkError:
528 break;
529 }
530 Complete = false;
531 Dequeue();
532 }
533
534 string const FailReason = LookupTag(Message, "FailReason");
535 if (FailReason == "MaximumSizeExceeded")
536 RenameOnError(MaximumSizeExceeded);
537 else if (Status == StatAuthError)
538 RenameOnError(HashSumMismatch);
539
540 // report mirror failure back to LP if we actually use a mirror
541 if (FailReason.empty() == false)
542 ReportMirrorFailure(FailReason);
543 else
544 ReportMirrorFailure(ErrorText);
545
546 if (QueueCounter > 1)
547 Status = StatIdle;
548 }
549 /*}}}*/
550 // Acquire::Item::Start - Item has begun to download /*{{{*/
551 // ---------------------------------------------------------------------
552 /* Stash status and the file size. Note that setting Complete means
553 sub-phases of the acquire process such as decompresion are operating */
554 void pkgAcquire::Item::Start(string const &/*Message*/, unsigned long long const Size)
555 {
556 Status = StatFetching;
557 ErrorText.clear();
558 if (FileSize == 0 && Complete == false)
559 FileSize = Size;
560 }
561 /*}}}*/
562 // Acquire::Item::VerifyDone - check if Item was downloaded OK /*{{{*/
563 /* Note that hash-verification is 'hardcoded' in acquire-worker and has
564 * already passed if this method is called. */
565 bool pkgAcquire::Item::VerifyDone(std::string const &Message,
566 pkgAcquire::MethodConfig const * const /*Cnf*/)
567 {
568 std::string const FileName = LookupTag(Message,"Filename");
569 if (FileName.empty() == true)
570 {
571 Status = StatError;
572 ErrorText = "Method gave a blank filename";
573 return false;
574 }
575
576 return true;
577 }
578 /*}}}*/
579 // Acquire::Item::Done - Item downloaded OK /*{{{*/
580 void pkgAcquire::Item::Done(string const &/*Message*/, HashStringList const &Hashes,
581 pkgAcquire::MethodConfig const * const /*Cnf*/)
582 {
583 // We just downloaded something..
584 if (FileSize == 0)
585 {
586 unsigned long long const downloadedSize = Hashes.FileSize();
587 if (downloadedSize != 0)
588 {
589 FileSize = downloadedSize;
590 }
591 }
592 Status = StatDone;
593 ErrorText = string();
594 Owner->Dequeue(this);
595 }
596 /*}}}*/
597 // Acquire::Item::Rename - Rename a file /*{{{*/
598 // ---------------------------------------------------------------------
599 /* This helper function is used by a lot of item methods as their final
600 step */
601 bool pkgAcquire::Item::Rename(string const &From,string const &To)
602 {
603 if (From == To || rename(From.c_str(),To.c_str()) == 0)
604 return true;
605
606 std::string S;
607 strprintf(S, _("rename failed, %s (%s -> %s)."), strerror(errno),
608 From.c_str(),To.c_str());
609 Status = StatError;
610 if (ErrorText.empty())
611 ErrorText = S;
612 else
613 ErrorText = ErrorText + ": " + S;
614 return false;
615 }
616 /*}}}*/
617 void pkgAcquire::Item::Dequeue() /*{{{*/
618 {
619 Owner->Dequeue(this);
620 }
621 /*}}}*/
622 bool pkgAcquire::Item::RenameOnError(pkgAcquire::Item::RenameOnErrorState const error)/*{{{*/
623 {
624 if (RealFileExists(DestFile))
625 Rename(DestFile, DestFile + ".FAILED");
626
627 std::string errtext;
628 switch (error)
629 {
630 case HashSumMismatch:
631 errtext = _("Hash Sum mismatch");
632 Status = StatAuthError;
633 ReportMirrorFailure("HashChecksumFailure");
634 break;
635 case SizeMismatch:
636 errtext = _("Size mismatch");
637 Status = StatAuthError;
638 ReportMirrorFailure("SizeFailure");
639 break;
640 case InvalidFormat:
641 errtext = _("Invalid file format");
642 Status = StatError;
643 // do not report as usually its not the mirrors fault, but Portal/Proxy
644 break;
645 case SignatureError:
646 errtext = _("Signature error");
647 Status = StatError;
648 break;
649 case NotClearsigned:
650 strprintf(errtext, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NOSPLIT");
651 Status = StatAuthError;
652 break;
653 case MaximumSizeExceeded:
654 // the method is expected to report a good error for this
655 Status = StatError;
656 break;
657 case PDiffError:
658 // no handling here, done by callers
659 break;
660 }
661 if (ErrorText.empty())
662 ErrorText = errtext;
663 return false;
664 }
665 /*}}}*/
666 void pkgAcquire::Item::SetActiveSubprocess(const std::string &subprocess)/*{{{*/
667 {
668 ActiveSubprocess = subprocess;
669 APT_IGNORE_DEPRECATED(Mode = ActiveSubprocess.c_str();)
670 }
671 /*}}}*/
672 // Acquire::Item::ReportMirrorFailure /*{{{*/
673 void pkgAcquire::Item::ReportMirrorFailure(string const &FailCode)
674 {
675 // we only act if a mirror was used at all
676 if(UsedMirror.empty())
677 return;
678 #if 0
679 std::cerr << "\nReportMirrorFailure: "
680 << UsedMirror
681 << " Uri: " << DescURI()
682 << " FailCode: "
683 << FailCode << std::endl;
684 #endif
685 string report = _config->Find("Methods::Mirror::ProblemReporting",
686 "/usr/lib/apt/apt-report-mirror-failure");
687 if(!FileExists(report))
688 return;
689
690 std::vector<char const*> Args;
691 Args.push_back(report.c_str());
692 Args.push_back(UsedMirror.c_str());
693 Args.push_back(DescURI().c_str());
694 Args.push_back(FailCode.c_str());
695 Args.push_back(NULL);
696
697 pid_t pid = ExecFork();
698 if(pid < 0)
699 {
700 _error->Error("ReportMirrorFailure Fork failed");
701 return;
702 }
703 else if(pid == 0)
704 {
705 execvp(Args[0], (char**)Args.data());
706 std::cerr << "Could not exec " << Args[0] << std::endl;
707 _exit(100);
708 }
709 if(!ExecWait(pid, "report-mirror-failure"))
710 {
711 _error->Warning("Couldn't report problem to '%s'",
712 _config->Find("Methods::Mirror::ProblemReporting").c_str());
713 }
714 }
715 /*}}}*/
716 std::string pkgAcquire::Item::HashSum() const /*{{{*/
717 {
718 HashStringList const hashes = GetExpectedHashes();
719 HashString const * const hs = hashes.find(NULL);
720 return hs != NULL ? hs->toStr() : "";
721 }
722 /*}}}*/
723
724 pkgAcqTransactionItem::pkgAcqTransactionItem(pkgAcquire * const Owner, /*{{{*/
725 pkgAcqMetaClearSig * const transactionManager, IndexTarget const &target) :
726 pkgAcquire::Item(Owner), d(NULL), Target(target), TransactionManager(transactionManager)
727 {
728 if (TransactionManager != this)
729 TransactionManager->Add(this);
730 }
731 /*}}}*/
732 pkgAcqTransactionItem::~pkgAcqTransactionItem() /*{{{*/
733 {
734 }
735 /*}}}*/
736 HashStringList pkgAcqTransactionItem::GetExpectedHashesFor(std::string const &MetaKey) const /*{{{*/
737 {
738 return GetExpectedHashesFromFor(TransactionManager->MetaIndexParser, MetaKey);
739 }
740 /*}}}*/
741
742 // AcqMetaBase - Constructor /*{{{*/
743 pkgAcqMetaBase::pkgAcqMetaBase(pkgAcquire * const Owner,
744 pkgAcqMetaClearSig * const TransactionManager,
745 std::vector<IndexTarget> const &IndexTargets,
746 IndexTarget const &DataTarget)
747 : pkgAcqTransactionItem(Owner, TransactionManager, DataTarget), d(NULL),
748 IndexTargets(IndexTargets),
749 AuthPass(false), IMSHit(false)
750 {
751 }
752 /*}}}*/
753 // AcqMetaBase::Add - Add a item to the current Transaction /*{{{*/
754 void pkgAcqMetaBase::Add(pkgAcqTransactionItem * const I)
755 {
756 Transaction.push_back(I);
757 }
758 /*}}}*/
759 // AcqMetaBase::AbortTransaction - Abort the current Transaction /*{{{*/
760 void pkgAcqMetaBase::AbortTransaction()
761 {
762 if(_config->FindB("Debug::Acquire::Transaction", false) == true)
763 std::clog << "AbortTransaction: " << TransactionManager << std::endl;
764
765 // ensure the toplevel is in error state too
766 for (std::vector<pkgAcqTransactionItem*>::iterator I = Transaction.begin();
767 I != Transaction.end(); ++I)
768 {
769 (*I)->TransactionState(TransactionAbort);
770 }
771 Transaction.clear();
772 }
773 /*}}}*/
774 // AcqMetaBase::TransactionHasError - Check for errors in Transaction /*{{{*/
775 APT_PURE bool pkgAcqMetaBase::TransactionHasError() const
776 {
777 for (std::vector<pkgAcqTransactionItem*>::const_iterator I = Transaction.begin();
778 I != Transaction.end(); ++I)
779 {
780 switch((*I)->Status) {
781 case StatDone: break;
782 case StatIdle: break;
783 case StatAuthError: return true;
784 case StatError: return true;
785 case StatTransientNetworkError: return true;
786 case StatFetching: break;
787 }
788 }
789 return false;
790 }
791 /*}}}*/
792 // AcqMetaBase::CommitTransaction - Commit a transaction /*{{{*/
793 void pkgAcqMetaBase::CommitTransaction()
794 {
795 if(_config->FindB("Debug::Acquire::Transaction", false) == true)
796 std::clog << "CommitTransaction: " << this << std::endl;
797
798 // move new files into place *and* remove files that are not
799 // part of the transaction but are still on disk
800 for (std::vector<pkgAcqTransactionItem*>::iterator I = Transaction.begin();
801 I != Transaction.end(); ++I)
802 {
803 (*I)->TransactionState(TransactionCommit);
804 }
805 Transaction.clear();
806 }
807 /*}}}*/
808 // AcqMetaBase::TransactionStageCopy - Stage a file for copying /*{{{*/
809 void pkgAcqMetaBase::TransactionStageCopy(pkgAcqTransactionItem * const I,
810 const std::string &From,
811 const std::string &To)
812 {
813 I->PartialFile = From;
814 I->DestFile = To;
815 }
816 /*}}}*/
817 // AcqMetaBase::TransactionStageRemoval - Stage a file for removal /*{{{*/
818 void pkgAcqMetaBase::TransactionStageRemoval(pkgAcqTransactionItem * const I,
819 const std::string &FinalFile)
820 {
821 I->PartialFile = "";
822 I->DestFile = FinalFile;
823 }
824 /*}}}*/
825 // AcqMetaBase::GenerateAuthWarning - Check gpg authentication error /*{{{*/
826 bool pkgAcqMetaBase::CheckStopAuthentication(pkgAcquire::Item * const I, const std::string &Message)
827 {
828 // FIXME: this entire function can do now that we disallow going to
829 // a unauthenticated state and can cleanly rollback
830
831 string const Final = I->GetFinalFilename();
832 if(FileExists(Final))
833 {
834 I->Status = StatTransientNetworkError;
835 _error->Warning(_("An error occurred during the signature "
836 "verification. The repository is not updated "
837 "and the previous index files will be used. "
838 "GPG error: %s: %s"),
839 Desc.Description.c_str(),
840 LookupTag(Message,"Message").c_str());
841 RunScripts("APT::Update::Auth-Failure");
842 return true;
843 } else if (LookupTag(Message,"Message").find("NODATA") != string::npos) {
844 /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */
845 _error->Error(_("GPG error: %s: %s"),
846 Desc.Description.c_str(),
847 LookupTag(Message,"Message").c_str());
848 I->Status = StatAuthError;
849 return true;
850 } else {
851 _error->Warning(_("GPG error: %s: %s"),
852 Desc.Description.c_str(),
853 LookupTag(Message,"Message").c_str());
854 }
855 // gpgv method failed
856 ReportMirrorFailure("GPGFailure");
857 return false;
858 }
859 /*}}}*/
860 // AcqMetaBase::Custom600Headers - Get header for AcqMetaBase /*{{{*/
861 // ---------------------------------------------------------------------
862 string pkgAcqMetaBase::Custom600Headers() const
863 {
864 std::string Header = "\nIndex-File: true";
865 std::string MaximumSize;
866 strprintf(MaximumSize, "\nMaximum-Size: %i",
867 _config->FindI("Acquire::MaxReleaseFileSize", 10*1000*1000));
868 Header += MaximumSize;
869
870 string const FinalFile = GetFinalFilename();
871 struct stat Buf;
872 if (stat(FinalFile.c_str(),&Buf) == 0)
873 Header += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
874
875 return Header;
876 }
877 /*}}}*/
878 // AcqMetaBase::QueueForSignatureVerify /*{{{*/
879 void pkgAcqMetaBase::QueueForSignatureVerify(pkgAcqTransactionItem * const I, std::string const &File, std::string const &Signature)
880 {
881 AuthPass = true;
882 I->Desc.URI = "gpgv:" + Signature;
883 I->DestFile = File;
884 QueueURI(I->Desc);
885 I->SetActiveSubprocess("gpgv");
886 }
887 /*}}}*/
888 // AcqMetaBase::CheckDownloadDone /*{{{*/
889 bool pkgAcqMetaBase::CheckDownloadDone(pkgAcqTransactionItem * const I, const std::string &Message, HashStringList const &Hashes) const
890 {
891 // We have just finished downloading a Release file (it is not
892 // verified yet)
893
894 std::string const FileName = LookupTag(Message,"Filename");
895 if (FileName != I->DestFile && RealFileExists(I->DestFile) == false)
896 {
897 I->Local = true;
898 I->Desc.URI = "copy:" + FileName;
899 I->QueueURI(I->Desc);
900 return false;
901 }
902
903 // make sure to verify against the right file on I-M-S hit
904 bool IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"), false);
905 if (IMSHit == false && Hashes.usable())
906 {
907 // detect IMS-Hits servers haven't detected by Hash comparison
908 std::string const FinalFile = I->GetFinalFilename();
909 if (RealFileExists(FinalFile) && Hashes.VerifyFile(FinalFile) == true)
910 {
911 IMSHit = true;
912 unlink(I->DestFile.c_str());
913 }
914 }
915
916 if(IMSHit == true)
917 {
918 // for simplicity, the transaction manager is always InRelease
919 // even if it doesn't exist.
920 if (TransactionManager != NULL)
921 TransactionManager->IMSHit = true;
922 I->PartialFile = I->DestFile = I->GetFinalFilename();
923 }
924
925 // set Item to complete as the remaining work is all local (verify etc)
926 I->Complete = true;
927
928 return true;
929 }
930 /*}}}*/
931 bool pkgAcqMetaBase::CheckAuthDone(string const &Message) /*{{{*/
932 {
933 // At this point, the gpgv method has succeeded, so there is a
934 // valid signature from a key in the trusted keyring. We
935 // perform additional verification of its contents, and use them
936 // to verify the indexes we are about to download
937
938 if (TransactionManager->IMSHit == false)
939 {
940 // open the last (In)Release if we have it
941 std::string const FinalFile = GetFinalFilename();
942 std::string FinalRelease;
943 std::string FinalInRelease;
944 if (APT::String::Endswith(FinalFile, "InRelease"))
945 {
946 FinalInRelease = FinalFile;
947 FinalRelease = FinalFile.substr(0, FinalFile.length() - strlen("InRelease")) + "Release";
948 }
949 else
950 {
951 FinalInRelease = FinalFile.substr(0, FinalFile.length() - strlen("Release")) + "InRelease";
952 FinalRelease = FinalFile;
953 }
954 if (RealFileExists(FinalInRelease) || RealFileExists(FinalRelease))
955 {
956 TransactionManager->LastMetaIndexParser = TransactionManager->MetaIndexParser->UnloadedClone();
957 if (TransactionManager->LastMetaIndexParser != NULL)
958 {
959 _error->PushToStack();
960 if (RealFileExists(FinalInRelease))
961 TransactionManager->LastMetaIndexParser->Load(FinalInRelease, NULL);
962 else
963 TransactionManager->LastMetaIndexParser->Load(FinalRelease, NULL);
964 // its unlikely to happen, but if what we have is bad ignore it
965 if (_error->PendingError())
966 {
967 delete TransactionManager->LastMetaIndexParser;
968 TransactionManager->LastMetaIndexParser = NULL;
969 }
970 _error->RevertToStack();
971 }
972 }
973 }
974
975 if (TransactionManager->MetaIndexParser->Load(DestFile, &ErrorText) == false)
976 {
977 Status = StatAuthError;
978 return false;
979 }
980
981 if (!VerifyVendor(Message))
982 {
983 Status = StatAuthError;
984 return false;
985 }
986
987 if (_config->FindB("Debug::pkgAcquire::Auth", false))
988 std::cerr << "Signature verification succeeded: "
989 << DestFile << std::endl;
990
991 // Download further indexes with verification
992 QueueIndexes(true);
993
994 return true;
995 }
996 /*}}}*/
997 void pkgAcqMetaBase::QueueIndexes(bool const verify) /*{{{*/
998 {
999 // at this point the real Items are loaded in the fetcher
1000 ExpectedAdditionalItems = 0;
1001
1002 for (std::vector <IndexTarget>::iterator Target = IndexTargets.begin();
1003 Target != IndexTargets.end();
1004 ++Target)
1005 {
1006 bool trypdiff = Target->OptionBool(IndexTarget::PDIFFS);
1007 if (verify == true)
1008 {
1009 if (TransactionManager->MetaIndexParser->Exists(Target->MetaKey) == false)
1010 {
1011 // optional targets that we do not have in the Release file are skipped
1012 if (Target->IsOptional)
1013 continue;
1014
1015 Status = StatAuthError;
1016 strprintf(ErrorText, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), Target->MetaKey.c_str());
1017 return;
1018 }
1019
1020 // autoselect the compression method
1021 std::vector<std::string> types = VectorizeString(Target->Option(IndexTarget::COMPRESSIONTYPES), ' ');
1022 types.erase(std::remove_if(types.begin(), types.end(), [&](std::string const &t) {
1023 if (t == "uncompressed")
1024 return TransactionManager->MetaIndexParser->Exists(Target->MetaKey) == false;
1025 std::string const MetaKey = Target->MetaKey + "." + t;
1026 return TransactionManager->MetaIndexParser->Exists(MetaKey) == false;
1027 }), types.end());
1028 if (types.empty() == false)
1029 {
1030 std::ostringstream os;
1031 std::copy(types.begin(), types.end()-1, std::ostream_iterator<std::string>(os, " "));
1032 os << *types.rbegin();
1033 Target->Options["COMPRESSIONTYPES"] = os.str();
1034 }
1035 else
1036 Target->Options["COMPRESSIONTYPES"].clear();
1037
1038 std::string filename = GetFinalFileNameFromURI(Target->URI);
1039 if (RealFileExists(filename) == false)
1040 {
1041 if (Target->KeepCompressed)
1042 {
1043 filename = GetKeepCompressedFileName(filename, *Target);
1044 if (RealFileExists(filename) == false)
1045 filename.clear();
1046 }
1047 else
1048 filename.clear();
1049 }
1050
1051 if (filename.empty() == false)
1052 {
1053 // if the Release file is a hit and we have an index it must be the current one
1054 if (TransactionManager->IMSHit == true)
1055 ;
1056 else if (TransactionManager->LastMetaIndexParser != NULL)
1057 {
1058 // see if the file changed since the last Release file
1059 // we use the uncompressed files as we might compress differently compared to the server,
1060 // so the hashes might not match, even if they contain the same data.
1061 HashStringList const newFile = GetExpectedHashesFromFor(TransactionManager->MetaIndexParser, Target->MetaKey);
1062 HashStringList const oldFile = GetExpectedHashesFromFor(TransactionManager->LastMetaIndexParser, Target->MetaKey);
1063 if (newFile != oldFile)
1064 filename.clear();
1065 }
1066 else
1067 filename.clear();
1068 }
1069 else
1070 trypdiff = false; // no file to patch
1071
1072 if (filename.empty() == false)
1073 {
1074 new NoActionItem(Owner, *Target, filename);
1075 continue;
1076 }
1077
1078 // check if we have patches available
1079 trypdiff &= TransactionManager->MetaIndexParser->Exists(Target->MetaKey + ".diff/Index");
1080 }
1081 else
1082 {
1083 // if we have no file to patch, no point in trying
1084 std::string filename = GetFinalFileNameFromURI(Target->URI);
1085 if (RealFileExists(filename) == false)
1086 {
1087 if (Target->KeepCompressed)
1088 {
1089 filename = GetKeepCompressedFileName(filename, *Target);
1090 if (RealFileExists(filename) == false)
1091 filename.clear();
1092 }
1093 else
1094 filename.clear();
1095 }
1096 trypdiff &= (filename.empty() == false);
1097 }
1098
1099 // no point in patching from local sources
1100 if (trypdiff)
1101 {
1102 std::string const proto = Target->URI.substr(0, strlen("file:/"));
1103 if (proto == "file:/" || proto == "copy:/" || proto == "cdrom:")
1104 trypdiff = false;
1105 }
1106
1107 // Queue the Index file (Packages, Sources, Translation-$foo, …)
1108 if (trypdiff)
1109 new pkgAcqDiffIndex(Owner, TransactionManager, *Target);
1110 else
1111 new pkgAcqIndex(Owner, TransactionManager, *Target);
1112 }
1113 }
1114 /*}}}*/
1115 bool pkgAcqMetaBase::VerifyVendor(string const &Message) /*{{{*/
1116 {
1117 string::size_type pos;
1118
1119 // check for missing sigs (that where not fatal because otherwise we had
1120 // bombed earlier)
1121 string missingkeys;
1122 string msg = _("There is no public key available for the "
1123 "following key IDs:\n");
1124 pos = Message.find("NO_PUBKEY ");
1125 if (pos != std::string::npos)
1126 {
1127 string::size_type start = pos+strlen("NO_PUBKEY ");
1128 string Fingerprint = Message.substr(start, Message.find("\n")-start);
1129 missingkeys += (Fingerprint);
1130 }
1131 if(!missingkeys.empty())
1132 _error->Warning("%s", (msg + missingkeys).c_str());
1133
1134 string Transformed = TransactionManager->MetaIndexParser->GetExpectedDist();
1135
1136 if (Transformed == "../project/experimental")
1137 {
1138 Transformed = "experimental";
1139 }
1140
1141 pos = Transformed.rfind('/');
1142 if (pos != string::npos)
1143 {
1144 Transformed = Transformed.substr(0, pos);
1145 }
1146
1147 if (Transformed == ".")
1148 {
1149 Transformed = "";
1150 }
1151
1152 if (TransactionManager->MetaIndexParser->GetValidUntil() > 0)
1153 {
1154 time_t const invalid_since = time(NULL) - TransactionManager->MetaIndexParser->GetValidUntil();
1155 if (invalid_since > 0)
1156 {
1157 std::string errmsg;
1158 strprintf(errmsg,
1159 // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
1160 // the time since then the file is invalid - formatted in the same way as in
1161 // the download progress display (e.g. 7d 3h 42min 1s)
1162 _("Release file for %s is expired (invalid since %s). "
1163 "Updates for this repository will not be applied."),
1164 Target.URI.c_str(), TimeToStr(invalid_since).c_str());
1165 if (ErrorText.empty())
1166 ErrorText = errmsg;
1167 return _error->Error("%s", errmsg.c_str());
1168 }
1169 }
1170
1171 /* Did we get a file older than what we have? This is a last minute IMS hit and doubles
1172 as a prevention of downgrading us to older (still valid) files */
1173 if (TransactionManager->IMSHit == false && TransactionManager->LastMetaIndexParser != NULL &&
1174 TransactionManager->LastMetaIndexParser->GetDate() > TransactionManager->MetaIndexParser->GetDate())
1175 {
1176 TransactionManager->IMSHit = true;
1177 unlink(DestFile.c_str());
1178 PartialFile = DestFile = GetFinalFilename();
1179 // load the 'old' file in the 'new' one instead of flipping pointers as
1180 // the new one isn't owned by us, while the old one is so cleanup would be confused.
1181 TransactionManager->MetaIndexParser->swapLoad(TransactionManager->LastMetaIndexParser);
1182 delete TransactionManager->LastMetaIndexParser;
1183 TransactionManager->LastMetaIndexParser = NULL;
1184 }
1185
1186 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1187 {
1188 std::cerr << "Got Codename: " << TransactionManager->MetaIndexParser->GetCodename() << std::endl;
1189 std::cerr << "Expecting Dist: " << TransactionManager->MetaIndexParser->GetExpectedDist() << std::endl;
1190 std::cerr << "Transformed Dist: " << Transformed << std::endl;
1191 }
1192
1193 if (TransactionManager->MetaIndexParser->CheckDist(Transformed) == false)
1194 {
1195 // This might become fatal one day
1196 // Status = StatAuthError;
1197 // ErrorText = "Conflicting distribution; expected "
1198 // + MetaIndexParser->GetExpectedDist() + " but got "
1199 // + MetaIndexParser->GetCodename();
1200 // return false;
1201 if (!Transformed.empty())
1202 {
1203 _error->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
1204 Desc.Description.c_str(),
1205 Transformed.c_str(),
1206 TransactionManager->MetaIndexParser->GetCodename().c_str());
1207 }
1208 }
1209
1210 return true;
1211 }
1212 /*}}}*/
1213 pkgAcqMetaBase::~pkgAcqMetaBase()
1214 {
1215 }
1216
1217 pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire * const Owner, /*{{{*/
1218 IndexTarget const &ClearsignedTarget,
1219 IndexTarget const &DetachedDataTarget, IndexTarget const &DetachedSigTarget,
1220 std::vector<IndexTarget> const &IndexTargets,
1221 metaIndex * const MetaIndexParser) :
1222 pkgAcqMetaIndex(Owner, this, ClearsignedTarget, DetachedSigTarget, IndexTargets),
1223 d(NULL), ClearsignedTarget(ClearsignedTarget),
1224 DetachedDataTarget(DetachedDataTarget),
1225 MetaIndexParser(MetaIndexParser), LastMetaIndexParser(NULL)
1226 {
1227 // index targets + (worst case:) Release/Release.gpg
1228 ExpectedAdditionalItems = IndexTargets.size() + 2;
1229 TransactionManager->Add(this);
1230 }
1231 /*}}}*/
1232 pkgAcqMetaClearSig::~pkgAcqMetaClearSig() /*{{{*/
1233 {
1234 if (LastMetaIndexParser != NULL)
1235 delete LastMetaIndexParser;
1236 }
1237 /*}}}*/
1238 // pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
1239 string pkgAcqMetaClearSig::Custom600Headers() const
1240 {
1241 string Header = pkgAcqMetaBase::Custom600Headers();
1242 Header += "\nFail-Ignore: true";
1243 std::string const key = TransactionManager->MetaIndexParser->GetSignedBy();
1244 if (key.empty() == false)
1245 Header += "\nSigned-By: " + key;
1246
1247 return Header;
1248 }
1249 /*}}}*/
1250 bool pkgAcqMetaClearSig::VerifyDone(std::string const &Message,
1251 pkgAcquire::MethodConfig const * const Cnf)
1252 {
1253 Item::VerifyDone(Message, Cnf);
1254
1255 if (FileExists(DestFile) && !StartsWithGPGClearTextSignature(DestFile))
1256 return RenameOnError(NotClearsigned);
1257
1258 return true;
1259 }
1260 // pkgAcqMetaClearSig::Done - We got a file /*{{{*/
1261 void pkgAcqMetaClearSig::Done(std::string const &Message,
1262 HashStringList const &Hashes,
1263 pkgAcquire::MethodConfig const * const Cnf)
1264 {
1265 Item::Done(Message, Hashes, Cnf);
1266
1267 if(AuthPass == false)
1268 {
1269 if(CheckDownloadDone(this, Message, Hashes) == true)
1270 QueueForSignatureVerify(this, DestFile, DestFile);
1271 return;
1272 }
1273 else if(CheckAuthDone(Message) == true)
1274 {
1275 if (TransactionManager->IMSHit == false)
1276 TransactionManager->TransactionStageCopy(this, DestFile, GetFinalFilename());
1277 else if (RealFileExists(GetFinalFilename()) == false)
1278 {
1279 // We got an InRelease file IMSHit, but we haven't one, which means
1280 // we had a valid Release/Release.gpg combo stepping in, which we have
1281 // to 'acquire' now to ensure list cleanup isn't removing them
1282 new NoActionItem(Owner, DetachedDataTarget);
1283 new NoActionItem(Owner, DetachedSigTarget);
1284 }
1285 }
1286 }
1287 /*}}}*/
1288 void pkgAcqMetaClearSig::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf) /*{{{*/
1289 {
1290 Item::Failed(Message, Cnf);
1291
1292 // we failed, we will not get additional items from this method
1293 ExpectedAdditionalItems = 0;
1294
1295 if (AuthPass == false)
1296 {
1297 if (Status == StatAuthError)
1298 {
1299 // if we expected a ClearTextSignature (InRelease) and got a file,
1300 // but it wasn't valid we end up here (see VerifyDone).
1301 // As these is usually called by web-portals we do not try Release/Release.gpg
1302 // as this is gonna fail anyway and instead abort our try (LP#346386)
1303 TransactionManager->AbortTransaction();
1304 return;
1305 }
1306
1307 // Queue the 'old' InRelease file for removal if we try Release.gpg
1308 // as otherwise the file will stay around and gives a false-auth
1309 // impression (CVE-2012-0214)
1310 TransactionManager->TransactionStageRemoval(this, GetFinalFilename());
1311 Status = StatDone;
1312
1313 new pkgAcqMetaIndex(Owner, TransactionManager, DetachedDataTarget, DetachedSigTarget, IndexTargets);
1314 }
1315 else
1316 {
1317 if(CheckStopAuthentication(this, Message))
1318 return;
1319
1320 _error->Warning(_("The data from '%s' is not signed. Packages "
1321 "from that repository can not be authenticated."),
1322 ClearsignedTarget.Description.c_str());
1323
1324 // No Release file was present, or verification failed, so fall
1325 // back to queueing Packages files without verification
1326 // only allow going further if the users explicitely wants it
1327 if(AllowInsecureRepositories(TransactionManager->MetaIndexParser, TransactionManager, this) == true)
1328 {
1329 Status = StatDone;
1330
1331 /* InRelease files become Release files, otherwise
1332 * they would be considered as trusted later on */
1333 string const FinalRelease = GetFinalFileNameFromURI(DetachedDataTarget.URI);
1334 string const PartialRelease = GetPartialFileNameFromURI(DetachedDataTarget.URI);
1335 string const FinalReleasegpg = GetFinalFileNameFromURI(DetachedSigTarget.URI);
1336 string const FinalInRelease = GetFinalFilename();
1337 Rename(DestFile, PartialRelease);
1338 TransactionManager->TransactionStageCopy(this, PartialRelease, FinalRelease);
1339
1340 if (RealFileExists(FinalReleasegpg) || RealFileExists(FinalInRelease))
1341 {
1342 // open the last Release if we have it
1343 if (TransactionManager->IMSHit == false)
1344 {
1345 TransactionManager->LastMetaIndexParser = TransactionManager->MetaIndexParser->UnloadedClone();
1346 if (TransactionManager->LastMetaIndexParser != NULL)
1347 {
1348 _error->PushToStack();
1349 if (RealFileExists(FinalInRelease))
1350 TransactionManager->LastMetaIndexParser->Load(FinalInRelease, NULL);
1351 else
1352 TransactionManager->LastMetaIndexParser->Load(FinalRelease, NULL);
1353 // its unlikely to happen, but if what we have is bad ignore it
1354 if (_error->PendingError())
1355 {
1356 delete TransactionManager->LastMetaIndexParser;
1357 TransactionManager->LastMetaIndexParser = NULL;
1358 }
1359 _error->RevertToStack();
1360 }
1361 }
1362 }
1363
1364 // we parse the indexes here because at this point the user wanted
1365 // a repository that may potentially harm him
1366 if (TransactionManager->MetaIndexParser->Load(PartialRelease, &ErrorText) == false || VerifyVendor(Message) == false)
1367 /* expired Release files are still a problem you need extra force for */;
1368 else
1369 QueueIndexes(true);
1370 }
1371 }
1372 }
1373 /*}}}*/
1374
1375 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire * const Owner, /*{{{*/
1376 pkgAcqMetaClearSig * const TransactionManager,
1377 IndexTarget const &DataTarget,
1378 IndexTarget const &DetachedSigTarget,
1379 vector<IndexTarget> const &IndexTargets) :
1380 pkgAcqMetaBase(Owner, TransactionManager, IndexTargets, DataTarget), d(NULL),
1381 DetachedSigTarget(DetachedSigTarget)
1382 {
1383 if(_config->FindB("Debug::Acquire::Transaction", false) == true)
1384 std::clog << "New pkgAcqMetaIndex with TransactionManager "
1385 << this->TransactionManager << std::endl;
1386
1387 DestFile = GetPartialFileNameFromURI(DataTarget.URI);
1388
1389 // Create the item
1390 Desc.Description = DataTarget.Description;
1391 Desc.Owner = this;
1392 Desc.ShortDesc = DataTarget.ShortDesc;
1393 Desc.URI = DataTarget.URI;
1394
1395 // we expect more item
1396 ExpectedAdditionalItems = IndexTargets.size();
1397 QueueURI(Desc);
1398 }
1399 /*}}}*/
1400 void pkgAcqMetaIndex::Done(string const &Message, /*{{{*/
1401 HashStringList const &Hashes,
1402 pkgAcquire::MethodConfig const * const Cfg)
1403 {
1404 Item::Done(Message,Hashes,Cfg);
1405
1406 if(CheckDownloadDone(this, Message, Hashes))
1407 {
1408 // we have a Release file, now download the Signature, all further
1409 // verify/queue for additional downloads will be done in the
1410 // pkgAcqMetaSig::Done() code
1411 new pkgAcqMetaSig(Owner, TransactionManager, DetachedSigTarget, this);
1412 }
1413 }
1414 /*}}}*/
1415 // pkgAcqMetaIndex::Failed - no Release file present /*{{{*/
1416 void pkgAcqMetaIndex::Failed(string const &Message,
1417 pkgAcquire::MethodConfig const * const Cnf)
1418 {
1419 pkgAcquire::Item::Failed(Message, Cnf);
1420 Status = StatDone;
1421
1422 _error->Warning(_("The repository '%s' does not have a Release file. "
1423 "This is deprecated, please contact the owner of the "
1424 "repository."), Target.Description.c_str());
1425
1426 // No Release file was present so fall
1427 // back to queueing Packages files without verification
1428 // only allow going further if the users explicitely wants it
1429 if(AllowInsecureRepositories(TransactionManager->MetaIndexParser, TransactionManager, this) == true)
1430 {
1431 // ensure old Release files are removed
1432 TransactionManager->TransactionStageRemoval(this, GetFinalFilename());
1433
1434 // queue without any kind of hashsum support
1435 QueueIndexes(false);
1436 }
1437 }
1438 /*}}}*/
1439 void pkgAcqMetaIndex::Finished() /*{{{*/
1440 {
1441 if(_config->FindB("Debug::Acquire::Transaction", false) == true)
1442 std::clog << "Finished: " << DestFile <<std::endl;
1443 if(TransactionManager != NULL &&
1444 TransactionManager->TransactionHasError() == false)
1445 TransactionManager->CommitTransaction();
1446 }
1447 /*}}}*/
1448 std::string pkgAcqMetaIndex::DescURI() const /*{{{*/
1449 {
1450 return Target.URI;
1451 }
1452 /*}}}*/
1453 pkgAcqMetaIndex::~pkgAcqMetaIndex() {}
1454
1455 // AcqMetaSig::AcqMetaSig - Constructor /*{{{*/
1456 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire * const Owner,
1457 pkgAcqMetaClearSig * const TransactionManager,
1458 IndexTarget const &Target,
1459 pkgAcqMetaIndex * const MetaIndex) :
1460 pkgAcqTransactionItem(Owner, TransactionManager, Target), d(NULL), MetaIndex(MetaIndex)
1461 {
1462 DestFile = GetPartialFileNameFromURI(Target.URI);
1463
1464 // remove any partial downloaded sig-file in partial/.
1465 // it may confuse proxies and is too small to warrant a
1466 // partial download anyway
1467 unlink(DestFile.c_str());
1468
1469 // set the TransactionManager
1470 if(_config->FindB("Debug::Acquire::Transaction", false) == true)
1471 std::clog << "New pkgAcqMetaSig with TransactionManager "
1472 << TransactionManager << std::endl;
1473
1474 // Create the item
1475 Desc.Description = Target.Description;
1476 Desc.Owner = this;
1477 Desc.ShortDesc = Target.ShortDesc;
1478 Desc.URI = Target.URI;
1479
1480 // If we got a hit for Release, we will get one for Release.gpg too (or obscure errors),
1481 // so we skip the download step and go instantly to verification
1482 if (TransactionManager->IMSHit == true && RealFileExists(GetFinalFilename()))
1483 {
1484 Complete = true;
1485 Status = StatDone;
1486 PartialFile = DestFile = GetFinalFilename();
1487 MetaIndexFileSignature = DestFile;
1488 MetaIndex->QueueForSignatureVerify(this, MetaIndex->DestFile, DestFile);
1489 }
1490 else
1491 QueueURI(Desc);
1492 }
1493 /*}}}*/
1494 pkgAcqMetaSig::~pkgAcqMetaSig() /*{{{*/
1495 {
1496 }
1497 /*}}}*/
1498 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
1499 std::string pkgAcqMetaSig::Custom600Headers() const
1500 {
1501 std::string Header = pkgAcqTransactionItem::Custom600Headers();
1502 std::string const key = TransactionManager->MetaIndexParser->GetSignedBy();
1503 if (key.empty() == false)
1504 Header += "\nSigned-By: " + key;
1505 return Header;
1506 }
1507 /*}}}*/
1508 // AcqMetaSig::Done - The signature was downloaded/verified /*{{{*/
1509 void pkgAcqMetaSig::Done(string const &Message, HashStringList const &Hashes,
1510 pkgAcquire::MethodConfig const * const Cfg)
1511 {
1512 if (MetaIndexFileSignature.empty() == false)
1513 {
1514 DestFile = MetaIndexFileSignature;
1515 MetaIndexFileSignature.clear();
1516 }
1517 Item::Done(Message, Hashes, Cfg);
1518
1519 if(MetaIndex->AuthPass == false)
1520 {
1521 if(MetaIndex->CheckDownloadDone(this, Message, Hashes) == true)
1522 {
1523 // destfile will be modified to point to MetaIndexFile for the
1524 // gpgv method, so we need to save it here
1525 MetaIndexFileSignature = DestFile;
1526 MetaIndex->QueueForSignatureVerify(this, MetaIndex->DestFile, DestFile);
1527 }
1528 return;
1529 }
1530 else if(MetaIndex->CheckAuthDone(Message) == true)
1531 {
1532 if (TransactionManager->IMSHit == false)
1533 {
1534 TransactionManager->TransactionStageCopy(this, DestFile, GetFinalFilename());
1535 TransactionManager->TransactionStageCopy(MetaIndex, MetaIndex->DestFile, MetaIndex->GetFinalFilename());
1536 }
1537 }
1538 }
1539 /*}}}*/
1540 void pkgAcqMetaSig::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/
1541 {
1542 Item::Failed(Message,Cnf);
1543
1544 // check if we need to fail at this point
1545 if (MetaIndex->AuthPass == true && MetaIndex->CheckStopAuthentication(this, Message))
1546 return;
1547
1548 string const FinalRelease = MetaIndex->GetFinalFilename();
1549 string const FinalReleasegpg = GetFinalFilename();
1550 string const FinalInRelease = TransactionManager->GetFinalFilename();
1551
1552 if (RealFileExists(FinalReleasegpg) || RealFileExists(FinalInRelease))
1553 {
1554 std::string downgrade_msg;
1555 strprintf(downgrade_msg, _("The repository '%s' is no longer signed."),
1556 MetaIndex->Target.Description.c_str());
1557 if(_config->FindB("Acquire::AllowDowngradeToInsecureRepositories"))
1558 {
1559 // meh, the users wants to take risks (we still mark the packages
1560 // from this repository as unauthenticated)
1561 _error->Warning("%s", downgrade_msg.c_str());
1562 _error->Warning(_("This is normally not allowed, but the option "
1563 "Acquire::AllowDowngradeToInsecureRepositories was "
1564 "given to override it."));
1565 Status = StatDone;
1566 } else {
1567 _error->Error("%s", downgrade_msg.c_str());
1568 if (TransactionManager->IMSHit == false)
1569 Rename(MetaIndex->DestFile, MetaIndex->DestFile + ".FAILED");
1570 Item::Failed("Message: " + downgrade_msg, Cnf);
1571 TransactionManager->AbortTransaction();
1572 return;
1573 }
1574 }
1575 else
1576 _error->Warning(_("The data from '%s' is not signed. Packages "
1577 "from that repository can not be authenticated."),
1578 MetaIndex->Target.Description.c_str());
1579
1580 // ensures that a Release.gpg file in the lists/ is removed by the transaction
1581 TransactionManager->TransactionStageRemoval(this, DestFile);
1582
1583 // only allow going further if the users explicitely wants it
1584 if(AllowInsecureRepositories(TransactionManager->MetaIndexParser, TransactionManager, this) == true)
1585 {
1586 if (RealFileExists(FinalReleasegpg) || RealFileExists(FinalInRelease))
1587 {
1588 // open the last Release if we have it
1589 if (TransactionManager->IMSHit == false)
1590 {
1591 TransactionManager->LastMetaIndexParser = TransactionManager->MetaIndexParser->UnloadedClone();
1592 if (TransactionManager->LastMetaIndexParser != NULL)
1593 {
1594 _error->PushToStack();
1595 if (RealFileExists(FinalInRelease))
1596 TransactionManager->LastMetaIndexParser->Load(FinalInRelease, NULL);
1597 else
1598 TransactionManager->LastMetaIndexParser->Load(FinalRelease, NULL);
1599 // its unlikely to happen, but if what we have is bad ignore it
1600 if (_error->PendingError())
1601 {
1602 delete TransactionManager->LastMetaIndexParser;
1603 TransactionManager->LastMetaIndexParser = NULL;
1604 }
1605 _error->RevertToStack();
1606 }
1607 }
1608 }
1609
1610 // we parse the indexes here because at this point the user wanted
1611 // a repository that may potentially harm him
1612 if (TransactionManager->MetaIndexParser->Load(MetaIndex->DestFile, &ErrorText) == false || MetaIndex->VerifyVendor(Message) == false)
1613 /* expired Release files are still a problem you need extra force for */;
1614 else
1615 MetaIndex->QueueIndexes(true);
1616
1617 TransactionManager->TransactionStageCopy(MetaIndex, MetaIndex->DestFile, MetaIndex->GetFinalFilename());
1618 }
1619
1620 // FIXME: this is used often (e.g. in pkgAcqIndexTrans) so refactor
1621 if (Cnf->LocalOnly == true ||
1622 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
1623 {
1624 // Ignore this
1625 Status = StatDone;
1626 }
1627 }
1628 /*}}}*/
1629
1630
1631 // AcqBaseIndex - Constructor /*{{{*/
1632 pkgAcqBaseIndex::pkgAcqBaseIndex(pkgAcquire * const Owner,
1633 pkgAcqMetaClearSig * const TransactionManager,
1634 IndexTarget const &Target)
1635 : pkgAcqTransactionItem(Owner, TransactionManager, Target), d(NULL)
1636 {
1637 }
1638 /*}}}*/
1639 pkgAcqBaseIndex::~pkgAcqBaseIndex() {}
1640
1641 // AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
1642 // ---------------------------------------------------------------------
1643 /* Get the DiffIndex file first and see if there are patches available
1644 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
1645 * patches. If anything goes wrong in that process, it will fall back to
1646 * the original packages file
1647 */
1648 pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire * const Owner,
1649 pkgAcqMetaClearSig * const TransactionManager,
1650 IndexTarget const &Target)
1651 : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL), diffs(NULL)
1652 {
1653 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
1654
1655 Desc.Owner = this;
1656 Desc.Description = Target.Description + ".diff/Index";
1657 Desc.ShortDesc = Target.ShortDesc;
1658 Desc.URI = Target.URI + ".diff/Index";
1659
1660 DestFile = GetPartialFileNameFromURI(Desc.URI);
1661
1662 if(Debug)
1663 std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
1664
1665 QueueURI(Desc);
1666 }
1667 /*}}}*/
1668 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
1669 // ---------------------------------------------------------------------
1670 /* The only header we use is the last-modified header. */
1671 string pkgAcqDiffIndex::Custom600Headers() const
1672 {
1673 string const Final = GetFinalFilename();
1674
1675 if(Debug)
1676 std::clog << "Custom600Header-IMS: " << Final << std::endl;
1677
1678 struct stat Buf;
1679 if (stat(Final.c_str(),&Buf) != 0)
1680 return "\nIndex-File: true";
1681
1682 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
1683 }
1684 /*}}}*/
1685 void pkgAcqDiffIndex::QueueOnIMSHit() const /*{{{*/
1686 {
1687 // list cleanup needs to know that this file as well as the already
1688 // present index is ours, so we create an empty diff to save it for us
1689 new pkgAcqIndexDiffs(Owner, TransactionManager, Target);
1690 }
1691 /*}}}*/
1692 bool pkgAcqDiffIndex::ParseDiffIndex(string const &IndexDiffFile) /*{{{*/
1693 {
1694 // failing here is fine: our caller will take care of trying to
1695 // get the complete file if patching fails
1696 if(Debug)
1697 std::clog << "pkgAcqDiffIndex::ParseIndexDiff() " << IndexDiffFile
1698 << std::endl;
1699
1700 FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
1701 pkgTagFile TF(&Fd);
1702 if (Fd.IsOpen() == false || Fd.Failed())
1703 return false;
1704
1705 pkgTagSection Tags;
1706 if(unlikely(TF.Step(Tags) == false))
1707 return false;
1708
1709 HashStringList ServerHashes;
1710 unsigned long long ServerSize = 0;
1711
1712 for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
1713 {
1714 std::string tagname = *type;
1715 tagname.append("-Current");
1716 std::string const tmp = Tags.FindS(tagname.c_str());
1717 if (tmp.empty() == true)
1718 continue;
1719
1720 string hash;
1721 unsigned long long size;
1722 std::stringstream ss(tmp);
1723 ss >> hash >> size;
1724 if (unlikely(hash.empty() == true))
1725 continue;
1726 if (unlikely(ServerSize != 0 && ServerSize != size))
1727 continue;
1728 ServerHashes.push_back(HashString(*type, hash));
1729 ServerSize = size;
1730 }
1731
1732 if (ServerHashes.usable() == false)
1733 {
1734 if (Debug == true)
1735 std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": Did not find a good hashsum in the index" << std::endl;
1736 return false;
1737 }
1738
1739 std::string const CurrentPackagesFile = GetFinalFileNameFromURI(Target.URI);
1740 HashStringList const TargetFileHashes = GetExpectedHashesFor(Target.MetaKey);
1741 if (TargetFileHashes.usable() == false || ServerHashes != TargetFileHashes)
1742 {
1743 if (Debug == true)
1744 {
1745 std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": Index has different hashes than parser, probably older, so fail pdiffing" << std::endl;
1746 printHashSumComparision(CurrentPackagesFile, ServerHashes, TargetFileHashes);
1747 }
1748 return false;
1749 }
1750
1751 HashStringList LocalHashes;
1752 // try avoiding calculating the hash here as this is costly
1753 if (TransactionManager->LastMetaIndexParser != NULL)
1754 LocalHashes = GetExpectedHashesFromFor(TransactionManager->LastMetaIndexParser, Target.MetaKey);
1755 if (LocalHashes.usable() == false)
1756 {
1757 FileFd fd(CurrentPackagesFile, FileFd::ReadOnly, FileFd::Auto);
1758 Hashes LocalHashesCalc(ServerHashes);
1759 LocalHashesCalc.AddFD(fd);
1760 LocalHashes = LocalHashesCalc.GetHashStringList();
1761 }
1762
1763 if (ServerHashes == LocalHashes)
1764 {
1765 // we have the same sha1 as the server so we are done here
1766 if(Debug)
1767 std::clog << "pkgAcqDiffIndex: Package file " << CurrentPackagesFile << " is up-to-date" << std::endl;
1768 QueueOnIMSHit();
1769 return true;
1770 }
1771
1772 if(Debug)
1773 std::clog << "Server-Current: " << ServerHashes.find(NULL)->toStr() << " and we start at "
1774 << CurrentPackagesFile << " " << LocalHashes.FileSize() << " " << LocalHashes.find(NULL)->toStr() << std::endl;
1775
1776 // parse all of (provided) history
1777 vector<DiffInfo> available_patches;
1778 bool firstAcceptedHashes = true;
1779 for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
1780 {
1781 if (LocalHashes.find(*type) == NULL)
1782 continue;
1783
1784 std::string tagname = *type;
1785 tagname.append("-History");
1786 std::string const tmp = Tags.FindS(tagname.c_str());
1787 if (tmp.empty() == true)
1788 continue;
1789
1790 string hash, filename;
1791 unsigned long long size;
1792 std::stringstream ss(tmp);
1793
1794 while (ss >> hash >> size >> filename)
1795 {
1796 if (unlikely(hash.empty() == true || filename.empty() == true))
1797 continue;
1798
1799 // see if we have a record for this file already
1800 std::vector<DiffInfo>::iterator cur = available_patches.begin();
1801 for (; cur != available_patches.end(); ++cur)
1802 {
1803 if (cur->file != filename)
1804 continue;
1805 cur->result_hashes.push_back(HashString(*type, hash));
1806 break;
1807 }
1808 if (cur != available_patches.end())
1809 continue;
1810 if (firstAcceptedHashes == true)
1811 {
1812 DiffInfo next;
1813 next.file = filename;
1814 next.result_hashes.push_back(HashString(*type, hash));
1815 next.result_hashes.FileSize(size);
1816 available_patches.push_back(next);
1817 }
1818 else
1819 {
1820 if (Debug == true)
1821 std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": File " << filename
1822 << " wasn't in the list for the first parsed hash! (history)" << std::endl;
1823 break;
1824 }
1825 }
1826 firstAcceptedHashes = false;
1827 }
1828
1829 if (unlikely(available_patches.empty() == true))
1830 {
1831 if (Debug)
1832 std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": "
1833 << "Couldn't find any patches for the patch series." << std::endl;
1834 return false;
1835 }
1836
1837 for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
1838 {
1839 if (LocalHashes.find(*type) == NULL)
1840 continue;
1841
1842 std::string tagname = *type;
1843 tagname.append("-Patches");
1844 std::string const tmp = Tags.FindS(tagname.c_str());
1845 if (tmp.empty() == true)
1846 continue;
1847
1848 string hash, filename;
1849 unsigned long long size;
1850 std::stringstream ss(tmp);
1851
1852 while (ss >> hash >> size >> filename)
1853 {
1854 if (unlikely(hash.empty() == true || filename.empty() == true))
1855 continue;
1856
1857 // see if we have a record for this file already
1858 std::vector<DiffInfo>::iterator cur = available_patches.begin();
1859 for (; cur != available_patches.end(); ++cur)
1860 {
1861 if (cur->file != filename)
1862 continue;
1863 if (cur->patch_hashes.empty())
1864 cur->patch_hashes.FileSize(size);
1865 cur->patch_hashes.push_back(HashString(*type, hash));
1866 break;
1867 }
1868 if (cur != available_patches.end())
1869 continue;
1870 if (Debug == true)
1871 std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": File " << filename
1872 << " wasn't in the list for the first parsed hash! (patches)" << std::endl;
1873 break;
1874 }
1875 }
1876
1877 for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
1878 {
1879 std::string tagname = *type;
1880 tagname.append("-Download");
1881 std::string const tmp = Tags.FindS(tagname.c_str());
1882 if (tmp.empty() == true)
1883 continue;
1884
1885 string hash, filename;
1886 unsigned long long size;
1887 std::stringstream ss(tmp);
1888
1889 // FIXME: all of pdiff supports only .gz compressed patches
1890 while (ss >> hash >> size >> filename)
1891 {
1892 if (unlikely(hash.empty() == true || filename.empty() == true))
1893 continue;
1894 if (unlikely(APT::String::Endswith(filename, ".gz") == false))
1895 continue;
1896 filename.erase(filename.length() - 3);
1897
1898 // see if we have a record for this file already
1899 std::vector<DiffInfo>::iterator cur = available_patches.begin();
1900 for (; cur != available_patches.end(); ++cur)
1901 {
1902 if (cur->file != filename)
1903 continue;
1904 if (cur->download_hashes.empty())
1905 cur->download_hashes.FileSize(size);
1906 cur->download_hashes.push_back(HashString(*type, hash));
1907 break;
1908 }
1909 if (cur != available_patches.end())
1910 continue;
1911 if (Debug == true)
1912 std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": File " << filename
1913 << " wasn't in the list for the first parsed hash! (download)" << std::endl;
1914 break;
1915 }
1916 }
1917
1918
1919 bool foundStart = false;
1920 for (std::vector<DiffInfo>::iterator cur = available_patches.begin();
1921 cur != available_patches.end(); ++cur)
1922 {
1923 if (LocalHashes != cur->result_hashes)
1924 continue;
1925
1926 available_patches.erase(available_patches.begin(), cur);
1927 foundStart = true;
1928 break;
1929 }
1930
1931 if (foundStart == false || unlikely(available_patches.empty() == true))
1932 {
1933 if (Debug)
1934 std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": "
1935 << "Couldn't find the start of the patch series." << std::endl;
1936 return false;
1937 }
1938
1939 // patching with too many files is rather slow compared to a fast download
1940 unsigned long const fileLimit = _config->FindI("Acquire::PDiffs::FileLimit", 0);
1941 if (fileLimit != 0 && fileLimit < available_patches.size())
1942 {
1943 if (Debug)
1944 std::clog << "Need " << available_patches.size() << " diffs (Limit is " << fileLimit
1945 << ") so fallback to complete download" << std::endl;
1946 return false;
1947 }
1948
1949 // calculate the size of all patches we have to get
1950 // note that all sizes are uncompressed, while we download compressed files
1951 unsigned long long patchesSize = 0;
1952 for (std::vector<DiffInfo>::const_iterator cur = available_patches.begin();
1953 cur != available_patches.end(); ++cur)
1954 patchesSize += cur->patch_hashes.FileSize();
1955 unsigned long long const sizeLimit = ServerSize * _config->FindI("Acquire::PDiffs::SizeLimit", 100);
1956 if (sizeLimit > 0 && (sizeLimit/100) < patchesSize)
1957 {
1958 if (Debug)
1959 std::clog << "Need " << patchesSize << " bytes (Limit is " << sizeLimit/100
1960 << ") so fallback to complete download" << std::endl;
1961 return false;
1962 }
1963
1964 // we have something, queue the diffs
1965 string::size_type const last_space = Description.rfind(" ");
1966 if(last_space != string::npos)
1967 Description.erase(last_space, Description.size()-last_space);
1968
1969 /* decide if we should download patches one by one or in one go:
1970 The first is good if the server merges patches, but many don't so client
1971 based merging can be attempt in which case the second is better.
1972 "bad things" will happen if patches are merged on the server,
1973 but client side merging is attempt as well */
1974 bool pdiff_merge = _config->FindB("Acquire::PDiffs::Merge", true);
1975 if (pdiff_merge == true)
1976 {
1977 // reprepro adds this flag if it has merged patches on the server
1978 std::string const precedence = Tags.FindS("X-Patch-Precedence");
1979 pdiff_merge = (precedence != "merged");
1980 }
1981
1982 if (pdiff_merge == false)
1983 new pkgAcqIndexDiffs(Owner, TransactionManager, Target, available_patches);
1984 else
1985 {
1986 diffs = new std::vector<pkgAcqIndexMergeDiffs*>(available_patches.size());
1987 for(size_t i = 0; i < available_patches.size(); ++i)
1988 (*diffs)[i] = new pkgAcqIndexMergeDiffs(Owner, TransactionManager,
1989 Target,
1990 available_patches[i],
1991 diffs);
1992 }
1993
1994 Complete = false;
1995 Status = StatDone;
1996 Dequeue();
1997 return true;
1998 }
1999 /*}}}*/
2000 void pkgAcqDiffIndex::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/
2001 {
2002 Item::Failed(Message,Cnf);
2003 Status = StatDone;
2004
2005 if(Debug)
2006 std::clog << "pkgAcqDiffIndex failed: " << Desc.URI << " with " << Message << std::endl
2007 << "Falling back to normal index file acquire" << std::endl;
2008
2009 new pkgAcqIndex(Owner, TransactionManager, Target);
2010 }
2011 /*}}}*/
2012 void pkgAcqDiffIndex::Done(string const &Message,HashStringList const &Hashes, /*{{{*/
2013 pkgAcquire::MethodConfig const * const Cnf)
2014 {
2015 if(Debug)
2016 std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl;
2017
2018 Item::Done(Message, Hashes, Cnf);
2019
2020 string const FinalFile = GetFinalFilename();
2021 if(StringToBool(LookupTag(Message,"IMS-Hit"),false))
2022 DestFile = FinalFile;
2023
2024 if(ParseDiffIndex(DestFile) == false)
2025 {
2026 Failed("Message: Couldn't parse pdiff index", Cnf);
2027 // queue for final move - this should happen even if we fail
2028 // while parsing (e.g. on sizelimit) and download the complete file.
2029 TransactionManager->TransactionStageCopy(this, DestFile, FinalFile);
2030 return;
2031 }
2032
2033 TransactionManager->TransactionStageCopy(this, DestFile, FinalFile);
2034
2035 Complete = true;
2036 Status = StatDone;
2037 Dequeue();
2038
2039 return;
2040 }
2041 /*}}}*/
2042 pkgAcqDiffIndex::~pkgAcqDiffIndex()
2043 {
2044 if (diffs != NULL)
2045 delete diffs;
2046 }
2047
2048 // AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
2049 // ---------------------------------------------------------------------
2050 /* The package diff is added to the queue. one object is constructed
2051 * for each diff and the index
2052 */
2053 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire * const Owner,
2054 pkgAcqMetaClearSig * const TransactionManager,
2055 IndexTarget const &Target,
2056 vector<DiffInfo> const &diffs)
2057 : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL),
2058 available_patches(diffs)
2059 {
2060 DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target);
2061
2062 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
2063
2064 Desc.Owner = this;
2065 Description = Target.Description;
2066 Desc.ShortDesc = Target.ShortDesc;
2067
2068 if(available_patches.empty() == true)
2069 {
2070 // we are done (yeah!), check hashes against the final file
2071 DestFile = GetKeepCompressedFileName(GetFinalFileNameFromURI(Target.URI), Target);
2072 Finish(true);
2073 }
2074 else
2075 {
2076 if (BootstrapPDiffWith(GetPartialFileNameFromURI(Target.URI), GetFinalFilename(), Target) == false)
2077 {
2078 Failed("Bootstrapping of " + DestFile + " failed", NULL);
2079 return;
2080 }
2081
2082 // get the next diff
2083 State = StateFetchDiff;
2084 QueueNextDiff();
2085 }
2086 }
2087 /*}}}*/
2088 void pkgAcqIndexDiffs::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/
2089 {
2090 Item::Failed(Message,Cnf);
2091 Status = StatDone;
2092
2093 DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target);
2094 if(Debug)
2095 std::clog << "pkgAcqIndexDiffs failed: " << Desc.URI << " with " << Message << std::endl
2096 << "Falling back to normal index file acquire " << std::endl;
2097 RenameOnError(PDiffError);
2098 std::string const patchname = GetDiffsPatchFileName(DestFile);
2099 if (RealFileExists(patchname))
2100 rename(patchname.c_str(), std::string(patchname + ".FAILED").c_str());
2101 new pkgAcqIndex(Owner, TransactionManager, Target);
2102 Finish();
2103 }
2104 /*}}}*/
2105 // Finish - helper that cleans the item out of the fetcher queue /*{{{*/
2106 void pkgAcqIndexDiffs::Finish(bool allDone)
2107 {
2108 if(Debug)
2109 std::clog << "pkgAcqIndexDiffs::Finish(): "
2110 << allDone << " "
2111 << Desc.URI << std::endl;
2112
2113 // we restore the original name, this is required, otherwise
2114 // the file will be cleaned
2115 if(allDone)
2116 {
2117 std::string Final = GetFinalFilename();
2118 if (Target.KeepCompressed)
2119 {
2120 std::string const ext = flExtension(DestFile);
2121 if (ext.empty() == false)
2122 Final.append(".").append(ext);
2123 }
2124 TransactionManager->TransactionStageCopy(this, DestFile, Final);
2125
2126 // this is for the "real" finish
2127 Complete = true;
2128 Status = StatDone;
2129 Dequeue();
2130 if(Debug)
2131 std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
2132 return;
2133 }
2134 else
2135 DestFile.clear();
2136
2137 if(Debug)
2138 std::clog << "Finishing: " << Desc.URI << std::endl;
2139 Complete = false;
2140 Status = StatDone;
2141 Dequeue();
2142 return;
2143 }
2144 /*}}}*/
2145 bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
2146 {
2147 // calc sha1 of the just patched file
2148 std::string const FinalFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target);
2149 if(!FileExists(FinalFile))
2150 {
2151 Failed("Message: No FinalFile " + FinalFile + " available", NULL);
2152 return false;
2153 }
2154
2155 FileFd fd(FinalFile, FileFd::ReadOnly, FileFd::Extension);
2156 Hashes LocalHashesCalc;
2157 LocalHashesCalc.AddFD(fd);
2158 HashStringList const LocalHashes = LocalHashesCalc.GetHashStringList();
2159
2160 if(Debug)
2161 std::clog << "QueueNextDiff: " << FinalFile << " (" << LocalHashes.find(NULL)->toStr() << ")" << std::endl;
2162
2163 HashStringList const TargetFileHashes = GetExpectedHashesFor(Target.MetaKey);
2164 if (unlikely(LocalHashes.usable() == false || TargetFileHashes.usable() == false))
2165 {
2166 Failed("Local/Expected hashes are not usable", NULL);
2167 return false;
2168 }
2169
2170
2171 // final file reached before all patches are applied
2172 if(LocalHashes == TargetFileHashes)
2173 {
2174 Finish(true);
2175 return true;
2176 }
2177
2178 // remove all patches until the next matching patch is found
2179 // this requires the Index file to be ordered
2180 for(vector<DiffInfo>::iterator I = available_patches.begin();
2181 available_patches.empty() == false &&
2182 I != available_patches.end() &&
2183 I->result_hashes != LocalHashes;
2184 ++I)
2185 {
2186 available_patches.erase(I);
2187 }
2188
2189 // error checking and falling back if no patch was found
2190 if(available_patches.empty() == true)
2191 {
2192 Failed("No patches left to reach target", NULL);
2193 return false;
2194 }
2195
2196 // queue the right diff
2197 Desc.URI = Target.URI + ".diff/" + available_patches[0].file + ".gz";
2198 Desc.Description = Description + " " + available_patches[0].file + string(".pdiff");
2199 DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI + ".diff/" + available_patches[0].file), Target);
2200
2201 if(Debug)
2202 std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
2203
2204 QueueURI(Desc);
2205
2206 return true;
2207 }
2208 /*}}}*/
2209 void pkgAcqIndexDiffs::Done(string const &Message, HashStringList const &Hashes, /*{{{*/
2210 pkgAcquire::MethodConfig const * const Cnf)
2211 {
2212 if(Debug)
2213 std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl;
2214
2215 Item::Done(Message, Hashes, Cnf);
2216
2217 std::string const FinalFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target);
2218 std::string const PatchFile = GetDiffsPatchFileName(FinalFile);
2219
2220 // success in downloading a diff, enter ApplyDiff state
2221 if(State == StateFetchDiff)
2222 {
2223 Rename(DestFile, PatchFile);
2224
2225 if(Debug)
2226 std::clog << "Sending to rred method: " << FinalFile << std::endl;
2227
2228 State = StateApplyDiff;
2229 Local = true;
2230 Desc.URI = "rred:" + FinalFile;
2231 QueueURI(Desc);
2232 SetActiveSubprocess("rred");
2233 return;
2234 }
2235
2236 // success in download/apply a diff, queue next (if needed)
2237 if(State == StateApplyDiff)
2238 {
2239 // remove the just applied patch
2240 available_patches.erase(available_patches.begin());
2241 unlink(PatchFile.c_str());
2242
2243 // move into place
2244 if(Debug)
2245 {
2246 std::clog << "Moving patched file in place: " << std::endl
2247 << DestFile << " -> " << FinalFile << std::endl;
2248 }
2249 Rename(DestFile,FinalFile);
2250 chmod(FinalFile.c_str(),0644);
2251
2252 // see if there is more to download
2253 if(available_patches.empty() == false) {
2254 new pkgAcqIndexDiffs(Owner, TransactionManager, Target,
2255 available_patches);
2256 return Finish();
2257 } else
2258 // update
2259 DestFile = FinalFile;
2260 return Finish(true);
2261 }
2262 }
2263 /*}}}*/
2264 std::string pkgAcqIndexDiffs::Custom600Headers() const /*{{{*/
2265 {
2266 if(State != StateApplyDiff)
2267 return pkgAcqBaseIndex::Custom600Headers();
2268 std::ostringstream patchhashes;
2269 HashStringList const ExpectedHashes = available_patches[0].patch_hashes;
2270 for (HashStringList::const_iterator hs = ExpectedHashes.begin(); hs != ExpectedHashes.end(); ++hs)
2271 patchhashes << "\nPatch-0-" << hs->HashType() << "-Hash: " << hs->HashValue();
2272 patchhashes << pkgAcqBaseIndex::Custom600Headers();
2273 return patchhashes.str();
2274 }
2275 /*}}}*/
2276 pkgAcqIndexDiffs::~pkgAcqIndexDiffs() {}
2277
2278 // AcqIndexMergeDiffs::AcqIndexMergeDiffs - Constructor /*{{{*/
2279 pkgAcqIndexMergeDiffs::pkgAcqIndexMergeDiffs(pkgAcquire * const Owner,
2280 pkgAcqMetaClearSig * const TransactionManager,
2281 IndexTarget const &Target,
2282 DiffInfo const &patch,
2283 std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches)
2284 : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL),
2285 patch(patch), allPatches(allPatches), State(StateFetchDiff)
2286 {
2287 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
2288
2289 Desc.Owner = this;
2290 Description = Target.Description;
2291 Desc.ShortDesc = Target.ShortDesc;
2292
2293 Desc.URI = Target.URI + ".diff/" + patch.file + ".gz";
2294 Desc.Description = Description + " " + patch.file + string(".pdiff");
2295
2296 DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI + ".diff/" + patch.file), Target);
2297
2298 if(Debug)
2299 std::clog << "pkgAcqIndexMergeDiffs: " << Desc.URI << std::endl;
2300
2301 QueueURI(Desc);
2302 }
2303 /*}}}*/
2304 void pkgAcqIndexMergeDiffs::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/
2305 {
2306 if(Debug)
2307 std::clog << "pkgAcqIndexMergeDiffs failed: " << Desc.URI << " with " << Message << std::endl;
2308
2309 Item::Failed(Message,Cnf);
2310 Status = StatDone;
2311
2312 // check if we are the first to fail, otherwise we are done here
2313 State = StateDoneDiff;
2314 for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin();
2315 I != allPatches->end(); ++I)
2316 if ((*I)->State == StateErrorDiff)
2317 return;
2318
2319 // first failure means we should fallback
2320 State = StateErrorDiff;
2321 if (Debug)
2322 std::clog << "Falling back to normal index file acquire" << std::endl;
2323 DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target);
2324 RenameOnError(PDiffError);
2325 std::string const patchname = GetMergeDiffsPatchFileName(DestFile, patch.file);
2326 if (RealFileExists(patchname))
2327 rename(patchname.c_str(), std::string(patchname + ".FAILED").c_str());
2328 new pkgAcqIndex(Owner, TransactionManager, Target);
2329 DestFile.clear();
2330 }
2331 /*}}}*/
2332 void pkgAcqIndexMergeDiffs::Done(string const &Message, HashStringList const &Hashes, /*{{{*/
2333 pkgAcquire::MethodConfig const * const Cnf)
2334 {
2335 if(Debug)
2336 std::clog << "pkgAcqIndexMergeDiffs::Done(): " << Desc.URI << std::endl;
2337
2338 Item::Done(Message, Hashes, Cnf);
2339
2340 std::string const UncompressedFinalFile = GetPartialFileNameFromURI(Target.URI);
2341 std::string const FinalFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target);
2342 if (State == StateFetchDiff)
2343 {
2344 Rename(DestFile, GetMergeDiffsPatchFileName(FinalFile, patch.file));
2345
2346 // check if this is the last completed diff
2347 State = StateDoneDiff;
2348 for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin();
2349 I != allPatches->end(); ++I)
2350 if ((*I)->State != StateDoneDiff)
2351 {
2352 if(Debug)
2353 std::clog << "Not the last done diff in the batch: " << Desc.URI << std::endl;
2354 return;
2355 }
2356
2357 // this is the last completed diff, so we are ready to apply now
2358 State = StateApplyDiff;
2359
2360 if (BootstrapPDiffWith(UncompressedFinalFile, GetFinalFilename(), Target) == false)
2361 {
2362 Failed("Bootstrapping of " + DestFile + " failed", NULL);
2363 return;
2364 }
2365
2366 if(Debug)
2367 std::clog << "Sending to rred method: " << FinalFile << std::endl;
2368
2369 Local = true;
2370 Desc.URI = "rred:" + FinalFile;
2371 QueueURI(Desc);
2372 SetActiveSubprocess("rred");
2373 return;
2374 }
2375 // success in download/apply all diffs, clean up
2376 else if (State == StateApplyDiff)
2377 {
2378 // move the result into place
2379 std::string const Final = GetKeepCompressedFileName(GetFinalFilename(), Target);
2380 if(Debug)
2381 std::clog << "Queue patched file in place: " << std::endl
2382 << DestFile << " -> " << Final << std::endl;
2383
2384 // queue for copy by the transaction manager
2385 TransactionManager->TransactionStageCopy(this, DestFile, Final);
2386
2387 // ensure the ed's are gone regardless of list-cleanup
2388 for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin();
2389 I != allPatches->end(); ++I)
2390 {
2391 std::string const PartialFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target);
2392 std::string const patch = GetMergeDiffsPatchFileName(PartialFile, (*I)->patch.file);
2393 unlink(patch.c_str());
2394 }
2395 unlink(FinalFile.c_str());
2396
2397 // all set and done
2398 Complete = true;
2399 if(Debug)
2400 std::clog << "allDone: " << DestFile << "\n" << std::endl;
2401 }
2402 }
2403 /*}}}*/
2404 std::string pkgAcqIndexMergeDiffs::Custom600Headers() const /*{{{*/
2405 {
2406 if(State != StateApplyDiff)
2407 return pkgAcqBaseIndex::Custom600Headers();
2408 std::ostringstream patchhashes;
2409 unsigned int seen_patches = 0;
2410 for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin();
2411 I != allPatches->end(); ++I)
2412 {
2413 HashStringList const ExpectedHashes = (*I)->patch.patch_hashes;
2414 for (HashStringList::const_iterator hs = ExpectedHashes.begin(); hs != ExpectedHashes.end(); ++hs)
2415 patchhashes << "\nPatch-" << seen_patches << "-" << hs->HashType() << "-Hash: " << hs->HashValue();
2416 ++seen_patches;
2417 }
2418 patchhashes << pkgAcqBaseIndex::Custom600Headers();
2419 return patchhashes.str();
2420 }
2421 /*}}}*/
2422 pkgAcqIndexMergeDiffs::~pkgAcqIndexMergeDiffs() {}
2423
2424 // AcqIndex::AcqIndex - Constructor /*{{{*/
2425 pkgAcqIndex::pkgAcqIndex(pkgAcquire * const Owner,
2426 pkgAcqMetaClearSig * const TransactionManager,
2427 IndexTarget const &Target)
2428 : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL), Stage(STAGE_DOWNLOAD),
2429 CompressionExtensions(Target.Option(IndexTarget::COMPRESSIONTYPES))
2430 {
2431 Init(Target.URI, Target.Description, Target.ShortDesc);
2432
2433 if(_config->FindB("Debug::Acquire::Transaction", false) == true)
2434 std::clog << "New pkgIndex with TransactionManager "
2435 << TransactionManager << std::endl;
2436 }
2437 /*}}}*/
2438 // AcqIndex::Init - defered Constructor /*{{{*/
2439 void pkgAcqIndex::Init(string const &URI, string const &URIDesc,
2440 string const &ShortDesc)
2441 {
2442 Stage = STAGE_DOWNLOAD;
2443
2444 DestFile = GetPartialFileNameFromURI(URI);
2445
2446 size_t const nextExt = CompressionExtensions.find(' ');
2447 if (nextExt == std::string::npos)
2448 {
2449 CurrentCompressionExtension = CompressionExtensions;
2450 CompressionExtensions.clear();
2451 }
2452 else
2453 {
2454 CurrentCompressionExtension = CompressionExtensions.substr(0, nextExt);
2455 CompressionExtensions = CompressionExtensions.substr(nextExt+1);
2456 }
2457
2458 if (CurrentCompressionExtension == "uncompressed")
2459 {
2460 Desc.URI = URI;
2461 }
2462 else if (unlikely(CurrentCompressionExtension.empty()))
2463 return;
2464 else
2465 {
2466 Desc.URI = URI + '.' + CurrentCompressionExtension;
2467 DestFile = DestFile + '.' + CurrentCompressionExtension;
2468 }
2469
2470 if(TransactionManager->MetaIndexParser != NULL)
2471 InitByHashIfNeeded();
2472
2473 Desc.Description = URIDesc;
2474 Desc.Owner = this;
2475 Desc.ShortDesc = ShortDesc;
2476
2477 QueueURI(Desc);
2478 }
2479 /*}}}*/
2480 // AcqIndex::AdjustForByHash - modify URI for by-hash support /*{{{*/
2481 void pkgAcqIndex::InitByHashIfNeeded()
2482 {
2483 // TODO:
2484 // - (maybe?) add support for by-hash into the sources.list as flag
2485 // - make apt-ftparchive generate the hashes (and expire?)
2486 std::string HostKnob = "APT::Acquire::" + ::URI(Desc.URI).Host + "::By-Hash";
2487 if(_config->FindB("APT::Acquire::By-Hash", false) == true ||
2488 _config->FindB(HostKnob, false) == true ||
2489 TransactionManager->MetaIndexParser->GetSupportsAcquireByHash())
2490 {
2491 HashStringList const Hashes = GetExpectedHashes();
2492 if(Hashes.usable())
2493 {
2494 // FIXME: should we really use the best hash here? or a fixed one?
2495 HashString const * const TargetHash = Hashes.find("");
2496 std::string const ByHash = "/by-hash/" + TargetHash->HashType() + "/" + TargetHash->HashValue();
2497 size_t const trailing_slash = Desc.URI.find_last_of("/");
2498 Desc.URI = Desc.URI.replace(
2499 trailing_slash,
2500 Desc.URI.substr(trailing_slash+1).size()+1,
2501 ByHash);
2502 } else {
2503 _error->Warning(
2504 "Fetching ByHash requested but can not find record for %s",
2505 GetMetaKey().c_str());
2506 }
2507 }
2508 }
2509 /*}}}*/
2510 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
2511 // ---------------------------------------------------------------------
2512 /* The only header we use is the last-modified header. */
2513 string pkgAcqIndex::Custom600Headers() const
2514 {
2515 string Final = GetFinalFilename();
2516
2517 string msg = "\nIndex-File: true";
2518 struct stat Buf;
2519 if (stat(Final.c_str(),&Buf) == 0)
2520 msg += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
2521
2522 if(Target.IsOptional)
2523 msg += "\nFail-Ignore: true";
2524
2525 return msg;
2526 }
2527 /*}}}*/
2528 // AcqIndex::Failed - getting the indexfile failed /*{{{*/
2529 void pkgAcqIndex::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)
2530 {
2531 Item::Failed(Message,Cnf);
2532
2533 // authorisation matches will not be fixed by other compression types
2534 if (Status != StatAuthError)
2535 {
2536 if (CompressionExtensions.empty() == false)
2537 {
2538 Init(Target.URI, Desc.Description, Desc.ShortDesc);
2539 Status = StatIdle;
2540 return;
2541 }
2542 }
2543
2544 if(Target.IsOptional && GetExpectedHashes().empty() && Stage == STAGE_DOWNLOAD)
2545 Status = StatDone;
2546 else
2547 TransactionManager->AbortTransaction();
2548 }
2549 /*}}}*/
2550 // AcqIndex::ReverifyAfterIMS - Reverify index after an ims-hit /*{{{*/
2551 void pkgAcqIndex::ReverifyAfterIMS()
2552 {
2553 // update destfile to *not* include the compression extension when doing
2554 // a reverify (as its uncompressed on disk already)
2555 DestFile = GetCompressedFileName(Target, GetPartialFileNameFromURI(Target.URI), CurrentCompressionExtension);
2556
2557 // copy FinalFile into partial/ so that we check the hash again
2558 string FinalFile = GetFinalFilename();
2559 Stage = STAGE_DECOMPRESS_AND_VERIFY;
2560 Desc.URI = "copy:" + FinalFile;
2561 QueueURI(Desc);
2562 }
2563 /*}}}*/
2564 // AcqIndex::Done - Finished a fetch /*{{{*/
2565 // ---------------------------------------------------------------------
2566 /* This goes through a number of states.. On the initial fetch the
2567 method could possibly return an alternate filename which points
2568 to the uncompressed version of the file. If this is so the file
2569 is copied into the partial directory. In all other cases the file
2570 is decompressed with a compressed uri. */
2571 void pkgAcqIndex::Done(string const &Message,
2572 HashStringList const &Hashes,
2573 pkgAcquire::MethodConfig const * const Cfg)
2574 {
2575 Item::Done(Message,Hashes,Cfg);
2576
2577 switch(Stage)
2578 {
2579 case STAGE_DOWNLOAD:
2580 StageDownloadDone(Message, Hashes, Cfg);
2581 break;
2582 case STAGE_DECOMPRESS_AND_VERIFY:
2583 StageDecompressDone(Message, Hashes, Cfg);
2584 break;
2585 }
2586 }
2587 /*}}}*/
2588 // AcqIndex::StageDownloadDone - Queue for decompress and verify /*{{{*/
2589 void pkgAcqIndex::StageDownloadDone(string const &Message, HashStringList const &,
2590 pkgAcquire::MethodConfig const * const)
2591 {
2592 Complete = true;
2593
2594 // Handle the unzipd case
2595 std::string FileName = LookupTag(Message,"Alt-Filename");
2596 if (FileName.empty() == false)
2597 {
2598 Stage = STAGE_DECOMPRESS_AND_VERIFY;
2599 Local = true;
2600 DestFile += ".decomp";
2601 Desc.URI = "copy:" + FileName;
2602 QueueURI(Desc);
2603 SetActiveSubprocess("copy");
2604 return;
2605 }
2606 FileName = LookupTag(Message,"Filename");
2607
2608 // Methods like e.g. "file:" will give us a (compressed) FileName that is
2609 // not the "DestFile" we set, in this case we uncompress from the local file
2610 if (FileName != DestFile && RealFileExists(DestFile) == false)
2611 Local = true;
2612 else
2613 EraseFileName = FileName;
2614
2615 // we need to verify the file against the current Release file again
2616 // on if-modfied-since hit to avoid a stale attack against us
2617 if(StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
2618 {
2619 // The files timestamp matches, reverify by copy into partial/
2620 EraseFileName = "";
2621 ReverifyAfterIMS();
2622 return;
2623 }
2624
2625 // If we want compressed indexes, just copy in place for hash verification
2626 if (Target.KeepCompressed == true)
2627 {
2628 DestFile = GetPartialFileNameFromURI(Target.URI + '.' + CurrentCompressionExtension);
2629 EraseFileName = "";
2630 Stage = STAGE_DECOMPRESS_AND_VERIFY;
2631 Desc.URI = "copy:" + FileName;
2632 QueueURI(Desc);
2633 SetActiveSubprocess("copy");
2634 return;
2635 }
2636
2637 // get the binary name for your used compression type
2638 string decompProg;
2639 if(CurrentCompressionExtension == "uncompressed")
2640 decompProg = "copy";
2641 else
2642 decompProg = _config->Find(string("Acquire::CompressionTypes::").append(CurrentCompressionExtension),"");
2643 if(decompProg.empty() == true)
2644 {
2645 _error->Error("Unsupported extension: %s", CurrentCompressionExtension.c_str());
2646 return;
2647 }
2648
2649 // queue uri for the next stage
2650 Stage = STAGE_DECOMPRESS_AND_VERIFY;
2651 DestFile += ".decomp";
2652 Desc.URI = decompProg + ":" + FileName;
2653 QueueURI(Desc);
2654 SetActiveSubprocess(decompProg);
2655 }
2656 /*}}}*/
2657 // AcqIndex::StageDecompressDone - Final verification /*{{{*/
2658 void pkgAcqIndex::StageDecompressDone(string const &,
2659 HashStringList const &,
2660 pkgAcquire::MethodConfig const * const)
2661 {
2662 // Done, queue for rename on transaction finished
2663 TransactionManager->TransactionStageCopy(this, DestFile, GetFinalFilename());
2664 return;
2665 }
2666 /*}}}*/
2667 pkgAcqIndex::~pkgAcqIndex() {}
2668
2669
2670 // AcqArchive::AcqArchive - Constructor /*{{{*/
2671 // ---------------------------------------------------------------------
2672 /* This just sets up the initial fetch environment and queues the first
2673 possibilitiy */
2674 pkgAcqArchive::pkgAcqArchive(pkgAcquire * const Owner,pkgSourceList * const Sources,
2675 pkgRecords * const Recs,pkgCache::VerIterator const &Version,
2676 string &StoreFilename) :
2677 Item(Owner), d(NULL), LocalSource(false), Version(Version), Sources(Sources), Recs(Recs),
2678 StoreFilename(StoreFilename), Vf(Version.FileList()),
2679 Trusted(false)
2680 {
2681 Retries = _config->FindI("Acquire::Retries",0);
2682
2683 if (Version.Arch() == 0)
2684 {
2685 _error->Error(_("I wasn't able to locate a file for the %s package. "
2686 "This might mean you need to manually fix this package. "
2687 "(due to missing arch)"),
2688 Version.ParentPkg().FullName().c_str());
2689 return;
2690 }
2691
2692 /* We need to find a filename to determine the extension. We make the
2693 assumption here that all the available sources for this version share
2694 the same extension.. */
2695 // Skip not source sources, they do not have file fields.
2696 for (; Vf.end() == false; ++Vf)
2697 {
2698 if (Vf.File().Flagged(pkgCache::Flag::NotSource))
2699 continue;
2700 break;
2701 }
2702
2703 // Does not really matter here.. we are going to fail out below
2704 if (Vf.end() != true)
2705 {
2706 // If this fails to get a file name we will bomb out below.
2707 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
2708 if (_error->PendingError() == true)
2709 return;
2710
2711 // Generate the final file name as: package_version_arch.foo
2712 StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
2713 QuoteString(Version.VerStr(),"_:") + '_' +
2714 QuoteString(Version.Arch(),"_:.") +
2715 "." + flExtension(Parse.FileName());
2716 }
2717
2718 // check if we have one trusted source for the package. if so, switch
2719 // to "TrustedOnly" mode - but only if not in AllowUnauthenticated mode
2720 bool const allowUnauth = _config->FindB("APT::Get::AllowUnauthenticated", false);
2721 bool const debugAuth = _config->FindB("Debug::pkgAcquire::Auth", false);
2722 bool seenUntrusted = false;
2723 for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; ++i)
2724 {
2725 pkgIndexFile *Index;
2726 if (Sources->FindIndex(i.File(),Index) == false)
2727 continue;
2728
2729 if (debugAuth == true)
2730 std::cerr << "Checking index: " << Index->Describe()
2731 << "(Trusted=" << Index->IsTrusted() << ")" << std::endl;
2732
2733 if (Index->IsTrusted() == true)
2734 {
2735 Trusted = true;
2736 if (allowUnauth == false)
2737 break;
2738 }
2739 else
2740 seenUntrusted = true;
2741 }
2742
2743 // "allow-unauthenticated" restores apts old fetching behaviour
2744 // that means that e.g. unauthenticated file:// uris are higher
2745 // priority than authenticated http:// uris
2746 if (allowUnauth == true && seenUntrusted == true)
2747 Trusted = false;
2748
2749 // Select a source
2750 if (QueueNext() == false && _error->PendingError() == false)
2751 _error->Error(_("Can't find a source to download version '%s' of '%s'"),
2752 Version.VerStr(), Version.ParentPkg().FullName(false).c_str());
2753 }
2754 /*}}}*/
2755 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
2756 // ---------------------------------------------------------------------
2757 /* This queues the next available file version for download. It checks if
2758 the archive is already available in the cache and stashs the MD5 for
2759 checking later. */
2760 bool pkgAcqArchive::QueueNext()
2761 {
2762 for (; Vf.end() == false; ++Vf)
2763 {
2764 pkgCache::PkgFileIterator const PkgF = Vf.File();
2765 // Ignore not source sources
2766 if (PkgF.Flagged(pkgCache::Flag::NotSource))
2767 continue;
2768
2769 // Try to cross match against the source list
2770 pkgIndexFile *Index;
2771 if (Sources->FindIndex(PkgF, Index) == false)
2772 continue;
2773 LocalSource = PkgF.Flagged(pkgCache::Flag::LocalSource);
2774
2775 // only try to get a trusted package from another source if that source
2776 // is also trusted
2777 if(Trusted && !Index->IsTrusted())
2778 continue;
2779
2780 // Grab the text package record
2781 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
2782 if (_error->PendingError() == true)
2783 return false;
2784
2785 string PkgFile = Parse.FileName();
2786 ExpectedHashes = Parse.Hashes();
2787
2788 if (PkgFile.empty() == true)
2789 return _error->Error(_("The package index files are corrupted. No Filename: "
2790 "field for package %s."),
2791 Version.ParentPkg().Name());
2792
2793 Desc.URI = Index->ArchiveURI(PkgFile);
2794 Desc.Description = Index->ArchiveInfo(Version);
2795 Desc.Owner = this;
2796 Desc.ShortDesc = Version.ParentPkg().FullName(true);
2797
2798 // See if we already have the file. (Legacy filenames)
2799 FileSize = Version->Size;
2800 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
2801 struct stat Buf;
2802 if (stat(FinalFile.c_str(),&Buf) == 0)
2803 {
2804 // Make sure the size matches
2805 if ((unsigned long long)Buf.st_size == Version->Size)
2806 {
2807 Complete = true;
2808 Local = true;
2809 Status = StatDone;
2810 StoreFilename = DestFile = FinalFile;
2811 return true;
2812 }
2813
2814 /* Hmm, we have a file and its size does not match, this means it is
2815 an old style mismatched arch */
2816 unlink(FinalFile.c_str());
2817 }
2818
2819 // Check it again using the new style output filenames
2820 FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
2821 if (stat(FinalFile.c_str(),&Buf) == 0)
2822 {
2823 // Make sure the size matches
2824 if ((unsigned long long)Buf.st_size == Version->Size)
2825 {
2826 Complete = true;
2827 Local = true;
2828 Status = StatDone;
2829 StoreFilename = DestFile = FinalFile;
2830 return true;
2831 }
2832
2833 /* Hmm, we have a file and its size does not match, this shouldn't
2834 happen.. */
2835 unlink(FinalFile.c_str());
2836 }
2837
2838 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
2839
2840 // Check the destination file
2841 if (stat(DestFile.c_str(),&Buf) == 0)
2842 {
2843 // Hmm, the partial file is too big, erase it
2844 if ((unsigned long long)Buf.st_size > Version->Size)
2845 unlink(DestFile.c_str());
2846 else
2847 PartialSize = Buf.st_size;
2848 }
2849
2850 // Disables download of archives - useful if no real installation follows,
2851 // e.g. if we are just interested in proposed installation order
2852 if (_config->FindB("Debug::pkgAcqArchive::NoQueue", false) == true)
2853 {
2854 Complete = true;
2855 Local = true;
2856 Status = StatDone;
2857 StoreFilename = DestFile = FinalFile;
2858 return true;
2859 }
2860
2861 // Create the item
2862 Local = false;
2863 QueueURI(Desc);
2864
2865 ++Vf;
2866 return true;
2867 }
2868 return false;
2869 }
2870 /*}}}*/
2871 // AcqArchive::Done - Finished fetching /*{{{*/
2872 // ---------------------------------------------------------------------
2873 /* */
2874 void pkgAcqArchive::Done(string const &Message, HashStringList const &Hashes,
2875 pkgAcquire::MethodConfig const * const Cfg)
2876 {
2877 Item::Done(Message, Hashes, Cfg);
2878
2879 // Grab the output filename
2880 std::string const FileName = LookupTag(Message,"Filename");
2881 if (DestFile != FileName && RealFileExists(DestFile) == false)
2882 {
2883 StoreFilename = DestFile = FileName;
2884 Local = true;
2885 Complete = true;
2886 return;
2887 }
2888
2889 // Done, move it into position
2890 string const FinalFile = GetFinalFilename();
2891 Rename(DestFile,FinalFile);
2892 StoreFilename = DestFile = FinalFile;
2893 Complete = true;
2894 }
2895 /*}}}*/
2896 // AcqArchive::Failed - Failure handler /*{{{*/
2897 // ---------------------------------------------------------------------
2898 /* Here we try other sources */
2899 void pkgAcqArchive::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)
2900 {
2901 Item::Failed(Message,Cnf);
2902
2903 /* We don't really want to retry on failed media swaps, this prevents
2904 that. An interesting observation is that permanent failures are not
2905 recorded. */
2906 if (Cnf->Removable == true &&
2907 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
2908 {
2909 // Vf = Version.FileList();
2910 while (Vf.end() == false) ++Vf;
2911 StoreFilename = string();
2912 return;
2913 }
2914
2915 Status = StatIdle;
2916 if (QueueNext() == false)
2917 {
2918 // This is the retry counter
2919 if (Retries != 0 &&
2920 Cnf->LocalOnly == false &&
2921 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
2922 {
2923 Retries--;
2924 Vf = Version.FileList();
2925 if (QueueNext() == true)
2926 return;
2927 }
2928
2929 StoreFilename = string();
2930 Status = StatError;
2931 }
2932 }
2933 /*}}}*/
2934 APT_PURE bool pkgAcqArchive::IsTrusted() const /*{{{*/
2935 {
2936 return Trusted;
2937 }
2938 /*}}}*/
2939 void pkgAcqArchive::Finished() /*{{{*/
2940 {
2941 if (Status == pkgAcquire::Item::StatDone &&
2942 Complete == true)
2943 return;
2944 StoreFilename = string();
2945 }
2946 /*}}}*/
2947 std::string pkgAcqArchive::DescURI() const /*{{{*/
2948 {
2949 return Desc.URI;
2950 }
2951 /*}}}*/
2952 std::string pkgAcqArchive::ShortDesc() const /*{{{*/
2953 {
2954 return Desc.ShortDesc;
2955 }
2956 /*}}}*/
2957 pkgAcqArchive::~pkgAcqArchive() {}
2958
2959 // AcqChangelog::pkgAcqChangelog - Constructors /*{{{*/
2960 pkgAcqChangelog::pkgAcqChangelog(pkgAcquire * const Owner, pkgCache::VerIterator const &Ver,
2961 std::string const &DestDir, std::string const &DestFilename) :
2962 pkgAcquire::Item(Owner), d(NULL), SrcName(Ver.SourcePkgName()), SrcVersion(Ver.SourceVerStr())
2963 {
2964 Desc.URI = URI(Ver);
2965 Init(DestDir, DestFilename);
2966 }
2967 // some parameters are char* here as they come likely from char* interfaces – which can also return NULL
2968 pkgAcqChangelog::pkgAcqChangelog(pkgAcquire * const Owner, pkgCache::RlsFileIterator const &RlsFile,
2969 char const * const Component, char const * const SrcName, char const * const SrcVersion,
2970 const string &DestDir, const string &DestFilename) :
2971 pkgAcquire::Item(Owner), d(NULL), SrcName(SrcName), SrcVersion(SrcVersion)
2972 {
2973 Desc.URI = URI(RlsFile, Component, SrcName, SrcVersion);
2974 Init(DestDir, DestFilename);
2975 }
2976 pkgAcqChangelog::pkgAcqChangelog(pkgAcquire * const Owner,
2977 std::string const &URI, char const * const SrcName, char const * const SrcVersion,
2978 const string &DestDir, const string &DestFilename) :
2979 pkgAcquire::Item(Owner), d(NULL), SrcName(SrcName), SrcVersion(SrcVersion)
2980 {
2981 Desc.URI = URI;
2982 Init(DestDir, DestFilename);
2983 }
2984 void pkgAcqChangelog::Init(std::string const &DestDir, std::string const &DestFilename)
2985 {
2986 if (Desc.URI.empty())
2987 {
2988 Status = StatError;
2989 // TRANSLATOR: %s=%s is sourcename=sourceversion, e.g. apt=1.1
2990 strprintf(ErrorText, _("Changelog unavailable for %s=%s"), SrcName.c_str(), SrcVersion.c_str());
2991 // Let the error message print something sensible rather than "Failed to fetch /"
2992 if (DestFilename.empty())
2993 DestFile = SrcName + ".changelog";
2994 else
2995 DestFile = DestFilename;
2996 Desc.URI = "changelog:/" + DestFile;
2997 return;
2998 }
2999
3000 if (DestDir.empty())
3001 {
3002 std::string const SandboxUser = _config->Find("APT::Sandbox::User");
3003 std::string const systemTemp = GetTempDir(SandboxUser);
3004 char tmpname[100];
3005 snprintf(tmpname, sizeof(tmpname), "%s/apt-changelog-XXXXXX", systemTemp.c_str());
3006 if (NULL == mkdtemp(tmpname))
3007 {
3008 _error->Errno("mkdtemp", "mkdtemp failed in changelog acquire of %s %s", SrcName.c_str(), SrcVersion.c_str());
3009 Status = StatError;
3010 return;
3011 }
3012 DestFile = TemporaryDirectory = tmpname;
3013
3014 ChangeOwnerAndPermissionOfFile("Item::QueueURI", DestFile.c_str(),
3015 SandboxUser.c_str(), "root", 0700);
3016 }
3017 else
3018 DestFile = DestDir;
3019
3020 if (DestFilename.empty())
3021 DestFile = flCombine(DestFile, SrcName + ".changelog");
3022 else
3023 DestFile = flCombine(DestFile, DestFilename);
3024
3025 Desc.ShortDesc = "Changelog";
3026 strprintf(Desc.Description, "%s %s %s Changelog", URI::SiteOnly(Desc.URI).c_str(), SrcName.c_str(), SrcVersion.c_str());
3027 Desc.Owner = this;
3028 QueueURI(Desc);
3029 }
3030 /*}}}*/
3031 std::string pkgAcqChangelog::URI(pkgCache::VerIterator const &Ver) /*{{{*/
3032 {
3033 char const * const SrcName = Ver.SourcePkgName();
3034 char const * const SrcVersion = Ver.SourceVerStr();
3035 pkgCache::PkgFileIterator PkgFile;
3036 // find the first source for this version which promises a changelog
3037 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF)
3038 {
3039 pkgCache::PkgFileIterator const PF = VF.File();
3040 if (PF.Flagged(pkgCache::Flag::NotSource) || PF->Release == 0)
3041 continue;
3042 PkgFile = PF;
3043 pkgCache::RlsFileIterator const RF = PF.ReleaseFile();
3044 std::string const uri = URI(RF, PF.Component(), SrcName, SrcVersion);
3045 if (uri.empty())
3046 continue;
3047 return uri;
3048 }
3049 return "";
3050 }
3051 std::string pkgAcqChangelog::URITemplate(pkgCache::RlsFileIterator const &Rls)
3052 {
3053 if (Rls.end() == true || (Rls->Label == 0 && Rls->Origin == 0))
3054 return "";
3055 std::string const serverConfig = "Acquire::Changelogs::URI";
3056 std::string server;
3057 #define APT_EMPTY_SERVER \
3058 if (server.empty() == false) \
3059 { \
3060 if (server != "no") \
3061 return server; \
3062 return ""; \
3063 }
3064 #define APT_CHECK_SERVER(X, Y) \
3065 if (Rls->X != 0) \
3066 { \
3067 std::string const specialServerConfig = serverConfig + "::" + Y + #X + "::" + Rls.X(); \
3068 server = _config->Find(specialServerConfig); \
3069 APT_EMPTY_SERVER \
3070 }
3071 // this way e.g. Debian-Security can fallback to Debian
3072 APT_CHECK_SERVER(Label, "Override::")
3073 APT_CHECK_SERVER(Origin, "Override::")
3074
3075 if (RealFileExists(Rls.FileName()))
3076 {
3077 _error->PushToStack();
3078 FileFd rf;
3079 /* This can be costly. A caller wanting to get millions of URIs might
3080 want to do this on its own once and use Override settings.
3081 We don't do this here as Origin/Label are not as unique as they
3082 should be so this could produce request order-dependent anomalies */
3083 if (OpenMaybeClearSignedFile(Rls.FileName(), rf) == true)
3084 {
3085 pkgTagFile TagFile(&rf, rf.Size());
3086 pkgTagSection Section;
3087 if (TagFile.Step(Section) == true)
3088 server = Section.FindS("Changelogs");
3089 }
3090 _error->RevertToStack();
3091 APT_EMPTY_SERVER
3092 }
3093
3094 APT_CHECK_SERVER(Label, "")
3095 APT_CHECK_SERVER(Origin, "")
3096 #undef APT_CHECK_SERVER
3097 #undef APT_EMPTY_SERVER
3098 return "";
3099 }
3100 std::string pkgAcqChangelog::URI(pkgCache::RlsFileIterator const &Rls,
3101 char const * const Component, char const * const SrcName,
3102 char const * const SrcVersion)
3103 {
3104 return URI(URITemplate(Rls), Component, SrcName, SrcVersion);
3105 }
3106 std::string pkgAcqChangelog::URI(std::string const &Template,
3107 char const * const Component, char const * const SrcName,
3108 char const * const SrcVersion)
3109 {
3110 if (Template.find("CHANGEPATH") == std::string::npos)
3111 return "";
3112
3113 // the path is: COMPONENT/SRC/SRCNAME/SRCNAME_SRCVER, e.g. main/a/apt/1.1 or contrib/liba/libapt/2.0
3114 std::string Src = SrcName;
3115 std::string path = APT::String::Startswith(SrcName, "lib") ? Src.substr(0, 4) : Src.substr(0,1);
3116 path.append("/").append(Src).append("/");
3117 path.append(Src).append("_").append(StripEpoch(SrcVersion));
3118 // we omit component for releases without one (= flat-style repositories)
3119 if (Component != NULL && strlen(Component) != 0)
3120 path = std::string(Component) + "/" + path;
3121
3122 return SubstVar(Template, "CHANGEPATH", path);
3123 }
3124 /*}}}*/
3125 // AcqChangelog::Failed - Failure handler /*{{{*/
3126 void pkgAcqChangelog::Failed(string const &Message, pkgAcquire::MethodConfig const * const Cnf)
3127 {
3128 Item::Failed(Message,Cnf);
3129
3130 std::string errText;
3131 // TRANSLATOR: %s=%s is sourcename=sourceversion, e.g. apt=1.1
3132 strprintf(errText, _("Changelog unavailable for %s=%s"), SrcName.c_str(), SrcVersion.c_str());
3133
3134 // Error is probably something techy like 404 Not Found
3135 if (ErrorText.empty())
3136 ErrorText = errText;
3137 else
3138 ErrorText = errText + " (" + ErrorText + ")";
3139 return;
3140 }
3141 /*}}}*/
3142 // AcqChangelog::Done - Item downloaded OK /*{{{*/
3143 void pkgAcqChangelog::Done(string const &Message,HashStringList const &CalcHashes,
3144 pkgAcquire::MethodConfig const * const Cnf)
3145 {
3146 Item::Done(Message,CalcHashes,Cnf);
3147
3148 Complete = true;
3149 }
3150 /*}}}*/
3151 pkgAcqChangelog::~pkgAcqChangelog() /*{{{*/
3152 {
3153 if (TemporaryDirectory.empty() == false)
3154 {
3155 unlink(DestFile.c_str());
3156 rmdir(TemporaryDirectory.c_str());
3157 }
3158 }
3159 /*}}}*/
3160
3161 // AcqFile::pkgAcqFile - Constructor /*{{{*/
3162 pkgAcqFile::pkgAcqFile(pkgAcquire * const Owner,string const &URI, HashStringList const &Hashes,
3163 unsigned long long const Size,string const &Dsc,string const &ShortDesc,
3164 const string &DestDir, const string &DestFilename,
3165 bool const IsIndexFile) :
3166 Item(Owner), d(NULL), IsIndexFile(IsIndexFile), ExpectedHashes(Hashes)
3167 {
3168 Retries = _config->FindI("Acquire::Retries",0);
3169
3170 if(!DestFilename.empty())
3171 DestFile = DestFilename;
3172 else if(!DestDir.empty())
3173 DestFile = DestDir + "/" + flNotDir(URI);
3174 else
3175 DestFile = flNotDir(URI);
3176
3177 // Create the item
3178 Desc.URI = URI;
3179 Desc.Description = Dsc;
3180 Desc.Owner = this;
3181
3182 // Set the short description to the archive component
3183 Desc.ShortDesc = ShortDesc;
3184
3185 // Get the transfer sizes
3186 FileSize = Size;
3187 struct stat Buf;
3188 if (stat(DestFile.c_str(),&Buf) == 0)
3189 {
3190 // Hmm, the partial file is too big, erase it
3191 if ((Size > 0) && (unsigned long long)Buf.st_size > Size)
3192 unlink(DestFile.c_str());
3193 else
3194 PartialSize = Buf.st_size;
3195 }
3196
3197 QueueURI(Desc);
3198 }
3199 /*}}}*/
3200 // AcqFile::Done - Item downloaded OK /*{{{*/
3201 void pkgAcqFile::Done(string const &Message,HashStringList const &CalcHashes,
3202 pkgAcquire::MethodConfig const * const Cnf)
3203 {
3204 Item::Done(Message,CalcHashes,Cnf);
3205
3206 std::string const FileName = LookupTag(Message,"Filename");
3207 Complete = true;
3208
3209 // The files timestamp matches
3210 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
3211 return;
3212
3213 // We have to copy it into place
3214 if (RealFileExists(DestFile.c_str()) == false)
3215 {
3216 Local = true;
3217 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
3218 Cnf->Removable == true)
3219 {
3220 Desc.URI = "copy:" + FileName;
3221 QueueURI(Desc);
3222 return;
3223 }
3224
3225 // Erase the file if it is a symlink so we can overwrite it
3226 struct stat St;
3227 if (lstat(DestFile.c_str(),&St) == 0)
3228 {
3229 if (S_ISLNK(St.st_mode) != 0)
3230 unlink(DestFile.c_str());
3231 }
3232
3233 // Symlink the file
3234 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
3235 {
3236 _error->PushToStack();
3237 _error->Errno("pkgAcqFile::Done", "Symlinking file %s failed", DestFile.c_str());
3238 std::stringstream msg;
3239 _error->DumpErrors(msg, GlobalError::DEBUG, false);
3240 _error->RevertToStack();
3241 ErrorText = msg.str();
3242 Status = StatError;
3243 Complete = false;
3244 }
3245 }
3246 }
3247 /*}}}*/
3248 // AcqFile::Failed - Failure handler /*{{{*/
3249 // ---------------------------------------------------------------------
3250 /* Here we try other sources */
3251 void pkgAcqFile::Failed(string const &Message, pkgAcquire::MethodConfig const * const Cnf)
3252 {
3253 Item::Failed(Message,Cnf);
3254
3255 // This is the retry counter
3256 if (Retries != 0 &&
3257 Cnf->LocalOnly == false &&
3258 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
3259 {
3260 --Retries;
3261 QueueURI(Desc);
3262 Status = StatIdle;
3263 return;
3264 }
3265
3266 }
3267 /*}}}*/
3268 string pkgAcqFile::Custom600Headers() const /*{{{*/
3269 {
3270 if (IsIndexFile)
3271 return "\nIndex-File: true";
3272 return "";
3273 }
3274 /*}}}*/
3275 pkgAcqFile::~pkgAcqFile() {}