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 // AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
188 // ---------------------------------------------------------------------
189 /* Get the DiffIndex file first and see if there are patches availabe
190 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
191 * patches. If anything goes wrong in that process, it will fall back to
192 * the original packages file
194 pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire
*Owner
,
195 string URI
,string URIDesc
,string ShortDesc
,
196 HashString ExpectedHash
)
197 : Item(Owner
), RealURI(URI
), ExpectedHash(ExpectedHash
),
201 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
203 Desc
.Description
= URIDesc
+ "/DiffIndex";
205 Desc
.ShortDesc
= ShortDesc
;
206 Desc
.URI
= URI
+ ".diff/Index";
208 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
209 DestFile
+= URItoFileName(URI
) + string(".DiffIndex");
212 std::clog
<< "pkgAcqDiffIndex: " << Desc
.URI
<< std::endl
;
214 // look for the current package file
215 CurrentPackagesFile
= _config
->FindDir("Dir::State::lists");
216 CurrentPackagesFile
+= URItoFileName(RealURI
);
218 // FIXME: this file:/ check is a hack to prevent fetching
219 // from local sources. this is really silly, and
220 // should be fixed cleanly as soon as possible
221 if(!FileExists(CurrentPackagesFile
) ||
222 Desc
.URI
.substr(0,strlen("file:/")) == "file:/")
224 // we don't have a pkg file or we don't want to queue
226 std::clog
<< "No index file, local or canceld by user" << std::endl
;
232 std::clog
<< "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
233 << CurrentPackagesFile
<< std::endl
;
239 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
240 // ---------------------------------------------------------------------
241 /* The only header we use is the last-modified header. */
242 string
pkgAcqDiffIndex::Custom600Headers()
244 string Final
= _config
->FindDir("Dir::State::lists");
245 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
248 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
251 if (stat(Final
.c_str(),&Buf
) != 0)
252 return "\nIndex-File: true";
254 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
257 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile
) /*{{{*/
260 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
265 vector
<DiffInfo
> available_patches
;
267 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
269 if (_error
->PendingError() == true)
272 if(TF
.Step(Tags
) == true)
278 string
const tmp
= Tags
.FindS("SHA1-Current");
279 std::stringstream
ss(tmp
);
280 ss
>> ServerSha1
>> size
;
281 unsigned long const ServerSize
= atol(size
.c_str());
283 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnlyGzip
);
285 SHA1
.AddFD(fd
.Fd(), fd
.Size());
286 string
const local_sha1
= SHA1
.Result();
288 if(local_sha1
== ServerSha1
)
290 // we have the same sha1 as the server
292 std::clog
<< "Package file is up-to-date" << std::endl
;
293 // set found to true, this will queue a pkgAcqIndexDiffs with
294 // a empty availabe_patches
300 std::clog
<< "SHA1-Current: " << ServerSha1
<< std::endl
;
302 // check the historie and see what patches we need
303 string
const history
= Tags
.FindS("SHA1-History");
304 std::stringstream
hist(history
);
305 while(hist
>> d
.sha1
>> size
>> d
.file
)
307 // read until the first match is found
308 // from that point on, we probably need all diffs
309 if(d
.sha1
== local_sha1
)
311 else if (found
== false)
315 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
316 available_patches
.push_back(d
);
319 if (available_patches
.empty() == false)
321 // patching with too many files is rather slow compared to a fast download
322 unsigned long const fileLimit
= _config
->FindI("Acquire::PDiffs::FileLimit", 0);
323 if (fileLimit
!= 0 && fileLimit
< available_patches
.size())
326 std::clog
<< "Need " << available_patches
.size() << " diffs (Limit is " << fileLimit
327 << ") so fallback to complete download" << std::endl
;
331 // see if the patches are too big
332 found
= false; // it was true and it will be true again at the end
333 d
= *available_patches
.begin();
334 string
const firstPatch
= d
.file
;
335 unsigned long patchesSize
= 0;
336 std::stringstream
patches(Tags
.FindS("SHA1-Patches"));
337 while(patches
>> d
.sha1
>> size
>> d
.file
)
339 if (firstPatch
== d
.file
)
341 else if (found
== false)
344 patchesSize
+= atol(size
.c_str());
346 unsigned long const sizeLimit
= ServerSize
* _config
->FindI("Acquire::PDiffs::SizeLimit", 100);
347 if (sizeLimit
> 0 && (sizeLimit
/100) < patchesSize
)
350 std::clog
<< "Need " << patchesSize
<< " bytes (Limit is " << sizeLimit
/100
351 << ") so fallback to complete download" << std::endl
;
357 // we have something, queue the next diff
361 string::size_type
const last_space
= Description
.rfind(" ");
362 if(last_space
!= string::npos
)
363 Description
.erase(last_space
, Description
.size()-last_space
);
364 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
365 ExpectedHash
, ServerSha1
, available_patches
);
373 // Nothing found, report and return false
374 // Failing here is ok, if we return false later, the full
375 // IndexFile is queued
377 std::clog
<< "Can't find a patch in the index file" << std::endl
;
381 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
384 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< std::endl
385 << "Falling back to normal index file aquire" << std::endl
;
387 new pkgAcqIndex(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
395 void pkgAcqDiffIndex::Done(string Message
,unsigned long Size
,string Md5Hash
, /*{{{*/
396 pkgAcquire::MethodConfig
*Cnf
)
399 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
401 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
404 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
406 // sucess in downloading the index
408 FinalFile
+= string(".IndexDiff");
410 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
412 Rename(DestFile
,FinalFile
);
413 chmod(FinalFile
.c_str(),0644);
414 DestFile
= FinalFile
;
416 if(!ParseDiffIndex(DestFile
))
417 return Failed("", NULL
);
425 // AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
426 // ---------------------------------------------------------------------
427 /* The package diff is added to the queue. one object is constructed
428 * for each diff and the index
430 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
431 string URI
,string URIDesc
,string ShortDesc
,
432 HashString ExpectedHash
,
434 vector
<DiffInfo
> diffs
)
435 : Item(Owner
), RealURI(URI
), ExpectedHash(ExpectedHash
),
436 available_patches(diffs
), ServerSha1(ServerSha1
)
439 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
440 DestFile
+= URItoFileName(URI
);
442 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
444 Description
= URIDesc
;
446 Desc
.ShortDesc
= ShortDesc
;
448 if(available_patches
.size() == 0)
450 // we are done (yeah!)
456 State
= StateFetchDiff
;
461 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
464 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< std::endl
465 << "Falling back to normal index file aquire" << std::endl
;
466 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
471 // Finish - helper that cleans the item out of the fetcher queue /*{{{*/
472 void pkgAcqIndexDiffs::Finish(bool allDone
)
474 // we restore the original name, this is required, otherwise
475 // the file will be cleaned
478 DestFile
= _config
->FindDir("Dir::State::lists");
479 DestFile
+= URItoFileName(RealURI
);
481 if(!ExpectedHash
.empty() && !ExpectedHash
.VerifyFile(DestFile
))
483 Status
= StatAuthError
;
484 ErrorText
= _("MD5Sum mismatch");
485 Rename(DestFile
,DestFile
+ ".FAILED");
490 // this is for the "real" finish
495 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
500 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
507 bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
510 // calc sha1 of the just patched file
511 string FinalFile
= _config
->FindDir("Dir::State::lists");
512 FinalFile
+= URItoFileName(RealURI
);
514 FileFd
fd(FinalFile
, FileFd::ReadOnlyGzip
);
516 SHA1
.AddFD(fd
.Fd(), fd
.Size());
517 string local_sha1
= string(SHA1
.Result());
519 std::clog
<< "QueueNextDiff: "
520 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
522 // final file reached before all patches are applied
523 if(local_sha1
== ServerSha1
)
529 // remove all patches until the next matching patch is found
530 // this requires the Index file to be ordered
531 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
532 available_patches
.size() > 0 &&
533 I
!= available_patches
.end() &&
534 (*I
).sha1
!= local_sha1
;
537 available_patches
.erase(I
);
540 // error checking and falling back if no patch was found
541 if(available_patches
.size() == 0)
547 // queue the right diff
548 Desc
.URI
= string(RealURI
) + ".diff/" + available_patches
[0].file
+ ".gz";
549 Desc
.Description
= Description
+ " " + available_patches
[0].file
+ string(".pdiff");
550 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
551 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
554 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
561 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
, /*{{{*/
562 pkgAcquire::MethodConfig
*Cnf
)
565 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
567 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
570 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
572 // sucess in downloading a diff, enter ApplyDiff state
573 if(State
== StateFetchDiff
)
577 std::clog
<< "Sending to gzip method: " << FinalFile
<< std::endl
;
579 string FileName
= LookupTag(Message
,"Filename");
580 State
= StateUnzipDiff
;
582 Desc
.URI
= "gzip:" + FileName
;
583 DestFile
+= ".decomp";
589 // sucess in downloading a diff, enter ApplyDiff state
590 if(State
== StateUnzipDiff
)
593 // rred excepts the patch as $FinalFile.ed
594 Rename(DestFile
,FinalFile
+".ed");
597 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
599 State
= StateApplyDiff
;
601 Desc
.URI
= "rred:" + FinalFile
;
608 // success in download/apply a diff, queue next (if needed)
609 if(State
== StateApplyDiff
)
611 // remove the just applied patch
612 available_patches
.erase(available_patches
.begin());
617 std::clog
<< "Moving patched file in place: " << std::endl
618 << DestFile
<< " -> " << FinalFile
<< std::endl
;
620 Rename(DestFile
,FinalFile
);
621 chmod(FinalFile
.c_str(),0644);
623 // see if there is more to download
624 if(available_patches
.size() > 0) {
625 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
626 ExpectedHash
, ServerSha1
, available_patches
);
633 // AcqIndex::AcqIndex - Constructor /*{{{*/
634 // ---------------------------------------------------------------------
635 /* The package file is added to the queue and a second class is
636 instantiated to fetch the revision file */
637 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
638 string URI
,string URIDesc
,string ShortDesc
,
639 HashString ExpectedHash
, string comprExt
)
640 : Item(Owner
), RealURI(URI
), ExpectedHash(ExpectedHash
)
642 Decompression
= false;
645 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
646 DestFile
+= URItoFileName(URI
);
650 // autoselect the compression method
651 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
652 if (types
.empty() == true)
655 comprExt
= "." + types
[0];
657 CompressionExtension
= ((comprExt
== "plain" || comprExt
== ".") ? "" : comprExt
);
659 Desc
.URI
= URI
+ CompressionExtension
;
661 Desc
.Description
= URIDesc
;
663 Desc
.ShortDesc
= ShortDesc
;
668 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
669 // ---------------------------------------------------------------------
670 /* The only header we use is the last-modified header. */
671 string
pkgAcqIndex::Custom600Headers()
673 string Final
= _config
->FindDir("Dir::State::lists");
674 Final
+= URItoFileName(RealURI
);
675 if (_config
->FindB("Acquire::GzipIndexes",false))
679 if (stat(Final
.c_str(),&Buf
) != 0)
680 return "\nIndex-File: true";
681 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
684 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
) /*{{{*/
686 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
688 for (std::vector
<std::string
>::const_iterator t
= types
.begin();
689 t
!= types
.end(); t
++)
691 // jump over all already tried compression types
692 const unsigned int nameLen
= Desc
.URI
.size() - (*t
).size();
693 if(Desc
.URI
.substr(nameLen
) != *t
)
696 // we want to try it with the next extension (and make sure to
697 // not skip over the end)
699 if (t
== types
.end())
702 // queue new download
703 Desc
.URI
= Desc
.URI
.substr(0, nameLen
) + *t
;
704 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
, Desc
.ShortDesc
,
705 ExpectedHash
, string(".").append(*t
));
713 // on decompression failure, remove bad versions in partial/
714 if(Decompression
&& Erase
) {
715 string s
= _config
->FindDir("Dir::State::lists") + "partial/";
716 s
+= URItoFileName(RealURI
);
720 Item::Failed(Message
,Cnf
);
723 // AcqIndex::Done - Finished a fetch /*{{{*/
724 // ---------------------------------------------------------------------
725 /* This goes through a number of states.. On the initial fetch the
726 method could possibly return an alternate filename which points
727 to the uncompressed version of the file. If this is so the file
728 is copied into the partial directory. In all other cases the file
729 is decompressed with a gzip uri. */
730 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string Hash
,
731 pkgAcquire::MethodConfig
*Cfg
)
733 Item::Done(Message
,Size
,Hash
,Cfg
);
735 if (Decompression
== true)
737 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
739 std::cerr
<< std::endl
<< RealURI
<< ": Computed Hash: " << Hash
;
740 std::cerr
<< " Expected Hash: " << ExpectedHash
.toStr() << std::endl
;
743 if (!ExpectedHash
.empty() && ExpectedHash
.toStr() != Hash
)
745 Status
= StatAuthError
;
746 ErrorText
= _("Hash Sum mismatch");
747 Rename(DestFile
,DestFile
+ ".FAILED");
748 ReportMirrorFailure("HashChecksumFailure");
751 // Done, move it into position
752 string FinalFile
= _config
->FindDir("Dir::State::lists");
753 FinalFile
+= URItoFileName(RealURI
);
754 Rename(DestFile
,FinalFile
);
755 chmod(FinalFile
.c_str(),0644);
757 /* We restore the original name to DestFile so that the clean operation
759 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
760 DestFile
+= URItoFileName(RealURI
);
762 // Remove the compressed version.
764 unlink(DestFile
.c_str());
771 // Handle the unzipd case
772 string FileName
= LookupTag(Message
,"Alt-Filename");
773 if (FileName
.empty() == false)
775 // The files timestamp matches
776 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
778 Decompression
= true;
780 DestFile
+= ".decomp";
781 Desc
.URI
= "copy:" + FileName
;
787 FileName
= LookupTag(Message
,"Filename");
788 if (FileName
.empty() == true)
791 ErrorText
= "Method gave a blank filename";
794 string compExt
= flExtension(flNotDir(URI(Desc
.URI
).Path
));
796 // The files timestamp matches
797 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true) {
798 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz")
799 // Update DestFile for .gz suffix so that the clean operation keeps it
804 if (FileName
== DestFile
)
811 // If we enable compressed indexes and already have gzip, keep it
812 if (_config
->FindB("Acquire::GzipIndexes",false) && compExt
== "gz" && !Local
) {
813 string FinalFile
= _config
->FindDir("Dir::State::lists");
814 FinalFile
+= URItoFileName(RealURI
) + ".gz";
815 Rename(DestFile
,FinalFile
);
816 chmod(FinalFile
.c_str(),0644);
818 // Update DestFile for .gz suffix so that the clean operation keeps it
819 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
820 DestFile
+= URItoFileName(RealURI
) + ".gz";
824 // get the binary name for your used compression type
825 decompProg
= _config
->Find(string("Acquire::CompressionTypes::").append(compExt
),"");
826 if(decompProg
.empty() == false);
827 // flExtensions returns the full name if no extension is found
828 // this is why we have this complicated compare operation here
829 // FIMXE: add a new flJustExtension() that return "" if no
830 // extension is found and use that above so that it can
831 // be tested against ""
832 else if(compExt
== flNotDir(URI(Desc
.URI
).Path
))
835 _error
->Error("Unsupported extension: %s", compExt
.c_str());
839 Decompression
= true;
840 DestFile
+= ".decomp";
841 Desc
.URI
= decompProg
+ ":" + FileName
;
843 Mode
= decompProg
.c_str();
846 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
847 // ---------------------------------------------------------------------
848 /* The Translation file is added to the queue */
849 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
850 string URI
,string URIDesc
,string ShortDesc
)
851 : pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, HashString(), "")
855 // AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
856 // ---------------------------------------------------------------------
857 string
pkgAcqIndexTrans::Custom600Headers()
859 string Final
= _config
->FindDir("Dir::State::lists");
860 Final
+= URItoFileName(RealURI
);
863 if (stat(Final
.c_str(),&Buf
) != 0)
864 return "\nFail-Ignore: true";
865 return "\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
868 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
869 // ---------------------------------------------------------------------
871 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
873 if (Cnf
->LocalOnly
== true ||
874 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
883 Item::Failed(Message
,Cnf
);
886 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
, /*{{{*/
887 string URI
,string URIDesc
,string ShortDesc
,
888 string MetaIndexURI
, string MetaIndexURIDesc
,
889 string MetaIndexShortDesc
,
890 const vector
<IndexTarget
*>* IndexTargets
,
891 indexRecords
* MetaIndexParser
) :
892 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
893 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
894 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
896 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
897 DestFile
+= URItoFileName(URI
);
899 // remove any partial downloaded sig-file in partial/.
900 // it may confuse proxies and is too small to warrant a
901 // partial download anyway
902 unlink(DestFile
.c_str());
905 Desc
.Description
= URIDesc
;
907 Desc
.ShortDesc
= ShortDesc
;
910 string Final
= _config
->FindDir("Dir::State::lists");
911 Final
+= URItoFileName(RealURI
);
913 if (stat(Final
.c_str(),&Buf
) == 0)
915 // File was already in place. It needs to be re-downloaded/verified
916 // because Release might have changed, we do give it a differnt
917 // name than DestFile because otherwise the http method will
918 // send If-Range requests and there are too many broken servers
919 // out there that do not understand them
920 LastGoodSig
= DestFile
+".reverify";
921 Rename(Final
,LastGoodSig
);
927 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
928 // ---------------------------------------------------------------------
929 /* The only header we use is the last-modified header. */
930 string
pkgAcqMetaSig::Custom600Headers()
933 if (stat(LastGoodSig
.c_str(),&Buf
) != 0)
934 return "\nIndex-File: true";
936 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
939 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
940 pkgAcquire::MethodConfig
*Cfg
)
942 Item::Done(Message
,Size
,MD5
,Cfg
);
944 string FileName
= LookupTag(Message
,"Filename");
945 if (FileName
.empty() == true)
948 ErrorText
= "Method gave a blank filename";
952 if (FileName
!= DestFile
)
954 // We have to copy it into place
956 Desc
.URI
= "copy:" + FileName
;
963 // put the last known good file back on i-m-s hit (it will
964 // be re-verified again)
965 // Else do nothing, we have the new file in DestFile then
966 if(StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
967 Rename(LastGoodSig
, DestFile
);
969 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
970 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
,
971 MetaIndexShortDesc
, DestFile
, IndexTargets
,
976 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)/*{{{*/
978 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
980 // if we get a network error we fail gracefully
981 if(Status
== StatTransientNetworkError
)
983 Item::Failed(Message
,Cnf
);
984 // move the sigfile back on transient network failures
985 if(FileExists(LastGoodSig
))
986 Rename(LastGoodSig
,Final
);
988 // set the status back to , Item::Failed likes to reset it
989 Status
= pkgAcquire::Item::StatTransientNetworkError
;
993 // Delete any existing sigfile when the acquire failed
994 unlink(Final
.c_str());
996 // queue a pkgAcqMetaIndex with no sigfile
997 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
998 "", IndexTargets
, MetaIndexParser
);
1000 if (Cnf
->LocalOnly
== true ||
1001 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
1010 Item::Failed(Message
,Cnf
);
1013 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
, /*{{{*/
1014 string URI
,string URIDesc
,string ShortDesc
,
1016 const vector
<struct IndexTarget
*>* IndexTargets
,
1017 indexRecords
* MetaIndexParser
) :
1018 Item(Owner
), RealURI(URI
), SigFile(SigFile
), IndexTargets(IndexTargets
),
1019 MetaIndexParser(MetaIndexParser
), AuthPass(false), IMSHit(false)
1021 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1022 DestFile
+= URItoFileName(URI
);
1025 Desc
.Description
= URIDesc
;
1027 Desc
.ShortDesc
= ShortDesc
;
1033 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
1034 // ---------------------------------------------------------------------
1035 /* The only header we use is the last-modified header. */
1036 string
pkgAcqMetaIndex::Custom600Headers()
1038 string Final
= _config
->FindDir("Dir::State::lists");
1039 Final
+= URItoFileName(RealURI
);
1042 if (stat(Final
.c_str(),&Buf
) != 0)
1043 return "\nIndex-File: true";
1045 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
1048 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string Hash
, /*{{{*/
1049 pkgAcquire::MethodConfig
*Cfg
)
1051 Item::Done(Message
,Size
,Hash
,Cfg
);
1053 // MetaIndexes are done in two passes: one to download the
1054 // metaindex with an appropriate method, and a second to verify it
1055 // with the gpgv method
1057 if (AuthPass
== true)
1061 // all cool, move Release file into place
1064 string FinalFile
= _config
->FindDir("Dir::State::lists");
1065 FinalFile
+= URItoFileName(RealURI
);
1066 Rename(DestFile
,FinalFile
);
1067 chmod(FinalFile
.c_str(),0644);
1068 DestFile
= FinalFile
;
1072 RetrievalDone(Message
);
1074 // Still more retrieving to do
1079 // There was no signature file, so we are finished. Download
1080 // the indexes without verification.
1081 QueueIndexes(false);
1085 // There was a signature file, so pass it to gpgv for
1088 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1089 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
1090 << SigFile
<< "," << DestFile
<< ")\n";
1092 Desc
.URI
= "gpgv:" + SigFile
;
1099 void pkgAcqMetaIndex::RetrievalDone(string Message
) /*{{{*/
1101 // We have just finished downloading a Release file (it is not
1104 string FileName
= LookupTag(Message
,"Filename");
1105 if (FileName
.empty() == true)
1108 ErrorText
= "Method gave a blank filename";
1112 if (FileName
!= DestFile
)
1115 Desc
.URI
= "copy:" + FileName
;
1120 // make sure to verify against the right file on I-M-S hit
1121 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
1124 string FinalFile
= _config
->FindDir("Dir::State::lists");
1125 FinalFile
+= URItoFileName(RealURI
);
1126 DestFile
= FinalFile
;
1131 void pkgAcqMetaIndex::AuthDone(string Message
) /*{{{*/
1133 // At this point, the gpgv method has succeeded, so there is a
1134 // valid signature from a key in the trusted keyring. We
1135 // perform additional verification of its contents, and use them
1136 // to verify the indexes we are about to download
1138 if (!MetaIndexParser
->Load(DestFile
))
1140 Status
= StatAuthError
;
1141 ErrorText
= MetaIndexParser
->ErrorText
;
1145 if (!VerifyVendor(Message
))
1150 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1151 std::cerr
<< "Signature verification succeeded: "
1152 << DestFile
<< std::endl
;
1154 // Download further indexes with verification
1157 // Done, move signature file into position
1158 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1159 URItoFileName(RealURI
) + ".gpg";
1160 Rename(SigFile
,VerifiedSigFile
);
1161 chmod(VerifiedSigFile
.c_str(),0644);
1164 void pkgAcqMetaIndex::QueueIndexes(bool verify
) /*{{{*/
1166 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
1167 Target
!= IndexTargets
->end();
1170 HashString ExpectedIndexHash
;
1173 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1176 Status
= StatAuthError
;
1177 ErrorText
= "Unable to find expected entry "
1178 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
1181 ExpectedIndexHash
= Record
->Hash
;
1182 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1184 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
1185 std::cerr
<< "Expected Hash: " << ExpectedIndexHash
.toStr() << std::endl
;
1187 if (ExpectedIndexHash
.empty())
1189 Status
= StatAuthError
;
1190 ErrorText
= "Unable to find hash sum for "
1191 + (*Target
)->MetaKey
+ " in Meta-index file";
1196 /* Queue Packages file (either diff or full packages files, depending
1197 on the users option) - we also check if the PDiff Index file is listed
1198 in the Meta-Index file. Ideal would be if pkgAcqDiffIndex would test this
1199 instead, but passing the required info to it is to much hassle */
1200 if(_config
->FindB("Acquire::PDiffs",true) == true && (verify
== false ||
1201 MetaIndexParser
->Exists(string((*Target
)->MetaKey
).append(".diff/Index")) == true))
1202 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1203 (*Target
)->ShortDesc
, ExpectedIndexHash
);
1205 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1206 (*Target
)->ShortDesc
, ExpectedIndexHash
);
1210 bool pkgAcqMetaIndex::VerifyVendor(string Message
) /*{{{*/
1212 // // Maybe this should be made available from above so we don't have
1213 // // to read and parse it every time?
1214 // pkgVendorList List;
1215 // List.ReadMainList();
1217 // const Vendor* Vndr = NULL;
1218 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1220 // string::size_type pos = (*I).find("VALIDSIG ");
1221 // if (_config->FindB("Debug::Vendor", false))
1222 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1224 // if (pos != std::string::npos)
1226 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1227 // if (_config->FindB("Debug::Vendor", false))
1228 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1230 // Vndr = List.FindVendor(Fingerprint) != "";
1231 // if (Vndr != NULL);
1235 string::size_type pos
;
1237 // check for missing sigs (that where not fatal because otherwise we had
1240 string msg
= _("There is no public key available for the "
1241 "following key IDs:\n");
1242 pos
= Message
.find("NO_PUBKEY ");
1243 if (pos
!= std::string::npos
)
1245 string::size_type start
= pos
+strlen("NO_PUBKEY ");
1246 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
1247 missingkeys
+= (Fingerprint
);
1249 if(!missingkeys
.empty())
1250 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
1252 string Transformed
= MetaIndexParser
->GetExpectedDist();
1254 if (Transformed
== "../project/experimental")
1256 Transformed
= "experimental";
1259 pos
= Transformed
.rfind('/');
1260 if (pos
!= string::npos
)
1262 Transformed
= Transformed
.substr(0, pos
);
1265 if (Transformed
== ".")
1270 if (_config
->FindB("Acquire::Check-Valid-Until", true) == true &&
1271 MetaIndexParser
->GetValidUntil() > 0) {
1272 time_t const invalid_since
= time(NULL
) - MetaIndexParser
->GetValidUntil();
1273 if (invalid_since
> 0)
1274 // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
1275 // the time since then the file is invalid - formated in the same way as in
1276 // the download progress display (e.g. 7d 3h 42min 1s)
1277 return _error
->Error(_("Release file expired, ignoring %s (invalid since %s)"),
1278 RealURI
.c_str(), TimeToStr(invalid_since
).c_str());
1281 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1283 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1284 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1285 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1288 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1290 // This might become fatal one day
1291 // Status = StatAuthError;
1292 // ErrorText = "Conflicting distribution; expected "
1293 // + MetaIndexParser->GetExpectedDist() + " but got "
1294 // + MetaIndexParser->GetDist();
1296 if (!Transformed
.empty())
1298 _error
->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
1299 Desc
.Description
.c_str(),
1300 Transformed
.c_str(),
1301 MetaIndexParser
->GetDist().c_str());
1308 // pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
1309 // ---------------------------------------------------------------------
1311 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1313 if (AuthPass
== true)
1315 // gpgv method failed, if we have a good signature
1316 string LastGoodSigFile
= _config
->FindDir("Dir::State::lists") +
1317 "partial/" + URItoFileName(RealURI
) + ".gpg.reverify";
1318 if(FileExists(LastGoodSigFile
))
1320 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1321 URItoFileName(RealURI
) + ".gpg";
1322 Rename(LastGoodSigFile
,VerifiedSigFile
);
1323 Status
= StatTransientNetworkError
;
1324 _error
->Warning(_("A error occurred during the signature "
1325 "verification. The repository is not updated "
1326 "and the previous index files will be used."
1327 "GPG error: %s: %s\n"),
1328 Desc
.Description
.c_str(),
1329 LookupTag(Message
,"Message").c_str());
1330 RunScripts("APT::Update::Auth-Failure");
1333 _error
->Warning(_("GPG error: %s: %s"),
1334 Desc
.Description
.c_str(),
1335 LookupTag(Message
,"Message").c_str());
1337 // gpgv method failed
1338 ReportMirrorFailure("GPGFailure");
1341 // No Release file was present, or verification failed, so fall
1342 // back to queueing Packages files without verification
1343 QueueIndexes(false);
1346 // AcqArchive::AcqArchive - Constructor /*{{{*/
1347 // ---------------------------------------------------------------------
1348 /* This just sets up the initial fetch environment and queues the first
1350 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1351 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1352 string
&StoreFilename
) :
1353 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1354 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1357 Retries
= _config
->FindI("Acquire::Retries",0);
1359 if (Version
.Arch() == 0)
1361 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1362 "This might mean you need to manually fix this package. "
1363 "(due to missing arch)"),
1364 Version
.ParentPkg().Name());
1368 /* We need to find a filename to determine the extension. We make the
1369 assumption here that all the available sources for this version share
1370 the same extension.. */
1371 // Skip not source sources, they do not have file fields.
1372 for (; Vf
.end() == false; Vf
++)
1374 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1379 // Does not really matter here.. we are going to fail out below
1380 if (Vf
.end() != true)
1382 // If this fails to get a file name we will bomb out below.
1383 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1384 if (_error
->PendingError() == true)
1387 // Generate the final file name as: package_version_arch.foo
1388 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1389 QuoteString(Version
.VerStr(),"_:") + '_' +
1390 QuoteString(Version
.Arch(),"_:.") +
1391 "." + flExtension(Parse
.FileName());
1394 // check if we have one trusted source for the package. if so, switch
1395 // to "TrustedOnly" mode
1396 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1398 pkgIndexFile
*Index
;
1399 if (Sources
->FindIndex(i
.File(),Index
) == false)
1401 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1403 std::cerr
<< "Checking index: " << Index
->Describe()
1404 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1406 if (Index
->IsTrusted()) {
1412 // "allow-unauthenticated" restores apts old fetching behaviour
1413 // that means that e.g. unauthenticated file:// uris are higher
1414 // priority than authenticated http:// uris
1415 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
1419 if (QueueNext() == false && _error
->PendingError() == false)
1420 _error
->Error(_("I wasn't able to locate file for the %s package. "
1421 "This might mean you need to manually fix this package."),
1422 Version
.ParentPkg().Name());
1425 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1426 // ---------------------------------------------------------------------
1427 /* This queues the next available file version for download. It checks if
1428 the archive is already available in the cache and stashs the MD5 for
1430 bool pkgAcqArchive::QueueNext()
1432 string
const ForceHash
= _config
->Find("Acquire::ForceHash");
1433 for (; Vf
.end() == false; Vf
++)
1435 // Ignore not source sources
1436 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1439 // Try to cross match against the source list
1440 pkgIndexFile
*Index
;
1441 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1444 // only try to get a trusted package from another source if that source
1446 if(Trusted
&& !Index
->IsTrusted())
1449 // Grab the text package record
1450 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1451 if (_error
->PendingError() == true)
1454 string PkgFile
= Parse
.FileName();
1455 if (ForceHash
.empty() == false)
1457 if(stringcasecmp(ForceHash
, "sha256") == 0)
1458 ExpectedHash
= HashString("SHA256", Parse
.SHA256Hash());
1459 else if (stringcasecmp(ForceHash
, "sha1") == 0)
1460 ExpectedHash
= HashString("SHA1", Parse
.SHA1Hash());
1462 ExpectedHash
= HashString("MD5Sum", Parse
.MD5Hash());
1467 if ((Hash
= Parse
.SHA256Hash()).empty() == false)
1468 ExpectedHash
= HashString("SHA256", Hash
);
1469 else if ((Hash
= Parse
.SHA1Hash()).empty() == false)
1470 ExpectedHash
= HashString("SHA1", Hash
);
1472 ExpectedHash
= HashString("MD5Sum", Parse
.MD5Hash());
1474 if (PkgFile
.empty() == true)
1475 return _error
->Error(_("The package index files are corrupted. No Filename: "
1476 "field for package %s."),
1477 Version
.ParentPkg().Name());
1479 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1480 Desc
.Description
= Index
->ArchiveInfo(Version
);
1482 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1484 // See if we already have the file. (Legacy filenames)
1485 FileSize
= Version
->Size
;
1486 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1488 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1490 // Make sure the size matches
1491 if ((unsigned)Buf
.st_size
== Version
->Size
)
1496 StoreFilename
= DestFile
= FinalFile
;
1500 /* Hmm, we have a file and its size does not match, this means it is
1501 an old style mismatched arch */
1502 unlink(FinalFile
.c_str());
1505 // Check it again using the new style output filenames
1506 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1507 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1509 // Make sure the size matches
1510 if ((unsigned)Buf
.st_size
== Version
->Size
)
1515 StoreFilename
= DestFile
= FinalFile
;
1519 /* Hmm, we have a file and its size does not match, this shouldnt
1521 unlink(FinalFile
.c_str());
1524 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1526 // Check the destination file
1527 if (stat(DestFile
.c_str(),&Buf
) == 0)
1529 // Hmm, the partial file is too big, erase it
1530 if ((unsigned)Buf
.st_size
> Version
->Size
)
1531 unlink(DestFile
.c_str());
1533 PartialSize
= Buf
.st_size
;
1538 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1539 Desc
.Description
= Index
->ArchiveInfo(Version
);
1541 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1550 // AcqArchive::Done - Finished fetching /*{{{*/
1551 // ---------------------------------------------------------------------
1553 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string CalcHash
,
1554 pkgAcquire::MethodConfig
*Cfg
)
1556 Item::Done(Message
,Size
,CalcHash
,Cfg
);
1559 if (Size
!= Version
->Size
)
1562 ErrorText
= _("Size mismatch");
1567 if(ExpectedHash
.toStr() != CalcHash
)
1570 ErrorText
= _("Hash Sum mismatch");
1571 if(FileExists(DestFile
))
1572 Rename(DestFile
,DestFile
+ ".FAILED");
1576 // Grab the output filename
1577 string FileName
= LookupTag(Message
,"Filename");
1578 if (FileName
.empty() == true)
1581 ErrorText
= "Method gave a blank filename";
1587 // Reference filename
1588 if (FileName
!= DestFile
)
1590 StoreFilename
= DestFile
= FileName
;
1595 // Done, move it into position
1596 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1597 FinalFile
+= flNotDir(StoreFilename
);
1598 Rename(DestFile
,FinalFile
);
1600 StoreFilename
= DestFile
= FinalFile
;
1604 // AcqArchive::Failed - Failure handler /*{{{*/
1605 // ---------------------------------------------------------------------
1606 /* Here we try other sources */
1607 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1609 ErrorText
= LookupTag(Message
,"Message");
1611 /* We don't really want to retry on failed media swaps, this prevents
1612 that. An interesting observation is that permanent failures are not
1614 if (Cnf
->Removable
== true &&
1615 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1617 // Vf = Version.FileList();
1618 while (Vf
.end() == false) Vf
++;
1619 StoreFilename
= string();
1620 Item::Failed(Message
,Cnf
);
1624 if (QueueNext() == false)
1626 // This is the retry counter
1628 Cnf
->LocalOnly
== false &&
1629 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1632 Vf
= Version
.FileList();
1633 if (QueueNext() == true)
1637 StoreFilename
= string();
1638 Item::Failed(Message
,Cnf
);
1642 // AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
1643 // ---------------------------------------------------------------------
1644 bool pkgAcqArchive::IsTrusted()
1649 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1650 // ---------------------------------------------------------------------
1652 void pkgAcqArchive::Finished()
1654 if (Status
== pkgAcquire::Item::StatDone
&&
1657 StoreFilename
= string();
1660 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1661 // ---------------------------------------------------------------------
1662 /* The file is added to the queue */
1663 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string Hash
,
1664 unsigned long Size
,string Dsc
,string ShortDesc
,
1665 const string
&DestDir
, const string
&DestFilename
,
1667 Item(Owner
), ExpectedHash(Hash
), IsIndexFile(IsIndexFile
)
1669 Retries
= _config
->FindI("Acquire::Retries",0);
1671 if(!DestFilename
.empty())
1672 DestFile
= DestFilename
;
1673 else if(!DestDir
.empty())
1674 DestFile
= DestDir
+ "/" + flNotDir(URI
);
1676 DestFile
= flNotDir(URI
);
1680 Desc
.Description
= Dsc
;
1683 // Set the short description to the archive component
1684 Desc
.ShortDesc
= ShortDesc
;
1686 // Get the transfer sizes
1689 if (stat(DestFile
.c_str(),&Buf
) == 0)
1691 // Hmm, the partial file is too big, erase it
1692 if ((unsigned)Buf
.st_size
> Size
)
1693 unlink(DestFile
.c_str());
1695 PartialSize
= Buf
.st_size
;
1701 // AcqFile::Done - Item downloaded OK /*{{{*/
1702 // ---------------------------------------------------------------------
1704 void pkgAcqFile::Done(string Message
,unsigned long Size
,string CalcHash
,
1705 pkgAcquire::MethodConfig
*Cnf
)
1707 Item::Done(Message
,Size
,CalcHash
,Cnf
);
1710 if(!ExpectedHash
.empty() && ExpectedHash
.toStr() != CalcHash
)
1713 ErrorText
= _("Hash Sum mismatch");
1714 Rename(DestFile
,DestFile
+ ".FAILED");
1718 string FileName
= LookupTag(Message
,"Filename");
1719 if (FileName
.empty() == true)
1722 ErrorText
= "Method gave a blank filename";
1728 // The files timestamp matches
1729 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1732 // We have to copy it into place
1733 if (FileName
!= DestFile
)
1736 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1737 Cnf
->Removable
== true)
1739 Desc
.URI
= "copy:" + FileName
;
1744 // Erase the file if it is a symlink so we can overwrite it
1746 if (lstat(DestFile
.c_str(),&St
) == 0)
1748 if (S_ISLNK(St
.st_mode
) != 0)
1749 unlink(DestFile
.c_str());
1753 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1755 ErrorText
= "Link to " + DestFile
+ " failure ";
1762 // AcqFile::Failed - Failure handler /*{{{*/
1763 // ---------------------------------------------------------------------
1764 /* Here we try other sources */
1765 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1767 ErrorText
= LookupTag(Message
,"Message");
1769 // This is the retry counter
1771 Cnf
->LocalOnly
== false &&
1772 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1779 Item::Failed(Message
,Cnf
);
1782 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
1783 // ---------------------------------------------------------------------
1784 /* The only header we use is the last-modified header. */
1785 string
pkgAcqFile::Custom600Headers()
1788 return "\nIndex-File: true";