1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
4 /* ######################################################################
6 Acquire Item - Item to acquire
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.
13 ##################################################################### */
15 // Include Files /*{{{*/
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/sha1.h>
26 #include <apt-pkg/tagfile.h>
27 #include <apt-pkg/indexrecords.h>
28 #include <apt-pkg/acquire.h>
29 #include <apt-pkg/hashes.h>
30 #include <apt-pkg/indexfile.h>
31 #include <apt-pkg/pkgcache.h>
32 #include <apt-pkg/cacheiterators.h>
33 #include <apt-pkg/pkgrecords.h>
53 static void printHashSumComparision(std::string
const &URI
, HashStringList
const &Expected
, HashStringList
const &Actual
) /*{{{*/
55 if (_config
->FindB("Debug::Acquire::HashSumMismatch", false) == false)
57 std::cerr
<< std::endl
<< URI
<< ":" << std::endl
<< " Expected Hash: " << std::endl
;
58 for (HashStringList::const_iterator hs
= Expected
.begin(); hs
!= Expected
.end(); ++hs
)
59 std::cerr
<< "\t- " << hs
->toStr() << std::endl
;
60 std::cerr
<< " Actual Hash: " << std::endl
;
61 for (HashStringList::const_iterator hs
= Actual
.begin(); hs
!= Actual
.end(); ++hs
)
62 std::cerr
<< "\t- " << hs
->toStr() << std::endl
;
66 // Acquire::Item::Item - Constructor /*{{{*/
67 pkgAcquire::Item::Item(pkgAcquire
*Owner
,
68 HashStringList
const &ExpectedHashes
,
69 unsigned long TransactionID
)
70 : Owner(Owner
), FileSize(0), PartialSize(0), Mode(0), ID(0), Complete(false),
71 Local(false), QueueCounter(0), TransactionID(TransactionID
),
72 ExpectedAdditionalItems(0), ExpectedHashes(ExpectedHashes
)
78 // Acquire::Item::~Item - Destructor /*{{{*/
79 // ---------------------------------------------------------------------
81 pkgAcquire::Item::~Item()
86 // Acquire::Item::Failed - Item failed to download /*{{{*/
87 // ---------------------------------------------------------------------
88 /* We return to an idle state if there are still other queues that could
90 void pkgAcquire::Item::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
94 ErrorText
= LookupTag(Message
,"Message");
95 UsedMirror
= LookupTag(Message
,"UsedMirror");
96 if (QueueCounter
<= 1)
98 /* This indicates that the file is not available right now but might
99 be sometime later. If we do a retry cycle then this should be
101 if (Cnf
->LocalOnly
== true &&
102 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
113 // report mirror failure back to LP if we actually use a mirror
114 string FailReason
= LookupTag(Message
, "FailReason");
115 if(FailReason
.size() != 0)
116 ReportMirrorFailure(FailReason
);
118 ReportMirrorFailure(ErrorText
);
121 // Acquire::Item::Start - Item has begun to download /*{{{*/
122 // ---------------------------------------------------------------------
123 /* Stash status and the file size. Note that setting Complete means
124 sub-phases of the acquire process such as decompresion are operating */
125 void pkgAcquire::Item::Start(string
/*Message*/,unsigned long long Size
)
127 Status
= StatFetching
;
128 if (FileSize
== 0 && Complete
== false)
132 // Acquire::Item::Done - Item downloaded OK /*{{{*/
133 // ---------------------------------------------------------------------
135 void pkgAcquire::Item::Done(string Message
,unsigned long long Size
,HashStringList
const &/*Hash*/,
136 pkgAcquire::MethodConfig
* /*Cnf*/)
138 // We just downloaded something..
139 string FileName
= LookupTag(Message
,"Filename");
140 UsedMirror
= LookupTag(Message
,"UsedMirror");
141 if (Complete
== false && !Local
&& FileName
== DestFile
)
144 Owner
->Log
->Fetched(Size
,atoi(LookupTag(Message
,"Resume-Point","0").c_str()));
150 ErrorText
= string();
151 Owner
->Dequeue(this);
154 // Acquire::Item::Rename - Rename a file /*{{{*/
155 // ---------------------------------------------------------------------
156 /* This helper function is used by a lot of item methods as their final
158 void pkgAcquire::Item::Rename(string From
,string To
)
160 if (rename(From
.c_str(),To
.c_str()) != 0)
163 snprintf(S
,sizeof(S
),_("rename failed, %s (%s -> %s)."),strerror(errno
),
164 From
.c_str(),To
.c_str());
170 bool pkgAcquire::Item::RenameOnError(pkgAcquire::Item::RenameOnErrorState
const error
)/*{{{*/
172 if(FileExists(DestFile
))
173 Rename(DestFile
, DestFile
+ ".FAILED");
177 case HashSumMismatch
:
178 ErrorText
= _("Hash Sum mismatch");
179 Status
= StatAuthError
;
180 ReportMirrorFailure("HashChecksumFailure");
183 ErrorText
= _("Size mismatch");
184 Status
= StatAuthError
;
185 ReportMirrorFailure("SizeFailure");
188 ErrorText
= _("Invalid file format");
190 // do not report as usually its not the mirrors fault, but Portal/Proxy
196 // Acquire::Item::ReportMirrorFailure /*{{{*/
197 // ---------------------------------------------------------------------
198 void pkgAcquire::Item::ReportMirrorFailure(string FailCode
)
200 // we only act if a mirror was used at all
201 if(UsedMirror
.empty())
204 std::cerr
<< "\nReportMirrorFailure: "
206 << " Uri: " << DescURI()
208 << FailCode
<< std::endl
;
210 const char *Args
[40];
212 string report
= _config
->Find("Methods::Mirror::ProblemReporting",
213 "/usr/lib/apt/apt-report-mirror-failure");
214 if(!FileExists(report
))
216 Args
[i
++] = report
.c_str();
217 Args
[i
++] = UsedMirror
.c_str();
218 Args
[i
++] = DescURI().c_str();
219 Args
[i
++] = FailCode
.c_str();
221 pid_t pid
= ExecFork();
224 _error
->Error("ReportMirrorFailure Fork failed");
229 execvp(Args
[0], (char**)Args
);
230 std::cerr
<< "Could not exec " << Args
[0] << std::endl
;
233 if(!ExecWait(pid
, "report-mirror-failure"))
235 _error
->Warning("Couldn't report problem to '%s'",
236 _config
->Find("Methods::Mirror::ProblemReporting").c_str());
240 // AcqSubIndex::AcqSubIndex - Constructor /*{{{*/
241 // ---------------------------------------------------------------------
242 /* Get a sub-index file based on checksums from a 'master' file and
243 possibly query additional files */
244 pkgAcqSubIndex::pkgAcqSubIndex(pkgAcquire
*Owner
,
245 unsigned long TransactionID
,
247 string
const &URIDesc
, string
const &ShortDesc
,
248 HashStringList
const &ExpectedHashes
)
249 : Item(Owner
, ExpectedHashes
, TransactionID
)
251 /* XXX: Beware: Currently this class does nothing (of value) anymore ! */
252 Debug
= _config
->FindB("Debug::pkgAcquire::SubIndex",false);
254 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
255 DestFile
+= URItoFileName(URI
);
258 Desc
.Description
= URIDesc
;
260 Desc
.ShortDesc
= ShortDesc
;
265 std::clog
<< "pkgAcqSubIndex: " << Desc
.URI
<< std::endl
;
268 // AcqSubIndex::Custom600Headers - Insert custom request headers /*{{{*/
269 // ---------------------------------------------------------------------
270 /* The only header we use is the last-modified header. */
271 string
pkgAcqSubIndex::Custom600Headers() const
273 string Final
= _config
->FindDir("Dir::State::lists");
274 Final
+= URItoFileName(Desc
.URI
);
277 if (stat(Final
.c_str(),&Buf
) != 0)
278 return "\nIndex-File: true\nFail-Ignore: true\n";
279 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
282 void pkgAcqSubIndex::Failed(string Message
,pkgAcquire::MethodConfig
* /*Cnf*/)/*{{{*/
285 std::clog
<< "pkgAcqSubIndex failed: " << Desc
.URI
<< " with " << Message
<< std::endl
;
291 // No good Index is provided
294 void pkgAcqSubIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
295 pkgAcquire::MethodConfig
*Cnf
)
298 std::clog
<< "pkgAcqSubIndex::Done(): " << Desc
.URI
<< std::endl
;
300 string FileName
= LookupTag(Message
,"Filename");
301 if (FileName
.empty() == true)
304 ErrorText
= "Method gave a blank filename";
308 if (FileName
!= DestFile
)
311 Desc
.URI
= "copy:" + FileName
;
316 Item::Done(Message
, Size
, Hashes
, Cnf
);
318 string FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(Desc
.URI
);
320 /* Downloaded invalid transindex => Error (LP: #346386) (Closes: #627642) */
321 indexRecords SubIndexParser
;
322 if (FileExists(DestFile
) == true && !SubIndexParser
.Load(DestFile
)) {
324 ErrorText
= SubIndexParser
.ErrorText
;
328 // success in downloading the index
331 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
<< std::endl
;
332 Rename(DestFile
,FinalFile
);
333 chmod(FinalFile
.c_str(),0644);
334 DestFile
= FinalFile
;
336 if(ParseIndex(DestFile
) == false)
337 return Failed("", NULL
);
345 bool pkgAcqSubIndex::ParseIndex(string
const &IndexFile
) /*{{{*/
347 indexRecords SubIndexParser
;
348 if (FileExists(IndexFile
) == false || SubIndexParser
.Load(IndexFile
) == false)
350 // so something with the downloaded index
354 // AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
355 // ---------------------------------------------------------------------
356 /* Get the DiffIndex file first and see if there are patches available
357 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
358 * patches. If anything goes wrong in that process, it will fall back to
359 * the original packages file
361 pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire
*Owner
,
362 unsigned long TransactionID
,
363 IndexTarget
const * const Target
,
364 HashStringList
const &ExpectedHashes
,
365 indexRecords
*MetaIndexParser
)
366 : pkgAcqBaseIndex(Owner
, TransactionID
, Target
, ExpectedHashes
,
370 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
372 RealURI
= Target
->URI
;
374 Desc
.Description
= Target
->Description
+ "/DiffIndex";
375 Desc
.ShortDesc
= Target
->ShortDesc
;
376 Desc
.URI
= Target
->URI
+ ".diff/Index";
378 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
379 DestFile
+= URItoFileName(Target
->URI
) + string(".DiffIndex");
382 std::clog
<< "pkgAcqDiffIndex: " << Desc
.URI
<< std::endl
;
384 // look for the current package file
385 CurrentPackagesFile
= _config
->FindDir("Dir::State::lists");
386 CurrentPackagesFile
+= URItoFileName(RealURI
);
388 // FIXME: this file:/ check is a hack to prevent fetching
389 // from local sources. this is really silly, and
390 // should be fixed cleanly as soon as possible
391 if(!FileExists(CurrentPackagesFile
) ||
392 Desc
.URI
.substr(0,strlen("file:/")) == "file:/")
394 // we don't have a pkg file or we don't want to queue
396 std::clog
<< "No index file, local or canceld by user" << std::endl
;
402 std::clog
<< "pkgAcqDiffIndex::pkgAcqDiffIndex(): "
403 << CurrentPackagesFile
<< std::endl
;
409 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
410 // ---------------------------------------------------------------------
411 /* The only header we use is the last-modified header. */
412 string
pkgAcqDiffIndex::Custom600Headers() const
414 string Final
= _config
->FindDir("Dir::State::lists");
415 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
418 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
421 if (stat(Final
.c_str(),&Buf
) != 0)
422 return "\nIndex-File: true";
424 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
427 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile
) /*{{{*/
430 std::clog
<< "pkgAcqDiffIndex::ParseIndexDiff() " << IndexDiffFile
435 vector
<DiffInfo
> available_patches
;
437 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
439 if (_error
->PendingError() == true)
442 if(TF
.Step(Tags
) == true)
448 string
const tmp
= Tags
.FindS("SHA1-Current");
449 std::stringstream
ss(tmp
);
450 ss
>> ServerSha1
>> size
;
451 unsigned long const ServerSize
= atol(size
.c_str());
453 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
456 string
const local_sha1
= SHA1
.Result();
458 if(local_sha1
== ServerSha1
)
460 // we have the same sha1 as the server so we are done here
462 std::clog
<< "Package file is up-to-date" << std::endl
;
463 // list cleanup needs to know that this file as well as the already
464 // present index is ours, so we create an empty diff to save it for us
465 new pkgAcqIndexDiffs(Owner
, TransactionID
, Target
,
466 ExpectedHashes
, MetaIndexParser
,
467 ServerSha1
, available_patches
);
473 std::clog
<< "SHA1-Current: " << ServerSha1
<< " and we start at "<< fd
.Name() << " " << fd
.Size() << " " << local_sha1
<< std::endl
;
475 // check the historie and see what patches we need
476 string
const history
= Tags
.FindS("SHA1-History");
477 std::stringstream
hist(history
);
478 while(hist
>> d
.sha1
>> size
>> d
.file
)
480 // read until the first match is found
481 // from that point on, we probably need all diffs
482 if(d
.sha1
== local_sha1
)
484 else if (found
== false)
488 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
489 available_patches
.push_back(d
);
492 if (available_patches
.empty() == false)
494 // patching with too many files is rather slow compared to a fast download
495 unsigned long const fileLimit
= _config
->FindI("Acquire::PDiffs::FileLimit", 0);
496 if (fileLimit
!= 0 && fileLimit
< available_patches
.size())
499 std::clog
<< "Need " << available_patches
.size() << " diffs (Limit is " << fileLimit
500 << ") so fallback to complete download" << std::endl
;
504 // see if the patches are too big
505 found
= false; // it was true and it will be true again at the end
506 d
= *available_patches
.begin();
507 string
const firstPatch
= d
.file
;
508 unsigned long patchesSize
= 0;
509 std::stringstream
patches(Tags
.FindS("SHA1-Patches"));
510 while(patches
>> d
.sha1
>> size
>> d
.file
)
512 if (firstPatch
== d
.file
)
514 else if (found
== false)
517 patchesSize
+= atol(size
.c_str());
519 unsigned long const sizeLimit
= ServerSize
* _config
->FindI("Acquire::PDiffs::SizeLimit", 100);
520 if (sizeLimit
> 0 && (sizeLimit
/100) < patchesSize
)
523 std::clog
<< "Need " << patchesSize
<< " bytes (Limit is " << sizeLimit
/100
524 << ") so fallback to complete download" << std::endl
;
530 // we have something, queue the next diff
534 string::size_type
const last_space
= Description
.rfind(" ");
535 if(last_space
!= string::npos
)
536 Description
.erase(last_space
, Description
.size()-last_space
);
538 /* decide if we should download patches one by one or in one go:
539 The first is good if the server merges patches, but many don't so client
540 based merging can be attempt in which case the second is better.
541 "bad things" will happen if patches are merged on the server,
542 but client side merging is attempt as well */
543 bool pdiff_merge
= _config
->FindB("Acquire::PDiffs::Merge", true);
544 if (pdiff_merge
== true)
546 // reprepro adds this flag if it has merged patches on the server
547 std::string
const precedence
= Tags
.FindS("X-Patch-Precedence");
548 pdiff_merge
= (precedence
!= "merged");
551 if (pdiff_merge
== false)
553 new pkgAcqIndexDiffs(Owner
, TransactionID
, Target
, ExpectedHashes
,
555 ServerSha1
, available_patches
);
559 std::vector
<pkgAcqIndexMergeDiffs
*> *diffs
= new std::vector
<pkgAcqIndexMergeDiffs
*>(available_patches
.size());
560 for(size_t i
= 0; i
< available_patches
.size(); ++i
)
561 (*diffs
)[i
] = new pkgAcqIndexMergeDiffs(Owner
,
566 available_patches
[i
],
577 // Nothing found, report and return false
578 // Failing here is ok, if we return false later, the full
579 // IndexFile is queued
581 std::clog
<< "Can't find a patch in the index file" << std::endl
;
585 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
* /*Cnf*/)/*{{{*/
588 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< " with " << Message
<< std::endl
589 << "Falling back to normal index file acquire" << std::endl
;
591 new pkgAcqIndex(Owner
, TransactionID
, Target
, ExpectedHashes
, MetaIndexParser
);
598 void pkgAcqDiffIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
599 pkgAcquire::MethodConfig
*Cnf
)
602 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
604 Item::Done(Message
, Size
, Hashes
, Cnf
);
607 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
609 // success in downloading the index
611 FinalFile
+= string(".IndexDiff");
613 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
615 Rename(DestFile
,FinalFile
);
616 chmod(FinalFile
.c_str(),0644);
617 DestFile
= FinalFile
;
619 if(!ParseDiffIndex(DestFile
))
620 return Failed("", NULL
);
628 // AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
629 // ---------------------------------------------------------------------
630 /* The package diff is added to the queue. one object is constructed
631 * for each diff and the index
633 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
634 unsigned long TransactionID
,
635 struct IndexTarget
const * const Target
,
636 HashStringList
const &ExpectedHashes
,
637 indexRecords
*MetaIndexParser
,
639 vector
<DiffInfo
> diffs
)
640 : pkgAcqBaseIndex(Owner
, TransactionID
, Target
, ExpectedHashes
, MetaIndexParser
),
641 available_patches(diffs
), ServerSha1(ServerSha1
)
644 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
645 DestFile
+= URItoFileName(Target
->URI
);
647 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
649 RealURI
= Target
->URI
;
651 Description
= Target
->Description
;
652 Desc
.ShortDesc
= Target
->ShortDesc
;
654 if(available_patches
.empty() == true)
656 // we are done (yeah!)
662 State
= StateFetchDiff
;
667 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
* /*Cnf*/)/*{{{*/
670 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< " with " << Message
<< std::endl
671 << "Falling back to normal index file acquire" << std::endl
;
672 new pkgAcqIndex(Owner
, TransactionID
, Target
, ExpectedHashes
, MetaIndexParser
);
676 // Finish - helper that cleans the item out of the fetcher queue /*{{{*/
677 void pkgAcqIndexDiffs::Finish(bool allDone
)
680 std::clog
<< "pkgAcqIndexDiffs::Finish(): "
682 << Desc
.URI
<< std::endl
;
684 // we restore the original name, this is required, otherwise
685 // the file will be cleaned
688 DestFile
= _config
->FindDir("Dir::State::lists");
689 DestFile
+= URItoFileName(RealURI
);
691 // FIXME: we want the rred stuff to use the real transactional update
692 // this is just a workaround
693 PartialFile
= DestFile
;
695 if(HashSums().usable() && !HashSums().VerifyFile(DestFile
))
697 RenameOnError(HashSumMismatch
);
702 // this is for the "real" finish
707 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
712 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
719 bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
722 // calc sha1 of the just patched file
723 string FinalFile
= _config
->FindDir("Dir::State::lists");
724 FinalFile
+= URItoFileName(RealURI
);
726 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
729 string local_sha1
= string(SHA1
.Result());
731 std::clog
<< "QueueNextDiff: "
732 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
734 // final file reached before all patches are applied
735 if(local_sha1
== ServerSha1
)
741 // remove all patches until the next matching patch is found
742 // this requires the Index file to be ordered
743 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
744 available_patches
.empty() == false &&
745 I
!= available_patches
.end() &&
746 I
->sha1
!= local_sha1
;
749 available_patches
.erase(I
);
752 // error checking and falling back if no patch was found
753 if(available_patches
.empty() == true)
759 // queue the right diff
760 Desc
.URI
= RealURI
+ ".diff/" + available_patches
[0].file
+ ".gz";
761 Desc
.Description
= Description
+ " " + available_patches
[0].file
+ string(".pdiff");
762 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
763 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
766 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
773 void pkgAcqIndexDiffs::Done(string Message
,unsigned long long Size
, HashStringList
const &Hashes
, /*{{{*/
774 pkgAcquire::MethodConfig
*Cnf
)
777 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
779 Item::Done(Message
, Size
, Hashes
, Cnf
);
782 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
784 // success in downloading a diff, enter ApplyDiff state
785 if(State
== StateFetchDiff
)
788 // rred excepts the patch as $FinalFile.ed
789 Rename(DestFile
,FinalFile
+".ed");
792 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
794 State
= StateApplyDiff
;
796 Desc
.URI
= "rred:" + FinalFile
;
803 // success in download/apply a diff, queue next (if needed)
804 if(State
== StateApplyDiff
)
806 // remove the just applied patch
807 available_patches
.erase(available_patches
.begin());
808 unlink((FinalFile
+ ".ed").c_str());
813 std::clog
<< "Moving patched file in place: " << std::endl
814 << DestFile
<< " -> " << FinalFile
<< std::endl
;
816 Rename(DestFile
,FinalFile
);
817 chmod(FinalFile
.c_str(),0644);
819 // see if there is more to download
820 if(available_patches
.empty() == false) {
821 new pkgAcqIndexDiffs(Owner
, TransactionID
, Target
,
822 ExpectedHashes
, MetaIndexParser
,
823 ServerSha1
, available_patches
);
830 // AcqIndexMergeDiffs::AcqIndexMergeDiffs - Constructor /*{{{*/
831 pkgAcqIndexMergeDiffs::pkgAcqIndexMergeDiffs(pkgAcquire
*Owner
,
832 unsigned long TransactionID
,
833 struct IndexTarget
const * const Target
,
834 HashStringList
const &ExpectedHashes
,
835 indexRecords
*MetaIndexParser
,
836 DiffInfo
const &patch
,
837 std::vector
<pkgAcqIndexMergeDiffs
*> const * const allPatches
)
838 : pkgAcqBaseIndex(Owner
, TransactionID
, Target
, ExpectedHashes
, MetaIndexParser
),
839 patch(patch
), allPatches(allPatches
), State(StateFetchDiff
)
842 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
843 DestFile
+= URItoFileName(Target
->URI
);
845 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
847 RealURI
= Target
->URI
;
849 Description
= Target
->Description
;
850 Desc
.ShortDesc
= Target
->ShortDesc
;
852 Desc
.URI
= RealURI
+ ".diff/" + patch
.file
+ ".gz";
853 Desc
.Description
= Description
+ " " + patch
.file
+ string(".pdiff");
854 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
855 DestFile
+= URItoFileName(RealURI
+ ".diff/" + patch
.file
);
858 std::clog
<< "pkgAcqIndexMergeDiffs: " << Desc
.URI
<< std::endl
;
863 void pkgAcqIndexMergeDiffs::Failed(string Message
,pkgAcquire::MethodConfig
* /*Cnf*/)/*{{{*/
866 std::clog
<< "pkgAcqIndexMergeDiffs failed: " << Desc
.URI
<< " with " << Message
<< std::endl
;
871 // check if we are the first to fail, otherwise we are done here
872 State
= StateDoneDiff
;
873 for (std::vector
<pkgAcqIndexMergeDiffs
*>::const_iterator I
= allPatches
->begin();
874 I
!= allPatches
->end(); ++I
)
875 if ((*I
)->State
== StateErrorDiff
)
878 // first failure means we should fallback
879 State
= StateErrorDiff
;
880 std::clog
<< "Falling back to normal index file acquire" << std::endl
;
881 new pkgAcqIndex(Owner
, TransactionID
, Target
, ExpectedHashes
, MetaIndexParser
);
884 void pkgAcqIndexMergeDiffs::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
885 pkgAcquire::MethodConfig
*Cnf
)
888 std::clog
<< "pkgAcqIndexMergeDiffs::Done(): " << Desc
.URI
<< std::endl
;
890 Item::Done(Message
,Size
,Hashes
,Cnf
);
892 string
const FinalFile
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
894 if (State
== StateFetchDiff
)
896 // rred expects the patch as $FinalFile.ed.$patchname.gz
897 Rename(DestFile
, FinalFile
+ ".ed." + patch
.file
+ ".gz");
899 // check if this is the last completed diff
900 State
= StateDoneDiff
;
901 for (std::vector
<pkgAcqIndexMergeDiffs
*>::const_iterator I
= allPatches
->begin();
902 I
!= allPatches
->end(); ++I
)
903 if ((*I
)->State
!= StateDoneDiff
)
906 std::clog
<< "Not the last done diff in the batch: " << Desc
.URI
<< std::endl
;
910 // this is the last completed diff, so we are ready to apply now
911 State
= StateApplyDiff
;
914 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
917 Desc
.URI
= "rred:" + FinalFile
;
922 // success in download/apply all diffs, clean up
923 else if (State
== StateApplyDiff
)
925 // see if we really got the expected file
926 if(ExpectedHashes
.usable() && !ExpectedHashes
.VerifyFile(DestFile
))
928 RenameOnError(HashSumMismatch
);
932 // move the result into place
934 std::clog
<< "Moving patched file in place: " << std::endl
935 << DestFile
<< " -> " << FinalFile
<< std::endl
;
936 Rename(DestFile
, FinalFile
);
937 chmod(FinalFile
.c_str(), 0644);
939 // otherwise lists cleanup will eat the file
940 DestFile
= FinalFile
;
941 // FIXME: make the merged rred code really transactional
942 PartialFile
= FinalFile
;
944 // ensure the ed's are gone regardless of list-cleanup
945 for (std::vector
<pkgAcqIndexMergeDiffs
*>::const_iterator I
= allPatches
->begin();
946 I
!= allPatches
->end(); ++I
)
948 std::string patch
= FinalFile
+ ".ed." + (*I
)->patch
.file
+ ".gz";
949 unlink(patch
.c_str());
955 std::clog
<< "allDone: " << DestFile
<< "\n" << std::endl
;
959 // AcqIndex::AcqIndex - Constructor /*{{{*/
960 // ---------------------------------------------------------------------
961 /* The package file is added to the queue and a second class is
962 instantiated to fetch the revision file */
963 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
964 string URI
,string URIDesc
,string ShortDesc
,
965 HashStringList
const &ExpectedHash
, string comprExt
)
966 : pkgAcqBaseIndex(Owner
, 0, NULL
, ExpectedHash
, NULL
), RealURI(URI
)
968 AutoSelectCompression();
969 Init(URI
, URIDesc
, ShortDesc
);
971 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
972 std::clog
<< "New pkgIndex with TransactionID "
973 << TransactionID
<< std::endl
;
976 // AcqIndex::AcqIndex - Constructor /*{{{*/
977 // ---------------------------------------------------------------------
978 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
979 unsigned long TransactionID
,
980 IndexTarget
const *Target
,
981 HashStringList
const &ExpectedHash
,
982 indexRecords
*MetaIndexParser
)
983 : pkgAcqBaseIndex(Owner
, TransactionID
, Target
, ExpectedHash
,
984 MetaIndexParser
), RealURI(Target
->URI
)
986 // autoselect the compression method
987 AutoSelectCompression();
988 Init(Target
->URI
, Target
->Description
, Target
->ShortDesc
);
990 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
991 std::clog
<< "New pkgIndex with TransactionID "
992 << TransactionID
<< std::endl
;
995 // AcqIndex::AutoSelectCompression - Select compression /*{{{*/
996 // ---------------------------------------------------------------------
997 void pkgAcqIndex::AutoSelectCompression()
999 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
1000 CompressionExtension
= "";
1001 if (ExpectedHashes
.usable())
1003 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
1004 if (*t
== "uncompressed" || MetaIndexParser
->Exists(string(Target
->MetaKey
).append(".").append(*t
)) == true)
1005 CompressionExtension
.append(*t
).append(" ");
1009 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
1010 CompressionExtension
.append(*t
).append(" ");
1012 if (CompressionExtension
.empty() == false)
1013 CompressionExtension
.erase(CompressionExtension
.end()-1);
1015 // AcqIndex::Init - defered Constructor /*{{{*/
1016 void pkgAcqIndex::Init(string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
) {
1017 Decompression
= false;
1020 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1021 DestFile
+= URItoFileName(URI
);
1023 std::string
const comprExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
1024 if (comprExt
== "uncompressed")
1028 MetaKey
= string(Target
->MetaKey
);
1032 Desc
.URI
= URI
+ '.' + comprExt
;
1034 MetaKey
= string(Target
->MetaKey
) + '.' + comprExt
;
1037 // load the filesize
1040 indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup(MetaKey
);
1042 FileSize
= Record
->Size
;
1044 InitByHashIfNeeded(MetaKey
);
1047 Desc
.Description
= URIDesc
;
1049 Desc
.ShortDesc
= ShortDesc
;
1054 // AcqIndex::AdjustForByHash - modify URI for by-hash support /*{{{*/
1055 // ---------------------------------------------------------------------
1057 void pkgAcqIndex::InitByHashIfNeeded(const std::string MetaKey
)
1060 // - (maybe?) add support for by-hash into the sources.list as flag
1061 // - make apt-ftparchive generate the hashes (and expire?)
1062 std::string HostKnob
= "APT::Acquire::" + ::URI(Desc
.URI
).Host
+ "::By-Hash";
1063 if(_config
->FindB("APT::Acquire::By-Hash", false) == true ||
1064 _config
->FindB(HostKnob
, false) == true ||
1065 MetaIndexParser
->GetSupportsAcquireByHash())
1067 indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup(MetaKey
);
1070 // FIXME: should we really use the best hash here? or a fixed one?
1071 const HashString
*TargetHash
= Record
->Hashes
.find("");
1072 std::string ByHash
= "/by-hash/" + TargetHash
->HashType() + "/" + TargetHash
->HashValue();
1073 size_t trailing_slash
= Desc
.URI
.find_last_of("/");
1074 Desc
.URI
= Desc
.URI
.replace(
1076 Desc
.URI
.substr(trailing_slash
+1).size()+1,
1080 "Fetching ByHash requested but can not find record for %s",
1086 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
1087 // ---------------------------------------------------------------------
1088 /* The only header we use is the last-modified header. */
1089 string
pkgAcqIndex::Custom600Headers() const
1091 string Final
= _config
->FindDir("Dir::State::lists");
1092 Final
+= URItoFileName(RealURI
);
1093 if (_config
->FindB("Acquire::GzipIndexes",false))
1096 string msg
= "\nIndex-File: true";
1097 // FIXME: this really should use "IndexTarget::IsOptional()" but that
1098 // seems to be difficult without breaking ABI
1099 if (ShortDesc().find("Translation") != 0)
1100 msg
+= "\nFail-Ignore: true";
1102 if (stat(Final
.c_str(),&Buf
) == 0)
1103 msg
+= "\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1108 // pkgAcqIndex::Failed - getting the indexfile failed /*{{{*/
1109 // ---------------------------------------------------------------------
1111 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
1113 size_t const nextExt
= CompressionExtension
.find(' ');
1114 if (nextExt
!= std::string::npos
)
1116 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
1117 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
1121 // on decompression failure, remove bad versions in partial/
1122 if (Decompression
&& Erase
) {
1123 string s
= _config
->FindDir("Dir::State::lists") + "partial/";
1124 s
.append(URItoFileName(RealURI
));
1128 Item::Failed(Message
,Cnf
);
1130 /// cancel the entire transaction
1131 Owner
->AbortTransaction(TransactionID
);
1134 // pkgAcqIndex::GetFinalFilename - Return the full final file path /*{{{*/
1135 // ---------------------------------------------------------------------
1137 std::string
pkgAcqIndex::GetFinalFilename(std::string
const &URI
,
1138 std::string
const &compExt
)
1140 std::string FinalFile
= _config
->FindDir("Dir::State::lists");
1141 FinalFile
+= URItoFileName(URI
);
1142 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz")
1147 // AcqIndex::ReverifyAfterIMS - Reverify index after an ims-hit /*{{{*/
1148 // ---------------------------------------------------------------------
1150 void pkgAcqIndex::ReverifyAfterIMS(std::string
const &FileName
)
1152 std::string
const compExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
1153 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz")
1156 // copy FinalFile into partial/ so that we check the hash again
1157 string FinalFile
= GetFinalFilename(RealURI
, compExt
);
1158 Decompression
= true;
1159 Desc
.URI
= "copy:" + FinalFile
;
1163 // AcqIndex::Done - Finished a fetch /*{{{*/
1164 // ---------------------------------------------------------------------
1165 /* This goes through a number of states.. On the initial fetch the
1166 method could possibly return an alternate filename which points
1167 to the uncompressed version of the file. If this is so the file
1168 is copied into the partial directory. In all other cases the file
1169 is decompressed with a compressed uri. */
1170 void pkgAcqIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
,
1171 pkgAcquire::MethodConfig
*Cfg
)
1173 Item::Done(Message
,Size
,Hashes
,Cfg
);
1174 std::string
const compExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
1176 if (Decompression
== true)
1178 if (ExpectedHashes
.usable() && ExpectedHashes
!= Hashes
)
1180 RenameOnError(HashSumMismatch
);
1181 printHashSumComparision(RealURI
, ExpectedHashes
, Hashes
);
1182 Failed(Message
, Cfg
);
1186 // FIXME: this can go away once we only ever download stuff that
1187 // has a valid hash and we never do GET based probing
1189 /* Always verify the index file for correctness (all indexes must
1190 * have a Package field) (LP: #346386) (Closes: #627642)
1192 FileFd
fd(DestFile
, FileFd::ReadOnly
, FileFd::Extension
);
1193 // Only test for correctness if the content of the file is not empty
1198 pkgTagFile
tag(&fd
);
1200 // all our current indexes have a field 'Package' in each section
1201 if (_error
->PendingError() == true || tag
.Step(sec
) == false || sec
.Exists("Package") == false)
1203 RenameOnError(InvalidFormat
);
1204 Failed(Message
, Cfg
);
1209 // FIXME: can we void the "Erase" bool here as its very non-local?
1210 std::string CompressedFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1211 CompressedFile
+= URItoFileName(RealURI
);
1212 // Remove the compressed version.
1214 unlink(CompressedFile
.c_str());
1216 // Done, queue for rename on transaction finished
1217 PartialFile
= DestFile
;
1218 DestFile
= GetFinalFilename(RealURI
, compExt
);
1223 // FIXME: use the same method to find
1224 // check the compressed hash too
1225 if(MetaKey
!= "" && Hashes
.size() > 0)
1227 indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup(MetaKey
);
1228 if(Record
&& Record
->Hashes
.usable() && Hashes
!= Record
->Hashes
)
1230 RenameOnError(HashSumMismatch
);
1231 printHashSumComparision(RealURI
, Record
->Hashes
, Hashes
);
1232 Failed(Message
, Cfg
);
1240 // Handle the unzipd case
1241 string FileName
= LookupTag(Message
,"Alt-Filename");
1242 if (FileName
.empty() == false)
1244 // The files timestamp matches
1245 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
1247 ReverifyAfterIMS(FileName
);
1250 Decompression
= true;
1252 DestFile
+= ".decomp";
1253 Desc
.URI
= "copy:" + FileName
;
1259 FileName
= LookupTag(Message
,"Filename");
1260 if (FileName
.empty() == true)
1263 ErrorText
= "Method gave a blank filename";
1266 // The files timestamp matches
1267 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1269 ReverifyAfterIMS(FileName
);
1273 if (FileName
== DestFile
)
1280 // If we enable compressed indexes and already have gzip, keep it
1281 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz" && !Local
) {
1282 // Done, queue for rename on transaction finished
1283 PartialFile
= DestFile
;
1284 DestFile
= GetFinalFilename(RealURI
, compExt
);
1288 // get the binary name for your used compression type
1289 decompProg
= _config
->Find(string("Acquire::CompressionTypes::").append(compExt
),"");
1290 if(decompProg
.empty() == false);
1291 else if(compExt
== "uncompressed")
1292 decompProg
= "copy";
1294 _error
->Error("Unsupported extension: %s", compExt
.c_str());
1298 Decompression
= true;
1299 DestFile
+= ".decomp";
1300 Desc
.URI
= decompProg
+ ":" + FileName
;
1303 // FIXME: this points to a c++ string that goes out of scope
1304 Mode
= decompProg
.c_str();
1307 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
1308 // ---------------------------------------------------------------------
1309 /* The Translation file is added to the queue */
1310 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
1311 string URI
,string URIDesc
,string ShortDesc
)
1312 : pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, HashStringList(), "")
1316 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
1317 unsigned long TransactionID
,
1318 IndexTarget
const * const Target
,
1319 HashStringList
const &ExpectedHashes
,
1320 indexRecords
*MetaIndexParser
)
1321 : pkgAcqIndex(Owner
, TransactionID
, Target
, ExpectedHashes
, MetaIndexParser
)
1323 // load the filesize
1324 indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup(string(Target
->MetaKey
));
1326 FileSize
= Record
->Size
;
1329 // AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
1330 // ---------------------------------------------------------------------
1331 string
pkgAcqIndexTrans::Custom600Headers() const
1333 string Final
= _config
->FindDir("Dir::State::lists");
1334 Final
+= URItoFileName(RealURI
);
1337 if (stat(Final
.c_str(),&Buf
) != 0)
1338 return "\nFail-Ignore: true\nIndex-File: true";
1339 return "\nFail-Ignore: true\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1342 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
1343 // ---------------------------------------------------------------------
1345 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1347 size_t const nextExt
= CompressionExtension
.find(' ');
1348 if (nextExt
!= std::string::npos
)
1350 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
1351 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
1356 // FIXME: this is used often (e.g. in pkgAcqIndexTrans) so refactor
1357 if (Cnf
->LocalOnly
== true ||
1358 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1367 Item::Failed(Message
,Cnf
);
1370 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
, /*{{{*/
1371 unsigned long TransactionID
,
1372 string URI
,string URIDesc
,string ShortDesc
,
1373 string MetaIndexFile
,
1374 const vector
<IndexTarget
*>* IndexTargets
,
1375 indexRecords
* MetaIndexParser
) :
1376 Item(Owner
, HashStringList(), TransactionID
), RealURI(URI
),
1377 MetaIndexParser(MetaIndexParser
), MetaIndexFile(MetaIndexFile
),
1378 IndexTargets(IndexTargets
), AuthPass(false), IMSHit(false)
1380 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1381 DestFile
+= URItoFileName(URI
);
1383 // remove any partial downloaded sig-file in partial/.
1384 // it may confuse proxies and is too small to warrant a
1385 // partial download anyway
1386 unlink(DestFile
.c_str());
1388 // set the TransactionID
1389 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
1390 std::clog
<< "New pkgAcqMetaSig with TransactionID "
1391 << TransactionID
<< std::endl
;
1394 Desc
.Description
= URIDesc
;
1396 Desc
.ShortDesc
= ShortDesc
;
1402 pkgAcqMetaSig::~pkgAcqMetaSig() /*{{{*/
1406 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
1407 // ---------------------------------------------------------------------
1408 /* The only header we use is the last-modified header. */
1409 string
pkgAcqMetaSig::Custom600Headers() const
1411 string FinalFile
= _config
->FindDir("Dir::State::lists");
1412 FinalFile
+= URItoFileName(RealURI
);
1415 if (stat(FinalFile
.c_str(),&Buf
) != 0)
1416 return "\nIndex-File: true";
1418 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1421 void pkgAcqMetaSig::Done(string Message
,unsigned long long Size
, HashStringList
const &Hashes
,
1422 pkgAcquire::MethodConfig
*Cfg
)
1424 Item::Done(Message
, Size
, Hashes
, Cfg
);
1426 string FileName
= LookupTag(Message
,"Filename");
1427 if (FileName
.empty() == true)
1430 ErrorText
= "Method gave a blank filename";
1434 if (FileName
!= DestFile
)
1436 // We have to copy it into place
1438 Desc
.URI
= "copy:" + FileName
;
1443 if(StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1446 // adjust paths if its a ims-hit
1449 string FinalFile
= _config
->FindDir("Dir::State::lists");
1450 FinalFile
+= URItoFileName(RealURI
);
1452 DestFile
= PartialFile
= FinalFile
;
1456 if(AuthPass
== false)
1459 Desc
.URI
= "gpgv:" + DestFile
;
1460 DestFile
= MetaIndexFile
;
1465 // queue to copy the file in place if it was not a ims hit, on ims
1466 // hit the file is already at the right place
1469 PartialFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1470 PartialFile
+= URItoFileName(RealURI
);
1472 DestFile
= _config
->FindDir("Dir::State::lists");
1473 DestFile
+= URItoFileName(RealURI
);
1480 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)/*{{{*/
1482 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1484 // this ensures that any file in the lists/ dir is removed by the
1486 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1487 DestFile
+= URItoFileName(RealURI
);
1490 // FIXME: duplicated code from pkgAcqMetaIndex
1491 if (AuthPass
== true)
1493 if(FileExists(Final
))
1495 Status
= StatTransientNetworkError
;
1496 _error
->Warning(_("An error occurred during the signature "
1497 "verification. The repository is not updated "
1498 "and the previous index files will be used. "
1499 "GPG error: %s: %s\n"),
1500 Desc
.Description
.c_str(),
1501 LookupTag(Message
,"Message").c_str());
1502 RunScripts("APT::Update::Auth-Failure");
1504 } else if (LookupTag(Message
,"Message").find("NODATA") != string::npos
) {
1505 /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */
1506 _error
->Error(_("GPG error: %s: %s"),
1507 Desc
.Description
.c_str(),
1508 LookupTag(Message
,"Message").c_str());
1512 _error
->Warning(_("GPG error: %s: %s"),
1513 Desc
.Description
.c_str(),
1514 LookupTag(Message
,"Message").c_str());
1516 // gpgv method failed
1517 ReportMirrorFailure("GPGFailure");
1520 // FIXME: this is used often (e.g. in pkgAcqIndexTrans) so refactor
1521 if (Cnf
->LocalOnly
== true ||
1522 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1530 Item::Failed(Message
,Cnf
);
1533 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
, /*{{{*/
1534 unsigned long TransactionID
,
1535 string URI
,string URIDesc
,string ShortDesc
,
1536 string MetaIndexSigURI
,string MetaIndexSigURIDesc
, string MetaIndexSigShortDesc
,
1537 const vector
<IndexTarget
*>* IndexTargets
,
1538 indexRecords
* MetaIndexParser
) :
1539 Item(Owner
, HashStringList(), TransactionID
), RealURI(URI
), IndexTargets(IndexTargets
),
1540 MetaIndexParser(MetaIndexParser
), AuthPass(false), IMSHit(false),
1541 MetaIndexSigURI(MetaIndexSigURI
), MetaIndexSigURIDesc(MetaIndexSigURIDesc
),
1542 MetaIndexSigShortDesc(MetaIndexSigShortDesc
)
1544 if(TransactionID
== 0)
1545 this->TransactionID
= (unsigned long)this;
1547 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
1548 std::clog
<< "New pkgAcqMetaIndex with TransactionID "
1549 << TransactionID
<< std::endl
;
1551 Init(URIDesc
, ShortDesc
);
1554 // pkgAcqMetaIndex::Init - Delayed constructor /*{{{*/
1555 void pkgAcqMetaIndex::Init(std::string URIDesc
, std::string ShortDesc
)
1557 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1558 DestFile
+= URItoFileName(RealURI
);
1561 Desc
.Description
= URIDesc
;
1563 Desc
.ShortDesc
= ShortDesc
;
1566 // we expect more item
1567 ExpectedAdditionalItems
= IndexTargets
->size();
1570 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
1571 // ---------------------------------------------------------------------
1572 /* The only header we use is the last-modified header. */
1573 string
pkgAcqMetaIndex::Custom600Headers() const
1575 string Final
= _config
->FindDir("Dir::State::lists");
1576 Final
+= URItoFileName(RealURI
);
1579 if (stat(Final
.c_str(),&Buf
) != 0)
1580 return "\nIndex-File: true";
1582 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1585 void pkgAcqMetaIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
1586 pkgAcquire::MethodConfig
*Cfg
)
1588 Item::Done(Message
,Size
,Hashes
,Cfg
);
1590 // MetaIndexes are done in two passes: one to download the
1591 // metaindex with an appropriate method, and a second to verify it
1592 // with the gpgv method
1594 if (AuthPass
== true)
1598 // all cool, move Release file into place
1603 RetrievalDone(Message
);
1605 // Still more retrieving to do
1610 // load indexes, the signature will downloaded afterwards
1611 MetaIndexParser
->Load(DestFile
);
1616 // There was a signature file, so pass it to gpgv for
1618 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1619 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
1620 << SigFile
<< "," << DestFile
<< ")\n";
1622 Desc
.URI
= "gpgv:" + SigFile
;
1629 if (Complete
== true)
1631 string FinalFile
= _config
->FindDir("Dir::State::lists");
1632 FinalFile
+= URItoFileName(RealURI
);
1633 if (SigFile
== DestFile
)
1634 SigFile
= FinalFile
;
1635 // queue for copy in place
1636 PartialFile
= DestFile
;
1637 DestFile
= FinalFile
;
1641 void pkgAcqMetaIndex::RetrievalDone(string Message
) /*{{{*/
1643 // We have just finished downloading a Release file (it is not
1646 string FileName
= LookupTag(Message
,"Filename");
1647 if (FileName
.empty() == true)
1650 ErrorText
= "Method gave a blank filename";
1654 if (FileName
!= DestFile
)
1657 Desc
.URI
= "copy:" + FileName
;
1662 // make sure to verify against the right file on I-M-S hit
1663 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
1666 string FinalFile
= _config
->FindDir("Dir::State::lists");
1667 FinalFile
+= URItoFileName(RealURI
);
1668 if (SigFile
== DestFile
)
1670 SigFile
= FinalFile
;
1672 // constructor of pkgAcqMetaClearSig moved it out of the way,
1673 // now move it back in on IMS hit for the 'old' file
1674 string
const OldClearSig
= DestFile
+ ".reverify";
1675 if (RealFileExists(OldClearSig
) == true)
1676 Rename(OldClearSig
, FinalFile
);
1679 DestFile
= FinalFile
;
1682 // queue a signature
1683 if(SigFile
!= DestFile
)
1684 new pkgAcqMetaSig(Owner
, TransactionID
,
1685 MetaIndexSigURI
, MetaIndexSigURIDesc
,
1686 MetaIndexSigShortDesc
, DestFile
, IndexTargets
,
1692 void pkgAcqMetaIndex::AuthDone(string Message
) /*{{{*/
1694 // At this point, the gpgv method has succeeded, so there is a
1695 // valid signature from a key in the trusted keyring. We
1696 // perform additional verification of its contents, and use them
1697 // to verify the indexes we are about to download
1699 if (!MetaIndexParser
->Load(DestFile
))
1701 Status
= StatAuthError
;
1702 ErrorText
= MetaIndexParser
->ErrorText
;
1706 if (!VerifyVendor(Message
))
1711 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1712 std::cerr
<< "Signature verification succeeded: "
1713 << DestFile
<< std::endl
;
1715 // Download further indexes with verification
1719 // is it a clearsigned MetaIndex file?
1720 if (DestFile
== SigFile
)
1723 // Done, move signature file into position
1724 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1725 URItoFileName(RealURI
) + ".gpg";
1726 Rename(SigFile
,VerifiedSigFile
);
1727 chmod(VerifiedSigFile
.c_str(),0644);
1731 void pkgAcqMetaIndex::QueueIndexes(bool verify
) /*{{{*/
1733 bool transInRelease
= false;
1735 std::vector
<std::string
> const keys
= MetaIndexParser
->MetaKeys();
1736 for (std::vector
<std::string
>::const_iterator k
= keys
.begin(); k
!= keys
.end(); ++k
)
1737 // FIXME: Feels wrong to check for hardcoded string here, but what should we do else…
1738 if (k
->find("Translation-") != std::string::npos
)
1740 transInRelease
= true;
1745 // at this point the real Items are loaded in the fetcher
1746 ExpectedAdditionalItems
= 0;
1747 for (vector
<IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
1748 Target
!= IndexTargets
->end();
1751 HashStringList ExpectedIndexHashes
;
1752 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1753 bool compressedAvailable
= false;
1756 if ((*Target
)->IsOptional() == true)
1758 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
1759 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
1760 if (MetaIndexParser
->Exists((*Target
)->MetaKey
+ "." + *t
) == true)
1762 compressedAvailable
= true;
1766 else if (verify
== true)
1768 Status
= StatAuthError
;
1769 strprintf(ErrorText
, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), (*Target
)->MetaKey
.c_str());
1775 ExpectedIndexHashes
= Record
->Hashes
;
1776 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1778 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
1779 << "Expected Hash:" << std::endl
;
1780 for (HashStringList::const_iterator hs
= ExpectedIndexHashes
.begin(); hs
!= ExpectedIndexHashes
.end(); ++hs
)
1781 std::cerr
<< "\t- " << hs
->toStr() << std::endl
;
1782 std::cerr
<< "For: " << Record
->MetaKeyFilename
<< std::endl
;
1784 if (verify
== true && ExpectedIndexHashes
.empty() == true && (*Target
)->IsOptional() == false)
1786 Status
= StatAuthError
;
1787 strprintf(ErrorText
, _("Unable to find hash sum for '%s' in Release file"), (*Target
)->MetaKey
.c_str());
1792 if ((*Target
)->IsOptional() == true)
1794 if ((*Target
)->IsSubIndex() == true)
1795 new pkgAcqSubIndex(Owner
, TransactionID
,
1796 (*Target
)->URI
, (*Target
)->Description
,
1797 (*Target
)->ShortDesc
, ExpectedIndexHashes
);
1798 else if (transInRelease
== false || Record
!= NULL
|| compressedAvailable
== true)
1800 if (_config
->FindB("Acquire::PDiffs",true) == true && transInRelease
== true &&
1801 MetaIndexParser
->Exists((*Target
)->MetaKey
+ ".diff/Index") == true)
1802 new pkgAcqDiffIndex(Owner
, TransactionID
, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1804 new pkgAcqIndexTrans(Owner
, TransactionID
, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1809 /* Queue Packages file (either diff or full packages files, depending
1810 on the users option) - we also check if the PDiff Index file is listed
1811 in the Meta-Index file. Ideal would be if pkgAcqDiffIndex would test this
1812 instead, but passing the required info to it is to much hassle */
1813 if(_config
->FindB("Acquire::PDiffs",true) == true && (verify
== false ||
1814 MetaIndexParser
->Exists((*Target
)->MetaKey
+ ".diff/Index") == true))
1815 new pkgAcqDiffIndex(Owner
, TransactionID
, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1817 new pkgAcqIndex(Owner
, TransactionID
, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1821 bool pkgAcqMetaIndex::VerifyVendor(string Message
) /*{{{*/
1823 string::size_type pos
;
1825 // check for missing sigs (that where not fatal because otherwise we had
1828 string msg
= _("There is no public key available for the "
1829 "following key IDs:\n");
1830 pos
= Message
.find("NO_PUBKEY ");
1831 if (pos
!= std::string::npos
)
1833 string::size_type start
= pos
+strlen("NO_PUBKEY ");
1834 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
1835 missingkeys
+= (Fingerprint
);
1837 if(!missingkeys
.empty())
1838 _error
->Warning("%s", (msg
+ missingkeys
).c_str());
1840 string Transformed
= MetaIndexParser
->GetExpectedDist();
1842 if (Transformed
== "../project/experimental")
1844 Transformed
= "experimental";
1847 pos
= Transformed
.rfind('/');
1848 if (pos
!= string::npos
)
1850 Transformed
= Transformed
.substr(0, pos
);
1853 if (Transformed
== ".")
1858 if (_config
->FindB("Acquire::Check-Valid-Until", true) == true &&
1859 MetaIndexParser
->GetValidUntil() > 0) {
1860 time_t const invalid_since
= time(NULL
) - MetaIndexParser
->GetValidUntil();
1861 if (invalid_since
> 0)
1862 // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
1863 // the time since then the file is invalid - formated in the same way as in
1864 // the download progress display (e.g. 7d 3h 42min 1s)
1865 return _error
->Error(
1866 _("Release file for %s is expired (invalid since %s). "
1867 "Updates for this repository will not be applied."),
1868 RealURI
.c_str(), TimeToStr(invalid_since
).c_str());
1871 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1873 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1874 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1875 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1878 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1880 // This might become fatal one day
1881 // Status = StatAuthError;
1882 // ErrorText = "Conflicting distribution; expected "
1883 // + MetaIndexParser->GetExpectedDist() + " but got "
1884 // + MetaIndexParser->GetDist();
1886 if (!Transformed
.empty())
1888 _error
->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
1889 Desc
.Description
.c_str(),
1890 Transformed
.c_str(),
1891 MetaIndexParser
->GetDist().c_str());
1898 // pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
1899 // ---------------------------------------------------------------------
1901 void pkgAcqMetaIndex::Failed(string Message
,
1902 pkgAcquire::MethodConfig
* /*Cnf*/)
1904 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1906 if (AuthPass
== true)
1908 if(FileExists(Final
))
1910 Status
= StatTransientNetworkError
;
1911 _error
->Warning(_("An error occurred during the signature "
1912 "verification. The repository is not updated "
1913 "and the previous index files will be used. "
1914 "GPG error: %s: %s\n"),
1915 Desc
.Description
.c_str(),
1916 LookupTag(Message
,"Message").c_str());
1917 RunScripts("APT::Update::Auth-Failure");
1919 } else if (LookupTag(Message
,"Message").find("NODATA") != string::npos
) {
1920 /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */
1921 _error
->Error(_("GPG error: %s: %s"),
1922 Desc
.Description
.c_str(),
1923 LookupTag(Message
,"Message").c_str());
1927 _error
->Warning(_("GPG error: %s: %s"),
1928 Desc
.Description
.c_str(),
1929 LookupTag(Message
,"Message").c_str());
1931 // gpgv method failed
1932 ReportMirrorFailure("GPGFailure");
1935 /* Always move the meta index, even if gpgv failed. This ensures
1936 * that PackageFile objects are correctly filled in */
1937 if (FileExists(DestFile
)) {
1938 string FinalFile
= _config
->FindDir("Dir::State::lists");
1939 FinalFile
+= URItoFileName(RealURI
);
1940 /* InRelease files become Release files, otherwise
1941 * they would be considered as trusted later on */
1942 if (SigFile
== DestFile
) {
1943 RealURI
= RealURI
.replace(RealURI
.rfind("InRelease"), 9,
1945 FinalFile
= FinalFile
.replace(FinalFile
.rfind("InRelease"), 9,
1947 SigFile
= FinalFile
;
1950 // Done, queue for rename on transaction finished
1951 PartialFile
= DestFile
;
1952 DestFile
= FinalFile
;
1955 // No Release file was present, or verification failed, so fall
1956 // back to queueing Packages files without verification
1957 QueueIndexes(false);
1961 void pkgAcqMetaIndex::Finished()
1963 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
1964 std::clog
<< "Finished: " << DestFile
<<std::endl
;
1965 if(Owner
->TransactionHasError(TransactionID
) == false &&
1967 Owner
->CommitTransaction(TransactionID
);
1971 pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire
*Owner
, /*{{{*/
1972 string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
,
1973 string
const &MetaIndexURI
, string
const &MetaIndexURIDesc
, string
const &MetaIndexShortDesc
,
1974 string
const &MetaSigURI
, string
const &MetaSigURIDesc
, string
const &MetaSigShortDesc
,
1975 const vector
<IndexTarget
*>* IndexTargets
,
1976 indexRecords
* MetaIndexParser
) :
1977 pkgAcqMetaIndex(Owner
, (unsigned long)this, URI
, URIDesc
, ShortDesc
, MetaSigURI
, MetaSigURIDesc
,MetaSigShortDesc
, IndexTargets
, MetaIndexParser
),
1978 MetaIndexURI(MetaIndexURI
), MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
1979 MetaSigURI(MetaSigURI
), MetaSigURIDesc(MetaSigURIDesc
), MetaSigShortDesc(MetaSigShortDesc
)
1983 // index targets + (worst case:) Release/Release.gpg
1984 ExpectedAdditionalItems
= IndexTargets
->size() + 2;
1987 // keep the old InRelease around in case of transistent network errors
1988 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1989 if (RealFileExists(Final
) == true)
1991 string
const LastGoodSig
= DestFile
+ ".reverify";
1992 Rename(Final
,LastGoodSig
);
1997 pkgAcqMetaClearSig::~pkgAcqMetaClearSig() /*{{{*/
2000 // if the file was never queued undo file-changes done in the constructor
2001 if (QueueCounter
== 1 && Status
== StatIdle
&& FileSize
== 0 && Complete
== false)
2003 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
2004 string
const LastGoodSig
= DestFile
+ ".reverify";
2005 if (RealFileExists(Final
) == false && RealFileExists(LastGoodSig
) == true)
2006 Rename(LastGoodSig
, Final
);
2011 // pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
2012 // ---------------------------------------------------------------------
2013 // FIXME: this can go away once the InRelease file is used widely
2014 string
pkgAcqMetaClearSig::Custom600Headers() const
2016 string Final
= _config
->FindDir("Dir::State::lists");
2017 Final
+= URItoFileName(RealURI
);
2020 if (stat(Final
.c_str(),&Buf
) != 0)
2022 if (stat(Final
.c_str(),&Buf
) != 0)
2023 return "\nIndex-File: true\nFail-Ignore: true\n";
2026 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
2029 // pkgAcqMetaClearSig::Done - We got a file /*{{{*/
2030 // ---------------------------------------------------------------------
2031 void pkgAcqMetaClearSig::Done(std::string Message
,unsigned long long Size
,
2032 HashStringList
const &Hashes
,
2033 pkgAcquire::MethodConfig
*Cnf
)
2035 // if we expect a ClearTextSignature (InRelase), ensure that
2036 // this is what we get and if not fail to queue a
2037 // Release/Release.gpg, see #346386
2038 if (FileExists(DestFile
) && !StartsWithGPGClearTextSignature(DestFile
))
2040 pkgAcquire::Item::Failed(Message
, Cnf
);
2041 ErrorText
= _("Does not start with a cleartext signature");
2044 pkgAcqMetaIndex::Done(Message
, Size
, Hashes
, Cnf
);
2047 void pkgAcqMetaClearSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
2049 // we failed, we will not get additional items from this method
2050 ExpectedAdditionalItems
= 0;
2052 if (AuthPass
== false)
2054 // Queue the 'old' InRelease file for removal if we try Release.gpg
2055 // as otherwise the file will stay around and gives a false-auth
2056 // impression (CVE-2012-0214)
2057 string FinalFile
= _config
->FindDir("Dir::State::lists");
2058 FinalFile
.append(URItoFileName(RealURI
));
2060 DestFile
= FinalFile
;
2062 new pkgAcqMetaIndex(Owner
, TransactionID
,
2063 MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
2064 MetaSigURI
, MetaSigURIDesc
, MetaSigShortDesc
,
2065 IndexTargets
, MetaIndexParser
);
2066 if (Cnf
->LocalOnly
== true ||
2067 StringToBool(LookupTag(Message
, "Transient-Failure"), false) == false)
2071 pkgAcqMetaIndex::Failed(Message
, Cnf
);
2074 // AcqArchive::AcqArchive - Constructor /*{{{*/
2075 // ---------------------------------------------------------------------
2076 /* This just sets up the initial fetch environment and queues the first
2078 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
2079 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
2080 string
&StoreFilename
) :
2081 Item(Owner
, HashStringList()), Version(Version
), Sources(Sources
), Recs(Recs
),
2082 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
2085 Retries
= _config
->FindI("Acquire::Retries",0);
2087 if (Version
.Arch() == 0)
2089 _error
->Error(_("I wasn't able to locate a file for the %s package. "
2090 "This might mean you need to manually fix this package. "
2091 "(due to missing arch)"),
2092 Version
.ParentPkg().FullName().c_str());
2096 /* We need to find a filename to determine the extension. We make the
2097 assumption here that all the available sources for this version share
2098 the same extension.. */
2099 // Skip not source sources, they do not have file fields.
2100 for (; Vf
.end() == false; ++Vf
)
2102 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
2107 // Does not really matter here.. we are going to fail out below
2108 if (Vf
.end() != true)
2110 // If this fails to get a file name we will bomb out below.
2111 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
2112 if (_error
->PendingError() == true)
2115 // Generate the final file name as: package_version_arch.foo
2116 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
2117 QuoteString(Version
.VerStr(),"_:") + '_' +
2118 QuoteString(Version
.Arch(),"_:.") +
2119 "." + flExtension(Parse
.FileName());
2122 // check if we have one trusted source for the package. if so, switch
2123 // to "TrustedOnly" mode - but only if not in AllowUnauthenticated mode
2124 bool const allowUnauth
= _config
->FindB("APT::Get::AllowUnauthenticated", false);
2125 bool const debugAuth
= _config
->FindB("Debug::pkgAcquire::Auth", false);
2126 bool seenUntrusted
= false;
2127 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; ++i
)
2129 pkgIndexFile
*Index
;
2130 if (Sources
->FindIndex(i
.File(),Index
) == false)
2133 if (debugAuth
== true)
2134 std::cerr
<< "Checking index: " << Index
->Describe()
2135 << "(Trusted=" << Index
->IsTrusted() << ")" << std::endl
;
2137 if (Index
->IsTrusted() == true)
2140 if (allowUnauth
== false)
2144 seenUntrusted
= true;
2147 // "allow-unauthenticated" restores apts old fetching behaviour
2148 // that means that e.g. unauthenticated file:// uris are higher
2149 // priority than authenticated http:// uris
2150 if (allowUnauth
== true && seenUntrusted
== true)
2154 if (QueueNext() == false && _error
->PendingError() == false)
2155 _error
->Error(_("Can't find a source to download version '%s' of '%s'"),
2156 Version
.VerStr(), Version
.ParentPkg().FullName(false).c_str());
2159 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
2160 // ---------------------------------------------------------------------
2161 /* This queues the next available file version for download. It checks if
2162 the archive is already available in the cache and stashs the MD5 for
2164 bool pkgAcqArchive::QueueNext()
2166 for (; Vf
.end() == false; ++Vf
)
2168 // Ignore not source sources
2169 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
2172 // Try to cross match against the source list
2173 pkgIndexFile
*Index
;
2174 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
2177 // only try to get a trusted package from another source if that source
2179 if(Trusted
&& !Index
->IsTrusted())
2182 // Grab the text package record
2183 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
2184 if (_error
->PendingError() == true)
2187 string PkgFile
= Parse
.FileName();
2188 ExpectedHashes
= Parse
.Hashes();
2190 if (PkgFile
.empty() == true)
2191 return _error
->Error(_("The package index files are corrupted. No Filename: "
2192 "field for package %s."),
2193 Version
.ParentPkg().Name());
2195 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
2196 Desc
.Description
= Index
->ArchiveInfo(Version
);
2198 Desc
.ShortDesc
= Version
.ParentPkg().FullName(true);
2200 // See if we already have the file. (Legacy filenames)
2201 FileSize
= Version
->Size
;
2202 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
2204 if (stat(FinalFile
.c_str(),&Buf
) == 0)
2206 // Make sure the size matches
2207 if ((unsigned long long)Buf
.st_size
== Version
->Size
)
2212 StoreFilename
= DestFile
= FinalFile
;
2216 /* Hmm, we have a file and its size does not match, this means it is
2217 an old style mismatched arch */
2218 unlink(FinalFile
.c_str());
2221 // Check it again using the new style output filenames
2222 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
2223 if (stat(FinalFile
.c_str(),&Buf
) == 0)
2225 // Make sure the size matches
2226 if ((unsigned long long)Buf
.st_size
== Version
->Size
)
2231 StoreFilename
= DestFile
= FinalFile
;
2235 /* Hmm, we have a file and its size does not match, this shouldn't
2237 unlink(FinalFile
.c_str());
2240 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
2242 // Check the destination file
2243 if (stat(DestFile
.c_str(),&Buf
) == 0)
2245 // Hmm, the partial file is too big, erase it
2246 if ((unsigned long long)Buf
.st_size
> Version
->Size
)
2247 unlink(DestFile
.c_str());
2249 PartialSize
= Buf
.st_size
;
2252 // Disables download of archives - useful if no real installation follows,
2253 // e.g. if we are just interested in proposed installation order
2254 if (_config
->FindB("Debug::pkgAcqArchive::NoQueue", false) == true)
2259 StoreFilename
= DestFile
= FinalFile
;
2273 // AcqArchive::Done - Finished fetching /*{{{*/
2274 // ---------------------------------------------------------------------
2276 void pkgAcqArchive::Done(string Message
,unsigned long long Size
, HashStringList
const &CalcHashes
,
2277 pkgAcquire::MethodConfig
*Cfg
)
2279 Item::Done(Message
, Size
, CalcHashes
, Cfg
);
2282 if (Size
!= Version
->Size
)
2284 RenameOnError(SizeMismatch
);
2288 // FIXME: could this empty() check impose *any* sort of security issue?
2289 if(ExpectedHashes
.usable() && ExpectedHashes
!= CalcHashes
)
2291 RenameOnError(HashSumMismatch
);
2292 printHashSumComparision(DestFile
, ExpectedHashes
, CalcHashes
);
2296 // Grab the output filename
2297 string FileName
= LookupTag(Message
,"Filename");
2298 if (FileName
.empty() == true)
2301 ErrorText
= "Method gave a blank filename";
2307 // Reference filename
2308 if (FileName
!= DestFile
)
2310 StoreFilename
= DestFile
= FileName
;
2315 // Done, move it into position
2316 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
2317 FinalFile
+= flNotDir(StoreFilename
);
2318 Rename(DestFile
,FinalFile
);
2320 StoreFilename
= DestFile
= FinalFile
;
2324 // AcqArchive::Failed - Failure handler /*{{{*/
2325 // ---------------------------------------------------------------------
2326 /* Here we try other sources */
2327 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
2329 ErrorText
= LookupTag(Message
,"Message");
2331 /* We don't really want to retry on failed media swaps, this prevents
2332 that. An interesting observation is that permanent failures are not
2334 if (Cnf
->Removable
== true &&
2335 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2337 // Vf = Version.FileList();
2338 while (Vf
.end() == false) ++Vf
;
2339 StoreFilename
= string();
2340 Item::Failed(Message
,Cnf
);
2344 if (QueueNext() == false)
2346 // This is the retry counter
2348 Cnf
->LocalOnly
== false &&
2349 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2352 Vf
= Version
.FileList();
2353 if (QueueNext() == true)
2357 StoreFilename
= string();
2358 Item::Failed(Message
,Cnf
);
2362 // AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
2363 // ---------------------------------------------------------------------
2364 APT_PURE
bool pkgAcqArchive::IsTrusted() const
2369 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
2370 // ---------------------------------------------------------------------
2372 void pkgAcqArchive::Finished()
2374 if (Status
== pkgAcquire::Item::StatDone
&&
2377 StoreFilename
= string();
2380 // AcqFile::pkgAcqFile - Constructor /*{{{*/
2381 // ---------------------------------------------------------------------
2382 /* The file is added to the queue */
2383 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
, HashStringList
const &Hashes
,
2384 unsigned long long Size
,string Dsc
,string ShortDesc
,
2385 const string
&DestDir
, const string
&DestFilename
,
2387 Item(Owner
, Hashes
), IsIndexFile(IsIndexFile
)
2389 Retries
= _config
->FindI("Acquire::Retries",0);
2391 if(!DestFilename
.empty())
2392 DestFile
= DestFilename
;
2393 else if(!DestDir
.empty())
2394 DestFile
= DestDir
+ "/" + flNotDir(URI
);
2396 DestFile
= flNotDir(URI
);
2400 Desc
.Description
= Dsc
;
2403 // Set the short description to the archive component
2404 Desc
.ShortDesc
= ShortDesc
;
2406 // Get the transfer sizes
2409 if (stat(DestFile
.c_str(),&Buf
) == 0)
2411 // Hmm, the partial file is too big, erase it
2412 if ((Size
> 0) && (unsigned long long)Buf
.st_size
> Size
)
2413 unlink(DestFile
.c_str());
2415 PartialSize
= Buf
.st_size
;
2421 // AcqFile::Done - Item downloaded OK /*{{{*/
2422 // ---------------------------------------------------------------------
2424 void pkgAcqFile::Done(string Message
,unsigned long long Size
,HashStringList
const &CalcHashes
,
2425 pkgAcquire::MethodConfig
*Cnf
)
2427 Item::Done(Message
,Size
,CalcHashes
,Cnf
);
2430 if(ExpectedHashes
.usable() && ExpectedHashes
!= CalcHashes
)
2432 RenameOnError(HashSumMismatch
);
2433 printHashSumComparision(DestFile
, ExpectedHashes
, CalcHashes
);
2437 string FileName
= LookupTag(Message
,"Filename");
2438 if (FileName
.empty() == true)
2441 ErrorText
= "Method gave a blank filename";
2447 // The files timestamp matches
2448 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
2451 // We have to copy it into place
2452 if (FileName
!= DestFile
)
2455 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
2456 Cnf
->Removable
== true)
2458 Desc
.URI
= "copy:" + FileName
;
2463 // Erase the file if it is a symlink so we can overwrite it
2465 if (lstat(DestFile
.c_str(),&St
) == 0)
2467 if (S_ISLNK(St
.st_mode
) != 0)
2468 unlink(DestFile
.c_str());
2472 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
2474 ErrorText
= "Link to " + DestFile
+ " failure ";
2481 // AcqFile::Failed - Failure handler /*{{{*/
2482 // ---------------------------------------------------------------------
2483 /* Here we try other sources */
2484 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
2486 ErrorText
= LookupTag(Message
,"Message");
2488 // This is the retry counter
2490 Cnf
->LocalOnly
== false &&
2491 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2498 Item::Failed(Message
,Cnf
);
2501 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
2502 // ---------------------------------------------------------------------
2503 /* The only header we use is the last-modified header. */
2504 string
pkgAcqFile::Custom600Headers() const
2507 return "\nIndex-File: true";