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/md5.h>
26 #include <apt-pkg/sha1.h>
27 #include <apt-pkg/tagfile.h>
28 #include <apt-pkg/indexrecords.h>
29 #include <apt-pkg/metaindex.h>
44 // Acquire::Item::Item - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
47 pkgAcquire::Item::Item(pkgAcquire
*Owner
) : Owner(Owner
), FileSize(0),
48 PartialSize(0), Mode(0), ID(0), Complete(false),
49 Local(false), QueueCounter(0)
55 // Acquire::Item::~Item - Destructor /*{{{*/
56 // ---------------------------------------------------------------------
58 pkgAcquire::Item::~Item()
63 // Acquire::Item::Failed - Item failed to download /*{{{*/
64 // ---------------------------------------------------------------------
65 /* We return to an idle state if there are still other queues that could
67 void pkgAcquire::Item::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
70 ErrorText
= LookupTag(Message
,"Message");
71 UsedMirror
= LookupTag(Message
,"UsedMirror");
72 if (QueueCounter
<= 1)
74 /* This indicates that the file is not available right now but might
75 be sometime later. If we do a retry cycle then this should be
77 if (Cnf
->LocalOnly
== true &&
78 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
89 // report mirror failure back to LP if we actually use a mirror
90 string FailReason
= LookupTag(Message
, "FailReason");
91 if(FailReason
.size() != 0)
92 ReportMirrorFailure(FailReason
);
94 ReportMirrorFailure(ErrorText
);
97 // Acquire::Item::Start - Item has begun to download /*{{{*/
98 // ---------------------------------------------------------------------
99 /* Stash status and the file size. Note that setting Complete means
100 sub-phases of the acquire process such as decompresion are operating */
101 void pkgAcquire::Item::Start(string
/*Message*/,unsigned long long Size
)
103 Status
= StatFetching
;
104 if (FileSize
== 0 && Complete
== false)
108 // Acquire::Item::Done - Item downloaded OK /*{{{*/
109 // ---------------------------------------------------------------------
111 void pkgAcquire::Item::Done(string Message
,unsigned long long Size
,string Hash
,
112 pkgAcquire::MethodConfig
*Cnf
)
114 // We just downloaded something..
115 string FileName
= LookupTag(Message
,"Filename");
116 UsedMirror
= LookupTag(Message
,"UsedMirror");
117 if (Complete
== false && !Local
&& FileName
== DestFile
)
120 Owner
->Log
->Fetched(Size
,atoi(LookupTag(Message
,"Resume-Point","0").c_str()));
126 ErrorText
= string();
127 Owner
->Dequeue(this);
130 // Acquire::Item::Rename - Rename a file /*{{{*/
131 // ---------------------------------------------------------------------
132 /* This helper function is used by alot of item methods as thier final
134 void pkgAcquire::Item::Rename(string From
,string To
)
136 if (rename(From
.c_str(),To
.c_str()) != 0)
139 snprintf(S
,sizeof(S
),_("rename failed, %s (%s -> %s)."),strerror(errno
),
140 From
.c_str(),To
.c_str());
146 // Acquire::Item::ReportMirrorFailure /*{{{*/
147 // ---------------------------------------------------------------------
148 void pkgAcquire::Item::ReportMirrorFailure(string FailCode
)
150 // we only act if a mirror was used at all
151 if(UsedMirror
.empty())
154 std::cerr
<< "\nReportMirrorFailure: "
156 << " Uri: " << DescURI()
158 << FailCode
<< std::endl
;
160 const char *Args
[40];
162 string report
= _config
->Find("Methods::Mirror::ProblemReporting",
163 "/usr/lib/apt/apt-report-mirror-failure");
164 if(!FileExists(report
))
166 Args
[i
++] = report
.c_str();
167 Args
[i
++] = UsedMirror
.c_str();
168 Args
[i
++] = DescURI().c_str();
169 Args
[i
++] = FailCode
.c_str();
171 pid_t pid
= ExecFork();
174 _error
->Error("ReportMirrorFailure Fork failed");
179 execvp(Args
[0], (char**)Args
);
180 std::cerr
<< "Could not exec " << Args
[0] << std::endl
;
183 if(!ExecWait(pid
, "report-mirror-failure"))
185 _error
->Warning("Couldn't report problem to '%s'",
186 _config
->Find("Methods::Mirror::ProblemReporting").c_str());
190 // AcqSubIndex::AcqSubIndex - Constructor /*{{{*/
191 // ---------------------------------------------------------------------
192 /* Get a sub-index file based on checksums from a 'master' file and
193 possibly query additional files */
194 pkgAcqSubIndex::pkgAcqSubIndex(pkgAcquire
*Owner
, string
const &URI
,
195 string
const &URIDesc
, string
const &ShortDesc
,
196 HashString
const &ExpectedHash
)
197 : Item(Owner
), ExpectedHash(ExpectedHash
)
199 /* XXX: Beware: Currently this class does nothing (of value) anymore ! */
200 Debug
= _config
->FindB("Debug::pkgAcquire::SubIndex",false);
202 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
203 DestFile
+= URItoFileName(URI
);
206 Desc
.Description
= URIDesc
;
208 Desc
.ShortDesc
= ShortDesc
;
213 std::clog
<< "pkgAcqSubIndex: " << Desc
.URI
<< std::endl
;
216 // AcqSubIndex::Custom600Headers - Insert custom request headers /*{{{*/
217 // ---------------------------------------------------------------------
218 /* The only header we use is the last-modified header. */
219 string
pkgAcqSubIndex::Custom600Headers()
221 string Final
= _config
->FindDir("Dir::State::lists");
222 Final
+= URItoFileName(Desc
.URI
);
225 if (stat(Final
.c_str(),&Buf
) != 0)
226 return "\nIndex-File: true\nFail-Ignore: true\n";
227 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
230 void pkgAcqSubIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
233 std::clog
<< "pkgAcqSubIndex failed: " << Desc
.URI
<< std::endl
;
239 // No good Index is provided
242 void pkgAcqSubIndex::Done(string Message
,unsigned long long Size
,string Md5Hash
, /*{{{*/
243 pkgAcquire::MethodConfig
*Cnf
)
246 std::clog
<< "pkgAcqSubIndex::Done(): " << Desc
.URI
<< std::endl
;
248 string FileName
= LookupTag(Message
,"Filename");
249 if (FileName
.empty() == true)
252 ErrorText
= "Method gave a blank filename";
256 if (FileName
!= DestFile
)
259 Desc
.URI
= "copy:" + FileName
;
264 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
266 string FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(Desc
.URI
);
268 /* Downloaded invalid transindex => Error (LP: #346386) (Closes: #627642) */
269 indexRecords SubIndexParser
;
270 if (FileExists(DestFile
) == true && !SubIndexParser
.Load(DestFile
)) {
272 ErrorText
= SubIndexParser
.ErrorText
;
276 // sucess in downloading the index
279 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
<< std::endl
;
280 Rename(DestFile
,FinalFile
);
281 chmod(FinalFile
.c_str(),0644);
282 DestFile
= FinalFile
;
284 if(ParseIndex(DestFile
) == false)
285 return Failed("", NULL
);
293 bool pkgAcqSubIndex::ParseIndex(string
const &IndexFile
) /*{{{*/
295 indexRecords SubIndexParser
;
296 if (FileExists(IndexFile
) == false || SubIndexParser
.Load(IndexFile
) == false)
298 // so something with the downloaded index
302 // AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
303 // ---------------------------------------------------------------------
304 /* Get the DiffIndex file first and see if there are patches availabe
305 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
306 * patches. If anything goes wrong in that process, it will fall back to
307 * the original packages file
309 pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire
*Owner
,
310 string URI
,string URIDesc
,string ShortDesc
,
311 HashString ExpectedHash
)
312 : Item(Owner
), RealURI(URI
), ExpectedHash(ExpectedHash
),
316 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
318 Desc
.Description
= URIDesc
+ "/DiffIndex";
320 Desc
.ShortDesc
= ShortDesc
;
321 Desc
.URI
= URI
+ ".diff/Index";
323 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
324 DestFile
+= URItoFileName(URI
) + string(".DiffIndex");
327 std::clog
<< "pkgAcqDiffIndex: " << Desc
.URI
<< std::endl
;
329 // look for the current package file
330 CurrentPackagesFile
= _config
->FindDir("Dir::State::lists");
331 CurrentPackagesFile
+= URItoFileName(RealURI
);
333 // FIXME: this file:/ check is a hack to prevent fetching
334 // from local sources. this is really silly, and
335 // should be fixed cleanly as soon as possible
336 if(!FileExists(CurrentPackagesFile
) ||
337 Desc
.URI
.substr(0,strlen("file:/")) == "file:/")
339 // we don't have a pkg file or we don't want to queue
341 std::clog
<< "No index file, local or canceld by user" << std::endl
;
347 std::clog
<< "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
348 << CurrentPackagesFile
<< std::endl
;
354 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
355 // ---------------------------------------------------------------------
356 /* The only header we use is the last-modified header. */
357 string
pkgAcqDiffIndex::Custom600Headers()
359 string Final
= _config
->FindDir("Dir::State::lists");
360 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
363 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
366 if (stat(Final
.c_str(),&Buf
) != 0)
367 return "\nIndex-File: true";
369 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
372 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile
) /*{{{*/
375 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
380 vector
<DiffInfo
> available_patches
;
382 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
384 if (_error
->PendingError() == true)
387 if(TF
.Step(Tags
) == true)
393 string
const tmp
= Tags
.FindS("SHA1-Current");
394 std::stringstream
ss(tmp
);
395 ss
>> ServerSha1
>> size
;
396 unsigned long const ServerSize
= atol(size
.c_str());
398 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
401 string
const local_sha1
= SHA1
.Result();
403 if(local_sha1
== ServerSha1
)
405 // we have the same sha1 as the server
407 std::clog
<< "Package file is up-to-date" << std::endl
;
408 // set found to true, this will queue a pkgAcqIndexDiffs with
409 // a empty availabe_patches
415 std::clog
<< "SHA1-Current: " << ServerSha1
<< " and we start at "<< fd
.Name() << " " << fd
.Size() << " " << local_sha1
<< std::endl
;
417 // check the historie and see what patches we need
418 string
const history
= Tags
.FindS("SHA1-History");
419 std::stringstream
hist(history
);
420 while(hist
>> d
.sha1
>> size
>> d
.file
)
422 // read until the first match is found
423 // from that point on, we probably need all diffs
424 if(d
.sha1
== local_sha1
)
426 else if (found
== false)
430 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
431 available_patches
.push_back(d
);
434 if (available_patches
.empty() == false)
436 // patching with too many files is rather slow compared to a fast download
437 unsigned long const fileLimit
= _config
->FindI("Acquire::PDiffs::FileLimit", 0);
438 if (fileLimit
!= 0 && fileLimit
< available_patches
.size())
441 std::clog
<< "Need " << available_patches
.size() << " diffs (Limit is " << fileLimit
442 << ") so fallback to complete download" << std::endl
;
446 // see if the patches are too big
447 found
= false; // it was true and it will be true again at the end
448 d
= *available_patches
.begin();
449 string
const firstPatch
= d
.file
;
450 unsigned long patchesSize
= 0;
451 std::stringstream
patches(Tags
.FindS("SHA1-Patches"));
452 while(patches
>> d
.sha1
>> size
>> d
.file
)
454 if (firstPatch
== d
.file
)
456 else if (found
== false)
459 patchesSize
+= atol(size
.c_str());
461 unsigned long const sizeLimit
= ServerSize
* _config
->FindI("Acquire::PDiffs::SizeLimit", 100);
462 if (sizeLimit
> 0 && (sizeLimit
/100) < patchesSize
)
465 std::clog
<< "Need " << patchesSize
<< " bytes (Limit is " << sizeLimit
/100
466 << ") so fallback to complete download" << std::endl
;
472 // we have something, queue the next diff
476 string::size_type
const last_space
= Description
.rfind(" ");
477 if(last_space
!= string::npos
)
478 Description
.erase(last_space
, Description
.size()-last_space
);
479 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
480 ExpectedHash
, ServerSha1
, available_patches
);
488 // Nothing found, report and return false
489 // Failing here is ok, if we return false later, the full
490 // IndexFile is queued
492 std::clog
<< "Can't find a patch in the index file" << std::endl
;
496 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
499 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< std::endl
500 << "Falling back to normal index file aquire" << std::endl
;
502 new pkgAcqIndex(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
510 void pkgAcqDiffIndex::Done(string Message
,unsigned long long Size
,string Md5Hash
, /*{{{*/
511 pkgAcquire::MethodConfig
*Cnf
)
514 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
516 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
519 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
521 // sucess in downloading the index
523 FinalFile
+= string(".IndexDiff");
525 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
527 Rename(DestFile
,FinalFile
);
528 chmod(FinalFile
.c_str(),0644);
529 DestFile
= FinalFile
;
531 if(!ParseDiffIndex(DestFile
))
532 return Failed("", NULL
);
540 // AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
541 // ---------------------------------------------------------------------
542 /* The package diff is added to the queue. one object is constructed
543 * for each diff and the index
545 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
546 string URI
,string URIDesc
,string ShortDesc
,
547 HashString ExpectedHash
,
549 vector
<DiffInfo
> diffs
)
550 : Item(Owner
), RealURI(URI
), ExpectedHash(ExpectedHash
),
551 available_patches(diffs
), ServerSha1(ServerSha1
)
554 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
555 DestFile
+= URItoFileName(URI
);
557 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
559 Description
= URIDesc
;
561 Desc
.ShortDesc
= ShortDesc
;
563 if(available_patches
.empty() == true)
565 // we are done (yeah!)
571 State
= StateFetchDiff
;
576 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
579 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< std::endl
580 << "Falling back to normal index file aquire" << std::endl
;
581 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
586 // Finish - helper that cleans the item out of the fetcher queue /*{{{*/
587 void pkgAcqIndexDiffs::Finish(bool allDone
)
589 // we restore the original name, this is required, otherwise
590 // the file will be cleaned
593 DestFile
= _config
->FindDir("Dir::State::lists");
594 DestFile
+= URItoFileName(RealURI
);
596 if(!ExpectedHash
.empty() && !ExpectedHash
.VerifyFile(DestFile
))
598 Status
= StatAuthError
;
599 ErrorText
= _("MD5Sum mismatch");
600 Rename(DestFile
,DestFile
+ ".FAILED");
605 // this is for the "real" finish
610 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
615 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
622 bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
625 // calc sha1 of the just patched file
626 string FinalFile
= _config
->FindDir("Dir::State::lists");
627 FinalFile
+= URItoFileName(RealURI
);
629 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
632 string local_sha1
= string(SHA1
.Result());
634 std::clog
<< "QueueNextDiff: "
635 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
637 // final file reached before all patches are applied
638 if(local_sha1
== ServerSha1
)
644 // remove all patches until the next matching patch is found
645 // this requires the Index file to be ordered
646 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
647 available_patches
.empty() == false &&
648 I
!= available_patches
.end() &&
649 I
->sha1
!= local_sha1
;
652 available_patches
.erase(I
);
655 // error checking and falling back if no patch was found
656 if(available_patches
.empty() == true)
662 // queue the right diff
663 Desc
.URI
= string(RealURI
) + ".diff/" + available_patches
[0].file
+ ".gz";
664 Desc
.Description
= Description
+ " " + available_patches
[0].file
+ string(".pdiff");
665 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
666 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
669 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
676 void pkgAcqIndexDiffs::Done(string Message
,unsigned long long Size
,string Md5Hash
, /*{{{*/
677 pkgAcquire::MethodConfig
*Cnf
)
680 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
682 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
685 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
687 // sucess in downloading a diff, enter ApplyDiff state
688 if(State
== StateFetchDiff
)
691 // rred excepts the patch as $FinalFile.ed
692 Rename(DestFile
,FinalFile
+".ed");
695 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
697 State
= StateApplyDiff
;
699 Desc
.URI
= "rred:" + FinalFile
;
706 // success in download/apply a diff, queue next (if needed)
707 if(State
== StateApplyDiff
)
709 // remove the just applied patch
710 available_patches
.erase(available_patches
.begin());
715 std::clog
<< "Moving patched file in place: " << std::endl
716 << DestFile
<< " -> " << FinalFile
<< std::endl
;
718 Rename(DestFile
,FinalFile
);
719 chmod(FinalFile
.c_str(),0644);
721 // see if there is more to download
722 if(available_patches
.empty() == false) {
723 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
724 ExpectedHash
, ServerSha1
, available_patches
);
731 // AcqIndex::AcqIndex - Constructor /*{{{*/
732 // ---------------------------------------------------------------------
733 /* The package file is added to the queue and a second class is
734 instantiated to fetch the revision file */
735 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
736 string URI
,string URIDesc
,string ShortDesc
,
737 HashString ExpectedHash
, string comprExt
)
738 : Item(Owner
), RealURI(URI
), ExpectedHash(ExpectedHash
)
740 if(comprExt
.empty() == true)
742 // autoselect the compression method
743 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
744 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
745 comprExt
.append(*t
).append(" ");
746 if (comprExt
.empty() == false)
747 comprExt
.erase(comprExt
.end()-1);
749 CompressionExtension
= comprExt
;
751 Init(URI
, URIDesc
, ShortDesc
);
753 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
, IndexTarget
const *Target
,
754 HashString
const &ExpectedHash
, indexRecords
const *MetaIndexParser
)
755 : Item(Owner
), RealURI(Target
->URI
), ExpectedHash(ExpectedHash
)
757 // autoselect the compression method
758 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
759 CompressionExtension
= "";
760 if (ExpectedHash
.empty() == false)
762 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
763 if (*t
== "uncompressed" || MetaIndexParser
->Exists(string(Target
->MetaKey
).append(".").append(*t
)) == true)
764 CompressionExtension
.append(*t
).append(" ");
768 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
769 CompressionExtension
.append(*t
).append(" ");
771 if (CompressionExtension
.empty() == false)
772 CompressionExtension
.erase(CompressionExtension
.end()-1);
774 // only verify non-optional targets, see acquire-item.h for a FIXME
775 // to make this more flexible
776 if (Target
->IsOptional())
781 Init(Target
->URI
, Target
->Description
, Target
->ShortDesc
);
784 // AcqIndex::Init - defered Constructor /*{{{*/
785 void pkgAcqIndex::Init(string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
) {
786 Decompression
= false;
789 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
790 DestFile
+= URItoFileName(URI
);
792 std::string
const comprExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
793 if (comprExt
== "uncompressed")
796 Desc
.URI
= URI
+ '.' + comprExt
;
798 Desc
.Description
= URIDesc
;
800 Desc
.ShortDesc
= ShortDesc
;
805 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
806 // ---------------------------------------------------------------------
807 /* The only header we use is the last-modified header. */
808 string
pkgAcqIndex::Custom600Headers()
810 string Final
= _config
->FindDir("Dir::State::lists");
811 Final
+= URItoFileName(RealURI
);
812 if (_config
->FindB("Acquire::GzipIndexes",false))
815 string msg
= "\nIndex-File: true";
816 // FIXME: this really should use "IndexTarget::IsOptional()" but that
817 // seems to be difficult without breaking ABI
818 if (ShortDesc().find("Translation") != 0)
819 msg
+= "\nFail-Ignore: true";
821 if (stat(Final
.c_str(),&Buf
) == 0)
822 msg
+= "\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
827 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
829 size_t const nextExt
= CompressionExtension
.find(' ');
830 if (nextExt
!= std::string::npos
)
832 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
833 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
837 // on decompression failure, remove bad versions in partial/
838 if (Decompression
&& Erase
) {
839 string s
= _config
->FindDir("Dir::State::lists") + "partial/";
840 s
.append(URItoFileName(RealURI
));
844 Item::Failed(Message
,Cnf
);
847 // AcqIndex::Done - Finished a fetch /*{{{*/
848 // ---------------------------------------------------------------------
849 /* This goes through a number of states.. On the initial fetch the
850 method could possibly return an alternate filename which points
851 to the uncompressed version of the file. If this is so the file
852 is copied into the partial directory. In all other cases the file
853 is decompressed with a gzip uri. */
854 void pkgAcqIndex::Done(string Message
,unsigned long long Size
,string Hash
,
855 pkgAcquire::MethodConfig
*Cfg
)
857 Item::Done(Message
,Size
,Hash
,Cfg
);
859 if (Decompression
== true)
861 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
863 std::cerr
<< std::endl
<< RealURI
<< ": Computed Hash: " << Hash
;
864 std::cerr
<< " Expected Hash: " << ExpectedHash
.toStr() << std::endl
;
867 if (!ExpectedHash
.empty() && ExpectedHash
.toStr() != Hash
)
869 Status
= StatAuthError
;
870 ErrorText
= _("Hash Sum mismatch");
871 Rename(DestFile
,DestFile
+ ".FAILED");
872 ReportMirrorFailure("HashChecksumFailure");
876 /* Verify the index file for correctness (all indexes must
877 * have a Package field) (LP: #346386) (Closes: #627642) */
880 FileFd
fd(DestFile
, FileFd::ReadOnly
);
884 // Only test for correctness if the file is not empty (empty is ok)
886 if (_error
->PendingError() || !tag
.Step(sec
)) {
888 _error
->DumpErrors();
889 Rename(DestFile
,DestFile
+ ".FAILED");
891 } else if (!sec
.Exists("Package")) {
893 ErrorText
= ("Encountered a section with no Package: header");
894 Rename(DestFile
,DestFile
+ ".FAILED");
900 // Done, move it into position
901 string FinalFile
= _config
->FindDir("Dir::State::lists");
902 FinalFile
+= URItoFileName(RealURI
);
903 Rename(DestFile
,FinalFile
);
904 chmod(FinalFile
.c_str(),0644);
906 /* We restore the original name to DestFile so that the clean operation
908 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
909 DestFile
+= URItoFileName(RealURI
);
911 // Remove the compressed version.
913 unlink(DestFile
.c_str());
920 // Handle the unzipd case
921 string FileName
= LookupTag(Message
,"Alt-Filename");
922 if (FileName
.empty() == false)
924 // The files timestamp matches
925 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
927 Decompression
= true;
929 DestFile
+= ".decomp";
930 Desc
.URI
= "copy:" + FileName
;
936 FileName
= LookupTag(Message
,"Filename");
937 if (FileName
.empty() == true)
940 ErrorText
= "Method gave a blank filename";
943 std::string
const compExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
945 // The files timestamp matches
946 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true) {
947 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz")
948 // Update DestFile for .gz suffix so that the clean operation keeps it
953 if (FileName
== DestFile
)
960 // If we enable compressed indexes and already have gzip, keep it
961 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz" && !Local
) {
962 string FinalFile
= _config
->FindDir("Dir::State::lists");
963 FinalFile
+= URItoFileName(RealURI
) + ".gz";
964 Rename(DestFile
,FinalFile
);
965 chmod(FinalFile
.c_str(),0644);
967 // Update DestFile for .gz suffix so that the clean operation keeps it
968 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
969 DestFile
+= URItoFileName(RealURI
) + ".gz";
973 // get the binary name for your used compression type
974 decompProg
= _config
->Find(string("Acquire::CompressionTypes::").append(compExt
),"");
975 if(decompProg
.empty() == false);
976 else if(compExt
== "uncompressed")
979 _error
->Error("Unsupported extension: %s", compExt
.c_str());
983 Decompression
= true;
984 DestFile
+= ".decomp";
985 Desc
.URI
= decompProg
+ ":" + FileName
;
988 // FIXME: this points to a c++ string that goes out of scope
989 Mode
= decompProg
.c_str();
992 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
993 // ---------------------------------------------------------------------
994 /* The Translation file is added to the queue */
995 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
996 string URI
,string URIDesc
,string ShortDesc
)
997 : pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, HashString(), "")
1000 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
, IndexTarget
const *Target
,
1001 HashString
const &ExpectedHash
, indexRecords
const *MetaIndexParser
)
1002 : pkgAcqIndex(Owner
, Target
, ExpectedHash
, MetaIndexParser
)
1006 // AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
1007 // ---------------------------------------------------------------------
1008 string
pkgAcqIndexTrans::Custom600Headers()
1010 string Final
= _config
->FindDir("Dir::State::lists");
1011 Final
+= URItoFileName(RealURI
);
1014 if (stat(Final
.c_str(),&Buf
) != 0)
1015 return "\nFail-Ignore: true\nIndex-File: true";
1016 return "\nFail-Ignore: true\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1019 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
1020 // ---------------------------------------------------------------------
1022 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1024 size_t const nextExt
= CompressionExtension
.find(' ');
1025 if (nextExt
!= std::string::npos
)
1027 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
1028 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
1033 if (Cnf
->LocalOnly
== true ||
1034 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1043 Item::Failed(Message
,Cnf
);
1046 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
, /*{{{*/
1047 string URI
,string URIDesc
,string ShortDesc
,
1048 string MetaIndexURI
, string MetaIndexURIDesc
,
1049 string MetaIndexShortDesc
,
1050 const vector
<IndexTarget
*>* IndexTargets
,
1051 indexRecords
* MetaIndexParser
) :
1052 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
1053 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
1054 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
1056 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1057 DestFile
+= URItoFileName(URI
);
1059 // remove any partial downloaded sig-file in partial/.
1060 // it may confuse proxies and is too small to warrant a
1061 // partial download anyway
1062 unlink(DestFile
.c_str());
1065 Desc
.Description
= URIDesc
;
1067 Desc
.ShortDesc
= ShortDesc
;
1070 string Final
= _config
->FindDir("Dir::State::lists");
1071 Final
+= URItoFileName(RealURI
);
1072 if (RealFileExists(Final
) == true)
1074 // File was already in place. It needs to be re-downloaded/verified
1075 // because Release might have changed, we do give it a differnt
1076 // name than DestFile because otherwise the http method will
1077 // send If-Range requests and there are too many broken servers
1078 // out there that do not understand them
1079 LastGoodSig
= DestFile
+".reverify";
1080 Rename(Final
,LastGoodSig
);
1086 pkgAcqMetaSig::~pkgAcqMetaSig() /*{{{*/
1088 // if the file was never queued undo file-changes done in the constructor
1089 if (QueueCounter
== 1 && Status
== StatIdle
&& FileSize
== 0 && Complete
== false &&
1090 LastGoodSig
.empty() == false)
1092 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1093 if (RealFileExists(Final
) == false && RealFileExists(LastGoodSig
) == true)
1094 Rename(LastGoodSig
, Final
);
1099 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
1100 // ---------------------------------------------------------------------
1101 /* The only header we use is the last-modified header. */
1102 string
pkgAcqMetaSig::Custom600Headers()
1105 if (stat(LastGoodSig
.c_str(),&Buf
) != 0)
1106 return "\nIndex-File: true";
1108 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1111 void pkgAcqMetaSig::Done(string Message
,unsigned long long Size
,string MD5
,
1112 pkgAcquire::MethodConfig
*Cfg
)
1114 Item::Done(Message
,Size
,MD5
,Cfg
);
1116 string FileName
= LookupTag(Message
,"Filename");
1117 if (FileName
.empty() == true)
1120 ErrorText
= "Method gave a blank filename";
1124 if (FileName
!= DestFile
)
1126 // We have to copy it into place
1128 Desc
.URI
= "copy:" + FileName
;
1135 // put the last known good file back on i-m-s hit (it will
1136 // be re-verified again)
1137 // Else do nothing, we have the new file in DestFile then
1138 if(StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1139 Rename(LastGoodSig
, DestFile
);
1141 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
1142 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
,
1143 MetaIndexShortDesc
, DestFile
, IndexTargets
,
1148 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)/*{{{*/
1150 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1152 // if we get a network error we fail gracefully
1153 if(Status
== StatTransientNetworkError
)
1155 Item::Failed(Message
,Cnf
);
1156 // move the sigfile back on transient network failures
1157 if(FileExists(LastGoodSig
))
1158 Rename(LastGoodSig
,Final
);
1160 // set the status back to , Item::Failed likes to reset it
1161 Status
= pkgAcquire::Item::StatTransientNetworkError
;
1165 // Delete any existing sigfile when the acquire failed
1166 unlink(Final
.c_str());
1168 // queue a pkgAcqMetaIndex with no sigfile
1169 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
1170 "", IndexTargets
, MetaIndexParser
);
1172 if (Cnf
->LocalOnly
== true ||
1173 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1182 Item::Failed(Message
,Cnf
);
1185 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
, /*{{{*/
1186 string URI
,string URIDesc
,string ShortDesc
,
1188 const vector
<struct IndexTarget
*>* IndexTargets
,
1189 indexRecords
* MetaIndexParser
) :
1190 Item(Owner
), RealURI(URI
), SigFile(SigFile
), IndexTargets(IndexTargets
),
1191 MetaIndexParser(MetaIndexParser
), AuthPass(false), IMSHit(false)
1193 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1194 DestFile
+= URItoFileName(URI
);
1197 Desc
.Description
= URIDesc
;
1199 Desc
.ShortDesc
= ShortDesc
;
1205 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
1206 // ---------------------------------------------------------------------
1207 /* The only header we use is the last-modified header. */
1208 string
pkgAcqMetaIndex::Custom600Headers()
1210 string Final
= _config
->FindDir("Dir::State::lists");
1211 Final
+= URItoFileName(RealURI
);
1214 if (stat(Final
.c_str(),&Buf
) != 0)
1215 return "\nIndex-File: true";
1217 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1220 void pkgAcqMetaIndex::Done(string Message
,unsigned long long Size
,string Hash
, /*{{{*/
1221 pkgAcquire::MethodConfig
*Cfg
)
1223 Item::Done(Message
,Size
,Hash
,Cfg
);
1225 // MetaIndexes are done in two passes: one to download the
1226 // metaindex with an appropriate method, and a second to verify it
1227 // with the gpgv method
1229 if (AuthPass
== true)
1233 // all cool, move Release file into place
1238 RetrievalDone(Message
);
1240 // Still more retrieving to do
1245 // There was no signature file, so we are finished. Download
1246 // the indexes and do only hashsum verification if possible
1247 MetaIndexParser
->Load(DestFile
);
1248 QueueIndexes(false);
1252 // There was a signature file, so pass it to gpgv for
1255 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1256 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
1257 << SigFile
<< "," << DestFile
<< ")\n";
1259 Desc
.URI
= "gpgv:" + SigFile
;
1266 if (Complete
== true)
1268 string FinalFile
= _config
->FindDir("Dir::State::lists");
1269 FinalFile
+= URItoFileName(RealURI
);
1270 if (SigFile
== DestFile
)
1271 SigFile
= FinalFile
;
1272 Rename(DestFile
,FinalFile
);
1273 chmod(FinalFile
.c_str(),0644);
1274 DestFile
= FinalFile
;
1278 void pkgAcqMetaIndex::RetrievalDone(string Message
) /*{{{*/
1280 // We have just finished downloading a Release file (it is not
1283 string FileName
= LookupTag(Message
,"Filename");
1284 if (FileName
.empty() == true)
1287 ErrorText
= "Method gave a blank filename";
1291 if (FileName
!= DestFile
)
1294 Desc
.URI
= "copy:" + FileName
;
1299 // make sure to verify against the right file on I-M-S hit
1300 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
1303 string FinalFile
= _config
->FindDir("Dir::State::lists");
1304 FinalFile
+= URItoFileName(RealURI
);
1305 if (SigFile
== DestFile
)
1307 SigFile
= FinalFile
;
1308 // constructor of pkgAcqMetaClearSig moved it out of the way,
1309 // now move it back in on IMS hit for the 'old' file
1310 string
const OldClearSig
= DestFile
+ ".reverify";
1311 if (RealFileExists(OldClearSig
) == true)
1312 Rename(OldClearSig
, FinalFile
);
1314 DestFile
= FinalFile
;
1319 void pkgAcqMetaIndex::AuthDone(string Message
) /*{{{*/
1321 // At this point, the gpgv method has succeeded, so there is a
1322 // valid signature from a key in the trusted keyring. We
1323 // perform additional verification of its contents, and use them
1324 // to verify the indexes we are about to download
1326 if (!MetaIndexParser
->Load(DestFile
))
1328 Status
= StatAuthError
;
1329 ErrorText
= MetaIndexParser
->ErrorText
;
1333 if (!VerifyVendor(Message
))
1338 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1339 std::cerr
<< "Signature verification succeeded: "
1340 << DestFile
<< std::endl
;
1342 // Download further indexes with verification
1345 // is it a clearsigned MetaIndex file?
1346 if (DestFile
== SigFile
)
1349 // Done, move signature file into position
1350 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1351 URItoFileName(RealURI
) + ".gpg";
1352 Rename(SigFile
,VerifiedSigFile
);
1353 chmod(VerifiedSigFile
.c_str(),0644);
1356 void pkgAcqMetaIndex::QueueIndexes(bool verify
) /*{{{*/
1359 /* Reject invalid, existing Release files (LP: #346386) (Closes: #627642)
1360 * FIXME: Disabled; it breaks unsigned repositories without hashes */
1361 if (!verify
&& FileExists(DestFile
) && !MetaIndexParser
->Load(DestFile
))
1364 ErrorText
= MetaIndexParser
->ErrorText
;
1368 bool transInRelease
= false;
1370 std::vector
<std::string
> const keys
= MetaIndexParser
->MetaKeys();
1371 for (std::vector
<std::string
>::const_iterator k
= keys
.begin(); k
!= keys
.end(); ++k
)
1372 // FIXME: Feels wrong to check for hardcoded string here, but what should we do else…
1373 if (k
->find("Translation-") != std::string::npos
)
1375 transInRelease
= true;
1380 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
1381 Target
!= IndexTargets
->end();
1384 HashString ExpectedIndexHash
;
1385 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1386 bool compressedAvailable
= false;
1389 if ((*Target
)->IsOptional() == true)
1391 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
1392 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
1393 if (MetaIndexParser
->Exists(string((*Target
)->MetaKey
).append(".").append(*t
)) == true)
1395 compressedAvailable
= true;
1399 else if (verify
== true)
1401 Status
= StatAuthError
;
1402 strprintf(ErrorText
, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), (*Target
)->MetaKey
.c_str());
1408 ExpectedIndexHash
= Record
->Hash
;
1409 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1411 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
1412 std::cerr
<< "Expected Hash: " << ExpectedIndexHash
.toStr() << std::endl
;
1413 std::cerr
<< "For: " << Record
->MetaKeyFilename
<< std::endl
;
1415 if (verify
== true && ExpectedIndexHash
.empty() == true && (*Target
)->IsOptional() == false)
1417 Status
= StatAuthError
;
1418 strprintf(ErrorText
, _("Unable to find hash sum for '%s' in Release file"), (*Target
)->MetaKey
.c_str());
1423 if ((*Target
)->IsOptional() == true)
1425 if ((*Target
)->IsSubIndex() == true)
1426 new pkgAcqSubIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1427 (*Target
)->ShortDesc
, ExpectedIndexHash
);
1428 else if (transInRelease
== false || Record
!= NULL
|| compressedAvailable
== true)
1430 if (_config
->FindB("Acquire::PDiffs",true) == true && transInRelease
== true &&
1431 MetaIndexParser
->Exists(string((*Target
)->MetaKey
).append(".diff/Index")) == true)
1432 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1433 (*Target
)->ShortDesc
, ExpectedIndexHash
);
1435 new pkgAcqIndexTrans(Owner
, *Target
, ExpectedIndexHash
, MetaIndexParser
);
1440 /* Queue Packages file (either diff or full packages files, depending
1441 on the users option) - we also check if the PDiff Index file is listed
1442 in the Meta-Index file. Ideal would be if pkgAcqDiffIndex would test this
1443 instead, but passing the required info to it is to much hassle */
1444 if(_config
->FindB("Acquire::PDiffs",true) == true && (verify
== false ||
1445 MetaIndexParser
->Exists(string((*Target
)->MetaKey
).append(".diff/Index")) == true))
1446 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1447 (*Target
)->ShortDesc
, ExpectedIndexHash
);
1449 new pkgAcqIndex(Owner
, *Target
, ExpectedIndexHash
, MetaIndexParser
);
1453 bool pkgAcqMetaIndex::VerifyVendor(string Message
) /*{{{*/
1455 string::size_type pos
;
1457 // check for missing sigs (that where not fatal because otherwise we had
1460 string msg
= _("There is no public key available for the "
1461 "following key IDs:\n");
1462 pos
= Message
.find("NO_PUBKEY ");
1463 if (pos
!= std::string::npos
)
1465 string::size_type start
= pos
+strlen("NO_PUBKEY ");
1466 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
1467 missingkeys
+= (Fingerprint
);
1469 if(!missingkeys
.empty())
1470 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
1472 string Transformed
= MetaIndexParser
->GetExpectedDist();
1474 if (Transformed
== "../project/experimental")
1476 Transformed
= "experimental";
1479 pos
= Transformed
.rfind('/');
1480 if (pos
!= string::npos
)
1482 Transformed
= Transformed
.substr(0, pos
);
1485 if (Transformed
== ".")
1490 if (_config
->FindB("Acquire::Check-Valid-Until", true) == true &&
1491 MetaIndexParser
->GetValidUntil() > 0) {
1492 time_t const invalid_since
= time(NULL
) - MetaIndexParser
->GetValidUntil();
1493 if (invalid_since
> 0)
1494 // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
1495 // the time since then the file is invalid - formated in the same way as in
1496 // the download progress display (e.g. 7d 3h 42min 1s)
1497 return _error
->Error(
1498 _("Release file for %s is expired (invalid since %s). "
1499 "Updates for this repository will not be applied."),
1500 RealURI
.c_str(), TimeToStr(invalid_since
).c_str());
1503 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1505 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1506 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1507 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1510 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1512 // This might become fatal one day
1513 // Status = StatAuthError;
1514 // ErrorText = "Conflicting distribution; expected "
1515 // + MetaIndexParser->GetExpectedDist() + " but got "
1516 // + MetaIndexParser->GetDist();
1518 if (!Transformed
.empty())
1520 _error
->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
1521 Desc
.Description
.c_str(),
1522 Transformed
.c_str(),
1523 MetaIndexParser
->GetDist().c_str());
1530 // pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
1531 // ---------------------------------------------------------------------
1533 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1535 if (AuthPass
== true)
1537 // gpgv method failed, if we have a good signature
1538 string LastGoodSigFile
= _config
->FindDir("Dir::State::lists").append("partial/").append(URItoFileName(RealURI
));
1539 if (DestFile
!= SigFile
)
1540 LastGoodSigFile
.append(".gpg");
1541 LastGoodSigFile
.append(".reverify");
1543 if(FileExists(LastGoodSigFile
))
1545 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1546 if (DestFile
!= SigFile
)
1547 VerifiedSigFile
.append(".gpg");
1548 Rename(LastGoodSigFile
, VerifiedSigFile
);
1549 Status
= StatTransientNetworkError
;
1550 _error
->Warning(_("An error occurred during the signature "
1551 "verification. The repository is not updated "
1552 "and the previous index files will be used. "
1553 "GPG error: %s: %s\n"),
1554 Desc
.Description
.c_str(),
1555 LookupTag(Message
,"Message").c_str());
1556 RunScripts("APT::Update::Auth-Failure");
1558 } else if (LookupTag(Message
,"Message").find("NODATA") != string::npos
) {
1559 /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */
1560 _error
->Error(_("GPG error: %s: %s"),
1561 Desc
.Description
.c_str(),
1562 LookupTag(Message
,"Message").c_str());
1565 _error
->Warning(_("GPG error: %s: %s"),
1566 Desc
.Description
.c_str(),
1567 LookupTag(Message
,"Message").c_str());
1569 // gpgv method failed
1570 ReportMirrorFailure("GPGFailure");
1573 /* Always move the meta index, even if gpgv failed. This ensures
1574 * that PackageFile objects are correctly filled in */
1575 if (FileExists(DestFile
)) {
1576 string FinalFile
= _config
->FindDir("Dir::State::lists");
1577 FinalFile
+= URItoFileName(RealURI
);
1578 /* InRelease files become Release files, otherwise
1579 * they would be considered as trusted later on */
1580 if (SigFile
== DestFile
) {
1581 RealURI
= RealURI
.replace(RealURI
.rfind("InRelease"), 9,
1583 FinalFile
= FinalFile
.replace(FinalFile
.rfind("InRelease"), 9,
1585 SigFile
= FinalFile
;
1587 Rename(DestFile
,FinalFile
);
1588 chmod(FinalFile
.c_str(),0644);
1590 DestFile
= FinalFile
;
1593 // No Release file was present, or verification failed, so fall
1594 // back to queueing Packages files without verification
1595 QueueIndexes(false);
1598 pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire
*Owner
, /*{{{*/
1599 string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
,
1600 string
const &MetaIndexURI
, string
const &MetaIndexURIDesc
, string
const &MetaIndexShortDesc
,
1601 string
const &MetaSigURI
, string
const &MetaSigURIDesc
, string
const &MetaSigShortDesc
,
1602 const vector
<struct IndexTarget
*>* IndexTargets
,
1603 indexRecords
* MetaIndexParser
) :
1604 pkgAcqMetaIndex(Owner
, URI
, URIDesc
, ShortDesc
, "", IndexTargets
, MetaIndexParser
),
1605 MetaIndexURI(MetaIndexURI
), MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
1606 MetaSigURI(MetaSigURI
), MetaSigURIDesc(MetaSigURIDesc
), MetaSigShortDesc(MetaSigShortDesc
)
1610 // keep the old InRelease around in case of transistent network errors
1611 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1612 if (RealFileExists(Final
) == true)
1614 string
const LastGoodSig
= DestFile
+ ".reverify";
1615 Rename(Final
,LastGoodSig
);
1619 pkgAcqMetaClearSig::~pkgAcqMetaClearSig() /*{{{*/
1621 // if the file was never queued undo file-changes done in the constructor
1622 if (QueueCounter
== 1 && Status
== StatIdle
&& FileSize
== 0 && Complete
== false)
1624 string
const Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1625 string
const LastGoodSig
= DestFile
+ ".reverify";
1626 if (RealFileExists(Final
) == false && RealFileExists(LastGoodSig
) == true)
1627 Rename(LastGoodSig
, Final
);
1631 // pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
1632 // ---------------------------------------------------------------------
1633 // FIXME: this can go away once the InRelease file is used widely
1634 string
pkgAcqMetaClearSig::Custom600Headers()
1636 string Final
= _config
->FindDir("Dir::State::lists");
1637 Final
+= URItoFileName(RealURI
);
1640 if (stat(Final
.c_str(),&Buf
) != 0)
1642 Final
= DestFile
+ ".reverify";
1643 if (stat(Final
.c_str(),&Buf
) != 0)
1644 return "\nIndex-File: true\nFail-Ignore: true\n";
1647 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1650 void pkgAcqMetaClearSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
1652 if (AuthPass
== false)
1654 // Remove the 'old' InRelease file if we try Release.gpg now as otherwise
1655 // the file will stay around and gives a false-auth impression (CVE-2012-0214)
1656 string FinalFile
= _config
->FindDir("Dir::State::lists");
1657 FinalFile
.append(URItoFileName(RealURI
));
1658 if (FileExists(FinalFile
))
1659 unlink(FinalFile
.c_str());
1661 new pkgAcqMetaSig(Owner
,
1662 MetaSigURI
, MetaSigURIDesc
, MetaSigShortDesc
,
1663 MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
1664 IndexTargets
, MetaIndexParser
);
1665 if (Cnf
->LocalOnly
== true ||
1666 StringToBool(LookupTag(Message
, "Transient-Failure"), false) == false)
1670 pkgAcqMetaIndex::Failed(Message
, Cnf
);
1673 // AcqArchive::AcqArchive - Constructor /*{{{*/
1674 // ---------------------------------------------------------------------
1675 /* This just sets up the initial fetch environment and queues the first
1677 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1678 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1679 string
&StoreFilename
) :
1680 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1681 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1684 Retries
= _config
->FindI("Acquire::Retries",0);
1686 if (Version
.Arch() == 0)
1688 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1689 "This might mean you need to manually fix this package. "
1690 "(due to missing arch)"),
1691 Version
.ParentPkg().Name());
1695 /* We need to find a filename to determine the extension. We make the
1696 assumption here that all the available sources for this version share
1697 the same extension.. */
1698 // Skip not source sources, they do not have file fields.
1699 for (; Vf
.end() == false; ++Vf
)
1701 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1706 // Does not really matter here.. we are going to fail out below
1707 if (Vf
.end() != true)
1709 // If this fails to get a file name we will bomb out below.
1710 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1711 if (_error
->PendingError() == true)
1714 // Generate the final file name as: package_version_arch.foo
1715 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1716 QuoteString(Version
.VerStr(),"_:") + '_' +
1717 QuoteString(Version
.Arch(),"_:.") +
1718 "." + flExtension(Parse
.FileName());
1721 // check if we have one trusted source for the package. if so, switch
1722 // to "TrustedOnly" mode - but only if not in AllowUnauthenticated mode
1723 bool const allowUnauth
= _config
->FindB("APT::Get::AllowUnauthenticated", false);
1724 bool const debugAuth
= _config
->FindB("Debug::pkgAcquire::Auth", false);
1725 bool seenUntrusted
= false;
1726 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; ++i
)
1728 pkgIndexFile
*Index
;
1729 if (Sources
->FindIndex(i
.File(),Index
) == false)
1732 if (debugAuth
== true)
1733 std::cerr
<< "Checking index: " << Index
->Describe()
1734 << "(Trusted=" << Index
->IsTrusted() << ")" << std::endl
;
1736 if (Index
->IsTrusted() == true)
1739 if (allowUnauth
== false)
1743 seenUntrusted
= true;
1746 // "allow-unauthenticated" restores apts old fetching behaviour
1747 // that means that e.g. unauthenticated file:// uris are higher
1748 // priority than authenticated http:// uris
1749 if (allowUnauth
== true && seenUntrusted
== true)
1753 if (QueueNext() == false && _error
->PendingError() == false)
1754 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1755 "This might mean you need to manually fix this package."),
1756 Version
.ParentPkg().Name());
1759 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1760 // ---------------------------------------------------------------------
1761 /* This queues the next available file version for download. It checks if
1762 the archive is already available in the cache and stashs the MD5 for
1764 bool pkgAcqArchive::QueueNext()
1766 string
const ForceHash
= _config
->Find("Acquire::ForceHash");
1767 for (; Vf
.end() == false; ++Vf
)
1769 // Ignore not source sources
1770 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1773 // Try to cross match against the source list
1774 pkgIndexFile
*Index
;
1775 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1778 // only try to get a trusted package from another source if that source
1780 if(Trusted
&& !Index
->IsTrusted())
1783 // Grab the text package record
1784 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1785 if (_error
->PendingError() == true)
1788 string PkgFile
= Parse
.FileName();
1789 if (ForceHash
.empty() == false)
1791 if(stringcasecmp(ForceHash
, "sha512") == 0)
1792 ExpectedHash
= HashString("SHA512", Parse
.SHA512Hash());
1793 else if(stringcasecmp(ForceHash
, "sha256") == 0)
1794 ExpectedHash
= HashString("SHA256", Parse
.SHA256Hash());
1795 else if (stringcasecmp(ForceHash
, "sha1") == 0)
1796 ExpectedHash
= HashString("SHA1", Parse
.SHA1Hash());
1798 ExpectedHash
= HashString("MD5Sum", Parse
.MD5Hash());
1803 if ((Hash
= Parse
.SHA512Hash()).empty() == false)
1804 ExpectedHash
= HashString("SHA512", Hash
);
1805 else if ((Hash
= Parse
.SHA256Hash()).empty() == false)
1806 ExpectedHash
= HashString("SHA256", Hash
);
1807 else if ((Hash
= Parse
.SHA1Hash()).empty() == false)
1808 ExpectedHash
= HashString("SHA1", Hash
);
1810 ExpectedHash
= HashString("MD5Sum", Parse
.MD5Hash());
1812 if (PkgFile
.empty() == true)
1813 return _error
->Error(_("The package index files are corrupted. No Filename: "
1814 "field for package %s."),
1815 Version
.ParentPkg().Name());
1817 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1818 Desc
.Description
= Index
->ArchiveInfo(Version
);
1820 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1822 // See if we already have the file. (Legacy filenames)
1823 FileSize
= Version
->Size
;
1824 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1826 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1828 // Make sure the size matches
1829 if ((unsigned long long)Buf
.st_size
== Version
->Size
)
1834 StoreFilename
= DestFile
= FinalFile
;
1838 /* Hmm, we have a file and its size does not match, this means it is
1839 an old style mismatched arch */
1840 unlink(FinalFile
.c_str());
1843 // Check it again using the new style output filenames
1844 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1845 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1847 // Make sure the size matches
1848 if ((unsigned long long)Buf
.st_size
== Version
->Size
)
1853 StoreFilename
= DestFile
= FinalFile
;
1857 /* Hmm, we have a file and its size does not match, this shouldnt
1859 unlink(FinalFile
.c_str());
1862 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1864 // Check the destination file
1865 if (stat(DestFile
.c_str(),&Buf
) == 0)
1867 // Hmm, the partial file is too big, erase it
1868 if ((unsigned long long)Buf
.st_size
> Version
->Size
)
1869 unlink(DestFile
.c_str());
1871 PartialSize
= Buf
.st_size
;
1874 // Disables download of archives - useful if no real installation follows,
1875 // e.g. if we are just interested in proposed installation order
1876 if (_config
->FindB("Debug::pkgAcqArchive::NoQueue", false) == true)
1881 StoreFilename
= DestFile
= FinalFile
;
1887 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1888 Desc
.Description
= Index
->ArchiveInfo(Version
);
1890 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1899 // AcqArchive::Done - Finished fetching /*{{{*/
1900 // ---------------------------------------------------------------------
1902 void pkgAcqArchive::Done(string Message
,unsigned long long Size
,string CalcHash
,
1903 pkgAcquire::MethodConfig
*Cfg
)
1905 Item::Done(Message
,Size
,CalcHash
,Cfg
);
1908 if (Size
!= Version
->Size
)
1911 ErrorText
= _("Size mismatch");
1916 if(ExpectedHash
.toStr() != CalcHash
)
1919 ErrorText
= _("Hash Sum mismatch");
1920 if(FileExists(DestFile
))
1921 Rename(DestFile
,DestFile
+ ".FAILED");
1925 // Grab the output filename
1926 string FileName
= LookupTag(Message
,"Filename");
1927 if (FileName
.empty() == true)
1930 ErrorText
= "Method gave a blank filename";
1936 // Reference filename
1937 if (FileName
!= DestFile
)
1939 StoreFilename
= DestFile
= FileName
;
1944 // Done, move it into position
1945 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1946 FinalFile
+= flNotDir(StoreFilename
);
1947 Rename(DestFile
,FinalFile
);
1949 StoreFilename
= DestFile
= FinalFile
;
1953 // AcqArchive::Failed - Failure handler /*{{{*/
1954 // ---------------------------------------------------------------------
1955 /* Here we try other sources */
1956 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1958 ErrorText
= LookupTag(Message
,"Message");
1960 /* We don't really want to retry on failed media swaps, this prevents
1961 that. An interesting observation is that permanent failures are not
1963 if (Cnf
->Removable
== true &&
1964 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1966 // Vf = Version.FileList();
1967 while (Vf
.end() == false) ++Vf
;
1968 StoreFilename
= string();
1969 Item::Failed(Message
,Cnf
);
1973 if (QueueNext() == false)
1975 // This is the retry counter
1977 Cnf
->LocalOnly
== false &&
1978 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1981 Vf
= Version
.FileList();
1982 if (QueueNext() == true)
1986 StoreFilename
= string();
1987 Item::Failed(Message
,Cnf
);
1991 // AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
1992 // ---------------------------------------------------------------------
1993 bool pkgAcqArchive::IsTrusted()
1998 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1999 // ---------------------------------------------------------------------
2001 void pkgAcqArchive::Finished()
2003 if (Status
== pkgAcquire::Item::StatDone
&&
2006 StoreFilename
= string();
2009 // AcqFile::pkgAcqFile - Constructor /*{{{*/
2010 // ---------------------------------------------------------------------
2011 /* The file is added to the queue */
2012 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string Hash
,
2013 unsigned long long Size
,string Dsc
,string ShortDesc
,
2014 const string
&DestDir
, const string
&DestFilename
,
2016 Item(Owner
), ExpectedHash(Hash
), IsIndexFile(IsIndexFile
)
2018 Retries
= _config
->FindI("Acquire::Retries",0);
2020 if(!DestFilename
.empty())
2021 DestFile
= DestFilename
;
2022 else if(!DestDir
.empty())
2023 DestFile
= DestDir
+ "/" + flNotDir(URI
);
2025 DestFile
= flNotDir(URI
);
2029 Desc
.Description
= Dsc
;
2032 // Set the short description to the archive component
2033 Desc
.ShortDesc
= ShortDesc
;
2035 // Get the transfer sizes
2038 if (stat(DestFile
.c_str(),&Buf
) == 0)
2040 // Hmm, the partial file is too big, erase it
2041 if ((unsigned long long)Buf
.st_size
> Size
)
2042 unlink(DestFile
.c_str());
2044 PartialSize
= Buf
.st_size
;
2050 // AcqFile::Done - Item downloaded OK /*{{{*/
2051 // ---------------------------------------------------------------------
2053 void pkgAcqFile::Done(string Message
,unsigned long long Size
,string CalcHash
,
2054 pkgAcquire::MethodConfig
*Cnf
)
2056 Item::Done(Message
,Size
,CalcHash
,Cnf
);
2059 if(!ExpectedHash
.empty() && ExpectedHash
.toStr() != CalcHash
)
2062 ErrorText
= _("Hash Sum mismatch");
2063 Rename(DestFile
,DestFile
+ ".FAILED");
2067 string FileName
= LookupTag(Message
,"Filename");
2068 if (FileName
.empty() == true)
2071 ErrorText
= "Method gave a blank filename";
2077 // The files timestamp matches
2078 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
2081 // We have to copy it into place
2082 if (FileName
!= DestFile
)
2085 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
2086 Cnf
->Removable
== true)
2088 Desc
.URI
= "copy:" + FileName
;
2093 // Erase the file if it is a symlink so we can overwrite it
2095 if (lstat(DestFile
.c_str(),&St
) == 0)
2097 if (S_ISLNK(St
.st_mode
) != 0)
2098 unlink(DestFile
.c_str());
2102 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
2104 ErrorText
= "Link to " + DestFile
+ " failure ";
2111 // AcqFile::Failed - Failure handler /*{{{*/
2112 // ---------------------------------------------------------------------
2113 /* Here we try other sources */
2114 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
2116 ErrorText
= LookupTag(Message
,"Message");
2118 // This is the retry counter
2120 Cnf
->LocalOnly
== false &&
2121 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2128 Item::Failed(Message
,Cnf
);
2131 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
2132 // ---------------------------------------------------------------------
2133 /* The only header we use is the last-modified header. */
2134 string
pkgAcqFile::Custom600Headers()
2137 return "\nIndex-File: true";