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
)
679 // we restore the original name, this is required, otherwise
680 // the file will be cleaned
683 DestFile
= _config
->FindDir("Dir::State::lists");
684 DestFile
+= URItoFileName(RealURI
);
686 if(HashSums().usable() && !HashSums().VerifyFile(DestFile
))
688 RenameOnError(HashSumMismatch
);
693 // this is for the "real" finish
698 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
703 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
710 bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
713 // calc sha1 of the just patched file
714 string FinalFile
= _config
->FindDir("Dir::State::lists");
715 FinalFile
+= URItoFileName(RealURI
);
717 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
720 string local_sha1
= string(SHA1
.Result());
722 std::clog
<< "QueueNextDiff: "
723 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
725 // final file reached before all patches are applied
726 if(local_sha1
== ServerSha1
)
732 // remove all patches until the next matching patch is found
733 // this requires the Index file to be ordered
734 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
735 available_patches
.empty() == false &&
736 I
!= available_patches
.end() &&
737 I
->sha1
!= local_sha1
;
740 available_patches
.erase(I
);
743 // error checking and falling back if no patch was found
744 if(available_patches
.empty() == true)
750 // queue the right diff
751 Desc
.URI
= RealURI
+ ".diff/" + available_patches
[0].file
+ ".gz";
752 Desc
.Description
= Description
+ " " + available_patches
[0].file
+ string(".pdiff");
753 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
754 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
757 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
764 void pkgAcqIndexDiffs::Done(string Message
,unsigned long long Size
, HashStringList
const &Hashes
, /*{{{*/
765 pkgAcquire::MethodConfig
*Cnf
)
768 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
770 Item::Done(Message
, Size
, Hashes
, Cnf
);
773 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
775 // success in downloading a diff, enter ApplyDiff state
776 if(State
== StateFetchDiff
)
779 // rred excepts the patch as $FinalFile.ed
780 Rename(DestFile
,FinalFile
+".ed");
783 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
785 State
= StateApplyDiff
;
787 Desc
.URI
= "rred:" + FinalFile
;
794 // success in download/apply a diff, queue next (if needed)
795 if(State
== StateApplyDiff
)
797 // remove the just applied patch
798 available_patches
.erase(available_patches
.begin());
799 unlink((FinalFile
+ ".ed").c_str());
804 std::clog
<< "Moving patched file in place: " << std::endl
805 << DestFile
<< " -> " << FinalFile
<< std::endl
;
807 Rename(DestFile
,FinalFile
);
808 chmod(FinalFile
.c_str(),0644);
810 // see if there is more to download
811 if(available_patches
.empty() == false) {
812 new pkgAcqIndexDiffs(Owner
, TransactionID
, Target
,
813 ExpectedHashes
, MetaIndexParser
,
814 ServerSha1
, available_patches
);
821 // AcqIndexMergeDiffs::AcqIndexMergeDiffs - Constructor /*{{{*/
822 pkgAcqIndexMergeDiffs::pkgAcqIndexMergeDiffs(pkgAcquire
*Owner
,
823 unsigned long TransactionID
,
824 struct IndexTarget
const * const Target
,
825 HashStringList
const &ExpectedHashes
,
826 indexRecords
*MetaIndexParser
,
827 DiffInfo
const &patch
,
828 std::vector
<pkgAcqIndexMergeDiffs
*> const * const allPatches
)
829 : pkgAcqBaseIndex(Owner
, TransactionID
, Target
, ExpectedHashes
, MetaIndexParser
),
830 patch(patch
), allPatches(allPatches
), State(StateFetchDiff
)
833 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
834 DestFile
+= URItoFileName(Target
->URI
);
836 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
838 RealURI
= Target
->URI
;
840 Description
= Target
->Description
;
841 Desc
.ShortDesc
= Target
->ShortDesc
;
843 Desc
.URI
= RealURI
+ ".diff/" + patch
.file
+ ".gz";
844 Desc
.Description
= Description
+ " " + patch
.file
+ string(".pdiff");
845 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
846 DestFile
+= URItoFileName(RealURI
+ ".diff/" + patch
.file
);
849 std::clog
<< "pkgAcqIndexMergeDiffs: " << Desc
.URI
<< std::endl
;
854 void pkgAcqIndexMergeDiffs::Failed(string Message
,pkgAcquire::MethodConfig
* /*Cnf*/)/*{{{*/
857 std::clog
<< "pkgAcqIndexMergeDiffs failed: " << Desc
.URI
<< " with " << Message
<< std::endl
;
862 // check if we are the first to fail, otherwise we are done here
863 State
= StateDoneDiff
;
864 for (std::vector
<pkgAcqIndexMergeDiffs
*>::const_iterator I
= allPatches
->begin();
865 I
!= allPatches
->end(); ++I
)
866 if ((*I
)->State
== StateErrorDiff
)
869 // first failure means we should fallback
870 State
= StateErrorDiff
;
871 std::clog
<< "Falling back to normal index file acquire" << std::endl
;
872 new pkgAcqIndex(Owner
, TransactionID
, Target
, ExpectedHashes
, MetaIndexParser
);
875 void pkgAcqIndexMergeDiffs::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
876 pkgAcquire::MethodConfig
*Cnf
)
879 std::clog
<< "pkgAcqIndexMergeDiffs::Done(): " << Desc
.URI
<< std::endl
;
881 Item::Done(Message
,Size
,Hashes
,Cnf
);
883 string
const FinalFile
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
885 if (State
== StateFetchDiff
)
887 // rred expects the patch as $FinalFile.ed.$patchname.gz
888 Rename(DestFile
, FinalFile
+ ".ed." + patch
.file
+ ".gz");
890 // check if this is the last completed diff
891 State
= StateDoneDiff
;
892 for (std::vector
<pkgAcqIndexMergeDiffs
*>::const_iterator I
= allPatches
->begin();
893 I
!= allPatches
->end(); ++I
)
894 if ((*I
)->State
!= StateDoneDiff
)
897 std::clog
<< "Not the last done diff in the batch: " << Desc
.URI
<< std::endl
;
901 // this is the last completed diff, so we are ready to apply now
902 State
= StateApplyDiff
;
905 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
908 Desc
.URI
= "rred:" + FinalFile
;
913 // success in download/apply all diffs, clean up
914 else if (State
== StateApplyDiff
)
916 // see if we really got the expected file
917 if(ExpectedHashes
.usable() && !ExpectedHashes
.VerifyFile(DestFile
))
919 RenameOnError(HashSumMismatch
);
923 // move the result into place
925 std::clog
<< "Moving patched file in place: " << std::endl
926 << DestFile
<< " -> " << FinalFile
<< std::endl
;
927 Rename(DestFile
, FinalFile
);
928 chmod(FinalFile
.c_str(), 0644);
930 // otherwise lists cleanup will eat the file
931 DestFile
= FinalFile
;
933 // ensure the ed's are gone regardless of list-cleanup
934 for (std::vector
<pkgAcqIndexMergeDiffs
*>::const_iterator I
= allPatches
->begin();
935 I
!= allPatches
->end(); ++I
)
937 std::string patch
= FinalFile
+ ".ed." + (*I
)->patch
.file
+ ".gz";
938 unlink(patch
.c_str());
944 std::clog
<< "allDone: " << DestFile
<< "\n" << std::endl
;
948 // AcqIndex::AcqIndex - Constructor /*{{{*/
949 // ---------------------------------------------------------------------
950 /* The package file is added to the queue and a second class is
951 instantiated to fetch the revision file */
952 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
953 string URI
,string URIDesc
,string ShortDesc
,
954 HashStringList
const &ExpectedHash
, string comprExt
)
955 : pkgAcqBaseIndex(Owner
, 0, NULL
, ExpectedHash
, NULL
), RealURI(URI
)
957 AutoSelectCompression();
958 Init(URI
, URIDesc
, ShortDesc
);
960 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
961 std::clog
<< "New pkgIndex with TransactionID "
962 << TransactionID
<< std::endl
;
965 // AcqIndex::AcqIndex - Constructor /*{{{*/
966 // ---------------------------------------------------------------------
967 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
968 unsigned long TransactionID
,
969 IndexTarget
const *Target
,
970 HashStringList
const &ExpectedHash
,
971 indexRecords
*MetaIndexParser
)
972 : pkgAcqBaseIndex(Owner
, TransactionID
, Target
, ExpectedHash
,
973 MetaIndexParser
), RealURI(Target
->URI
)
975 // autoselect the compression method
976 AutoSelectCompression();
977 Init(Target
->URI
, Target
->Description
, Target
->ShortDesc
);
979 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
980 std::clog
<< "New pkgIndex with TransactionID "
981 << TransactionID
<< std::endl
;
984 // AcqIndex::AutoSelectCompression - Select compression /*{{{*/
985 // ---------------------------------------------------------------------
986 void pkgAcqIndex::AutoSelectCompression()
988 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
989 CompressionExtension
= "";
990 if (ExpectedHashes
.usable())
992 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
993 if (*t
== "uncompressed" || MetaIndexParser
->Exists(string(Target
->MetaKey
).append(".").append(*t
)) == true)
994 CompressionExtension
.append(*t
).append(" ");
998 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
999 CompressionExtension
.append(*t
).append(" ");
1001 if (CompressionExtension
.empty() == false)
1002 CompressionExtension
.erase(CompressionExtension
.end()-1);
1004 // AcqIndex::Init - defered Constructor /*{{{*/
1005 void pkgAcqIndex::Init(string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
) {
1006 Decompression
= false;
1009 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1010 DestFile
+= URItoFileName(URI
);
1012 std::string
const comprExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
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 // pkgAcqIndex::Failed - getting the indexfile failed /*{{{*/
1098 // ---------------------------------------------------------------------
1100 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
1102 size_t const nextExt
= CompressionExtension
.find(' ');
1103 if (nextExt
!= std::string::npos
)
1105 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
1106 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
1110 // on decompression failure, remove bad versions in partial/
1111 if (Decompression
&& Erase
) {
1112 string s
= _config
->FindDir("Dir::State::lists") + "partial/";
1113 s
.append(URItoFileName(RealURI
));
1117 Item::Failed(Message
,Cnf
);
1119 /// cancel the entire transaction
1120 Owner
->AbortTransaction(TransactionID
);
1123 // pkgAcqIndex::GetFinalFilename - Return the full final file path /*{{{*/
1124 // ---------------------------------------------------------------------
1126 std::string
pkgAcqIndex::GetFinalFilename(std::string
const &URI
,
1127 std::string
const &compExt
)
1129 std::string FinalFile
= _config
->FindDir("Dir::State::lists");
1130 FinalFile
+= URItoFileName(URI
);
1131 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz")
1136 // AcqIndex::ReverifyAfterIMS - Reverify index after an ims-hit /*{{{*/
1137 // ---------------------------------------------------------------------
1139 void pkgAcqIndex::ReverifyAfterIMS(std::string
const &FileName
)
1141 std::string
const compExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
1142 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz")
1145 // copy FinalFile into partial/ so that we check the hash again
1146 string FinalFile
= GetFinalFilename(RealURI
, compExt
);
1147 Decompression
= true;
1148 Desc
.URI
= "copy:" + FinalFile
;
1152 // AcqIndex::Done - Finished a fetch /*{{{*/
1153 // ---------------------------------------------------------------------
1154 /* This goes through a number of states.. On the initial fetch the
1155 method could possibly return an alternate filename which points
1156 to the uncompressed version of the file. If this is so the file
1157 is copied into the partial directory. In all other cases the file
1158 is decompressed with a compressed uri. */
1159 void pkgAcqIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
,
1160 pkgAcquire::MethodConfig
*Cfg
)
1162 Item::Done(Message
,Size
,Hashes
,Cfg
);
1163 std::string
const compExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
1165 if (Decompression
== true)
1167 if (ExpectedHashes
.usable() && ExpectedHashes
!= Hashes
)
1169 RenameOnError(HashSumMismatch
);
1170 printHashSumComparision(RealURI
, ExpectedHashes
, Hashes
);
1171 Failed(Message
, Cfg
);
1175 // FIXME: this can go away once we only ever download stuff that
1176 // has a valid hash and we never do GET based probing
1178 /* Always verify the index file for correctness (all indexes must
1179 * have a Package field) (LP: #346386) (Closes: #627642)
1181 FileFd
fd(DestFile
, FileFd::ReadOnly
, FileFd::Extension
);
1182 // Only test for correctness if the content of the file is not empty
1187 pkgTagFile
tag(&fd
);
1189 // all our current indexes have a field 'Package' in each section
1190 if (_error
->PendingError() == true || tag
.Step(sec
) == false || sec
.Exists("Package") == false)
1192 RenameOnError(InvalidFormat
);
1193 Failed(Message
, Cfg
);
1198 // FIXME: can we void the "Erase" bool here as its very non-local?
1199 std::string CompressedFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1200 CompressedFile
+= URItoFileName(RealURI
);
1201 // Remove the compressed version.
1203 unlink(CompressedFile
.c_str());
1205 // Done, queue for rename on transaction finished
1206 PartialFile
= DestFile
;
1207 DestFile
= GetFinalFilename(RealURI
, compExt
);
1212 // FIXME: use the same method to find
1213 // check the compressed hash too
1214 if(MetaKey
!= "" && Hashes
.size() > 0)
1216 indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup(MetaKey
);
1217 if(Record
&& Record
->Hashes
.usable() && Hashes
!= Record
->Hashes
)
1219 RenameOnError(HashSumMismatch
);
1220 printHashSumComparision(RealURI
, Record
->Hashes
, Hashes
);
1221 Failed(Message
, Cfg
);
1229 // Handle the unzipd case
1230 string FileName
= LookupTag(Message
,"Alt-Filename");
1231 if (FileName
.empty() == false)
1233 // The files timestamp matches
1234 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
1236 ReverifyAfterIMS(FileName
);
1239 Decompression
= true;
1241 DestFile
+= ".decomp";
1242 Desc
.URI
= "copy:" + FileName
;
1248 FileName
= LookupTag(Message
,"Filename");
1249 if (FileName
.empty() == true)
1252 ErrorText
= "Method gave a blank filename";
1255 // The files timestamp matches
1256 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1258 ReverifyAfterIMS(FileName
);
1262 if (FileName
== DestFile
)
1269 // If we enable compressed indexes and already have gzip, keep it
1270 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz" && !Local
) {
1271 // Done, queue for rename on transaction finished
1272 PartialFile
= DestFile
;
1273 DestFile
= GetFinalFilename(RealURI
, compExt
);
1277 // get the binary name for your used compression type
1278 decompProg
= _config
->Find(string("Acquire::CompressionTypes::").append(compExt
),"");
1279 if(decompProg
.empty() == false);
1280 else if(compExt
== "uncompressed")
1281 decompProg
= "copy";
1283 _error
->Error("Unsupported extension: %s", compExt
.c_str());
1287 Decompression
= true;
1288 DestFile
+= ".decomp";
1289 Desc
.URI
= decompProg
+ ":" + FileName
;
1292 // FIXME: this points to a c++ string that goes out of scope
1293 Mode
= decompProg
.c_str();
1296 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
1297 // ---------------------------------------------------------------------
1298 /* The Translation file is added to the queue */
1299 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
1300 string URI
,string URIDesc
,string ShortDesc
)
1301 : pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, HashStringList(), "")
1305 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
1306 unsigned long TransactionID
,
1307 IndexTarget
const * const Target
,
1308 HashStringList
const &ExpectedHashes
,
1309 indexRecords
*MetaIndexParser
)
1310 : pkgAcqIndex(Owner
, TransactionID
, Target
, ExpectedHashes
, MetaIndexParser
)
1312 // load the filesize
1313 indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup(string(Target
->MetaKey
));
1315 FileSize
= Record
->Size
;
1318 // AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
1319 // ---------------------------------------------------------------------
1320 string
pkgAcqIndexTrans::Custom600Headers() const
1322 string Final
= _config
->FindDir("Dir::State::lists");
1323 Final
+= URItoFileName(RealURI
);
1326 if (stat(Final
.c_str(),&Buf
) != 0)
1327 return "\nFail-Ignore: true\nIndex-File: true";
1328 return "\nFail-Ignore: true\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1331 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
1332 // ---------------------------------------------------------------------
1334 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1336 size_t const nextExt
= CompressionExtension
.find(' ');
1337 if (nextExt
!= std::string::npos
)
1339 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
1340 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
1345 // FIXME: this is used often (e.g. in pkgAcqIndexTrans) so refactor
1346 if (Cnf
->LocalOnly
== true ||
1347 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1356 Item::Failed(Message
,Cnf
);
1359 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
, /*{{{*/
1360 unsigned long TransactionID
,
1361 string URI
,string URIDesc
,string ShortDesc
,
1362 string MetaIndexFile
,
1363 const vector
<IndexTarget
*>* IndexTargets
,
1364 indexRecords
* MetaIndexParser
) :
1365 Item(Owner
, HashStringList(), TransactionID
), RealURI(URI
),
1366 MetaIndexParser(MetaIndexParser
), MetaIndexFile(MetaIndexFile
),
1367 IndexTargets(IndexTargets
), AuthPass(false), IMSHit(false)
1369 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1370 DestFile
+= URItoFileName(URI
);
1372 // remove any partial downloaded sig-file in partial/.
1373 // it may confuse proxies and is too small to warrant a
1374 // partial download anyway
1375 unlink(DestFile
.c_str());
1377 // set the TransactionID
1378 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
1379 std::clog
<< "New pkgAcqMetaSig with TransactionID "
1380 << TransactionID
<< std::endl
;
1383 Desc
.Description
= URIDesc
;
1385 Desc
.ShortDesc
= ShortDesc
;
1391 pkgAcqMetaSig::~pkgAcqMetaSig() /*{{{*/
1395 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
1396 // ---------------------------------------------------------------------
1397 /* The only header we use is the last-modified header. */
1398 string
pkgAcqMetaSig::Custom600Headers() const
1400 string FinalFile
= _config
->FindDir("Dir::State::lists");
1401 FinalFile
+= URItoFileName(RealURI
);
1404 if (stat(FinalFile
.c_str(),&Buf
) != 0)
1405 return "\nIndex-File: true";
1407 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1410 void pkgAcqMetaSig::Done(string Message
,unsigned long long Size
, HashStringList
const &Hashes
,
1411 pkgAcquire::MethodConfig
*Cfg
)
1413 Item::Done(Message
, Size
, Hashes
, Cfg
);
1415 string FileName
= LookupTag(Message
,"Filename");
1416 if (FileName
.empty() == true)
1419 ErrorText
= "Method gave a blank filename";
1423 if (FileName
!= DestFile
)
1425 // We have to copy it into place
1427 Desc
.URI
= "copy:" + FileName
;
1432 if(StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1435 // adjust paths if its a ims-hit
1438 string FinalFile
= _config
->FindDir("Dir::State::lists");
1439 FinalFile
+= URItoFileName(RealURI
);
1441 DestFile
= PartialFile
= FinalFile
;
1445 if(AuthPass
== false)
1448 Desc
.URI
= "gpgv:" + DestFile
;
1449 DestFile
= MetaIndexFile
;
1454 // queue to copy the file in place if it was not a ims hit, on ims
1455 // hit the file is already at the right place
1458 PartialFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1459 PartialFile
+= URItoFileName(RealURI
);
1461 DestFile
= _config
->FindDir("Dir::State::lists");
1462 DestFile
+= URItoFileName(RealURI
);
1469 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)/*{{{*/
1471 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1473 // this ensures that any file in the lists/ dir is removed by the
1475 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1476 DestFile
+= URItoFileName(RealURI
);
1479 // FIXME: this is used often (e.g. in pkgAcqIndexTrans) so refactor
1480 if (Cnf
->LocalOnly
== true ||
1481 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1489 Item::Failed(Message
,Cnf
);
1492 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
, /*{{{*/
1493 unsigned long TransactionID
,
1494 string URI
,string URIDesc
,string ShortDesc
,
1495 string MetaIndexSigURI
,string MetaIndexSigURIDesc
, string MetaIndexSigShortDesc
,
1496 const vector
<IndexTarget
*>* IndexTargets
,
1497 indexRecords
* MetaIndexParser
) :
1498 Item(Owner
, HashStringList(), TransactionID
), RealURI(URI
), IndexTargets(IndexTargets
),
1499 MetaIndexParser(MetaIndexParser
), AuthPass(false), IMSHit(false),
1500 MetaIndexSigURI(MetaIndexSigURI
), MetaIndexSigURIDesc(MetaIndexSigURIDesc
),
1501 MetaIndexSigShortDesc(MetaIndexSigShortDesc
)
1503 if(TransactionID
== 0)
1504 this->TransactionID
= (unsigned long)this;
1506 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
1507 std::clog
<< "New pkgAcqMetaIndex with TransactionID "
1508 << TransactionID
<< std::endl
;
1510 Init(URIDesc
, ShortDesc
);
1513 // pkgAcqMetaIndex::Init - Delayed constructor /*{{{*/
1514 void pkgAcqMetaIndex::Init(std::string URIDesc
, std::string ShortDesc
)
1516 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1517 DestFile
+= URItoFileName(RealURI
);
1520 Desc
.Description
= URIDesc
;
1522 Desc
.ShortDesc
= ShortDesc
;
1525 // we expect more item
1526 ExpectedAdditionalItems
= IndexTargets
->size();
1529 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
1530 // ---------------------------------------------------------------------
1531 /* The only header we use is the last-modified header. */
1532 string
pkgAcqMetaIndex::Custom600Headers() const
1534 string Final
= _config
->FindDir("Dir::State::lists");
1535 Final
+= URItoFileName(RealURI
);
1538 if (stat(Final
.c_str(),&Buf
) != 0)
1539 return "\nIndex-File: true";
1541 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1544 void pkgAcqMetaIndex::Done(string Message
,unsigned long long Size
,HashStringList
const &Hashes
, /*{{{*/
1545 pkgAcquire::MethodConfig
*Cfg
)
1547 Item::Done(Message
,Size
,Hashes
,Cfg
);
1549 // MetaIndexes are done in two passes: one to download the
1550 // metaindex with an appropriate method, and a second to verify it
1551 // with the gpgv method
1553 if (AuthPass
== true)
1557 // all cool, move Release file into place
1562 RetrievalDone(Message
);
1564 // Still more retrieving to do
1569 // load indexes, the signature will downloaded afterwards
1570 MetaIndexParser
->Load(DestFile
);
1575 // There was a signature file, so pass it to gpgv for
1577 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1578 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
1579 << SigFile
<< "," << DestFile
<< ")\n";
1581 Desc
.URI
= "gpgv:" + SigFile
;
1588 if (Complete
== true)
1590 string FinalFile
= _config
->FindDir("Dir::State::lists");
1591 FinalFile
+= URItoFileName(RealURI
);
1592 if (SigFile
== DestFile
)
1593 SigFile
= FinalFile
;
1594 // queue for copy in place
1595 PartialFile
= DestFile
;
1596 DestFile
= FinalFile
;
1600 void pkgAcqMetaIndex::RetrievalDone(string Message
) /*{{{*/
1602 // We have just finished downloading a Release file (it is not
1605 string FileName
= LookupTag(Message
,"Filename");
1606 if (FileName
.empty() == true)
1609 ErrorText
= "Method gave a blank filename";
1613 if (FileName
!= DestFile
)
1616 Desc
.URI
= "copy:" + FileName
;
1621 // make sure to verify against the right file on I-M-S hit
1622 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
1625 string FinalFile
= _config
->FindDir("Dir::State::lists");
1626 FinalFile
+= URItoFileName(RealURI
);
1627 if (SigFile
== DestFile
)
1629 SigFile
= FinalFile
;
1631 // constructor of pkgAcqMetaClearSig moved it out of the way,
1632 // now move it back in on IMS hit for the 'old' file
1633 string
const OldClearSig
= DestFile
+ ".reverify";
1634 if (RealFileExists(OldClearSig
) == true)
1635 Rename(OldClearSig
, FinalFile
);
1638 DestFile
= FinalFile
;
1641 // queue a signature
1642 if(SigFile
!= DestFile
)
1643 new pkgAcqMetaSig(Owner
, TransactionID
,
1644 MetaIndexSigURI
, MetaIndexSigURIDesc
,
1645 MetaIndexSigShortDesc
, DestFile
, IndexTargets
,
1651 void pkgAcqMetaIndex::AuthDone(string Message
) /*{{{*/
1653 // At this point, the gpgv method has succeeded, so there is a
1654 // valid signature from a key in the trusted keyring. We
1655 // perform additional verification of its contents, and use them
1656 // to verify the indexes we are about to download
1658 if (!MetaIndexParser
->Load(DestFile
))
1660 Status
= StatAuthError
;
1661 ErrorText
= MetaIndexParser
->ErrorText
;
1665 if (!VerifyVendor(Message
))
1670 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1671 std::cerr
<< "Signature verification succeeded: "
1672 << DestFile
<< std::endl
;
1674 // Download further indexes with verification
1678 // is it a clearsigned MetaIndex file?
1679 if (DestFile
== SigFile
)
1682 // Done, move signature file into position
1683 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1684 URItoFileName(RealURI
) + ".gpg";
1685 Rename(SigFile
,VerifiedSigFile
);
1686 chmod(VerifiedSigFile
.c_str(),0644);
1690 void pkgAcqMetaIndex::QueueIndexes(bool verify
) /*{{{*/
1692 bool transInRelease
= false;
1694 std::vector
<std::string
> const keys
= MetaIndexParser
->MetaKeys();
1695 for (std::vector
<std::string
>::const_iterator k
= keys
.begin(); k
!= keys
.end(); ++k
)
1696 // FIXME: Feels wrong to check for hardcoded string here, but what should we do else…
1697 if (k
->find("Translation-") != std::string::npos
)
1699 transInRelease
= true;
1704 // at this point the real Items are loaded in the fetcher
1705 ExpectedAdditionalItems
= 0;
1706 for (vector
<IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
1707 Target
!= IndexTargets
->end();
1710 HashStringList ExpectedIndexHashes
;
1711 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1712 bool compressedAvailable
= false;
1715 if ((*Target
)->IsOptional() == true)
1717 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
1718 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
1719 if (MetaIndexParser
->Exists((*Target
)->MetaKey
+ "." + *t
) == true)
1721 compressedAvailable
= true;
1725 else if (verify
== true)
1727 Status
= StatAuthError
;
1728 strprintf(ErrorText
, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), (*Target
)->MetaKey
.c_str());
1734 ExpectedIndexHashes
= Record
->Hashes
;
1735 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1737 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
1738 << "Expected Hash:" << std::endl
;
1739 for (HashStringList::const_iterator hs
= ExpectedIndexHashes
.begin(); hs
!= ExpectedIndexHashes
.end(); ++hs
)
1740 std::cerr
<< "\t- " << hs
->toStr() << std::endl
;
1741 std::cerr
<< "For: " << Record
->MetaKeyFilename
<< std::endl
;
1743 if (verify
== true && ExpectedIndexHashes
.empty() == true && (*Target
)->IsOptional() == false)
1745 Status
= StatAuthError
;
1746 strprintf(ErrorText
, _("Unable to find hash sum for '%s' in Release file"), (*Target
)->MetaKey
.c_str());
1751 if ((*Target
)->IsOptional() == true)
1753 if ((*Target
)->IsSubIndex() == true)
1754 new pkgAcqSubIndex(Owner
, TransactionID
,
1755 (*Target
)->URI
, (*Target
)->Description
,
1756 (*Target
)->ShortDesc
, ExpectedIndexHashes
);
1757 else if (transInRelease
== false || Record
!= NULL
|| compressedAvailable
== true)
1759 if (_config
->FindB("Acquire::PDiffs",true) == true && transInRelease
== true &&
1760 MetaIndexParser
->Exists((*Target
)->MetaKey
+ ".diff/Index") == true)
1761 new pkgAcqDiffIndex(Owner
, TransactionID
, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1763 new pkgAcqIndexTrans(Owner
, TransactionID
, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1768 /* Queue Packages file (either diff or full packages files, depending
1769 on the users option) - we also check if the PDiff Index file is listed
1770 in the Meta-Index file. Ideal would be if pkgAcqDiffIndex would test this
1771 instead, but passing the required info to it is to much hassle */
1772 if(_config
->FindB("Acquire::PDiffs",true) == true && (verify
== false ||
1773 MetaIndexParser
->Exists((*Target
)->MetaKey
+ ".diff/Index") == true))
1774 new pkgAcqDiffIndex(Owner
, TransactionID
, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1776 new pkgAcqIndex(Owner
, TransactionID
, *Target
, ExpectedIndexHashes
, MetaIndexParser
);
1780 bool pkgAcqMetaIndex::VerifyVendor(string Message
) /*{{{*/
1782 string::size_type pos
;
1784 // check for missing sigs (that where not fatal because otherwise we had
1787 string msg
= _("There is no public key available for the "
1788 "following key IDs:\n");
1789 pos
= Message
.find("NO_PUBKEY ");
1790 if (pos
!= std::string::npos
)
1792 string::size_type start
= pos
+strlen("NO_PUBKEY ");
1793 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
1794 missingkeys
+= (Fingerprint
);
1796 if(!missingkeys
.empty())
1797 _error
->Warning("%s", (msg
+ missingkeys
).c_str());
1799 string Transformed
= MetaIndexParser
->GetExpectedDist();
1801 if (Transformed
== "../project/experimental")
1803 Transformed
= "experimental";
1806 pos
= Transformed
.rfind('/');
1807 if (pos
!= string::npos
)
1809 Transformed
= Transformed
.substr(0, pos
);
1812 if (Transformed
== ".")
1817 if (_config
->FindB("Acquire::Check-Valid-Until", true) == true &&
1818 MetaIndexParser
->GetValidUntil() > 0) {
1819 time_t const invalid_since
= time(NULL
) - MetaIndexParser
->GetValidUntil();
1820 if (invalid_since
> 0)
1821 // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
1822 // the time since then the file is invalid - formated in the same way as in
1823 // the download progress display (e.g. 7d 3h 42min 1s)
1824 return _error
->Error(
1825 _("Release file for %s is expired (invalid since %s). "
1826 "Updates for this repository will not be applied."),
1827 RealURI
.c_str(), TimeToStr(invalid_since
).c_str());
1830 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1832 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1833 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1834 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1837 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1839 // This might become fatal one day
1840 // Status = StatAuthError;
1841 // ErrorText = "Conflicting distribution; expected "
1842 // + MetaIndexParser->GetExpectedDist() + " but got "
1843 // + MetaIndexParser->GetDist();
1845 if (!Transformed
.empty())
1847 _error
->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
1848 Desc
.Description
.c_str(),
1849 Transformed
.c_str(),
1850 MetaIndexParser
->GetDist().c_str());
1857 // pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
1858 // ---------------------------------------------------------------------
1860 void pkgAcqMetaIndex::Failed(string Message
,
1861 pkgAcquire::MethodConfig
* /*Cnf*/)
1863 if (AuthPass
== true)
1865 // gpgv method failed, if we have a good signature
1866 string LastGoodSigFile
= _config
->FindDir("Dir::State::lists");
1867 LastGoodSigFile
+= URItoFileName(RealURI
);
1868 if (DestFile
!= SigFile
)
1869 LastGoodSigFile
.append(".gpg");
1871 if(FileExists(LastGoodSigFile
))
1873 Status
= StatTransientNetworkError
;
1874 _error
->Warning(_("An error occurred during the signature "
1875 "verification. The repository is not updated "
1876 "and the previous index files will be used. "
1877 "GPG error: %s: %s\n"),
1878 Desc
.Description
.c_str(),
1879 LookupTag(Message
,"Message").c_str());
1880 RunScripts("APT::Update::Auth-Failure");
1882 } else if (LookupTag(Message
,"Message").find("NODATA") != string::npos
) {
1883 /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */
1884 _error
->Error(_("GPG error: %s: %s"),
1885 Desc
.Description
.c_str(),
1886 LookupTag(Message
,"Message").c_str());
1890 _error
->Warning(_("GPG error: %s: %s"),
1891 Desc
.Description
.c_str(),
1892 LookupTag(Message
,"Message").c_str());
1894 // gpgv method failed
1895 ReportMirrorFailure("GPGFailure");
1898 /* Always move the meta index, even if gpgv failed. This ensures
1899 * that PackageFile objects are correctly filled in */
1900 if (FileExists(DestFile
)) {
1901 string FinalFile
= _config
->FindDir("Dir::State::lists");
1902 FinalFile
+= URItoFileName(RealURI
);
1903 /* InRelease files become Release files, otherwise
1904 * they would be considered as trusted later on */
1905 if (SigFile
== DestFile
) {
1906 RealURI
= RealURI
.replace(RealURI
.rfind("InRelease"), 9,
1908 FinalFile
= FinalFile
.replace(FinalFile
.rfind("InRelease"), 9,
1910 SigFile
= FinalFile
;
1913 // Done, queue for rename on transaction finished
1914 PartialFile
= DestFile
;
1915 DestFile
= FinalFile
;
1918 // No Release file was present, or verification failed, so fall
1919 // back to queueing Packages files without verification
1920 QueueIndexes(false);
1924 void pkgAcqMetaIndex::Finished()
1926 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
1927 std::clog
<< "Finished: " << DestFile
<<std::endl
;
1928 if(Owner
->TransactionHasError(TransactionID
) == false &&
1930 Owner
->CommitTransaction(TransactionID
);
1934 pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire
*Owner
, /*{{{*/
1935 string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
,
1936 string
const &MetaIndexURI
, string
const &MetaIndexURIDesc
, string
const &MetaIndexShortDesc
,
1937 string
const &MetaSigURI
, string
const &MetaSigURIDesc
, string
const &MetaSigShortDesc
,
1938 const vector
<IndexTarget
*>* IndexTargets
,
1939 indexRecords
* MetaIndexParser
) :
1940 pkgAcqMetaIndex(Owner
, (unsigned long)this, URI
, URIDesc
, ShortDesc
, MetaSigURI
, MetaSigURIDesc
,MetaSigShortDesc
, IndexTargets
, MetaIndexParser
),
1941 MetaIndexURI(MetaIndexURI
), MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
1942 MetaSigURI(MetaSigURI
), MetaSigURIDesc(MetaSigURIDesc
), MetaSigShortDesc(MetaSigShortDesc
)
1946 // index targets + (worst case:) Release/Release.gpg
1947 ExpectedAdditionalItems
= IndexTargets
->size() + 2;
1950 // keep the old InRelease around in case of transistent network errors
1951 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1952 if (RealFileExists(Final
) == true)
1954 string
const LastGoodSig
= DestFile
+ ".reverify";
1955 Rename(Final
,LastGoodSig
);
1960 pkgAcqMetaClearSig::~pkgAcqMetaClearSig() /*{{{*/
1963 // if the file was never queued undo file-changes done in the constructor
1964 if (QueueCounter
== 1 && Status
== StatIdle
&& FileSize
== 0 && Complete
== false)
1966 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1967 string
const LastGoodSig
= DestFile
+ ".reverify";
1968 if (RealFileExists(Final
) == false && RealFileExists(LastGoodSig
) == true)
1969 Rename(LastGoodSig
, Final
);
1974 // pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
1975 // ---------------------------------------------------------------------
1976 // FIXME: this can go away once the InRelease file is used widely
1977 string
pkgAcqMetaClearSig::Custom600Headers() const
1979 string Final
= _config
->FindDir("Dir::State::lists");
1980 Final
+= URItoFileName(RealURI
);
1983 if (stat(Final
.c_str(),&Buf
) != 0)
1985 if (stat(Final
.c_str(),&Buf
) != 0)
1986 return "\nIndex-File: true\nFail-Ignore: true\n";
1989 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1992 // pkgAcqMetaClearSig::Done - We got a file /*{{{*/
1993 // ---------------------------------------------------------------------
1994 void pkgAcqMetaClearSig::Done(std::string Message
,unsigned long long Size
,
1995 HashStringList
const &Hashes
,
1996 pkgAcquire::MethodConfig
*Cnf
)
1998 // if we expect a ClearTextSignature (InRelase), ensure that
1999 // this is what we get and if not fail to queue a
2000 // Release/Release.gpg, see #346386
2001 if (FileExists(DestFile
) && !StartsWithGPGClearTextSignature(DestFile
))
2003 //_error->Error(_("Does not start with a clear sign signature"));
2004 pkgAcquire::Item::Failed(Message
, Cnf
);
2007 pkgAcqMetaIndex::Done(Message
, Size
, Hashes
, Cnf
);
2010 void pkgAcqMetaClearSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
2012 // we failed, we will not get additional items from this method
2013 ExpectedAdditionalItems
= 0;
2015 if (AuthPass
== false)
2017 // Queue the 'old' InRelease file for removal if we try Release.gpg
2018 // as otherwise the file will stay around and gives a false-auth
2019 // impression (CVE-2012-0214)
2020 string FinalFile
= _config
->FindDir("Dir::State::lists");
2021 FinalFile
.append(URItoFileName(RealURI
));
2023 DestFile
= FinalFile
;
2025 new pkgAcqMetaIndex(Owner
, TransactionID
,
2026 MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
2027 MetaSigURI
, MetaSigURIDesc
, MetaSigShortDesc
,
2028 IndexTargets
, MetaIndexParser
);
2029 if (Cnf
->LocalOnly
== true ||
2030 StringToBool(LookupTag(Message
, "Transient-Failure"), false) == false)
2034 pkgAcqMetaIndex::Failed(Message
, Cnf
);
2037 // AcqArchive::AcqArchive - Constructor /*{{{*/
2038 // ---------------------------------------------------------------------
2039 /* This just sets up the initial fetch environment and queues the first
2041 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
2042 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
2043 string
&StoreFilename
) :
2044 Item(Owner
, HashStringList()), Version(Version
), Sources(Sources
), Recs(Recs
),
2045 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
2048 Retries
= _config
->FindI("Acquire::Retries",0);
2050 if (Version
.Arch() == 0)
2052 _error
->Error(_("I wasn't able to locate a file for the %s package. "
2053 "This might mean you need to manually fix this package. "
2054 "(due to missing arch)"),
2055 Version
.ParentPkg().FullName().c_str());
2059 /* We need to find a filename to determine the extension. We make the
2060 assumption here that all the available sources for this version share
2061 the same extension.. */
2062 // Skip not source sources, they do not have file fields.
2063 for (; Vf
.end() == false; ++Vf
)
2065 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
2070 // Does not really matter here.. we are going to fail out below
2071 if (Vf
.end() != true)
2073 // If this fails to get a file name we will bomb out below.
2074 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
2075 if (_error
->PendingError() == true)
2078 // Generate the final file name as: package_version_arch.foo
2079 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
2080 QuoteString(Version
.VerStr(),"_:") + '_' +
2081 QuoteString(Version
.Arch(),"_:.") +
2082 "." + flExtension(Parse
.FileName());
2085 // check if we have one trusted source for the package. if so, switch
2086 // to "TrustedOnly" mode - but only if not in AllowUnauthenticated mode
2087 bool const allowUnauth
= _config
->FindB("APT::Get::AllowUnauthenticated", false);
2088 bool const debugAuth
= _config
->FindB("Debug::pkgAcquire::Auth", false);
2089 bool seenUntrusted
= false;
2090 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; ++i
)
2092 pkgIndexFile
*Index
;
2093 if (Sources
->FindIndex(i
.File(),Index
) == false)
2096 if (debugAuth
== true)
2097 std::cerr
<< "Checking index: " << Index
->Describe()
2098 << "(Trusted=" << Index
->IsTrusted() << ")" << std::endl
;
2100 if (Index
->IsTrusted() == true)
2103 if (allowUnauth
== false)
2107 seenUntrusted
= true;
2110 // "allow-unauthenticated" restores apts old fetching behaviour
2111 // that means that e.g. unauthenticated file:// uris are higher
2112 // priority than authenticated http:// uris
2113 if (allowUnauth
== true && seenUntrusted
== true)
2117 if (QueueNext() == false && _error
->PendingError() == false)
2118 _error
->Error(_("Can't find a source to download version '%s' of '%s'"),
2119 Version
.VerStr(), Version
.ParentPkg().FullName(false).c_str());
2122 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
2123 // ---------------------------------------------------------------------
2124 /* This queues the next available file version for download. It checks if
2125 the archive is already available in the cache and stashs the MD5 for
2127 bool pkgAcqArchive::QueueNext()
2129 for (; Vf
.end() == false; ++Vf
)
2131 // Ignore not source sources
2132 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
2135 // Try to cross match against the source list
2136 pkgIndexFile
*Index
;
2137 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
2140 // only try to get a trusted package from another source if that source
2142 if(Trusted
&& !Index
->IsTrusted())
2145 // Grab the text package record
2146 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
2147 if (_error
->PendingError() == true)
2150 string PkgFile
= Parse
.FileName();
2151 ExpectedHashes
= Parse
.Hashes();
2153 if (PkgFile
.empty() == true)
2154 return _error
->Error(_("The package index files are corrupted. No Filename: "
2155 "field for package %s."),
2156 Version
.ParentPkg().Name());
2158 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
2159 Desc
.Description
= Index
->ArchiveInfo(Version
);
2161 Desc
.ShortDesc
= Version
.ParentPkg().FullName(true);
2163 // See if we already have the file. (Legacy filenames)
2164 FileSize
= Version
->Size
;
2165 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
2167 if (stat(FinalFile
.c_str(),&Buf
) == 0)
2169 // Make sure the size matches
2170 if ((unsigned long long)Buf
.st_size
== Version
->Size
)
2175 StoreFilename
= DestFile
= FinalFile
;
2179 /* Hmm, we have a file and its size does not match, this means it is
2180 an old style mismatched arch */
2181 unlink(FinalFile
.c_str());
2184 // Check it again using the new style output filenames
2185 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
2186 if (stat(FinalFile
.c_str(),&Buf
) == 0)
2188 // Make sure the size matches
2189 if ((unsigned long long)Buf
.st_size
== Version
->Size
)
2194 StoreFilename
= DestFile
= FinalFile
;
2198 /* Hmm, we have a file and its size does not match, this shouldn't
2200 unlink(FinalFile
.c_str());
2203 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
2205 // Check the destination file
2206 if (stat(DestFile
.c_str(),&Buf
) == 0)
2208 // Hmm, the partial file is too big, erase it
2209 if ((unsigned long long)Buf
.st_size
> Version
->Size
)
2210 unlink(DestFile
.c_str());
2212 PartialSize
= Buf
.st_size
;
2215 // Disables download of archives - useful if no real installation follows,
2216 // e.g. if we are just interested in proposed installation order
2217 if (_config
->FindB("Debug::pkgAcqArchive::NoQueue", false) == true)
2222 StoreFilename
= DestFile
= FinalFile
;
2236 // AcqArchive::Done - Finished fetching /*{{{*/
2237 // ---------------------------------------------------------------------
2239 void pkgAcqArchive::Done(string Message
,unsigned long long Size
, HashStringList
const &CalcHashes
,
2240 pkgAcquire::MethodConfig
*Cfg
)
2242 Item::Done(Message
, Size
, CalcHashes
, Cfg
);
2245 if (Size
!= Version
->Size
)
2247 RenameOnError(SizeMismatch
);
2251 // FIXME: could this empty() check impose *any* sort of security issue?
2252 if(ExpectedHashes
.usable() && ExpectedHashes
!= CalcHashes
)
2254 RenameOnError(HashSumMismatch
);
2255 printHashSumComparision(DestFile
, ExpectedHashes
, CalcHashes
);
2259 // Grab the output filename
2260 string FileName
= LookupTag(Message
,"Filename");
2261 if (FileName
.empty() == true)
2264 ErrorText
= "Method gave a blank filename";
2270 // Reference filename
2271 if (FileName
!= DestFile
)
2273 StoreFilename
= DestFile
= FileName
;
2278 // Done, move it into position
2279 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
2280 FinalFile
+= flNotDir(StoreFilename
);
2281 Rename(DestFile
,FinalFile
);
2283 StoreFilename
= DestFile
= FinalFile
;
2287 // AcqArchive::Failed - Failure handler /*{{{*/
2288 // ---------------------------------------------------------------------
2289 /* Here we try other sources */
2290 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
2292 ErrorText
= LookupTag(Message
,"Message");
2294 /* We don't really want to retry on failed media swaps, this prevents
2295 that. An interesting observation is that permanent failures are not
2297 if (Cnf
->Removable
== true &&
2298 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2300 // Vf = Version.FileList();
2301 while (Vf
.end() == false) ++Vf
;
2302 StoreFilename
= string();
2303 Item::Failed(Message
,Cnf
);
2307 if (QueueNext() == false)
2309 // This is the retry counter
2311 Cnf
->LocalOnly
== false &&
2312 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2315 Vf
= Version
.FileList();
2316 if (QueueNext() == true)
2320 StoreFilename
= string();
2321 Item::Failed(Message
,Cnf
);
2325 // AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
2326 // ---------------------------------------------------------------------
2327 APT_PURE
bool pkgAcqArchive::IsTrusted() const
2332 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
2333 // ---------------------------------------------------------------------
2335 void pkgAcqArchive::Finished()
2337 if (Status
== pkgAcquire::Item::StatDone
&&
2340 StoreFilename
= string();
2343 // AcqFile::pkgAcqFile - Constructor /*{{{*/
2344 // ---------------------------------------------------------------------
2345 /* The file is added to the queue */
2346 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
, HashStringList
const &Hashes
,
2347 unsigned long long Size
,string Dsc
,string ShortDesc
,
2348 const string
&DestDir
, const string
&DestFilename
,
2350 Item(Owner
, Hashes
), IsIndexFile(IsIndexFile
)
2352 Retries
= _config
->FindI("Acquire::Retries",0);
2354 if(!DestFilename
.empty())
2355 DestFile
= DestFilename
;
2356 else if(!DestDir
.empty())
2357 DestFile
= DestDir
+ "/" + flNotDir(URI
);
2359 DestFile
= flNotDir(URI
);
2363 Desc
.Description
= Dsc
;
2366 // Set the short description to the archive component
2367 Desc
.ShortDesc
= ShortDesc
;
2369 // Get the transfer sizes
2372 if (stat(DestFile
.c_str(),&Buf
) == 0)
2374 // Hmm, the partial file is too big, erase it
2375 if ((Size
> 0) && (unsigned long long)Buf
.st_size
> Size
)
2376 unlink(DestFile
.c_str());
2378 PartialSize
= Buf
.st_size
;
2384 // AcqFile::Done - Item downloaded OK /*{{{*/
2385 // ---------------------------------------------------------------------
2387 void pkgAcqFile::Done(string Message
,unsigned long long Size
,HashStringList
const &CalcHashes
,
2388 pkgAcquire::MethodConfig
*Cnf
)
2390 Item::Done(Message
,Size
,CalcHashes
,Cnf
);
2393 if(ExpectedHashes
.usable() && ExpectedHashes
!= CalcHashes
)
2395 RenameOnError(HashSumMismatch
);
2396 printHashSumComparision(DestFile
, ExpectedHashes
, CalcHashes
);
2400 string FileName
= LookupTag(Message
,"Filename");
2401 if (FileName
.empty() == true)
2404 ErrorText
= "Method gave a blank filename";
2410 // The files timestamp matches
2411 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
2414 // We have to copy it into place
2415 if (FileName
!= DestFile
)
2418 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
2419 Cnf
->Removable
== true)
2421 Desc
.URI
= "copy:" + FileName
;
2426 // Erase the file if it is a symlink so we can overwrite it
2428 if (lstat(DestFile
.c_str(),&St
) == 0)
2430 if (S_ISLNK(St
.st_mode
) != 0)
2431 unlink(DestFile
.c_str());
2435 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
2437 ErrorText
= "Link to " + DestFile
+ " failure ";
2444 // AcqFile::Failed - Failure handler /*{{{*/
2445 // ---------------------------------------------------------------------
2446 /* Here we try other sources */
2447 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
2449 ErrorText
= LookupTag(Message
,"Message");
2451 // This is the retry counter
2453 Cnf
->LocalOnly
== false &&
2454 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2461 Item::Failed(Message
,Cnf
);
2464 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
2465 // ---------------------------------------------------------------------
2466 /* The only header we use is the last-modified header. */
2467 string
pkgAcqFile::Custom600Headers() const
2470 return "\nIndex-File: true";