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 /*{{{*/
16 #include <apt-pkg/acquire-item.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/aptconfiguration.h>
19 #include <apt-pkg/sourcelist.h>
20 #include <apt-pkg/vendorlist.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/fileutl.h>
24 #include <apt-pkg/md5.h>
25 #include <apt-pkg/sha1.h>
26 #include <apt-pkg/tagfile.h>
41 // Acquire::Item::Item - Constructor /*{{{*/
42 // ---------------------------------------------------------------------
44 pkgAcquire::Item::Item(pkgAcquire
*Owner
) : Owner(Owner
), FileSize(0),
45 PartialSize(0), Mode(0), ID(0), Complete(false),
46 Local(false), QueueCounter(0)
52 // Acquire::Item::~Item - Destructor /*{{{*/
53 // ---------------------------------------------------------------------
55 pkgAcquire::Item::~Item()
60 // Acquire::Item::Failed - Item failed to download /*{{{*/
61 // ---------------------------------------------------------------------
62 /* We return to an idle state if there are still other queues that could
64 void pkgAcquire::Item::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
67 ErrorText
= LookupTag(Message
,"Message");
68 UsedMirror
= LookupTag(Message
,"UsedMirror");
69 if (QueueCounter
<= 1)
71 /* This indicates that the file is not available right now but might
72 be sometime later. If we do a retry cycle then this should be
74 if (Cnf
->LocalOnly
== true &&
75 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
86 // report mirror failure back to LP if we actually use a mirror
87 string FailReason
= LookupTag(Message
, "FailReason");
88 if(FailReason
.size() != 0)
89 ReportMirrorFailure(FailReason
);
91 ReportMirrorFailure(ErrorText
);
94 // Acquire::Item::Start - Item has begun to download /*{{{*/
95 // ---------------------------------------------------------------------
96 /* Stash status and the file size. Note that setting Complete means
97 sub-phases of the acquire process such as decompresion are operating */
98 void pkgAcquire::Item::Start(string
/*Message*/,unsigned long Size
)
100 Status
= StatFetching
;
101 if (FileSize
== 0 && Complete
== false)
105 // Acquire::Item::Done - Item downloaded OK /*{{{*/
106 // ---------------------------------------------------------------------
108 void pkgAcquire::Item::Done(string Message
,unsigned long Size
,string Hash
,
109 pkgAcquire::MethodConfig
*Cnf
)
111 // We just downloaded something..
112 string FileName
= LookupTag(Message
,"Filename");
113 UsedMirror
= LookupTag(Message
,"UsedMirror");
114 if (Complete
== false && !Local
&& FileName
== DestFile
)
117 Owner
->Log
->Fetched(Size
,atoi(LookupTag(Message
,"Resume-Point","0").c_str()));
123 ErrorText
= string();
124 Owner
->Dequeue(this);
127 // Acquire::Item::Rename - Rename a file /*{{{*/
128 // ---------------------------------------------------------------------
129 /* This helper function is used by alot of item methods as thier final
131 void pkgAcquire::Item::Rename(string From
,string To
)
133 if (rename(From
.c_str(),To
.c_str()) != 0)
136 snprintf(S
,sizeof(S
),_("rename failed, %s (%s -> %s)."),strerror(errno
),
137 From
.c_str(),To
.c_str());
143 // Acquire::Item::ReportMirrorFailure /*{{{*/
144 // ---------------------------------------------------------------------
145 void pkgAcquire::Item::ReportMirrorFailure(string FailCode
)
147 // we only act if a mirror was used at all
148 if(UsedMirror
.empty())
151 std::cerr
<< "\nReportMirrorFailure: "
153 << " Uri: " << DescURI()
155 << FailCode
<< std::endl
;
157 const char *Args
[40];
159 string report
= _config
->Find("Methods::Mirror::ProblemReporting",
160 "/usr/lib/apt/apt-report-mirror-failure");
161 if(!FileExists(report
))
163 Args
[i
++] = report
.c_str();
164 Args
[i
++] = UsedMirror
.c_str();
165 Args
[i
++] = DescURI().c_str();
166 Args
[i
++] = FailCode
.c_str();
168 pid_t pid
= ExecFork();
171 _error
->Error("ReportMirrorFailure Fork failed");
176 execvp(Args
[0], (char**)Args
);
177 std::cerr
<< "Could not exec " << Args
[0] << std::endl
;
180 if(!ExecWait(pid
, "report-mirror-failure"))
182 _error
->Warning("Couldn't report problem to '%s'",
183 _config
->Find("Methods::Mirror::ProblemReporting").c_str());
187 // AcqSubIndex::AcqSubIndex - Constructor /*{{{*/
188 // ---------------------------------------------------------------------
189 /* Get the Index file first and see if there are languages available
190 * If so, create a pkgAcqIndexTrans for the found language(s).
192 pkgAcqSubIndex::pkgAcqSubIndex(pkgAcquire
*Owner
, string
const &URI
,
193 string
const &URIDesc
, string
const &ShortDesc
,
194 HashString
const &ExpectedHash
)
195 : Item(Owner
), ExpectedHash(ExpectedHash
)
197 Debug
= _config
->FindB("Debug::pkgAcquire::SubIndex",false);
199 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
200 DestFile
+= URItoFileName(URI
);
203 Desc
.Description
= URIDesc
;
205 Desc
.ShortDesc
= ShortDesc
;
210 std::clog
<< "pkgAcqSubIndex: " << Desc
.URI
<< std::endl
;
213 // AcqSubIndex::Custom600Headers - Insert custom request headers /*{{{*/
214 // ---------------------------------------------------------------------
215 /* The only header we use is the last-modified header. */
216 string
pkgAcqSubIndex::Custom600Headers()
218 string Final
= _config
->FindDir("Dir::State::lists");
219 Final
+= URItoFileName(Desc
.URI
);
222 if (stat(Final
.c_str(),&Buf
) != 0)
223 return "\nIndex-File: true";
224 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
227 void pkgAcqSubIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
230 std::clog
<< "pkgAcqSubIndex failed: " << Desc
.URI
<< std::endl
;
236 // No good Index is provided, so try guessing
237 std::vector
<std::string
> langs
= APT::Configuration::getLanguages(true);
238 for (std::vector
<std::string
>::const_iterator l
= langs
.begin();
239 l
!= langs
.end(); ++l
)
241 if (*l
== "none") continue;
242 string
const file
= "Translation-" + *l
;
243 new pkgAcqIndexTrans(Owner
, Desc
.URI
.substr(0, Desc
.URI
.rfind('/')+1).append(file
),
244 Desc
.Description
.erase(Desc
.Description
.rfind(' ')+1).append(file
),
249 void pkgAcqSubIndex::Done(string Message
,unsigned long Size
,string Md5Hash
, /*{{{*/
250 pkgAcquire::MethodConfig
*Cnf
)
253 std::clog
<< "pkgAcqSubIndex::Done(): " << Desc
.URI
<< std::endl
;
255 string FileName
= LookupTag(Message
,"Filename");
256 if (FileName
.empty() == true)
259 ErrorText
= "Method gave a blank filename";
263 if (FileName
!= DestFile
)
266 Desc
.URI
= "copy:" + FileName
;
271 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
273 string FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(Desc
.URI
);
275 // sucess in downloading the index
278 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
<< std::endl
;
279 Rename(DestFile
,FinalFile
);
280 chmod(FinalFile
.c_str(),0644);
281 DestFile
= FinalFile
;
283 if(ParseIndex(DestFile
) == false)
284 return Failed("", NULL
);
292 bool pkgAcqSubIndex::ParseIndex(string
const &IndexFile
) /*{{{*/
294 indexRecords SubIndexParser
;
295 if (FileExists(IndexFile
) == false || SubIndexParser
.Load(IndexFile
) == false)
298 std::vector
<std::string
> lang
= APT::Configuration::getLanguages(true);
299 for (std::vector
<std::string
>::const_iterator l
= lang
.begin();
300 l
!= lang
.end(); ++l
)
305 string file
= "Translation-" + *l
;
306 indexRecords::checkSum
const *Record
= SubIndexParser
.Lookup(file
);
310 // FIXME: the Index file provided by debian currently only includes bz2 records
311 Record
= SubIndexParser
.Lookup(file
+ ".bz2");
317 expected
= Record
->Hash
;
318 if (expected
.empty() == true)
323 target
.Description
= Desc
.Description
.erase(Desc
.Description
.rfind(' ')+1).append(file
);
324 target
.MetaKey
= file
;
325 target
.ShortDesc
= file
;
326 target
.URI
= Desc
.URI
.substr(0, Desc
.URI
.rfind('/')+1).append(file
);
327 new pkgAcqIndexTrans(Owner
, &target
, expected
, &SubIndexParser
);
332 // AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
333 // ---------------------------------------------------------------------
334 /* Get the DiffIndex file first and see if there are patches availabe
335 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
336 * patches. If anything goes wrong in that process, it will fall back to
337 * the original packages file
339 pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire
*Owner
,
340 string URI
,string URIDesc
,string ShortDesc
,
341 HashString ExpectedHash
)
342 : Item(Owner
), RealURI(URI
), ExpectedHash(ExpectedHash
),
346 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
348 Desc
.Description
= URIDesc
+ "/DiffIndex";
350 Desc
.ShortDesc
= ShortDesc
;
351 Desc
.URI
= URI
+ ".diff/Index";
353 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
354 DestFile
+= URItoFileName(URI
) + string(".DiffIndex");
357 std::clog
<< "pkgAcqDiffIndex: " << Desc
.URI
<< std::endl
;
359 // look for the current package file
360 CurrentPackagesFile
= _config
->FindDir("Dir::State::lists");
361 CurrentPackagesFile
+= URItoFileName(RealURI
);
363 // FIXME: this file:/ check is a hack to prevent fetching
364 // from local sources. this is really silly, and
365 // should be fixed cleanly as soon as possible
366 if(!FileExists(CurrentPackagesFile
) ||
367 Desc
.URI
.substr(0,strlen("file:/")) == "file:/")
369 // we don't have a pkg file or we don't want to queue
371 std::clog
<< "No index file, local or canceld by user" << std::endl
;
377 std::clog
<< "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
378 << CurrentPackagesFile
<< std::endl
;
384 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
385 // ---------------------------------------------------------------------
386 /* The only header we use is the last-modified header. */
387 string
pkgAcqDiffIndex::Custom600Headers()
389 string Final
= _config
->FindDir("Dir::State::lists");
390 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
393 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
396 if (stat(Final
.c_str(),&Buf
) != 0)
397 return "\nIndex-File: true";
399 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
402 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile
) /*{{{*/
405 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
410 vector
<DiffInfo
> available_patches
;
412 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
414 if (_error
->PendingError() == true)
417 if(TF
.Step(Tags
) == true)
423 string
const tmp
= Tags
.FindS("SHA1-Current");
424 std::stringstream
ss(tmp
);
425 ss
>> ServerSha1
>> size
;
426 unsigned long const ServerSize
= atol(size
.c_str());
428 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
430 SHA1
.AddFD(fd
.Fd(), fd
.Size());
431 string
const local_sha1
= SHA1
.Result();
433 if(local_sha1
== ServerSha1
)
435 // we have the same sha1 as the server
437 std::clog
<< "Package file is up-to-date" << std::endl
;
438 // set found to true, this will queue a pkgAcqIndexDiffs with
439 // a empty availabe_patches
445 std::clog
<< "SHA1-Current: " << ServerSha1
<< " and we start at "<< fd
.Name() << " " << fd
.Size() << " " << local_sha1
<< std::endl
;
447 // check the historie and see what patches we need
448 string
const history
= Tags
.FindS("SHA1-History");
449 std::stringstream
hist(history
);
450 while(hist
>> d
.sha1
>> size
>> d
.file
)
452 // read until the first match is found
453 // from that point on, we probably need all diffs
454 if(d
.sha1
== local_sha1
)
456 else if (found
== false)
460 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
461 available_patches
.push_back(d
);
464 if (available_patches
.empty() == false)
466 // patching with too many files is rather slow compared to a fast download
467 unsigned long const fileLimit
= _config
->FindI("Acquire::PDiffs::FileLimit", 0);
468 if (fileLimit
!= 0 && fileLimit
< available_patches
.size())
471 std::clog
<< "Need " << available_patches
.size() << " diffs (Limit is " << fileLimit
472 << ") so fallback to complete download" << std::endl
;
476 // see if the patches are too big
477 found
= false; // it was true and it will be true again at the end
478 d
= *available_patches
.begin();
479 string
const firstPatch
= d
.file
;
480 unsigned long patchesSize
= 0;
481 std::stringstream
patches(Tags
.FindS("SHA1-Patches"));
482 while(patches
>> d
.sha1
>> size
>> d
.file
)
484 if (firstPatch
== d
.file
)
486 else if (found
== false)
489 patchesSize
+= atol(size
.c_str());
491 unsigned long const sizeLimit
= ServerSize
* _config
->FindI("Acquire::PDiffs::SizeLimit", 100);
492 if (sizeLimit
> 0 && (sizeLimit
/100) < patchesSize
)
495 std::clog
<< "Need " << patchesSize
<< " bytes (Limit is " << sizeLimit
/100
496 << ") so fallback to complete download" << std::endl
;
502 // we have something, queue the next diff
506 string::size_type
const last_space
= Description
.rfind(" ");
507 if(last_space
!= string::npos
)
508 Description
.erase(last_space
, Description
.size()-last_space
);
509 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
510 ExpectedHash
, ServerSha1
, available_patches
);
518 // Nothing found, report and return false
519 // Failing here is ok, if we return false later, the full
520 // IndexFile is queued
522 std::clog
<< "Can't find a patch in the index file" << std::endl
;
526 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
529 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< std::endl
530 << "Falling back to normal index file aquire" << std::endl
;
532 new pkgAcqIndex(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
540 void pkgAcqDiffIndex::Done(string Message
,unsigned long Size
,string Md5Hash
, /*{{{*/
541 pkgAcquire::MethodConfig
*Cnf
)
544 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
546 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
549 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
551 // sucess in downloading the index
553 FinalFile
+= string(".IndexDiff");
555 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
557 Rename(DestFile
,FinalFile
);
558 chmod(FinalFile
.c_str(),0644);
559 DestFile
= FinalFile
;
561 if(!ParseDiffIndex(DestFile
))
562 return Failed("", NULL
);
570 // AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
571 // ---------------------------------------------------------------------
572 /* The package diff is added to the queue. one object is constructed
573 * for each diff and the index
575 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
576 string URI
,string URIDesc
,string ShortDesc
,
577 HashString ExpectedHash
,
579 vector
<DiffInfo
> diffs
)
580 : Item(Owner
), RealURI(URI
), ExpectedHash(ExpectedHash
),
581 available_patches(diffs
), ServerSha1(ServerSha1
)
584 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
585 DestFile
+= URItoFileName(URI
);
587 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
589 Description
= URIDesc
;
591 Desc
.ShortDesc
= ShortDesc
;
593 if(available_patches
.size() == 0)
595 // we are done (yeah!)
601 State
= StateFetchDiff
;
606 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
609 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< std::endl
610 << "Falling back to normal index file aquire" << std::endl
;
611 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
616 // Finish - helper that cleans the item out of the fetcher queue /*{{{*/
617 void pkgAcqIndexDiffs::Finish(bool allDone
)
619 // we restore the original name, this is required, otherwise
620 // the file will be cleaned
623 DestFile
= _config
->FindDir("Dir::State::lists");
624 DestFile
+= URItoFileName(RealURI
);
626 if(!ExpectedHash
.empty() && !ExpectedHash
.VerifyFile(DestFile
))
628 Status
= StatAuthError
;
629 ErrorText
= _("MD5Sum mismatch");
630 Rename(DestFile
,DestFile
+ ".FAILED");
635 // this is for the "real" finish
640 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
645 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
652 bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
655 // calc sha1 of the just patched file
656 string FinalFile
= _config
->FindDir("Dir::State::lists");
657 FinalFile
+= URItoFileName(RealURI
);
659 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
661 SHA1
.AddFD(fd
.Fd(), fd
.Size());
662 string local_sha1
= string(SHA1
.Result());
664 std::clog
<< "QueueNextDiff: "
665 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
667 // final file reached before all patches are applied
668 if(local_sha1
== ServerSha1
)
674 // remove all patches until the next matching patch is found
675 // this requires the Index file to be ordered
676 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
677 available_patches
.size() > 0 &&
678 I
!= available_patches
.end() &&
679 (*I
).sha1
!= local_sha1
;
682 available_patches
.erase(I
);
685 // error checking and falling back if no patch was found
686 if(available_patches
.size() == 0)
692 // queue the right diff
693 Desc
.URI
= string(RealURI
) + ".diff/" + available_patches
[0].file
+ ".gz";
694 Desc
.Description
= Description
+ " " + available_patches
[0].file
+ string(".pdiff");
695 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
696 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
699 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
706 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
, /*{{{*/
707 pkgAcquire::MethodConfig
*Cnf
)
710 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
712 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
715 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
717 // sucess in downloading a diff, enter ApplyDiff state
718 if(State
== StateFetchDiff
)
721 // rred excepts the patch as $FinalFile.ed
722 Rename(DestFile
,FinalFile
+".ed");
725 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
727 State
= StateApplyDiff
;
729 Desc
.URI
= "rred:" + FinalFile
;
736 // success in download/apply a diff, queue next (if needed)
737 if(State
== StateApplyDiff
)
739 // remove the just applied patch
740 available_patches
.erase(available_patches
.begin());
745 std::clog
<< "Moving patched file in place: " << std::endl
746 << DestFile
<< " -> " << FinalFile
<< std::endl
;
748 Rename(DestFile
,FinalFile
);
749 chmod(FinalFile
.c_str(),0644);
751 // see if there is more to download
752 if(available_patches
.size() > 0) {
753 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
754 ExpectedHash
, ServerSha1
, available_patches
);
761 // AcqIndex::AcqIndex - Constructor /*{{{*/
762 // ---------------------------------------------------------------------
763 /* The package file is added to the queue and a second class is
764 instantiated to fetch the revision file */
765 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
766 string URI
,string URIDesc
,string ShortDesc
,
767 HashString ExpectedHash
, string comprExt
)
768 : Item(Owner
), RealURI(URI
), ExpectedHash(ExpectedHash
)
770 if(comprExt
.empty() == true)
772 // autoselect the compression method
773 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
774 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
775 comprExt
.append(*t
).append(" ");
776 if (comprExt
.empty() == false)
777 comprExt
.erase(comprExt
.end()-1);
779 CompressionExtension
= comprExt
;
781 Init(URI
, URIDesc
, ShortDesc
);
783 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
, IndexTarget
const *Target
,
784 HashString
const &ExpectedHash
, indexRecords
const *MetaIndexParser
)
785 : Item(Owner
), RealURI(Target
->URI
), ExpectedHash(ExpectedHash
)
787 // autoselect the compression method
788 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
789 CompressionExtension
= "";
790 if (ExpectedHash
.empty() == false)
792 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
793 if (*t
== "uncompressed" || MetaIndexParser
->Exists(string(Target
->MetaKey
).append(".").append(*t
)) == true)
794 CompressionExtension
.append(*t
).append(" ");
798 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
799 CompressionExtension
.append(*t
).append(" ");
801 if (CompressionExtension
.empty() == false)
802 CompressionExtension
.erase(CompressionExtension
.end()-1);
804 Init(Target
->URI
, Target
->Description
, Target
->ShortDesc
);
807 // AcqIndex::Init - defered Constructor /*{{{*/
808 void pkgAcqIndex::Init(string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
) {
809 Decompression
= false;
812 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
813 DestFile
+= URItoFileName(URI
);
815 std::string
const comprExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
816 if (comprExt
== "uncompressed")
819 Desc
.URI
= URI
+ '.' + comprExt
;
821 Desc
.Description
= URIDesc
;
823 Desc
.ShortDesc
= ShortDesc
;
828 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
829 // ---------------------------------------------------------------------
830 /* The only header we use is the last-modified header. */
831 string
pkgAcqIndex::Custom600Headers()
833 string Final
= _config
->FindDir("Dir::State::lists");
834 Final
+= URItoFileName(RealURI
);
835 if (_config
->FindB("Acquire::GzipIndexes",false))
839 if (stat(Final
.c_str(),&Buf
) != 0)
840 return "\nIndex-File: true";
841 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
844 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
846 size_t const nextExt
= CompressionExtension
.find(' ');
847 if (nextExt
!= std::string::npos
)
849 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
850 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
854 // on decompression failure, remove bad versions in partial/
855 if (Decompression
&& Erase
) {
856 string s
= _config
->FindDir("Dir::State::lists") + "partial/";
857 s
.append(URItoFileName(RealURI
));
861 Item::Failed(Message
,Cnf
);
864 // AcqIndex::Done - Finished a fetch /*{{{*/
865 // ---------------------------------------------------------------------
866 /* This goes through a number of states.. On the initial fetch the
867 method could possibly return an alternate filename which points
868 to the uncompressed version of the file. If this is so the file
869 is copied into the partial directory. In all other cases the file
870 is decompressed with a gzip uri. */
871 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string Hash
,
872 pkgAcquire::MethodConfig
*Cfg
)
874 Item::Done(Message
,Size
,Hash
,Cfg
);
876 if (Decompression
== true)
878 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
880 std::cerr
<< std::endl
<< RealURI
<< ": Computed Hash: " << Hash
;
881 std::cerr
<< " Expected Hash: " << ExpectedHash
.toStr() << std::endl
;
884 if (!ExpectedHash
.empty() && ExpectedHash
.toStr() != Hash
)
886 Status
= StatAuthError
;
887 ErrorText
= _("Hash Sum mismatch");
888 Rename(DestFile
,DestFile
+ ".FAILED");
889 ReportMirrorFailure("HashChecksumFailure");
892 // Done, move it into position
893 string FinalFile
= _config
->FindDir("Dir::State::lists");
894 FinalFile
+= URItoFileName(RealURI
);
895 Rename(DestFile
,FinalFile
);
896 chmod(FinalFile
.c_str(),0644);
898 /* We restore the original name to DestFile so that the clean operation
900 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
901 DestFile
+= URItoFileName(RealURI
);
903 // Remove the compressed version.
905 unlink(DestFile
.c_str());
912 // Handle the unzipd case
913 string FileName
= LookupTag(Message
,"Alt-Filename");
914 if (FileName
.empty() == false)
916 // The files timestamp matches
917 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
919 Decompression
= true;
921 DestFile
+= ".decomp";
922 Desc
.URI
= "copy:" + FileName
;
928 FileName
= LookupTag(Message
,"Filename");
929 if (FileName
.empty() == true)
932 ErrorText
= "Method gave a blank filename";
935 std::string
const compExt
= CompressionExtension
.substr(0, CompressionExtension
.find(' '));
937 // The files timestamp matches
938 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true) {
939 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz")
940 // Update DestFile for .gz suffix so that the clean operation keeps it
945 if (FileName
== DestFile
)
952 // If we enable compressed indexes and already have gzip, keep it
953 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz" && !Local
) {
954 string FinalFile
= _config
->FindDir("Dir::State::lists");
955 FinalFile
+= URItoFileName(RealURI
) + ".gz";
956 Rename(DestFile
,FinalFile
);
957 chmod(FinalFile
.c_str(),0644);
959 // Update DestFile for .gz suffix so that the clean operation keeps it
960 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
961 DestFile
+= URItoFileName(RealURI
) + ".gz";
965 // get the binary name for your used compression type
966 decompProg
= _config
->Find(string("Acquire::CompressionTypes::").append(compExt
),"");
967 if(decompProg
.empty() == false);
968 else if(compExt
== "uncompressed")
971 _error
->Error("Unsupported extension: %s", compExt
.c_str());
975 Decompression
= true;
976 DestFile
+= ".decomp";
977 Desc
.URI
= decompProg
+ ":" + FileName
;
979 Mode
= decompProg
.c_str();
982 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
983 // ---------------------------------------------------------------------
984 /* The Translation file is added to the queue */
985 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
986 string URI
,string URIDesc
,string ShortDesc
)
987 : pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, HashString(), "")
990 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
, IndexTarget
const *Target
,
991 HashString
const &ExpectedHash
, indexRecords
const *MetaIndexParser
)
992 : pkgAcqIndex(Owner
, Target
, ExpectedHash
, MetaIndexParser
)
996 // AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
997 // ---------------------------------------------------------------------
998 string
pkgAcqIndexTrans::Custom600Headers()
1000 string Final
= _config
->FindDir("Dir::State::lists");
1001 Final
+= URItoFileName(RealURI
);
1004 if (stat(Final
.c_str(),&Buf
) != 0)
1005 return "\nFail-Ignore: true\nIndex-File: true";
1006 return "\nFail-Ignore: true\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1009 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
1010 // ---------------------------------------------------------------------
1012 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1014 size_t const nextExt
= CompressionExtension
.find(' ');
1015 if (nextExt
!= std::string::npos
)
1017 CompressionExtension
= CompressionExtension
.substr(nextExt
+1);
1018 Init(RealURI
, Desc
.Description
, Desc
.ShortDesc
);
1023 if (Cnf
->LocalOnly
== true ||
1024 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1033 Item::Failed(Message
,Cnf
);
1036 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
, /*{{{*/
1037 string URI
,string URIDesc
,string ShortDesc
,
1038 string MetaIndexURI
, string MetaIndexURIDesc
,
1039 string MetaIndexShortDesc
,
1040 const vector
<IndexTarget
*>* IndexTargets
,
1041 indexRecords
* MetaIndexParser
) :
1042 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
1043 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
1044 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
1046 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1047 DestFile
+= URItoFileName(URI
);
1049 // remove any partial downloaded sig-file in partial/.
1050 // it may confuse proxies and is too small to warrant a
1051 // partial download anyway
1052 unlink(DestFile
.c_str());
1055 Desc
.Description
= URIDesc
;
1057 Desc
.ShortDesc
= ShortDesc
;
1060 string Final
= _config
->FindDir("Dir::State::lists");
1061 Final
+= URItoFileName(RealURI
);
1063 if (stat(Final
.c_str(),&Buf
) == 0)
1065 // File was already in place. It needs to be re-downloaded/verified
1066 // because Release might have changed, we do give it a differnt
1067 // name than DestFile because otherwise the http method will
1068 // send If-Range requests and there are too many broken servers
1069 // out there that do not understand them
1070 LastGoodSig
= DestFile
+".reverify";
1071 Rename(Final
,LastGoodSig
);
1077 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
1078 // ---------------------------------------------------------------------
1079 /* The only header we use is the last-modified header. */
1080 string
pkgAcqMetaSig::Custom600Headers()
1083 if (stat(LastGoodSig
.c_str(),&Buf
) != 0)
1084 return "\nIndex-File: true";
1086 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1089 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
1090 pkgAcquire::MethodConfig
*Cfg
)
1092 Item::Done(Message
,Size
,MD5
,Cfg
);
1094 string FileName
= LookupTag(Message
,"Filename");
1095 if (FileName
.empty() == true)
1098 ErrorText
= "Method gave a blank filename";
1102 if (FileName
!= DestFile
)
1104 // We have to copy it into place
1106 Desc
.URI
= "copy:" + FileName
;
1113 // put the last known good file back on i-m-s hit (it will
1114 // be re-verified again)
1115 // Else do nothing, we have the new file in DestFile then
1116 if(StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1117 Rename(LastGoodSig
, DestFile
);
1119 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
1120 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
,
1121 MetaIndexShortDesc
, DestFile
, IndexTargets
,
1126 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)/*{{{*/
1128 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
1130 // if we get a network error we fail gracefully
1131 if(Status
== StatTransientNetworkError
)
1133 Item::Failed(Message
,Cnf
);
1134 // move the sigfile back on transient network failures
1135 if(FileExists(LastGoodSig
))
1136 Rename(LastGoodSig
,Final
);
1138 // set the status back to , Item::Failed likes to reset it
1139 Status
= pkgAcquire::Item::StatTransientNetworkError
;
1143 // Delete any existing sigfile when the acquire failed
1144 unlink(Final
.c_str());
1146 // queue a pkgAcqMetaIndex with no sigfile
1147 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
1148 "", IndexTargets
, MetaIndexParser
);
1150 if (Cnf
->LocalOnly
== true ||
1151 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1160 Item::Failed(Message
,Cnf
);
1163 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
, /*{{{*/
1164 string URI
,string URIDesc
,string ShortDesc
,
1166 const vector
<struct IndexTarget
*>* IndexTargets
,
1167 indexRecords
* MetaIndexParser
) :
1168 Item(Owner
), RealURI(URI
), SigFile(SigFile
), IndexTargets(IndexTargets
),
1169 MetaIndexParser(MetaIndexParser
), AuthPass(false), IMSHit(false)
1171 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1172 DestFile
+= URItoFileName(URI
);
1175 Desc
.Description
= URIDesc
;
1177 Desc
.ShortDesc
= ShortDesc
;
1183 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
1184 // ---------------------------------------------------------------------
1185 /* The only header we use is the last-modified header. */
1186 string
pkgAcqMetaIndex::Custom600Headers()
1188 string Final
= _config
->FindDir("Dir::State::lists");
1189 Final
+= URItoFileName(RealURI
);
1192 if (stat(Final
.c_str(),&Buf
) != 0)
1193 return "\nIndex-File: true";
1195 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1198 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string Hash
, /*{{{*/
1199 pkgAcquire::MethodConfig
*Cfg
)
1201 Item::Done(Message
,Size
,Hash
,Cfg
);
1203 // MetaIndexes are done in two passes: one to download the
1204 // metaindex with an appropriate method, and a second to verify it
1205 // with the gpgv method
1207 if (AuthPass
== true)
1211 // all cool, move Release file into place
1216 RetrievalDone(Message
);
1218 // Still more retrieving to do
1223 // There was no signature file, so we are finished. Download
1224 // the indexes without verification.
1225 QueueIndexes(false);
1229 // There was a signature file, so pass it to gpgv for
1232 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1233 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
1234 << SigFile
<< "," << DestFile
<< ")\n";
1236 Desc
.URI
= "gpgv:" + SigFile
;
1243 if (Complete
== true)
1245 string FinalFile
= _config
->FindDir("Dir::State::lists");
1246 FinalFile
+= URItoFileName(RealURI
);
1247 if (SigFile
== DestFile
)
1248 SigFile
= FinalFile
;
1249 Rename(DestFile
,FinalFile
);
1250 chmod(FinalFile
.c_str(),0644);
1251 DestFile
= FinalFile
;
1255 void pkgAcqMetaIndex::RetrievalDone(string Message
) /*{{{*/
1257 // We have just finished downloading a Release file (it is not
1260 string FileName
= LookupTag(Message
,"Filename");
1261 if (FileName
.empty() == true)
1264 ErrorText
= "Method gave a blank filename";
1268 if (FileName
!= DestFile
)
1271 Desc
.URI
= "copy:" + FileName
;
1276 // make sure to verify against the right file on I-M-S hit
1277 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
1280 string FinalFile
= _config
->FindDir("Dir::State::lists");
1281 FinalFile
+= URItoFileName(RealURI
);
1282 if (SigFile
== DestFile
)
1283 SigFile
= FinalFile
;
1284 DestFile
= FinalFile
;
1289 void pkgAcqMetaIndex::AuthDone(string Message
) /*{{{*/
1291 // At this point, the gpgv method has succeeded, so there is a
1292 // valid signature from a key in the trusted keyring. We
1293 // perform additional verification of its contents, and use them
1294 // to verify the indexes we are about to download
1296 if (!MetaIndexParser
->Load(DestFile
))
1298 Status
= StatAuthError
;
1299 ErrorText
= MetaIndexParser
->ErrorText
;
1303 if (!VerifyVendor(Message
))
1308 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1309 std::cerr
<< "Signature verification succeeded: "
1310 << DestFile
<< std::endl
;
1312 // Download further indexes with verification
1315 // is it a clearsigned MetaIndex file?
1316 if (DestFile
== SigFile
)
1319 // Done, move signature file into position
1320 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1321 URItoFileName(RealURI
) + ".gpg";
1322 Rename(SigFile
,VerifiedSigFile
);
1323 chmod(VerifiedSigFile
.c_str(),0644);
1326 void pkgAcqMetaIndex::QueueIndexes(bool verify
) /*{{{*/
1328 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
1329 Target
!= IndexTargets
->end();
1332 HashString ExpectedIndexHash
;
1335 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1338 if ((*Target
)->IsOptional() == false)
1340 Status
= StatAuthError
;
1341 strprintf(ErrorText
, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), (*Target
)->MetaKey
.c_str());
1347 ExpectedIndexHash
= Record
->Hash
;
1348 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1350 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
1351 std::cerr
<< "Expected Hash: " << ExpectedIndexHash
.toStr() << std::endl
;
1353 if (ExpectedIndexHash
.empty() == true && (*Target
)->IsOptional() == false)
1355 Status
= StatAuthError
;
1356 strprintf(ErrorText
, _("Unable to find hash sum for '%s' in Release file"), (*Target
)->MetaKey
.c_str());
1362 if ((*Target
)->IsOptional() == true)
1364 if ((*Target
)->IsSubIndex() == true)
1365 new pkgAcqSubIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1366 (*Target
)->ShortDesc
, ExpectedIndexHash
);
1368 new pkgAcqIndexTrans(Owner
, *Target
, ExpectedIndexHash
, MetaIndexParser
);
1372 /* Queue Packages file (either diff or full packages files, depending
1373 on the users option) - we also check if the PDiff Index file is listed
1374 in the Meta-Index file. Ideal would be if pkgAcqDiffIndex would test this
1375 instead, but passing the required info to it is to much hassle */
1376 if(_config
->FindB("Acquire::PDiffs",true) == true && (verify
== false ||
1377 MetaIndexParser
->Exists(string((*Target
)->MetaKey
).append(".diff/Index")) == true))
1378 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1379 (*Target
)->ShortDesc
, ExpectedIndexHash
);
1381 new pkgAcqIndex(Owner
, *Target
, ExpectedIndexHash
, MetaIndexParser
);
1385 bool pkgAcqMetaIndex::VerifyVendor(string Message
) /*{{{*/
1387 // // Maybe this should be made available from above so we don't have
1388 // // to read and parse it every time?
1389 // pkgVendorList List;
1390 // List.ReadMainList();
1392 // const Vendor* Vndr = NULL;
1393 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1395 // string::size_type pos = (*I).find("VALIDSIG ");
1396 // if (_config->FindB("Debug::Vendor", false))
1397 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1399 // if (pos != std::string::npos)
1401 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1402 // if (_config->FindB("Debug::Vendor", false))
1403 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1405 // Vndr = List.FindVendor(Fingerprint) != "";
1406 // if (Vndr != NULL);
1410 string::size_type pos
;
1412 // check for missing sigs (that where not fatal because otherwise we had
1415 string msg
= _("There is no public key available for the "
1416 "following key IDs:\n");
1417 pos
= Message
.find("NO_PUBKEY ");
1418 if (pos
!= std::string::npos
)
1420 string::size_type start
= pos
+strlen("NO_PUBKEY ");
1421 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
1422 missingkeys
+= (Fingerprint
);
1424 if(!missingkeys
.empty())
1425 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
1427 string Transformed
= MetaIndexParser
->GetExpectedDist();
1429 if (Transformed
== "../project/experimental")
1431 Transformed
= "experimental";
1434 pos
= Transformed
.rfind('/');
1435 if (pos
!= string::npos
)
1437 Transformed
= Transformed
.substr(0, pos
);
1440 if (Transformed
== ".")
1445 if (_config
->FindB("Acquire::Check-Valid-Until", true) == true &&
1446 MetaIndexParser
->GetValidUntil() > 0) {
1447 time_t const invalid_since
= time(NULL
) - MetaIndexParser
->GetValidUntil();
1448 if (invalid_since
> 0)
1449 // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
1450 // the time since then the file is invalid - formated in the same way as in
1451 // the download progress display (e.g. 7d 3h 42min 1s)
1452 return _error
->Error(_("Release file expired, ignoring %s (invalid since %s)"),
1453 RealURI
.c_str(), TimeToStr(invalid_since
).c_str());
1456 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1458 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1459 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1460 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1463 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1465 // This might become fatal one day
1466 // Status = StatAuthError;
1467 // ErrorText = "Conflicting distribution; expected "
1468 // + MetaIndexParser->GetExpectedDist() + " but got "
1469 // + MetaIndexParser->GetDist();
1471 if (!Transformed
.empty())
1473 _error
->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
1474 Desc
.Description
.c_str(),
1475 Transformed
.c_str(),
1476 MetaIndexParser
->GetDist().c_str());
1483 // pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
1484 // ---------------------------------------------------------------------
1486 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1488 if (AuthPass
== true)
1490 // gpgv method failed, if we have a good signature
1491 string LastGoodSigFile
= _config
->FindDir("Dir::State::lists");
1492 if (DestFile
== SigFile
)
1493 LastGoodSigFile
.append(URItoFileName(RealURI
));
1495 LastGoodSigFile
.append("partial/").append(URItoFileName(RealURI
)).append(".gpg.reverify");
1497 if(FileExists(LastGoodSigFile
))
1499 if (DestFile
!= SigFile
)
1501 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1502 URItoFileName(RealURI
) + ".gpg";
1503 Rename(LastGoodSigFile
,VerifiedSigFile
);
1505 Status
= StatTransientNetworkError
;
1506 _error
->Warning(_("A error occurred during the signature "
1507 "verification. The repository is not updated "
1508 "and the previous index files will be used. "
1509 "GPG error: %s: %s\n"),
1510 Desc
.Description
.c_str(),
1511 LookupTag(Message
,"Message").c_str());
1512 RunScripts("APT::Update::Auth-Failure");
1515 _error
->Warning(_("GPG error: %s: %s"),
1516 Desc
.Description
.c_str(),
1517 LookupTag(Message
,"Message").c_str());
1519 // gpgv method failed
1520 ReportMirrorFailure("GPGFailure");
1523 // No Release file was present, or verification failed, so fall
1524 // back to queueing Packages files without verification
1525 QueueIndexes(false);
1528 pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire
*Owner
, /*{{{*/
1529 string
const &URI
, string
const &URIDesc
, string
const &ShortDesc
,
1530 string
const &MetaIndexURI
, string
const &MetaIndexURIDesc
, string
const &MetaIndexShortDesc
,
1531 string
const &MetaSigURI
, string
const &MetaSigURIDesc
, string
const &MetaSigShortDesc
,
1532 const vector
<struct IndexTarget
*>* IndexTargets
,
1533 indexRecords
* MetaIndexParser
) :
1534 pkgAcqMetaIndex(Owner
, URI
, URIDesc
, ShortDesc
, "", IndexTargets
, MetaIndexParser
),
1535 MetaIndexURI(MetaIndexURI
), MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
1536 MetaSigURI(MetaSigURI
), MetaSigURIDesc(MetaSigURIDesc
), MetaSigShortDesc(MetaSigShortDesc
)
1541 // pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
1542 // ---------------------------------------------------------------------
1543 // FIXME: this can go away once the InRelease file is used widely
1544 string
pkgAcqMetaClearSig::Custom600Headers()
1546 string Final
= _config
->FindDir("Dir::State::lists");
1547 Final
+= URItoFileName(RealURI
);
1550 if (stat(Final
.c_str(),&Buf
) != 0)
1551 return "\nIndex-File: true\nFail-Ignore: true\n";
1553 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1556 void pkgAcqMetaClearSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
1558 if (AuthPass
== false)
1560 new pkgAcqMetaSig(Owner
,
1561 MetaSigURI
, MetaSigURIDesc
, MetaSigShortDesc
,
1562 MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
1563 IndexTargets
, MetaIndexParser
);
1564 if (Cnf
->LocalOnly
== true ||
1565 StringToBool(LookupTag(Message
, "Transient-Failure"), false) == false)
1569 pkgAcqMetaIndex::Failed(Message
, Cnf
);
1572 // AcqArchive::AcqArchive - Constructor /*{{{*/
1573 // ---------------------------------------------------------------------
1574 /* This just sets up the initial fetch environment and queues the first
1576 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1577 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1578 string
&StoreFilename
) :
1579 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1580 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1583 Retries
= _config
->FindI("Acquire::Retries",0);
1585 if (Version
.Arch() == 0)
1587 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1588 "This might mean you need to manually fix this package. "
1589 "(due to missing arch)"),
1590 Version
.ParentPkg().Name());
1594 /* We need to find a filename to determine the extension. We make the
1595 assumption here that all the available sources for this version share
1596 the same extension.. */
1597 // Skip not source sources, they do not have file fields.
1598 for (; Vf
.end() == false; Vf
++)
1600 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1605 // Does not really matter here.. we are going to fail out below
1606 if (Vf
.end() != true)
1608 // If this fails to get a file name we will bomb out below.
1609 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1610 if (_error
->PendingError() == true)
1613 // Generate the final file name as: package_version_arch.foo
1614 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1615 QuoteString(Version
.VerStr(),"_:") + '_' +
1616 QuoteString(Version
.Arch(),"_:.") +
1617 "." + flExtension(Parse
.FileName());
1620 // check if we have one trusted source for the package. if so, switch
1621 // to "TrustedOnly" mode
1622 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1624 pkgIndexFile
*Index
;
1625 if (Sources
->FindIndex(i
.File(),Index
) == false)
1627 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1629 std::cerr
<< "Checking index: " << Index
->Describe()
1630 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1632 if (Index
->IsTrusted()) {
1638 // "allow-unauthenticated" restores apts old fetching behaviour
1639 // that means that e.g. unauthenticated file:// uris are higher
1640 // priority than authenticated http:// uris
1641 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
1645 if (QueueNext() == false && _error
->PendingError() == false)
1646 _error
->Error(_("I wasn't able to locate file for the %s package. "
1647 "This might mean you need to manually fix this package."),
1648 Version
.ParentPkg().Name());
1651 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1652 // ---------------------------------------------------------------------
1653 /* This queues the next available file version for download. It checks if
1654 the archive is already available in the cache and stashs the MD5 for
1656 bool pkgAcqArchive::QueueNext()
1658 string
const ForceHash
= _config
->Find("Acquire::ForceHash");
1659 for (; Vf
.end() == false; Vf
++)
1661 // Ignore not source sources
1662 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1665 // Try to cross match against the source list
1666 pkgIndexFile
*Index
;
1667 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1670 // only try to get a trusted package from another source if that source
1672 if(Trusted
&& !Index
->IsTrusted())
1675 // Grab the text package record
1676 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1677 if (_error
->PendingError() == true)
1680 string PkgFile
= Parse
.FileName();
1681 if (ForceHash
.empty() == false)
1683 if(stringcasecmp(ForceHash
, "sha256") == 0)
1684 ExpectedHash
= HashString("SHA256", Parse
.SHA256Hash());
1685 else if (stringcasecmp(ForceHash
, "sha1") == 0)
1686 ExpectedHash
= HashString("SHA1", Parse
.SHA1Hash());
1688 ExpectedHash
= HashString("MD5Sum", Parse
.MD5Hash());
1693 if ((Hash
= Parse
.SHA256Hash()).empty() == false)
1694 ExpectedHash
= HashString("SHA256", Hash
);
1695 else if ((Hash
= Parse
.SHA1Hash()).empty() == false)
1696 ExpectedHash
= HashString("SHA1", Hash
);
1698 ExpectedHash
= HashString("MD5Sum", Parse
.MD5Hash());
1700 if (PkgFile
.empty() == true)
1701 return _error
->Error(_("The package index files are corrupted. No Filename: "
1702 "field for package %s."),
1703 Version
.ParentPkg().Name());
1705 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1706 Desc
.Description
= Index
->ArchiveInfo(Version
);
1708 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1710 // See if we already have the file. (Legacy filenames)
1711 FileSize
= Version
->Size
;
1712 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1714 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1716 // Make sure the size matches
1717 if ((unsigned)Buf
.st_size
== Version
->Size
)
1722 StoreFilename
= DestFile
= FinalFile
;
1726 /* Hmm, we have a file and its size does not match, this means it is
1727 an old style mismatched arch */
1728 unlink(FinalFile
.c_str());
1731 // Check it again using the new style output filenames
1732 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1733 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1735 // Make sure the size matches
1736 if ((unsigned)Buf
.st_size
== Version
->Size
)
1741 StoreFilename
= DestFile
= FinalFile
;
1745 /* Hmm, we have a file and its size does not match, this shouldnt
1747 unlink(FinalFile
.c_str());
1750 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1752 // Check the destination file
1753 if (stat(DestFile
.c_str(),&Buf
) == 0)
1755 // Hmm, the partial file is too big, erase it
1756 if ((unsigned)Buf
.st_size
> Version
->Size
)
1757 unlink(DestFile
.c_str());
1759 PartialSize
= Buf
.st_size
;
1764 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1765 Desc
.Description
= Index
->ArchiveInfo(Version
);
1767 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1776 // AcqArchive::Done - Finished fetching /*{{{*/
1777 // ---------------------------------------------------------------------
1779 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string CalcHash
,
1780 pkgAcquire::MethodConfig
*Cfg
)
1782 Item::Done(Message
,Size
,CalcHash
,Cfg
);
1785 if (Size
!= Version
->Size
)
1788 ErrorText
= _("Size mismatch");
1793 if(ExpectedHash
.toStr() != CalcHash
)
1796 ErrorText
= _("Hash Sum mismatch");
1797 if(FileExists(DestFile
))
1798 Rename(DestFile
,DestFile
+ ".FAILED");
1802 // Grab the output filename
1803 string FileName
= LookupTag(Message
,"Filename");
1804 if (FileName
.empty() == true)
1807 ErrorText
= "Method gave a blank filename";
1813 // Reference filename
1814 if (FileName
!= DestFile
)
1816 StoreFilename
= DestFile
= FileName
;
1821 // Done, move it into position
1822 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1823 FinalFile
+= flNotDir(StoreFilename
);
1824 Rename(DestFile
,FinalFile
);
1826 StoreFilename
= DestFile
= FinalFile
;
1830 // AcqArchive::Failed - Failure handler /*{{{*/
1831 // ---------------------------------------------------------------------
1832 /* Here we try other sources */
1833 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1835 ErrorText
= LookupTag(Message
,"Message");
1837 /* We don't really want to retry on failed media swaps, this prevents
1838 that. An interesting observation is that permanent failures are not
1840 if (Cnf
->Removable
== true &&
1841 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1843 // Vf = Version.FileList();
1844 while (Vf
.end() == false) Vf
++;
1845 StoreFilename
= string();
1846 Item::Failed(Message
,Cnf
);
1850 if (QueueNext() == false)
1852 // This is the retry counter
1854 Cnf
->LocalOnly
== false &&
1855 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1858 Vf
= Version
.FileList();
1859 if (QueueNext() == true)
1863 StoreFilename
= string();
1864 Item::Failed(Message
,Cnf
);
1868 // AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
1869 // ---------------------------------------------------------------------
1870 bool pkgAcqArchive::IsTrusted()
1875 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1876 // ---------------------------------------------------------------------
1878 void pkgAcqArchive::Finished()
1880 if (Status
== pkgAcquire::Item::StatDone
&&
1883 StoreFilename
= string();
1886 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1887 // ---------------------------------------------------------------------
1888 /* The file is added to the queue */
1889 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string Hash
,
1890 unsigned long Size
,string Dsc
,string ShortDesc
,
1891 const string
&DestDir
, const string
&DestFilename
,
1893 Item(Owner
), ExpectedHash(Hash
), IsIndexFile(IsIndexFile
)
1895 Retries
= _config
->FindI("Acquire::Retries",0);
1897 if(!DestFilename
.empty())
1898 DestFile
= DestFilename
;
1899 else if(!DestDir
.empty())
1900 DestFile
= DestDir
+ "/" + flNotDir(URI
);
1902 DestFile
= flNotDir(URI
);
1906 Desc
.Description
= Dsc
;
1909 // Set the short description to the archive component
1910 Desc
.ShortDesc
= ShortDesc
;
1912 // Get the transfer sizes
1915 if (stat(DestFile
.c_str(),&Buf
) == 0)
1917 // Hmm, the partial file is too big, erase it
1918 if ((unsigned)Buf
.st_size
> Size
)
1919 unlink(DestFile
.c_str());
1921 PartialSize
= Buf
.st_size
;
1927 // AcqFile::Done - Item downloaded OK /*{{{*/
1928 // ---------------------------------------------------------------------
1930 void pkgAcqFile::Done(string Message
,unsigned long Size
,string CalcHash
,
1931 pkgAcquire::MethodConfig
*Cnf
)
1933 Item::Done(Message
,Size
,CalcHash
,Cnf
);
1936 if(!ExpectedHash
.empty() && ExpectedHash
.toStr() != CalcHash
)
1939 ErrorText
= _("Hash Sum mismatch");
1940 Rename(DestFile
,DestFile
+ ".FAILED");
1944 string FileName
= LookupTag(Message
,"Filename");
1945 if (FileName
.empty() == true)
1948 ErrorText
= "Method gave a blank filename";
1954 // The files timestamp matches
1955 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1958 // We have to copy it into place
1959 if (FileName
!= DestFile
)
1962 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1963 Cnf
->Removable
== true)
1965 Desc
.URI
= "copy:" + FileName
;
1970 // Erase the file if it is a symlink so we can overwrite it
1972 if (lstat(DestFile
.c_str(),&St
) == 0)
1974 if (S_ISLNK(St
.st_mode
) != 0)
1975 unlink(DestFile
.c_str());
1979 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1981 ErrorText
= "Link to " + DestFile
+ " failure ";
1988 // AcqFile::Failed - Failure handler /*{{{*/
1989 // ---------------------------------------------------------------------
1990 /* Here we try other sources */
1991 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1993 ErrorText
= LookupTag(Message
,"Message");
1995 // This is the retry counter
1997 Cnf
->LocalOnly
== false &&
1998 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
2005 Item::Failed(Message
,Cnf
);
2008 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
2009 // ---------------------------------------------------------------------
2010 /* The only header we use is the last-modified header. */
2011 string
pkgAcqFile::Custom600Headers()
2014 return "\nIndex-File: true";
2018 bool IndexTarget::IsOptional() const {
2019 if (strncmp(ShortDesc
.c_str(), "Translation", 11) != 0)
2023 bool IndexTarget::IsSubIndex() const {
2024 if (ShortDesc
!= "TranslationIndex")