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
, HashStringList
const &ExpectedHashes
) :
68 Owner(Owner
), FileSize(0), PartialSize(0), Mode(0), ID(0), Complete(false),
69 Local(false), QueueCounter(0), TransactionID(0), ExpectedAdditionalItems(0),
70 ExpectedHashes(ExpectedHashes
)
76 // Acquire::Item::~Item - Destructor /*{{{*/
77 // ---------------------------------------------------------------------
79 pkgAcquire::Item::~Item()
84 // Acquire::Item::Failed - Item failed to download /*{{{*/
85 // ---------------------------------------------------------------------
86 /* We return to an idle state if there are still other queues that could
88 void pkgAcquire::Item::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
91 ErrorText
= LookupTag(Message
,"Message");
92 UsedMirror
= LookupTag(Message
,"UsedMirror");
93 if (QueueCounter
<= 1)
95 /* This indicates that the file is not available right now but might
96 be sometime later. If we do a retry cycle then this should be
98 if (Cnf
->LocalOnly
== true &&
99 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
110 // report mirror failure back to LP if we actually use a mirror
111 string FailReason
= LookupTag(Message
, "FailReason");
112 if(FailReason
.size() != 0)
113 ReportMirrorFailure(FailReason
);
115 ReportMirrorFailure(ErrorText
);
118 // Acquire::Item::Start - Item has begun to download /*{{{*/
119 // ---------------------------------------------------------------------
120 /* Stash status and the file size. Note that setting Complete means
121 sub-phases of the acquire process such as decompresion are operating */
122 void pkgAcquire::Item::Start(string
/*Message*/,unsigned long long Size
)
124 Status
= StatFetching
;
125 if (FileSize
== 0 && Complete
== false)
129 // Acquire::Item::Done - Item downloaded OK /*{{{*/
130 // ---------------------------------------------------------------------
132 void pkgAcquire::Item::Done(string Message
,unsigned long long Size
,HashStringList
const &/*Hash*/,
133 pkgAcquire::MethodConfig
* /*Cnf*/)
135 // We just downloaded something..
136 string FileName
= LookupTag(Message
,"Filename");
137 UsedMirror
= LookupTag(Message
,"UsedMirror");
138 if (Complete
== false && !Local
&& FileName
== DestFile
)
141 Owner
->Log
->Fetched(Size
,atoi(LookupTag(Message
,"Resume-Point","0").c_str()));
147 ErrorText
= string();
148 Owner
->Dequeue(this);
151 // Acquire::Item::Rename - Rename a file /*{{{*/
152 // ---------------------------------------------------------------------
153 /* This helper function is used by a lot of item methods as their final
155 void pkgAcquire::Item::Rename(string From
,string To
)
157 if (rename(From
.c_str(),To
.c_str()) != 0)
160 snprintf(S
,sizeof(S
),_("rename failed, %s (%s -> %s)."),strerror(errno
),
161 From
.c_str(),To
.c_str());
167 bool pkgAcquire::Item::RenameOnError(pkgAcquire::Item::RenameOnErrorState
const error
)/*{{{*/
169 if(FileExists(DestFile
))
170 Rename(DestFile
, DestFile
+ ".FAILED");
174 case HashSumMismatch
:
175 ErrorText
= _("Hash Sum mismatch");
176 Status
= StatAuthError
;
177 ReportMirrorFailure("HashChecksumFailure");
180 ErrorText
= _("Size mismatch");
181 Status
= StatAuthError
;
182 ReportMirrorFailure("SizeFailure");
185 ErrorText
= _("Invalid file format");
187 // do not report as usually its not the mirrors fault, but Portal/Proxy
193 // Acquire::Item::ReportMirrorFailure /*{{{*/
194 // ---------------------------------------------------------------------
195 void pkgAcquire::Item::ReportMirrorFailure(string FailCode
)
197 // we only act if a mirror was used at all
198 if(UsedMirror
.empty())
201 std::cerr
<< "\nReportMirrorFailure: "
203 << " Uri: " << DescURI()
205 << FailCode
<< std::endl
;
207 const char *Args
[40];
209 string report
= _config
->Find("Methods::Mirror::ProblemReporting",
210 "/usr/lib/apt/apt-report-mirror-failure");
211 if(!FileExists(report
))
213 Args
[i
++] = report
.c_str();
214 Args
[i
++] = UsedMirror
.c_str();
215 Args
[i
++] = DescURI().c_str();
216 Args
[i
++] = FailCode
.c_str();
218 pid_t pid
= ExecFork();
221 _error
->Error("ReportMirrorFailure Fork failed");
226 execvp(Args
[0], (char**)Args
);
227 std::cerr
<< "Could not exec " << Args
[0] << std::endl
;
230 if(!ExecWait(pid
, "report-mirror-failure"))
232 _error
->Warning("Couldn't report problem to '%s'",
233 _config
->Find("Methods::Mirror::ProblemReporting").c_str());
237 // AcqSubIndex::AcqSubIndex - Constructor /*{{{*/
238 // ---------------------------------------------------------------------
239 /* Get a sub-index file based on checksums from a 'master' file and
240 possibly query additional files */
241 pkgAcqSubIndex::pkgAcqSubIndex(pkgAcquire
*Owner
, string
const &URI
,
242 string
const &URIDesc
, string
const &ShortDesc
,
243 HashStringList
const &ExpectedHashes
)
244 : Item(Owner
, ExpectedHashes
)
246 /* XXX: Beware: Currently this class does nothing (of value) anymore ! */
247 Debug
= _config
->FindB("Debug::pkgAcquire::SubIndex",false);
249 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
250 DestFile
+= URItoFileName(URI
);
253 Desc
.Description
= URIDesc
;
255 Desc
.ShortDesc
= ShortDesc
;
260 std::clog
<< "pkgAcqSubIndex: " << Desc
.URI
<< std::endl
;
263 // AcqSubIndex::Custom600Headers - Insert custom request headers /*{{{*/
264 // ---------------------------------------------------------------------
265 /* The only header we use is the last-modified header. */
266 string
pkgAcqSubIndex::Custom600Headers() const
268 string Final
= _config
->FindDir("Dir::State::lists");
269 Final
+= URItoFileName(Desc
.URI
);
272 if (stat(Final
.c_str(),&Buf
) != 0)
273 return "\nIndex-File: true\nFail-Ignore: true\n";
274 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
277 void pkgAcqSubIndex::Failed(string Message
,pkgAcquire::MethodConfig
* /*Cnf*/)/*{{{*/
280 std::clog
<< "pkgAcqSubIndex failed: " << Desc
.URI
<< " with " << Message
<< std::endl
;
286 // No good Index is provided
289 void pkgAcqSubIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
290 pkgAcquire::MethodConfig
*Cnf
)
293 std::clog
<< "pkgAcqSubIndex::Done(): " << Desc
.URI
<< std::endl
;
295 string FileName
= LookupTag(Message
,"Filename");
296 if (FileName
.empty() == true)
299 ErrorText
= "Method gave a blank filename";
303 if (FileName
!= DestFile
)
306 Desc
.URI
= "copy:" + FileName
;
311 Item::Done(Message
, Size
, Hashes
, Cnf
);
313 string FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(Desc
.URI
);
315 /* Downloaded invalid transindex => Error (LP: #346386) (Closes: #627642) */
316 indexRecords SubIndexParser
;
317 if (FileExists(DestFile
) == true && !SubIndexParser
.Load(DestFile
)) {
319 ErrorText
= SubIndexParser
.ErrorText
;
323 // success in downloading the index
326 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
<< std::endl
;
327 Rename(DestFile
,FinalFile
);
328 chmod(FinalFile
.c_str(),0644);
329 DestFile
= FinalFile
;
331 if(ParseIndex(DestFile
) == false)
332 return Failed("", NULL
);
340 bool pkgAcqSubIndex::ParseIndex(string
const &IndexFile
) /*{{{*/
342 indexRecords SubIndexParser
;
343 if (FileExists(IndexFile
) == false || SubIndexParser
.Load(IndexFile
) == false)
345 // so something with the downloaded index
349 // AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
350 // ---------------------------------------------------------------------
351 /* Get the DiffIndex file first and see if there are patches available
352 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
353 * patches. If anything goes wrong in that process, it will fall back to
354 * the original packages file
356 pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcqMetaIndex
*MetaOwner
,
357 IndexTarget
const * const Target
,
358 HashStringList
const &ExpectedHashes
,
359 indexRecords
*MetaIndexParser
)
360 : pkgAcqBaseIndex(MetaOwner
, Target
, ExpectedHashes
,
364 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
366 RealURI
= Target
->URI
;
368 Desc
.Description
= Target
->Description
+ "/DiffIndex";
369 Desc
.ShortDesc
= Target
->ShortDesc
;
370 Desc
.URI
= Target
->URI
+ ".diff/Index";
372 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
373 DestFile
+= URItoFileName(Target
->URI
) + string(".DiffIndex");
376 std::clog
<< "pkgAcqDiffIndex: " << Desc
.URI
<< std::endl
;
378 // look for the current package file
379 CurrentPackagesFile
= _config
->FindDir("Dir::State::lists");
380 CurrentPackagesFile
+= URItoFileName(RealURI
);
382 // FIXME: this file:/ check is a hack to prevent fetching
383 // from local sources. this is really silly, and
384 // should be fixed cleanly as soon as possible
385 if(!FileExists(CurrentPackagesFile
) ||
386 Desc
.URI
.substr(0,strlen("file:/")) == "file:/")
388 // we don't have a pkg file or we don't want to queue
390 std::clog
<< "No index file, local or canceld by user" << std::endl
;
396 std::clog
<< "pkgAcqDiffIndex::pkgAcqDiffIndex(): "
397 << CurrentPackagesFile
<< std::endl
;
403 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
404 // ---------------------------------------------------------------------
405 /* The only header we use is the last-modified header. */
406 string
pkgAcqDiffIndex::Custom600Headers() const
408 string Final
= _config
->FindDir("Dir::State::lists");
409 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
412 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
415 if (stat(Final
.c_str(),&Buf
) != 0)
416 return "\nIndex-File: true";
418 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
421 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile
) /*{{{*/
424 std::clog
<< "pkgAcqDiffIndex::ParseIndexDiff() " << IndexDiffFile
429 vector
<DiffInfo
> available_patches
;
431 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
433 if (_error
->PendingError() == true)
436 if(TF
.Step(Tags
) == true)
442 string
const tmp
= Tags
.FindS("SHA1-Current");
443 std::stringstream
ss(tmp
);
444 ss
>> ServerSha1
>> size
;
445 unsigned long const ServerSize
= atol(size
.c_str());
447 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
450 string
const local_sha1
= SHA1
.Result();
452 if(local_sha1
== ServerSha1
)
454 // we have the same sha1 as the server so we are done here
456 std::clog
<< "Package file is up-to-date" << std::endl
;
457 // list cleanup needs to know that this file as well as the already
458 // present index is ours, so we create an empty diff to save it for us
459 new pkgAcqIndexDiffs(MetaOwner
, Target
, ExpectedHashes
, MetaIndexParser
,
460 ServerSha1
, available_patches
);
466 std::clog
<< "SHA1-Current: " << ServerSha1
<< " and we start at "<< fd
.Name() << " " << fd
.Size() << " " << local_sha1
<< std::endl
;
468 // check the historie and see what patches we need
469 string
const history
= Tags
.FindS("SHA1-History");
470 std::stringstream
hist(history
);
471 while(hist
>> d
.sha1
>> size
>> d
.file
)
473 // read until the first match is found
474 // from that point on, we probably need all diffs
475 if(d
.sha1
== local_sha1
)
477 else if (found
== false)
481 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
482 available_patches
.push_back(d
);
485 if (available_patches
.empty() == false)
487 // patching with too many files is rather slow compared to a fast download
488 unsigned long const fileLimit
= _config
->FindI("Acquire::PDiffs::FileLimit", 0);
489 if (fileLimit
!= 0 && fileLimit
< available_patches
.size())
492 std::clog
<< "Need " << available_patches
.size() << " diffs (Limit is " << fileLimit
493 << ") so fallback to complete download" << std::endl
;
497 // see if the patches are too big
498 found
= false; // it was true and it will be true again at the end
499 d
= *available_patches
.begin();
500 string
const firstPatch
= d
.file
;
501 unsigned long patchesSize
= 0;
502 std::stringstream
patches(Tags
.FindS("SHA1-Patches"));
503 while(patches
>> d
.sha1
>> size
>> d
.file
)
505 if (firstPatch
== d
.file
)
507 else if (found
== false)
510 patchesSize
+= atol(size
.c_str());
512 unsigned long const sizeLimit
= ServerSize
* _config
->FindI("Acquire::PDiffs::SizeLimit", 100);
513 if (sizeLimit
> 0 && (sizeLimit
/100) < patchesSize
)
516 std::clog
<< "Need " << patchesSize
<< " bytes (Limit is " << sizeLimit
/100
517 << ") so fallback to complete download" << std::endl
;
523 // we have something, queue the next diff
527 string::size_type
const last_space
= Description
.rfind(" ");
528 if(last_space
!= string::npos
)
529 Description
.erase(last_space
, Description
.size()-last_space
);
531 /* decide if we should download patches one by one or in one go:
532 The first is good if the server merges patches, but many don't so client
533 based merging can be attempt in which case the second is better.
534 "bad things" will happen if patches are merged on the server,
535 but client side merging is attempt as well */
536 bool pdiff_merge
= _config
->FindB("Acquire::PDiffs::Merge", true);
537 if (pdiff_merge
== true)
539 // reprepro adds this flag if it has merged patches on the server
540 std::string
const precedence
= Tags
.FindS("X-Patch-Precedence");
541 pdiff_merge
= (precedence
!= "merged");
544 if (pdiff_merge
== false)
546 new pkgAcqIndexDiffs(MetaOwner
, Target
, ExpectedHashes
, MetaIndexParser
,
547 ServerSha1
, available_patches
);
551 std::vector
<pkgAcqIndexMergeDiffs
*> *diffs
= new std::vector
<pkgAcqIndexMergeDiffs
*>(available_patches
.size());
552 for(size_t i
= 0; i
< available_patches
.size(); ++i
)
553 (*diffs
)[i
] = new pkgAcqIndexMergeDiffs(MetaOwner
, Target
,
556 available_patches
[i
],
567 // Nothing found, report and return false
568 // Failing here is ok, if we return false later, the full
569 // IndexFile is queued
571 std::clog
<< "Can't find a patch in the index file" << std::endl
;
575 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
* /*Cnf*/)/*{{{*/
578 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< " with " << Message
<< std::endl
579 << "Falling back to normal index file acquire" << std::endl
;
581 new pkgAcqIndex(MetaOwner
, Target
, ExpectedHashes
, MetaIndexParser
);
588 void pkgAcqDiffIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
589 pkgAcquire::MethodConfig
*Cnf
)
592 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
594 Item::Done(Message
, Size
, Hashes
, Cnf
);
597 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
599 // success in downloading the index
601 FinalFile
+= string(".IndexDiff");
603 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
605 Rename(DestFile
,FinalFile
);
606 chmod(FinalFile
.c_str(),0644);
607 DestFile
= FinalFile
;
609 if(!ParseDiffIndex(DestFile
))
610 return Failed("", NULL
);
618 // AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
619 // ---------------------------------------------------------------------
620 /* The package diff is added to the queue. one object is constructed
621 * for each diff and the index
623 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcqMetaIndex
*MetaOwner
,
624 struct IndexTarget
const * const Target
,
625 HashStringList
const &ExpectedHashes
,
626 indexRecords
*MetaIndexParser
,
628 vector
<DiffInfo
> diffs
)
629 : pkgAcqBaseIndex(MetaOwner
, Target
, ExpectedHashes
, MetaIndexParser
),
630 available_patches(diffs
), ServerSha1(ServerSha1
)
633 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
634 DestFile
+= URItoFileName(Target
->URI
);
636 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
638 RealURI
= Target
->URI
;
640 Description
= Target
->Description
;
641 Desc
.ShortDesc
= Target
->ShortDesc
;
643 if(available_patches
.empty() == true)
645 // we are done (yeah!)
651 State
= StateFetchDiff
;
656 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
* /*Cnf*/)/*{{{*/
659 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< " with " << Message
<< std::endl
660 << "Falling back to normal index file acquire" << std::endl
;
661 new pkgAcqIndex(MetaOwner
, Target
, ExpectedHashes
, MetaIndexParser
);
665 // Finish - helper that cleans the item out of the fetcher queue /*{{{*/
666 void pkgAcqIndexDiffs::Finish(bool allDone
)
668 // we restore the original name, this is required, otherwise
669 // the file will be cleaned
672 DestFile
= _config
->FindDir("Dir::State::lists");
673 DestFile
+= URItoFileName(RealURI
);
675 if(HashSums().usable() && !HashSums().VerifyFile(DestFile
))
677 RenameOnError(HashSumMismatch
);
682 // this is for the "real" finish
687 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
692 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
699 bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
702 // calc sha1 of the just patched file
703 string FinalFile
= _config
->FindDir("Dir::State::lists");
704 FinalFile
+= URItoFileName(RealURI
);
706 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
709 string local_sha1
= string(SHA1
.Result());
711 std::clog
<< "QueueNextDiff: "
712 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
714 // final file reached before all patches are applied
715 if(local_sha1
== ServerSha1
)
721 // remove all patches until the next matching patch is found
722 // this requires the Index file to be ordered
723 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
724 available_patches
.empty() == false &&
725 I
!= available_patches
.end() &&
726 I
->sha1
!= local_sha1
;
729 available_patches
.erase(I
);
732 // error checking and falling back if no patch was found
733 if(available_patches
.empty() == true)
739 // queue the right diff
740 Desc
.URI
= RealURI
+ ".diff/" + available_patches
[0].file
+ ".gz";
741 Desc
.Description
= Description
+ " " + available_patches
[0].file
+ string(".pdiff");
742 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
743 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
746 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
753 void pkgAcqIndexDiffs::Done(string Message
,unsigned long long Size
, HashStringList
const &Hashes
, /*{{{*/
754 pkgAcquire::MethodConfig
*Cnf
)
757 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
759 Item::Done(Message
, Size
, Hashes
, Cnf
);
762 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
764 // success in downloading a diff, enter ApplyDiff state
765 if(State
== StateFetchDiff
)
768 // rred excepts the patch as $FinalFile.ed
769 Rename(DestFile
,FinalFile
+".ed");
772 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
774 State
= StateApplyDiff
;
776 Desc
.URI
= "rred:" + FinalFile
;
783 // success in download/apply a diff, queue next (if needed)
784 if(State
== StateApplyDiff
)
786 // remove the just applied patch
787 available_patches
.erase(available_patches
.begin());
788 unlink((FinalFile
+ ".ed").c_str());
793 std::clog
<< "Moving patched file in place: " << std::endl
794 << DestFile
<< " -> " << FinalFile
<< std::endl
;
796 Rename(DestFile
,FinalFile
);
797 chmod(FinalFile
.c_str(),0644);
799 // see if there is more to download
800 if(available_patches
.empty() == false) {
801 new pkgAcqIndexDiffs(MetaOwner
, Target
,
802 ExpectedHashes
, MetaIndexParser
,
803 ServerSha1
, available_patches
);
810 // AcqIndexMergeDiffs::AcqIndexMergeDiffs - Constructor /*{{{*/
811 pkgAcqIndexMergeDiffs::pkgAcqIndexMergeDiffs(pkgAcqMetaIndex
*MetaOwner
,
812 struct IndexTarget
const * const Target
,
813 HashStringList
const &ExpectedHashes
,
814 indexRecords
*MetaIndexParser
,
815 DiffInfo
const &patch
,
816 std::vector
<pkgAcqIndexMergeDiffs
*> const * const allPatches
)
817 : pkgAcqBaseIndex(MetaOwner
, Target
, ExpectedHashes
, MetaIndexParser
),
818 patch(patch
), allPatches(allPatches
), State(StateFetchDiff
)
821 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
822 DestFile
+= URItoFileName(Target
->URI
);
824 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
826 RealURI
= Target
->URI
;
828 Description
= Target
->Description
;
829 Desc
.ShortDesc
= Target
->ShortDesc
;
831 Desc
.URI
= RealURI
+ ".diff/" + patch
.file
+ ".gz";
832 Desc
.Description
= Description
+ " " + patch
.file
+ string(".pdiff");
833 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
834 DestFile
+= URItoFileName(RealURI
+ ".diff/" + patch
.file
);
837 std::clog
<< "pkgAcqIndexMergeDiffs: " << Desc
.URI
<< std::endl
;
842 void pkgAcqIndexMergeDiffs::Failed(string Message
,pkgAcquire::MethodConfig
* /*Cnf*/)/*{{{*/
845 std::clog
<< "pkgAcqIndexMergeDiffs failed: " << Desc
.URI
<< " with " << Message
<< std::endl
;
850 // check if we are the first to fail, otherwise we are done here
851 State
= StateDoneDiff
;
852 for (std::vector
<pkgAcqIndexMergeDiffs
*>::const_iterator I
= allPatches
->begin();
853 I
!= allPatches
->end(); ++I
)
854 if ((*I
)->State
== StateErrorDiff
)
857 // first failure means we should fallback
858 State
= StateErrorDiff
;
859 std::clog
<< "Falling back to normal index file acquire" << std::endl
;
860 new pkgAcqIndex(MetaOwner
, Target
, ExpectedHashes
, MetaIndexParser
);
863 void pkgAcqIndexMergeDiffs::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
864 pkgAcquire::MethodConfig
*Cnf
)
867 std::clog
<< "pkgAcqIndexMergeDiffs::Done(): " << Desc
.URI
<< std::endl
;
869 Item::Done(Message
,Size
,Hashes
,Cnf
);
871 string
const FinalFile
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
873 if (State
== StateFetchDiff
)
875 // rred expects the patch as $FinalFile.ed.$patchname.gz
876 Rename(DestFile
, FinalFile
+ ".ed." + patch
.file
+ ".gz");
878 // check if this is the last completed diff
879 State
= StateDoneDiff
;
880 for (std::vector
<pkgAcqIndexMergeDiffs
*>::const_iterator I
= allPatches
->begin();
881 I
!= allPatches
->end(); ++I
)
882 if ((*I
)->State
!= StateDoneDiff
)
885 std::clog
<< "Not the last done diff in the batch: " << Desc
.URI
<< std::endl
;
889 // this is the last completed diff, so we are ready to apply now
890 State
= StateApplyDiff
;
893 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
896 Desc
.URI
= "rred:" + FinalFile
;
901 // success in download/apply all diffs, clean up
902 else if (State
== StateApplyDiff
)
904 // see if we really got the expected file
905 if(ExpectedHashes
.usable() && !ExpectedHashes
.VerifyFile(DestFile
))
907 RenameOnError(HashSumMismatch
);
911 // move the result into place
913 std::clog
<< "Moving patched file in place: " << std::endl
914 << DestFile
<< " -> " << FinalFile
<< std::endl
;
915 Rename(DestFile
, FinalFile
);
916 chmod(FinalFile
.c_str(), 0644);
918 // otherwise lists cleanup will eat the file
919 DestFile
= FinalFile
;
921 // ensure the ed's are gone regardless of list-cleanup
922 for (std::vector
<pkgAcqIndexMergeDiffs
*>::const_iterator I
= allPatches
->begin();
923 I
!= allPatches
->end(); ++I
)
925 std::string patch
= FinalFile
+ ".ed." + (*I
)->patch
.file
+ ".gz";
926 unlink(patch
.c_str());
932 std::clog
<< "allDone: " << DestFile
<< "\n" << std::endl
;
936 // AcqIndex::AcqIndex - Constructor /*{{{*/
937 // ---------------------------------------------------------------------
938 /* The package file is added to the queue and a second class is
939 instantiated to fetch the revision file */
940 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
941 string URI
,string URIDesc
,string ShortDesc
,
942 HashStringList
const &ExpectedHash
, string comprExt
)
943 : pkgAcqBaseIndex(Owner
, NULL
, ExpectedHash
, NULL
), RealURI(URI
)
945 if(comprExt
.empty() == true)
947 // autoselect the compression method
948 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
949 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
950 comprExt
.append(*t
).append(" ");
951 if (comprExt
.empty() == false)
952 comprExt
.erase(comprExt
.end()-1);
954 CompressionExtension
= comprExt
;
956 Init(URI
, URIDesc
, ShortDesc
);
959 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
, IndexTarget
const *Target
,
960 HashStringList
const &ExpectedHash
,
961 indexRecords
*MetaIndexParser
)
962 : pkgAcqBaseIndex(Owner
, Target
, ExpectedHash
, MetaIndexParser
),
965 // autoselect the compression method
966 AutoSelectCompression();
967 Init(Target
->URI
, Target
->Description
, Target
->ShortDesc
);
971 pkgAcqIndex::pkgAcqIndex(pkgAcqMetaIndex
*MetaOwner
,
972 IndexTarget
const *Target
,
973 HashStringList
const &ExpectedHash
,
974 indexRecords
*MetaIndexParser
)
975 : pkgAcqBaseIndex(MetaOwner
->GetOwner(), Target
, ExpectedHash
,
976 MetaIndexParser
), RealURI(Target
->URI
)
978 // autoselect the compression method
979 AutoSelectCompression();
980 Init(Target
->URI
, Target
->Description
, Target
->ShortDesc
);
982 TransactionID
= (unsigned long)MetaOwner
;
985 void pkgAcqIndex::AutoSelectCompression()
987 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
988 CompressionExtension
= "";
989 if (ExpectedHashes
.usable())
991 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
992 if (*t
== "uncompressed" || MetaIndexParser
->Exists(string(Target
->MetaKey
).append(".").append(*t
)) == true)
993 CompressionExtension
.append(*t
).append(" ");
997 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
998 CompressionExtension
.append(*t
).append(" ");
1000 if (CompressionExtension
.empty() == false)
1001 CompressionExtension
.erase(CompressionExtension
.end()-1);
1003 // AcqIndex::Init - defered Constructor /*{{{*/
1004 void pkgAcqIndex::Init(string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
) {
1005 Decompression
= false;
1008 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1009 DestFile
+= URItoFileName(URI
);
1011 std::string
const comprExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
1012 std::string MetaKey
;
1013 if (comprExt
== "uncompressed")
1017 MetaKey
= string(Target
->MetaKey
);
1021 Desc
.URI
= URI
+ '.' + comprExt
;
1023 MetaKey
= string(Target
->MetaKey
) + '.' + comprExt
;
1026 // load the filesize
1029 indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup(MetaKey
);
1031 FileSize
= Record
->Size
;
1033 InitByHashIfNeeded(MetaKey
);
1036 Desc
.Description
= URIDesc
;
1038 Desc
.ShortDesc
= ShortDesc
;
1043 // AcqIndex::AdjustForByHash - modify URI for by-hash support /*{{{*/
1044 // ---------------------------------------------------------------------
1046 void pkgAcqIndex::InitByHashIfNeeded(const std::string MetaKey
)
1049 // - (maybe?) add support for by-hash into the sources.list as flag
1050 // - make apt-ftparchive generate the hashes (and expire?)
1051 std::string HostKnob
= "APT::Acquire::" + ::URI(Desc
.URI
).Host
+ "::By-Hash";
1052 if(_config
->FindB("APT::Acquire::By-Hash", false) == true ||
1053 _config
->FindB(HostKnob
, false) == true ||
1054 MetaIndexParser
->GetSupportsAcquireByHash())
1056 indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup(MetaKey
);
1059 // FIXME: should we really use the best hash here? or a fixed one?
1060 const HashString
*TargetHash
= Record
->Hashes
.find("");
1061 std::string ByHash
= "/by-hash/" + TargetHash
->HashType() + "/" + TargetHash
->HashValue();
1062 size_t trailing_slash
= Desc
.URI
.find_last_of("/");
1063 Desc
.URI
= Desc
.URI
.replace(
1065 Desc
.URI
.substr(trailing_slash
+1).size()+1,
1069 "Fetching ByHash requested but can not find record for %s",
1075 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
1076 // ---------------------------------------------------------------------
1077 /* The only header we use is the last-modified header. */
1078 string
pkgAcqIndex::Custom600Headers() const
1080 string Final
= _config
->FindDir("Dir::State::lists");
1081 Final
+= URItoFileName(RealURI
);
1082 if (_config
->FindB("Acquire::GzipIndexes",false))
1085 string msg
= "\nIndex-File: true";
1086 // FIXME: this really should use "IndexTarget::IsOptional()" but that
1087 // seems to be difficult without breaking ABI
1088 if (ShortDesc().find("Translation") != 0)
1089 msg
+= "\nFail-Ignore: true";
1091 if (stat(Final
.c_str(),&Buf
) == 0)
1092 msg
+= "\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1097 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
1099 size_t const nextExt
= CompressionExtension
.find(' ');
1100 if (nextExt
!= std::string::npos
)
1102 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
1103 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
1107 // on decompression failure, remove bad versions in partial/
1108 if (Decompression
&& Erase
) {
1109 string s
= _config
->FindDir("Dir::State::lists") + "partial/";
1110 s
.append(URItoFileName(RealURI
));
1114 Item::Failed(Message
,Cnf
);
1116 /// cancel the entire transaction
1117 Owner
->AbortTransaction(TransactionID
);
1120 // AcqIndex::Done - Finished a fetch /*{{{*/
1121 // ---------------------------------------------------------------------
1122 /* This goes through a number of states.. On the initial fetch the
1123 method could possibly return an alternate filename which points
1124 to the uncompressed version of the file. If this is so the file
1125 is copied into the partial directory. In all other cases the file
1126 is decompressed with a gzip uri. */
1127 void pkgAcqIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
,
1128 pkgAcquire::MethodConfig
*Cfg
)
1130 Item::Done(Message
,Size
,Hashes
,Cfg
);
1132 if (Decompression
== true)
1134 if (ExpectedHashes
.usable() && ExpectedHashes
!= Hashes
)
1136 RenameOnError(HashSumMismatch
);
1137 printHashSumComparision(RealURI
, ExpectedHashes
, Hashes
);
1138 Failed(Message
, Cfg
);
1142 // FIXME: this can go away once we only ever download stuff that
1143 // has a valid hash and we never do GET based probing
1145 /* Always verify the index file for correctness (all indexes must
1146 * have a Package field) (LP: #346386) (Closes: #627642)
1148 FileFd
fd(DestFile
, FileFd::ReadOnly
);
1149 // Only test for correctness if the file is not empty (empty is ok)
1150 if (fd
.FileSize() > 0)
1153 pkgTagFile
tag(&fd
);
1155 // all our current indexes have a field 'Package' in each section
1156 if (_error
->PendingError() == true || tag
.Step(sec
) == false || sec
.Exists("Package") == false)
1158 RenameOnError(InvalidFormat
);
1159 Failed(Message
, Cfg
);
1164 // Done, queue for rename on transaction finished
1165 PartialFile
= DestFile
;
1167 string FinalFile
= _config
->FindDir("Dir::State::lists");
1168 FinalFile
+= URItoFileName(RealURI
);
1169 DestFile
= FinalFile
;
1171 /* We restore the original name to DestFile so that the clean operation
1173 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1174 DestFile
+= URItoFileName(RealURI
);
1176 // Remove the compressed version.
1178 unlink(DestFile
.c_str());
1186 // Handle the unzipd case
1187 string FileName
= LookupTag(Message
,"Alt-Filename");
1188 if (FileName
.empty() == false)
1190 // The files timestamp matches
1191 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
1193 Decompression
= true;
1195 DestFile
+= ".decomp";
1196 Desc
.URI
= "copy:" + FileName
;
1202 FileName
= LookupTag(Message
,"Filename");
1203 if (FileName
.empty() == true)
1206 ErrorText
= "Method gave a blank filename";
1209 std::string
const compExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
1211 // The files timestamp matches
1212 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true) {
1213 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz")
1214 // Update DestFile for .gz suffix so that the clean operation keeps it
1219 if (FileName
== DestFile
)
1226 // If we enable compressed indexes and already have gzip, keep it
1227 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz" && !Local
) {
1228 string FinalFile
= _config
->FindDir("Dir::State::lists");
1229 FinalFile
+= URItoFileName(RealURI
) + ".gz";
1230 Rename(DestFile
,FinalFile
);
1231 chmod(FinalFile
.c_str(),0644);
1233 // Update DestFile for .gz suffix so that the clean operation keeps it
1234 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1235 DestFile
+= URItoFileName(RealURI
) + ".gz";
1239 // get the binary name for your used compression type
1240 decompProg
= _config
->Find(string("Acquire::CompressionTypes::").append(compExt
),"");
1241 if(decompProg
.empty() == false);
1242 else if(compExt
== "uncompressed")
1243 decompProg
= "copy";
1245 _error
->Error("Unsupported extension: %s", compExt
.c_str());
1249 Decompression
= true;
1250 DestFile
+= ".decomp";
1251 Desc
.URI
= decompProg
+ ":" + FileName
;
1254 // FIXME: this points to a c++ string that goes out of scope
1255 Mode
= decompProg
.c_str();
1258 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
1259 // ---------------------------------------------------------------------
1260 /* The Translation file is added to the queue */
1261 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
1262 string URI
,string URIDesc
,string ShortDesc
)
1263 : pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, HashStringList(), "")
1267 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcqMetaIndex
*MetaOwner
, IndexTarget
const * const Target
,
1268 HashStringList
const &ExpectedHashes
, indexRecords
*MetaIndexParser
)
1269 : pkgAcqIndex(MetaOwner
, Target
, ExpectedHashes
, MetaIndexParser
)
1271 // load the filesize
1272 indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup(string(Target
->MetaKey
));
1274 FileSize
= Record
->Size
;
1277 // AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
1278 // ---------------------------------------------------------------------
1279 string
pkgAcqIndexTrans::Custom600Headers() const
1281 string Final
= _config
->FindDir("Dir::State::lists");
1282 Final
+= URItoFileName(RealURI
);
1285 if (stat(Final
.c_str(),&Buf
) != 0)
1286 return "\nFail-Ignore: true\nIndex-File: true";
1287 return "\nFail-Ignore: true\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1290 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
1291 // ---------------------------------------------------------------------
1293 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1295 size_t const nextExt
= CompressionExtension
.find(' ');
1296 if (nextExt
!= std::string::npos
)
1298 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
1299 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
1304 if (Cnf
->LocalOnly
== true ||
1305 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1314 Item::Failed(Message
,Cnf
);
1317 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
, /*{{{*/
1318 string URI
,string URIDesc
,string ShortDesc
,
1319 string MetaIndexURI
, string MetaIndexURIDesc
,
1320 string MetaIndexShortDesc
,
1321 const vector
<IndexTarget
*>* IndexTargets
,
1322 indexRecords
* MetaIndexParser
) :
1323 Item(Owner
, HashStringList()), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
1324 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
1325 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
1327 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1328 DestFile
+= URItoFileName(URI
);
1330 // remove any partial downloaded sig-file in partial/.
1331 // it may confuse proxies and is too small to warrant a
1332 // partial download anyway
1333 unlink(DestFile
.c_str());
1336 Desc
.Description
= URIDesc
;
1338 Desc
.ShortDesc
= ShortDesc
;
1341 string Final
= _config
->FindDir("Dir::State::lists");
1342 Final
+= URItoFileName(RealURI
);
1343 if (RealFileExists(Final
) == true)
1345 // File was already in place. It needs to be re-downloaded/verified
1346 // because Release might have changed, we do give it a different
1347 // name than DestFile because otherwise the http method will
1348 // send If-Range requests and there are too many broken servers
1349 // out there that do not understand them
1350 LastGoodSig
= DestFile
+".reverify";
1351 Rename(Final
,LastGoodSig
);
1354 // we expect the indextargets + one additional Release file
1355 ExpectedAdditionalItems
= IndexTargets
->size() + 1;
1360 pkgAcqMetaSig::~pkgAcqMetaSig() /*{{{*/
1362 // if the file was never queued undo file-changes done in the constructor
1363 if (QueueCounter
== 1 && Status
== StatIdle
&& FileSize
== 0 && Complete
== false &&
1364 LastGoodSig
.empty() == false)
1366 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1367 if (RealFileExists(Final
) == false && RealFileExists(LastGoodSig
) == true)
1368 Rename(LastGoodSig
, Final
);
1373 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
1374 // ---------------------------------------------------------------------
1375 /* The only header we use is the last-modified header. */
1376 string
pkgAcqMetaSig::Custom600Headers() const
1379 if (stat(LastGoodSig
.c_str(),&Buf
) != 0)
1380 return "\nIndex-File: true";
1382 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1385 void pkgAcqMetaSig::Done(string Message
,unsigned long long Size
, HashStringList
const &Hashes
,
1386 pkgAcquire::MethodConfig
*Cfg
)
1388 Item::Done(Message
, Size
, Hashes
, Cfg
);
1390 string FileName
= LookupTag(Message
,"Filename");
1391 if (FileName
.empty() == true)
1394 ErrorText
= "Method gave a blank filename";
1398 if (FileName
!= DestFile
)
1400 // We have to copy it into place
1402 Desc
.URI
= "copy:" + FileName
;
1409 // at this point pkgAcqMetaIndex takes over
1410 ExpectedAdditionalItems
= 0;
1412 // put the last known good file back on i-m-s hit (it will
1413 // be re-verified again)
1414 // Else do nothing, we have the new file in DestFile then
1415 if(StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1416 Rename(LastGoodSig
, DestFile
);
1419 PartialFile
= DestFile
;
1420 DestFile
= _config
->FindDir("Dir::State::lists");
1421 DestFile
+= URItoFileName(RealURI
);
1423 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
1424 pkgAcqMetaIndex
*mi
= new pkgAcqMetaIndex(
1425 Owner
, MetaIndexURI
, MetaIndexURIDesc
,
1426 MetaIndexShortDesc
, DestFile
, IndexTargets
,
1429 TransactionID
= (unsigned long)mi
;
1432 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)/*{{{*/
1434 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1436 // at this point pkgAcqMetaIndex takes over
1437 ExpectedAdditionalItems
= 0;
1439 // if we get a network error we fail gracefully
1440 if(Status
== StatTransientNetworkError
)
1442 Item::Failed(Message
,Cnf
);
1443 // move the sigfile back on transient network failures
1444 if(FileExists(LastGoodSig
))
1445 Rename(LastGoodSig
,Final
);
1447 // set the status back to , Item::Failed likes to reset it
1448 Status
= pkgAcquire::Item::StatTransientNetworkError
;
1452 // Delete any existing sigfile when the acquire failed
1453 unlink(Final
.c_str());
1455 // queue a pkgAcqMetaIndex with no sigfile
1456 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
1457 "", IndexTargets
, MetaIndexParser
);
1459 if (Cnf
->LocalOnly
== true ||
1460 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1469 Item::Failed(Message
,Cnf
);
1472 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
, /*{{{*/
1473 string URI
,string URIDesc
,string ShortDesc
,
1475 const vector
<IndexTarget
*>* IndexTargets
,
1476 indexRecords
* MetaIndexParser
) :
1477 Item(Owner
, HashStringList()), RealURI(URI
), SigFile(SigFile
), IndexTargets(IndexTargets
),
1478 MetaIndexParser(MetaIndexParser
), AuthPass(false), IMSHit(false)
1480 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1481 DestFile
+= URItoFileName(URI
);
1483 TransactionID
= (unsigned long)this;
1486 Desc
.Description
= URIDesc
;
1488 Desc
.ShortDesc
= ShortDesc
;
1491 // we expect more item
1492 ExpectedAdditionalItems
= IndexTargets
->size();
1497 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
1498 // ---------------------------------------------------------------------
1499 /* The only header we use is the last-modified header. */
1500 string
pkgAcqMetaIndex::Custom600Headers() const
1502 string Final
= _config
->FindDir("Dir::State::lists");
1503 Final
+= URItoFileName(RealURI
);
1506 if (stat(Final
.c_str(),&Buf
) != 0)
1507 return "\nIndex-File: true";
1509 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1512 void pkgAcqMetaIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
1513 pkgAcquire::MethodConfig
*Cfg
)
1515 Item::Done(Message
,Size
,Hashes
,Cfg
);
1517 // MetaIndexes are done in two passes: one to download the
1518 // metaindex with an appropriate method, and a second to verify it
1519 // with the gpgv method
1521 if (AuthPass
== true)
1525 // all cool, move Release file into place
1530 RetrievalDone(Message
);
1532 // Still more retrieving to do
1537 // There was no signature file, so we are finished. Download
1538 // the indexes and do only hashsum verification if possible
1539 MetaIndexParser
->Load(DestFile
);
1540 QueueIndexes(false);
1544 // FIXME: move this into pkgAcqMetaClearSig::Done on the next
1547 // if we expect a ClearTextSignature (InRelase), ensure that
1548 // this is what we get and if not fail to queue a
1549 // Release/Release.gpg, see #346386
1550 if (SigFile
== DestFile
&& !StartsWithGPGClearTextSignature(DestFile
))
1552 Failed(Message
, Cfg
);
1556 // There was a signature file, so pass it to gpgv for
1558 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1559 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
1560 << SigFile
<< "," << DestFile
<< ")\n";
1562 Desc
.URI
= "gpgv:" + SigFile
;
1569 if (Complete
== true)
1571 string FinalFile
= _config
->FindDir("Dir::State::lists");
1572 FinalFile
+= URItoFileName(RealURI
);
1573 if (SigFile
== DestFile
)
1574 SigFile
= FinalFile
;
1575 PartialFile
= DestFile
;
1576 DestFile
= FinalFile
;
1580 void pkgAcqMetaIndex::RetrievalDone(string Message
) /*{{{*/
1582 // We have just finished downloading a Release file (it is not
1585 string FileName
= LookupTag(Message
,"Filename");
1586 if (FileName
.empty() == true)
1589 ErrorText
= "Method gave a blank filename";
1593 if (FileName
!= DestFile
)
1596 Desc
.URI
= "copy:" + FileName
;
1601 // make sure to verify against the right file on I-M-S hit
1602 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
1605 string FinalFile
= _config
->FindDir("Dir::State::lists");
1606 FinalFile
+= URItoFileName(RealURI
);
1607 if (SigFile
== DestFile
)
1609 SigFile
= FinalFile
;
1610 // constructor of pkgAcqMetaClearSig moved it out of the way,
1611 // now move it back in on IMS hit for the 'old' file
1612 string
const OldClearSig
= DestFile
+ ".reverify";
1613 if (RealFileExists(OldClearSig
) == true)
1614 Rename(OldClearSig
, FinalFile
);
1616 DestFile
= FinalFile
;
1621 void pkgAcqMetaIndex::AuthDone(string Message
) /*{{{*/
1623 // At this point, the gpgv method has succeeded, so there is a
1624 // valid signature from a key in the trusted keyring. We
1625 // perform additional verification of its contents, and use them
1626 // to verify the indexes we are about to download
1628 if (!MetaIndexParser
->Load(DestFile
))
1630 Status
= StatAuthError
;
1631 ErrorText
= MetaIndexParser
->ErrorText
;
1635 if (!VerifyVendor(Message
))
1640 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1641 std::cerr
<< "Signature verification succeeded: "
1642 << DestFile
<< std::endl
;
1644 // Download further indexes with verification
1648 // is it a clearsigned MetaIndex file?
1649 if (DestFile
== SigFile
)
1652 // Done, move signature file into position
1653 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1654 URItoFileName(RealURI
) + ".gpg";
1655 Rename(SigFile
,VerifiedSigFile
);
1656 chmod(VerifiedSigFile
.c_str(),0644);
1660 void pkgAcqMetaIndex::QueueIndexes(bool verify
) /*{{{*/
1663 /* Reject invalid, existing Release files (LP: #346386) (Closes: #627642)
1664 * FIXME: Disabled; it breaks unsigned repositories without hashes */
1665 if (!verify
&& FileExists(DestFile
) && !MetaIndexParser
->Load(DestFile
))
1668 ErrorText
= MetaIndexParser
->ErrorText
;
1672 bool transInRelease
= false;
1674 std::vector
<std::string
> const keys
= MetaIndexParser
->MetaKeys();
1675 for (std::vector
<std::string
>::const_iterator k
= keys
.begin(); k
!= keys
.end(); ++k
)
1676 // FIXME: Feels wrong to check for hardcoded string here, but what should we do else…
1677 if (k
->find("Translation-") != std::string::npos
)
1679 transInRelease
= true;
1684 // at this point the real Items are loaded in the fetcher
1685 ExpectedAdditionalItems
= 0;
1686 for (vector
<IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
1687 Target
!= IndexTargets
->end();
1690 HashStringList ExpectedIndexHashes
;
1691 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1692 bool compressedAvailable
= false;
1695 if ((*Target
)->IsOptional() == true)
1697 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
1698 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
1699 if (MetaIndexParser
->Exists((*Target
)->MetaKey
+ "." + *t
) == true)
1701 compressedAvailable
= true;
1705 else if (verify
== true)
1707 Status
= StatAuthError
;
1708 strprintf(ErrorText
, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), (*Target
)->MetaKey
.c_str());
1714 ExpectedIndexHashes
= Record
->Hashes
;
1715 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1717 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
1718 << "Expected Hash:" << std::endl
;
1719 for (HashStringList::const_iterator hs
= ExpectedIndexHashes
.begin(); hs
!= ExpectedIndexHashes
.end(); ++hs
)
1720 std::cerr
<< "\t- " << hs
->toStr() << std::endl
;
1721 std::cerr
<< "For: " << Record
->MetaKeyFilename
<< std::endl
;
1723 if (verify
== true && ExpectedIndexHashes
.empty() == true && (*Target
)->IsOptional() == false)
1725 Status
= StatAuthError
;
1726 strprintf(ErrorText
, _("Unable to find hash sum for '%s' in Release file"), (*Target
)->MetaKey
.c_str());
1731 if ((*Target
)->IsOptional() == true)
1733 if ((*Target
)->IsSubIndex() == true)
1734 new pkgAcqSubIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1735 (*Target
)->ShortDesc
, ExpectedIndexHashes
);
1736 else if (transInRelease
== false || Record
!= NULL
|| compressedAvailable
== true)
1738 if (_config
->FindB("Acquire::PDiffs",true) == true && transInRelease
== true &&
1739 MetaIndexParser
->Exists((*Target
)->MetaKey
+ ".diff/Index") == true)
1740 new pkgAcqDiffIndex(this, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1742 new pkgAcqIndexTrans(this, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1747 /* Queue Packages file (either diff or full packages files, depending
1748 on the users option) - we also check if the PDiff Index file is listed
1749 in the Meta-Index file. Ideal would be if pkgAcqDiffIndex would test this
1750 instead, but passing the required info to it is to much hassle */
1751 if(_config
->FindB("Acquire::PDiffs",true) == true && (verify
== false ||
1752 MetaIndexParser
->Exists((*Target
)->MetaKey
+ ".diff/Index") == true))
1753 new pkgAcqDiffIndex(this, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1755 new pkgAcqIndex(this, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1759 bool pkgAcqMetaIndex::VerifyVendor(string Message
) /*{{{*/
1761 string::size_type pos
;
1763 // check for missing sigs (that where not fatal because otherwise we had
1766 string msg
= _("There is no public key available for the "
1767 "following key IDs:\n");
1768 pos
= Message
.find("NO_PUBKEY ");
1769 if (pos
!= std::string::npos
)
1771 string::size_type start
= pos
+strlen("NO_PUBKEY ");
1772 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
1773 missingkeys
+= (Fingerprint
);
1775 if(!missingkeys
.empty())
1776 _error
->Warning("%s", (msg
+ missingkeys
).c_str());
1778 string Transformed
= MetaIndexParser
->GetExpectedDist();
1780 if (Transformed
== "../project/experimental")
1782 Transformed
= "experimental";
1785 pos
= Transformed
.rfind('/');
1786 if (pos
!= string::npos
)
1788 Transformed
= Transformed
.substr(0, pos
);
1791 if (Transformed
== ".")
1796 if (_config
->FindB("Acquire::Check-Valid-Until", true) == true &&
1797 MetaIndexParser
->GetValidUntil() > 0) {
1798 time_t const invalid_since
= time(NULL
) - MetaIndexParser
->GetValidUntil();
1799 if (invalid_since
> 0)
1800 // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
1801 // the time since then the file is invalid - formated in the same way as in
1802 // the download progress display (e.g. 7d 3h 42min 1s)
1803 return _error
->Error(
1804 _("Release file for %s is expired (invalid since %s). "
1805 "Updates for this repository will not be applied."),
1806 RealURI
.c_str(), TimeToStr(invalid_since
).c_str());
1809 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1811 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1812 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1813 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1816 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1818 // This might become fatal one day
1819 // Status = StatAuthError;
1820 // ErrorText = "Conflicting distribution; expected "
1821 // + MetaIndexParser->GetExpectedDist() + " but got "
1822 // + MetaIndexParser->GetDist();
1824 if (!Transformed
.empty())
1826 _error
->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
1827 Desc
.Description
.c_str(),
1828 Transformed
.c_str(),
1829 MetaIndexParser
->GetDist().c_str());
1836 // pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
1837 // ---------------------------------------------------------------------
1839 void pkgAcqMetaIndex::Failed(string
/*Message*/,
1840 pkgAcquire::MethodConfig
* /*Cnf*/)
1843 if (AuthPass
== true)
1845 // gpgv method failed, if we have a good signature
1846 string LastGoodSigFile
= _config
->FindDir("Dir::State::lists").append("partial/").append(URItoFileName(RealURI
));
1847 if (DestFile
!= SigFile
)
1848 LastGoodSigFile
.append(".gpg");
1849 LastGoodSigFile
.append(".reverify");
1851 if(FileExists(LastGoodSigFile
))
1853 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1854 if (DestFile
!= SigFile
)
1855 VerifiedSigFile
.append(".gpg");
1856 Rename(LastGoodSigFile
, VerifiedSigFile
);
1857 Status
= StatTransientNetworkError
;
1858 _error
->Warning(_("An error occurred during the signature "
1859 "verification. The repository is not updated "
1860 "and the previous index files will be used. "
1861 "GPG error: %s: %s\n"),
1862 Desc
.Description
.c_str(),
1863 LookupTag(Message
,"Message").c_str());
1864 RunScripts("APT::Update::Auth-Failure");
1866 } else if (LookupTag(Message
,"Message").find("NODATA") != string::npos
) {
1867 /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */
1868 _error
->Error(_("GPG error: %s: %s"),
1869 Desc
.Description
.c_str(),
1870 LookupTag(Message
,"Message").c_str());
1873 _error
->Warning(_("GPG error: %s: %s"),
1874 Desc
.Description
.c_str(),
1875 LookupTag(Message
,"Message").c_str());
1877 // gpgv method failed
1878 ReportMirrorFailure("GPGFailure");
1881 /* Always move the meta index, even if gpgv failed. This ensures
1882 * that PackageFile objects are correctly filled in */
1883 if (FileExists(DestFile
)) {
1884 string FinalFile
= _config
->FindDir("Dir::State::lists");
1885 FinalFile
+= URItoFileName(RealURI
);
1886 /* InRelease files become Release files, otherwise
1887 * they would be considered as trusted later on */
1888 if (SigFile
== DestFile
) {
1889 RealURI
= RealURI
.replace(RealURI
.rfind("InRelease"), 9,
1891 FinalFile
= FinalFile
.replace(FinalFile
.rfind("InRelease"), 9,
1893 SigFile
= FinalFile
;
1895 Rename(DestFile
,FinalFile
);
1896 chmod(FinalFile
.c_str(),0644);
1898 DestFile
= FinalFile
;
1901 // No Release file was present, or verification failed, so fall
1902 // back to queueing Packages files without verification
1903 QueueIndexes(false);
1907 void pkgAcqMetaIndex::Finished()
1909 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
1910 std::clog
<< "Finished: " << DestFile
<<std::endl
;
1911 Owner
->CommitTransaction((unsigned long)this);
1915 pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire
*Owner
, /*{{{*/
1916 string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
,
1917 string
const &MetaIndexURI
, string
const &MetaIndexURIDesc
, string
const &MetaIndexShortDesc
,
1918 string
const &MetaSigURI
, string
const &MetaSigURIDesc
, string
const &MetaSigShortDesc
,
1919 const vector
<IndexTarget
*>* IndexTargets
,
1920 indexRecords
* MetaIndexParser
) :
1921 pkgAcqMetaIndex(Owner
, URI
, URIDesc
, ShortDesc
, "", IndexTargets
, MetaIndexParser
),
1922 MetaIndexURI(MetaIndexURI
), MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
1923 MetaSigURI(MetaSigURI
), MetaSigURIDesc(MetaSigURIDesc
), MetaSigShortDesc(MetaSigShortDesc
)
1927 // index targets + (worst case:) Release/Release.gpg
1928 ExpectedAdditionalItems
= IndexTargets
->size() + 2;
1931 // keep the old InRelease around in case of transistent network errors
1932 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1933 if (RealFileExists(Final
) == true)
1935 string
const LastGoodSig
= DestFile
+ ".reverify";
1936 Rename(Final
,LastGoodSig
);
1940 pkgAcqMetaClearSig::~pkgAcqMetaClearSig() /*{{{*/
1942 // if the file was never queued undo file-changes done in the constructor
1943 if (QueueCounter
== 1 && Status
== StatIdle
&& FileSize
== 0 && Complete
== false)
1945 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1946 string
const LastGoodSig
= DestFile
+ ".reverify";
1947 if (RealFileExists(Final
) == false && RealFileExists(LastGoodSig
) == true)
1948 Rename(LastGoodSig
, Final
);
1952 // pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
1953 // ---------------------------------------------------------------------
1954 // FIXME: this can go away once the InRelease file is used widely
1955 string
pkgAcqMetaClearSig::Custom600Headers() const
1957 string Final
= _config
->FindDir("Dir::State::lists");
1958 Final
+= URItoFileName(RealURI
);
1961 if (stat(Final
.c_str(),&Buf
) != 0)
1963 Final
= DestFile
+ ".reverify";
1964 if (stat(Final
.c_str(),&Buf
) != 0)
1965 return "\nIndex-File: true\nFail-Ignore: true\n";
1968 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1971 void pkgAcqMetaClearSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
1973 // we failed, we will not get additional items from this method
1974 ExpectedAdditionalItems
= 0;
1976 if (AuthPass
== false)
1978 // Remove the 'old' InRelease file if we try Release.gpg now as otherwise
1979 // the file will stay around and gives a false-auth impression (CVE-2012-0214)
1980 string FinalFile
= _config
->FindDir("Dir::State::lists");
1981 FinalFile
.append(URItoFileName(RealURI
));
1982 if (FileExists(FinalFile
))
1983 unlink(FinalFile
.c_str());
1985 new pkgAcqMetaSig(Owner
,
1986 MetaSigURI
, MetaSigURIDesc
, MetaSigShortDesc
,
1987 MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
1988 IndexTargets
, MetaIndexParser
);
1989 if (Cnf
->LocalOnly
== true ||
1990 StringToBool(LookupTag(Message
, "Transient-Failure"), false) == false)
1994 pkgAcqMetaIndex::Failed(Message
, Cnf
);
1997 // AcqArchive::AcqArchive - Constructor /*{{{*/
1998 // ---------------------------------------------------------------------
1999 /* This just sets up the initial fetch environment and queues the first
2001 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
2002 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
2003 string
&StoreFilename
) :
2004 Item(Owner
, HashStringList()), Version(Version
), Sources(Sources
), Recs(Recs
),
2005 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
2008 Retries
= _config
->FindI("Acquire::Retries",0);
2010 if (Version
.Arch() == 0)
2012 _error
->Error(_("I wasn't able to locate a file for the %s package. "
2013 "This might mean you need to manually fix this package. "
2014 "(due to missing arch)"),
2015 Version
.ParentPkg().FullName().c_str());
2019 /* We need to find a filename to determine the extension. We make the
2020 assumption here that all the available sources for this version share
2021 the same extension.. */
2022 // Skip not source sources, they do not have file fields.
2023 for (; Vf
.end() == false; ++Vf
)
2025 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
2030 // Does not really matter here.. we are going to fail out below
2031 if (Vf
.end() != true)
2033 // If this fails to get a file name we will bomb out below.
2034 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
2035 if (_error
->PendingError() == true)
2038 // Generate the final file name as: package_version_arch.foo
2039 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
2040 QuoteString(Version
.VerStr(),"_:") + '_' +
2041 QuoteString(Version
.Arch(),"_:.") +
2042 "." + flExtension(Parse
.FileName());
2045 // check if we have one trusted source for the package. if so, switch
2046 // to "TrustedOnly" mode - but only if not in AllowUnauthenticated mode
2047 bool const allowUnauth
= _config
->FindB("APT::Get::AllowUnauthenticated", false);
2048 bool const debugAuth
= _config
->FindB("Debug::pkgAcquire::Auth", false);
2049 bool seenUntrusted
= false;
2050 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; ++i
)
2052 pkgIndexFile
*Index
;
2053 if (Sources
->FindIndex(i
.File(),Index
) == false)
2056 if (debugAuth
== true)
2057 std::cerr
<< "Checking index: " << Index
->Describe()
2058 << "(Trusted=" << Index
->IsTrusted() << ")" << std::endl
;
2060 if (Index
->IsTrusted() == true)
2063 if (allowUnauth
== false)
2067 seenUntrusted
= true;
2070 // "allow-unauthenticated" restores apts old fetching behaviour
2071 // that means that e.g. unauthenticated file:// uris are higher
2072 // priority than authenticated http:// uris
2073 if (allowUnauth
== true && seenUntrusted
== true)
2077 if (QueueNext() == false && _error
->PendingError() == false)
2078 _error
->Error(_("Can't find a source to download version '%s' of '%s'"),
2079 Version
.VerStr(), Version
.ParentPkg().FullName(false).c_str());
2082 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
2083 // ---------------------------------------------------------------------
2084 /* This queues the next available file version for download. It checks if
2085 the archive is already available in the cache and stashs the MD5 for
2087 bool pkgAcqArchive::QueueNext()
2089 for (; Vf
.end() == false; ++Vf
)
2091 // Ignore not source sources
2092 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
2095 // Try to cross match against the source list
2096 pkgIndexFile
*Index
;
2097 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
2100 // only try to get a trusted package from another source if that source
2102 if(Trusted
&& !Index
->IsTrusted())
2105 // Grab the text package record
2106 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
2107 if (_error
->PendingError() == true)
2110 string PkgFile
= Parse
.FileName();
2111 ExpectedHashes
= Parse
.Hashes();
2113 if (PkgFile
.empty() == true)
2114 return _error
->Error(_("The package index files are corrupted. No Filename: "
2115 "field for package %s."),
2116 Version
.ParentPkg().Name());
2118 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
2119 Desc
.Description
= Index
->ArchiveInfo(Version
);
2121 Desc
.ShortDesc
= Version
.ParentPkg().FullName(true);
2123 // See if we already have the file. (Legacy filenames)
2124 FileSize
= Version
->Size
;
2125 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
2127 if (stat(FinalFile
.c_str(),&Buf
) == 0)
2129 // Make sure the size matches
2130 if ((unsigned long long)Buf
.st_size
== Version
->Size
)
2135 StoreFilename
= DestFile
= FinalFile
;
2139 /* Hmm, we have a file and its size does not match, this means it is
2140 an old style mismatched arch */
2141 unlink(FinalFile
.c_str());
2144 // Check it again using the new style output filenames
2145 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
2146 if (stat(FinalFile
.c_str(),&Buf
) == 0)
2148 // Make sure the size matches
2149 if ((unsigned long long)Buf
.st_size
== Version
->Size
)
2154 StoreFilename
= DestFile
= FinalFile
;
2158 /* Hmm, we have a file and its size does not match, this shouldn't
2160 unlink(FinalFile
.c_str());
2163 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
2165 // Check the destination file
2166 if (stat(DestFile
.c_str(),&Buf
) == 0)
2168 // Hmm, the partial file is too big, erase it
2169 if ((unsigned long long)Buf
.st_size
> Version
->Size
)
2170 unlink(DestFile
.c_str());
2172 PartialSize
= Buf
.st_size
;
2175 // Disables download of archives - useful if no real installation follows,
2176 // e.g. if we are just interested in proposed installation order
2177 if (_config
->FindB("Debug::pkgAcqArchive::NoQueue", false) == true)
2182 StoreFilename
= DestFile
= FinalFile
;
2196 // AcqArchive::Done - Finished fetching /*{{{*/
2197 // ---------------------------------------------------------------------
2199 void pkgAcqArchive::Done(string Message
,unsigned long long Size
, HashStringList
const &CalcHashes
,
2200 pkgAcquire::MethodConfig
*Cfg
)
2202 Item::Done(Message
, Size
, CalcHashes
, Cfg
);
2205 if (Size
!= Version
->Size
)
2207 RenameOnError(SizeMismatch
);
2211 // FIXME: could this empty() check impose *any* sort of security issue?
2212 if(ExpectedHashes
.usable() && ExpectedHashes
!= CalcHashes
)
2214 RenameOnError(HashSumMismatch
);
2215 printHashSumComparision(DestFile
, ExpectedHashes
, CalcHashes
);
2219 // Grab the output filename
2220 string FileName
= LookupTag(Message
,"Filename");
2221 if (FileName
.empty() == true)
2224 ErrorText
= "Method gave a blank filename";
2230 // Reference filename
2231 if (FileName
!= DestFile
)
2233 StoreFilename
= DestFile
= FileName
;
2238 // Done, move it into position
2239 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
2240 FinalFile
+= flNotDir(StoreFilename
);
2241 Rename(DestFile
,FinalFile
);
2243 StoreFilename
= DestFile
= FinalFile
;
2247 // AcqArchive::Failed - Failure handler /*{{{*/
2248 // ---------------------------------------------------------------------
2249 /* Here we try other sources */
2250 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
2252 ErrorText
= LookupTag(Message
,"Message");
2254 /* We don't really want to retry on failed media swaps, this prevents
2255 that. An interesting observation is that permanent failures are not
2257 if (Cnf
->Removable
== true &&
2258 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2260 // Vf = Version.FileList();
2261 while (Vf
.end() == false) ++Vf
;
2262 StoreFilename
= string();
2263 Item::Failed(Message
,Cnf
);
2267 if (QueueNext() == false)
2269 // This is the retry counter
2271 Cnf
->LocalOnly
== false &&
2272 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2275 Vf
= Version
.FileList();
2276 if (QueueNext() == true)
2280 StoreFilename
= string();
2281 Item::Failed(Message
,Cnf
);
2285 // AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
2286 // ---------------------------------------------------------------------
2287 APT_PURE
bool pkgAcqArchive::IsTrusted() const
2292 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
2293 // ---------------------------------------------------------------------
2295 void pkgAcqArchive::Finished()
2297 if (Status
== pkgAcquire::Item::StatDone
&&
2300 StoreFilename
= string();
2303 // AcqFile::pkgAcqFile - Constructor /*{{{*/
2304 // ---------------------------------------------------------------------
2305 /* The file is added to the queue */
2306 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
, HashStringList
const &Hashes
,
2307 unsigned long long Size
,string Dsc
,string ShortDesc
,
2308 const string
&DestDir
, const string
&DestFilename
,
2310 Item(Owner
, Hashes
), IsIndexFile(IsIndexFile
)
2312 Retries
= _config
->FindI("Acquire::Retries",0);
2314 if(!DestFilename
.empty())
2315 DestFile
= DestFilename
;
2316 else if(!DestDir
.empty())
2317 DestFile
= DestDir
+ "/" + flNotDir(URI
);
2319 DestFile
= flNotDir(URI
);
2323 Desc
.Description
= Dsc
;
2326 // Set the short description to the archive component
2327 Desc
.ShortDesc
= ShortDesc
;
2329 // Get the transfer sizes
2332 if (stat(DestFile
.c_str(),&Buf
) == 0)
2334 // Hmm, the partial file is too big, erase it
2335 if ((Size
> 0) && (unsigned long long)Buf
.st_size
> Size
)
2336 unlink(DestFile
.c_str());
2338 PartialSize
= Buf
.st_size
;
2344 // AcqFile::Done - Item downloaded OK /*{{{*/
2345 // ---------------------------------------------------------------------
2347 void pkgAcqFile::Done(string Message
,unsigned long long Size
,HashStringList
const &CalcHashes
,
2348 pkgAcquire::MethodConfig
*Cnf
)
2350 Item::Done(Message
,Size
,CalcHashes
,Cnf
);
2353 if(ExpectedHashes
.usable() && ExpectedHashes
!= CalcHashes
)
2355 RenameOnError(HashSumMismatch
);
2356 printHashSumComparision(DestFile
, ExpectedHashes
, CalcHashes
);
2360 string FileName
= LookupTag(Message
,"Filename");
2361 if (FileName
.empty() == true)
2364 ErrorText
= "Method gave a blank filename";
2370 // The files timestamp matches
2371 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
2374 // We have to copy it into place
2375 if (FileName
!= DestFile
)
2378 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
2379 Cnf
->Removable
== true)
2381 Desc
.URI
= "copy:" + FileName
;
2386 // Erase the file if it is a symlink so we can overwrite it
2388 if (lstat(DestFile
.c_str(),&St
) == 0)
2390 if (S_ISLNK(St
.st_mode
) != 0)
2391 unlink(DestFile
.c_str());
2395 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
2397 ErrorText
= "Link to " + DestFile
+ " failure ";
2404 // AcqFile::Failed - Failure handler /*{{{*/
2405 // ---------------------------------------------------------------------
2406 /* Here we try other sources */
2407 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
2409 ErrorText
= LookupTag(Message
,"Message");
2411 // This is the retry counter
2413 Cnf
->LocalOnly
== false &&
2414 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2421 Item::Failed(Message
,Cnf
);
2424 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
2425 // ---------------------------------------------------------------------
2426 /* The only header we use is the last-modified header. */
2427 string
pkgAcqFile::Custom600Headers() const
2430 return "\nIndex-File: true";