]> git.saurik.com Git - apt.git/blame - apt-pkg/acquire-item.cc
show in madison command again also source packages (LP: #614589)
[apt.git] / apt-pkg / acquire-item.cc
CommitLineData
0118833a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 3// $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
0118833a
AL
4/* ######################################################################
5
6 Acquire Item - Item to acquire
7
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.
b185acc2 12
0118833a
AL
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
0118833a
AL
16#include <apt-pkg/acquire-item.h>
17#include <apt-pkg/configuration.h>
e878aedb 18#include <apt-pkg/aptconfiguration.h>
b2e465d6 19#include <apt-pkg/sourcelist.h>
b3d44315 20#include <apt-pkg/vendorlist.h>
03e39e59 21#include <apt-pkg/error.h>
cdcc6d34 22#include <apt-pkg/strutl.h>
36375005 23#include <apt-pkg/fileutl.h>
b3d44315 24#include <apt-pkg/md5.h>
ac5b205a
MV
25#include <apt-pkg/sha1.h>
26#include <apt-pkg/tagfile.h>
0a8a80e5 27
b2e465d6
AL
28#include <apti18n.h>
29
0a8a80e5
AL
30#include <sys/stat.h>
31#include <unistd.h>
c88edf1d 32#include <errno.h>
5819a761 33#include <string>
ac5b205a 34#include <sstream>
c88edf1d 35#include <stdio.h>
1ddb8596 36#include <ctime>
0118833a
AL
37 /*}}}*/
38
b3d44315 39using namespace std;
5819a761 40
0118833a
AL
41// Acquire::Item::Item - Constructor /*{{{*/
42// ---------------------------------------------------------------------
43/* */
8267fe24 44pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
6b1ff003
AL
45 PartialSize(0), Mode(0), ID(0), Complete(false),
46 Local(false), QueueCounter(0)
0118833a
AL
47{
48 Owner->Add(this);
c88edf1d 49 Status = StatIdle;
0118833a
AL
50}
51 /*}}}*/
52// Acquire::Item::~Item - Destructor /*{{{*/
53// ---------------------------------------------------------------------
54/* */
55pkgAcquire::Item::~Item()
56{
57 Owner->Remove(this);
58}
59 /*}}}*/
c88edf1d
AL
60// Acquire::Item::Failed - Item failed to download /*{{{*/
61// ---------------------------------------------------------------------
93bf083d
AL
62/* We return to an idle state if there are still other queues that could
63 fetch this object */
7d8afa39 64void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
c88edf1d 65{
93bf083d 66 Status = StatIdle;
db890fdb 67 ErrorText = LookupTag(Message,"Message");
361593e9 68 UsedMirror = LookupTag(Message,"UsedMirror");
c88edf1d 69 if (QueueCounter <= 1)
93bf083d 70 {
a72ace20 71 /* This indicates that the file is not available right now but might
7d8afa39 72 be sometime later. If we do a retry cycle then this should be
17caf1b1 73 retried [CDROMs] */
7d8afa39
AL
74 if (Cnf->LocalOnly == true &&
75 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
a72ace20
AL
76 {
77 Status = StatIdle;
681d76d0 78 Dequeue();
a72ace20
AL
79 return;
80 }
7e5f33eb 81
93bf083d 82 Status = StatError;
681d76d0 83 Dequeue();
93bf083d 84 }
23c5897c 85
36280399 86 // report mirror failure back to LP if we actually use a mirror
f0b509cd
MV
87 string FailReason = LookupTag(Message, "FailReason");
88 if(FailReason.size() != 0)
89 ReportMirrorFailure(FailReason);
90 else
91 ReportMirrorFailure(ErrorText);
c88edf1d
AL
92}
93 /*}}}*/
8267fe24
AL
94// Acquire::Item::Start - Item has begun to download /*{{{*/
95// ---------------------------------------------------------------------
17caf1b1
AL
96/* Stash status and the file size. Note that setting Complete means
97 sub-phases of the acquire process such as decompresion are operating */
727f18af 98void pkgAcquire::Item::Start(string /*Message*/,unsigned long Size)
8267fe24
AL
99{
100 Status = StatFetching;
101 if (FileSize == 0 && Complete == false)
102 FileSize = Size;
103}
104 /*}}}*/
c88edf1d
AL
105// Acquire::Item::Done - Item downloaded OK /*{{{*/
106// ---------------------------------------------------------------------
107/* */
495e5cb2 108void pkgAcquire::Item::Done(string Message,unsigned long Size,string Hash,
459681d3 109 pkgAcquire::MethodConfig *Cnf)
c88edf1d 110{
b98f2859
AL
111 // We just downloaded something..
112 string FileName = LookupTag(Message,"Filename");
36280399 113 UsedMirror = LookupTag(Message,"UsedMirror");
8f30ca30 114 if (Complete == false && !Local && FileName == DestFile)
b98f2859
AL
115 {
116 if (Owner->Log != 0)
117 Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
118 }
aa0e1101
AL
119
120 if (FileSize == 0)
121 FileSize= Size;
c88edf1d
AL
122 Status = StatDone;
123 ErrorText = string();
124 Owner->Dequeue(this);
125}
126 /*}}}*/
8b89e57f
AL
127// Acquire::Item::Rename - Rename a file /*{{{*/
128// ---------------------------------------------------------------------
129/* This helper function is used by alot of item methods as thier final
130 step */
131void pkgAcquire::Item::Rename(string From,string To)
132{
133 if (rename(From.c_str(),To.c_str()) != 0)
134 {
135 char S[300];
0fcd01de 136 snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
8b89e57f
AL
137 From.c_str(),To.c_str());
138 Status = StatError;
139 ErrorText = S;
7a3c2ab0 140 }
8b89e57f
AL
141}
142 /*}}}*/
c91d9a63
DK
143// Acquire::Item::ReportMirrorFailure /*{{{*/
144// ---------------------------------------------------------------------
36280399
MV
145void pkgAcquire::Item::ReportMirrorFailure(string FailCode)
146{
59271f62
MV
147 // we only act if a mirror was used at all
148 if(UsedMirror.empty())
149 return;
36280399
MV
150#if 0
151 std::cerr << "\nReportMirrorFailure: "
152 << UsedMirror
59271f62 153 << " Uri: " << DescURI()
36280399
MV
154 << " FailCode: "
155 << FailCode << std::endl;
156#endif
157 const char *Args[40];
158 unsigned int i = 0;
159 string report = _config->Find("Methods::Mirror::ProblemReporting",
3f599bb7 160 "/usr/lib/apt/apt-report-mirror-failure");
36280399
MV
161 if(!FileExists(report))
162 return;
163 Args[i++] = report.c_str();
164 Args[i++] = UsedMirror.c_str();
f0b509cd 165 Args[i++] = DescURI().c_str();
36280399 166 Args[i++] = FailCode.c_str();
361593e9 167 Args[i++] = NULL;
36280399
MV
168 pid_t pid = ExecFork();
169 if(pid < 0)
170 {
171 _error->Error("ReportMirrorFailure Fork failed");
172 return;
173 }
174 else if(pid == 0)
175 {
361593e9
MV
176 execvp(Args[0], (char**)Args);
177 std::cerr << "Could not exec " << Args[0] << std::endl;
178 _exit(100);
36280399
MV
179 }
180 if(!ExecWait(pid, "report-mirror-failure"))
181 {
182 _error->Warning("Couldn't report problem to '%s'",
361593e9 183 _config->Find("Methods::Mirror::ProblemReporting").c_str());
36280399
MV
184 }
185}
c91d9a63 186 /*}}}*/
92fcbfc1 187// AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
ac5b205a 188// ---------------------------------------------------------------------
2237bd01
MV
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
ac5b205a 193 */
2237bd01
MV
194pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
195 string URI,string URIDesc,string ShortDesc,
495e5cb2
MV
196 HashString ExpectedHash)
197 : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash),
198 Description(URIDesc)
ac5b205a
MV
199{
200
ac5b205a
MV
201 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
202
2237bd01 203 Desc.Description = URIDesc + "/DiffIndex";
ac5b205a
MV
204 Desc.Owner = this;
205 Desc.ShortDesc = ShortDesc;
2237bd01
MV
206 Desc.URI = URI + ".diff/Index";
207
208 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
209 DestFile += URItoFileName(URI) + string(".DiffIndex");
210
211 if(Debug)
212 std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
ac5b205a 213
2237bd01 214 // look for the current package file
ac5b205a
MV
215 CurrentPackagesFile = _config->FindDir("Dir::State::lists");
216 CurrentPackagesFile += URItoFileName(RealURI);
217
b4e57d2d
MV
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
ac5b205a 221 if(!FileExists(CurrentPackagesFile) ||
81fcf9e2 222 Desc.URI.substr(0,strlen("file:/")) == "file:/")
2ac3eeb6 223 {
ac5b205a
MV
224 // we don't have a pkg file or we don't want to queue
225 if(Debug)
b4e57d2d 226 std::clog << "No index file, local or canceld by user" << std::endl;
ac5b205a
MV
227 Failed("", NULL);
228 return;
229 }
230
2ac3eeb6 231 if(Debug)
2237bd01
MV
232 std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
233 << CurrentPackagesFile << std::endl;
2ac3eeb6 234
ac5b205a 235 QueueURI(Desc);
2237bd01 236
ac5b205a 237}
92fcbfc1 238 /*}}}*/
6cb30d01
MV
239// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
240// ---------------------------------------------------------------------
241/* The only header we use is the last-modified header. */
2237bd01 242string pkgAcqDiffIndex::Custom600Headers()
6cb30d01 243{
6cb30d01
MV
244 string Final = _config->FindDir("Dir::State::lists");
245 Final += URItoFileName(RealURI) + string(".IndexDiff");
246
247 if(Debug)
248 std::clog << "Custom600Header-IMS: " << Final << std::endl;
249
250 struct stat Buf;
251 if (stat(Final.c_str(),&Buf) != 0)
252 return "\nIndex-File: true";
253
254 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
255}
92fcbfc1
DK
256 /*}}}*/
257bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile) /*{{{*/
2237bd01
MV
258{
259 if(Debug)
260 std::clog << "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
261 << std::endl;
262
263 pkgTagSection Tags;
264 string ServerSha1;
265 vector<DiffInfo> available_patches;
266
267 FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
268 pkgTagFile TF(&Fd);
269 if (_error->PendingError() == true)
270 return false;
271
272 if(TF.Step(Tags) == true)
273 {
002d9943
MV
274 bool found = false;
275 DiffInfo d;
276 string size;
277
02dceb31 278 string const tmp = Tags.FindS("SHA1-Current");
2237bd01 279 std::stringstream ss(tmp);
02dceb31
DK
280 ss >> ServerSha1 >> size;
281 unsigned long const ServerSize = atol(size.c_str());
2237bd01 282
f213b6ea 283 FileFd fd(CurrentPackagesFile, FileFd::ReadOnly);
2237bd01
MV
284 SHA1Summation SHA1;
285 SHA1.AddFD(fd.Fd(), fd.Size());
02dceb31 286 string const local_sha1 = SHA1.Result();
2237bd01 287
2ac3eeb6
MV
288 if(local_sha1 == ServerSha1)
289 {
290 // we have the same sha1 as the server
2237bd01
MV
291 if(Debug)
292 std::clog << "Package file is up-to-date" << std::endl;
002d9943
MV
293 // set found to true, this will queue a pkgAcqIndexDiffs with
294 // a empty availabe_patches
295 found = true;
2ac3eeb6
MV
296 }
297 else
298 {
002d9943 299 if(Debug)
f213b6ea 300 std::clog << "SHA1-Current: " << ServerSha1 << " and we start at "<< fd.Name() << " " << fd.Size() << " " << local_sha1 << std::endl;
002d9943
MV
301
302 // check the historie and see what patches we need
02dceb31 303 string const history = Tags.FindS("SHA1-History");
002d9943 304 std::stringstream hist(history);
02dceb31 305 while(hist >> d.sha1 >> size >> d.file)
2ac3eeb6 306 {
002d9943 307 // read until the first match is found
02dceb31 308 // from that point on, we probably need all diffs
002d9943
MV
309 if(d.sha1 == local_sha1)
310 found=true;
02dceb31
DK
311 else if (found == false)
312 continue;
313
314 if(Debug)
315 std::clog << "Need to get diff: " << d.file << std::endl;
316 available_patches.push_back(d);
317 }
318
319 if (available_patches.empty() == false)
320 {
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())
324 {
325 if (Debug)
326 std::clog << "Need " << available_patches.size() << " diffs (Limit is " << fileLimit
327 << ") so fallback to complete download" << std::endl;
328 return false;
329 }
330
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)
338 {
339 if (firstPatch == d.file)
340 found = true;
341 else if (found == false)
342 continue;
343
344 patchesSize += atol(size.c_str());
345 }
346 unsigned long const sizeLimit = ServerSize * _config->FindI("Acquire::PDiffs::SizeLimit", 100);
347 if (sizeLimit > 0 && (sizeLimit/100) < patchesSize)
2ac3eeb6 348 {
02dceb31
DK
349 if (Debug)
350 std::clog << "Need " << patchesSize << " bytes (Limit is " << sizeLimit/100
351 << ") so fallback to complete download" << std::endl;
352 return false;
002d9943 353 }
2237bd01
MV
354 }
355 }
356
05aab406
MV
357 // we have something, queue the next diff
358 if(found)
2ac3eeb6 359 {
2237bd01 360 // queue the diffs
02dceb31 361 string::size_type const last_space = Description.rfind(" ");
05aab406
MV
362 if(last_space != string::npos)
363 Description.erase(last_space, Description.size()-last_space);
2237bd01 364 new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
8a3207f4 365 ExpectedHash, ServerSha1, available_patches);
2237bd01
MV
366 Complete = false;
367 Status = StatDone;
368 Dequeue();
369 return true;
370 }
371 }
05aab406
MV
372
373 // Nothing found, report and return false
374 // Failing here is ok, if we return false later, the full
375 // IndexFile is queued
376 if(Debug)
377 std::clog << "Can't find a patch in the index file" << std::endl;
2237bd01
MV
378 return false;
379}
92fcbfc1
DK
380 /*}}}*/
381void pkgAcqDiffIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
2237bd01
MV
382{
383 if(Debug)
384 std::clog << "pkgAcqDiffIndex failed: " << Desc.URI << std::endl
385 << "Falling back to normal index file aquire" << std::endl;
386
002d9943 387 new pkgAcqIndex(Owner, RealURI, Description, Desc.ShortDesc,
495e5cb2 388 ExpectedHash);
2237bd01
MV
389
390 Complete = false;
391 Status = StatDone;
392 Dequeue();
393}
92fcbfc1
DK
394 /*}}}*/
395void pkgAcqDiffIndex::Done(string Message,unsigned long Size,string Md5Hash, /*{{{*/
2237bd01
MV
396 pkgAcquire::MethodConfig *Cnf)
397{
398 if(Debug)
399 std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl;
400
401 Item::Done(Message,Size,Md5Hash,Cnf);
402
403 string FinalFile;
404 FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
405
406 // sucess in downloading the index
407 // rename the index
408 FinalFile += string(".IndexDiff");
409 if(Debug)
410 std::clog << "Renaming: " << DestFile << " -> " << FinalFile
411 << std::endl;
412 Rename(DestFile,FinalFile);
413 chmod(FinalFile.c_str(),0644);
414 DestFile = FinalFile;
415
416 if(!ParseDiffIndex(DestFile))
417 return Failed("", NULL);
418
419 Complete = true;
420 Status = StatDone;
421 Dequeue();
422 return;
423}
92fcbfc1
DK
424 /*}}}*/
425// AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
2237bd01
MV
426// ---------------------------------------------------------------------
427/* The package diff is added to the queue. one object is constructed
428 * for each diff and the index
429 */
430pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
431 string URI,string URIDesc,string ShortDesc,
59d5cc74 432 HashString ExpectedHash,
8a3207f4 433 string ServerSha1,
495e5cb2
MV
434 vector<DiffInfo> diffs)
435 : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash),
8a3207f4 436 available_patches(diffs), ServerSha1(ServerSha1)
2237bd01
MV
437{
438
439 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
440 DestFile += URItoFileName(URI);
441
442 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
443
05aab406 444 Description = URIDesc;
2237bd01
MV
445 Desc.Owner = this;
446 Desc.ShortDesc = ShortDesc;
447
2ac3eeb6
MV
448 if(available_patches.size() == 0)
449 {
2237bd01
MV
450 // we are done (yeah!)
451 Finish(true);
2ac3eeb6
MV
452 }
453 else
454 {
2237bd01
MV
455 // get the next diff
456 State = StateFetchDiff;
457 QueueNextDiff();
458 }
459}
92fcbfc1
DK
460 /*}}}*/
461void pkgAcqIndexDiffs::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
ac5b205a 462{
2237bd01
MV
463 if(Debug)
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,
495e5cb2 467 ExpectedHash);
ac5b205a
MV
468 Finish();
469}
92fcbfc1
DK
470 /*}}}*/
471// Finish - helper that cleans the item out of the fetcher queue /*{{{*/
ac5b205a
MV
472void pkgAcqIndexDiffs::Finish(bool allDone)
473{
474 // we restore the original name, this is required, otherwise
475 // the file will be cleaned
2ac3eeb6
MV
476 if(allDone)
477 {
ac5b205a
MV
478 DestFile = _config->FindDir("Dir::State::lists");
479 DestFile += URItoFileName(RealURI);
2d4722e2 480
495e5cb2 481 if(!ExpectedHash.empty() && !ExpectedHash.VerifyFile(DestFile))
2d4722e2
MV
482 {
483 Status = StatAuthError;
484 ErrorText = _("MD5Sum mismatch");
485 Rename(DestFile,DestFile + ".FAILED");
486 Dequeue();
487 return;
488 }
489
490 // this is for the "real" finish
ac5b205a 491 Complete = true;
cffc2ddd 492 Status = StatDone;
ac5b205a
MV
493 Dequeue();
494 if(Debug)
495 std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
496 return;
ac5b205a
MV
497 }
498
499 if(Debug)
500 std::clog << "Finishing: " << Desc.URI << std::endl;
501 Complete = false;
502 Status = StatDone;
503 Dequeue();
504 return;
505}
92fcbfc1
DK
506 /*}}}*/
507bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
ac5b205a 508{
3de9ff77 509
94dc9d7d
MV
510 // calc sha1 of the just patched file
511 string FinalFile = _config->FindDir("Dir::State::lists");
512 FinalFile += URItoFileName(RealURI);
513
f213b6ea 514 FileFd fd(FinalFile, FileFd::ReadOnly);
94dc9d7d
MV
515 SHA1Summation SHA1;
516 SHA1.AddFD(fd.Fd(), fd.Size());
517 string local_sha1 = string(SHA1.Result());
3de9ff77
MV
518 if(Debug)
519 std::clog << "QueueNextDiff: "
520 << FinalFile << " (" << local_sha1 << ")"<<std::endl;
94dc9d7d 521
8a3207f4
DK
522 // final file reached before all patches are applied
523 if(local_sha1 == ServerSha1)
524 {
525 Finish(true);
526 return true;
527 }
528
26d27645
MV
529 // remove all patches until the next matching patch is found
530 // this requires the Index file to be ordered
94dc9d7d 531 for(vector<DiffInfo>::iterator I=available_patches.begin();
2ac3eeb6
MV
532 available_patches.size() > 0 &&
533 I != available_patches.end() &&
534 (*I).sha1 != local_sha1;
535 I++)
536 {
26d27645 537 available_patches.erase(I);
59a704f0 538 }
94dc9d7d
MV
539
540 // error checking and falling back if no patch was found
2ac3eeb6
MV
541 if(available_patches.size() == 0)
542 {
94dc9d7d
MV
543 Failed("", NULL);
544 return false;
545 }
6cb30d01 546
94dc9d7d 547 // queue the right diff
2237bd01 548 Desc.URI = string(RealURI) + ".diff/" + available_patches[0].file + ".gz";
05aab406 549 Desc.Description = Description + " " + available_patches[0].file + string(".pdiff");
ac5b205a 550 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
2237bd01 551 DestFile += URItoFileName(RealURI + ".diff/" + available_patches[0].file);
ac5b205a
MV
552
553 if(Debug)
554 std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
555
556 QueueURI(Desc);
557
558 return true;
559}
92fcbfc1
DK
560 /*}}}*/
561void pkgAcqIndexDiffs::Done(string Message,unsigned long Size,string Md5Hash, /*{{{*/
ac5b205a
MV
562 pkgAcquire::MethodConfig *Cnf)
563{
564 if(Debug)
565 std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl;
566
567 Item::Done(Message,Size,Md5Hash,Cnf);
568
4a0a786f
MV
569 string FinalFile;
570 FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
6cb30d01 571
4a0a786f
MV
572 // sucess in downloading a diff, enter ApplyDiff state
573 if(State == StateFetchDiff)
574 {
575
576 if(Debug)
577 std::clog << "Sending to gzip method: " << FinalFile << std::endl;
578
579 string FileName = LookupTag(Message,"Filename");
580 State = StateUnzipDiff;
b7347826 581 Local = true;
4a0a786f
MV
582 Desc.URI = "gzip:" + FileName;
583 DestFile += ".decomp";
584 QueueURI(Desc);
585 Mode = "gzip";
586 return;
587 }
588
589 // sucess in downloading a diff, enter ApplyDiff state
590 if(State == StateUnzipDiff)
591 {
592
593 // rred excepts the patch as $FinalFile.ed
594 Rename(DestFile,FinalFile+".ed");
595
596 if(Debug)
597 std::clog << "Sending to rred method: " << FinalFile << std::endl;
598
599 State = StateApplyDiff;
b7347826 600 Local = true;
4a0a786f
MV
601 Desc.URI = "rred:" + FinalFile;
602 QueueURI(Desc);
603 Mode = "rred";
604 return;
605 }
606
607
608 // success in download/apply a diff, queue next (if needed)
609 if(State == StateApplyDiff)
610 {
611 // remove the just applied patch
94dc9d7d 612 available_patches.erase(available_patches.begin());
ac5b205a 613
4a0a786f 614 // move into place
59a704f0
MV
615 if(Debug)
616 {
4a0a786f
MV
617 std::clog << "Moving patched file in place: " << std::endl
618 << DestFile << " -> " << FinalFile << std::endl;
59a704f0 619 }
4a0a786f 620 Rename(DestFile,FinalFile);
1790e0cf 621 chmod(FinalFile.c_str(),0644);
4a0a786f
MV
622
623 // see if there is more to download
94dc9d7d 624 if(available_patches.size() > 0) {
ac5b205a 625 new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
8a3207f4 626 ExpectedHash, ServerSha1, available_patches);
4a0a786f
MV
627 return Finish();
628 } else
629 return Finish(true);
ac5b205a 630 }
ac5b205a 631}
92fcbfc1 632 /*}}}*/
0118833a
AL
633// AcqIndex::AcqIndex - Constructor /*{{{*/
634// ---------------------------------------------------------------------
635/* The package file is added to the queue and a second class is
b2e465d6
AL
636 instantiated to fetch the revision file */
637pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
b3d44315 638 string URI,string URIDesc,string ShortDesc,
495e5cb2
MV
639 HashString ExpectedHash, string comprExt)
640 : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash)
0118833a 641{
8b89e57f 642 Decompression = false;
bfd22fc0 643 Erase = false;
13e8426f 644
0a8a80e5 645 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
b2e465d6 646 DestFile += URItoFileName(URI);
8267fe24 647
b3d44315
MV
648 if(comprExt.empty())
649 {
13e8426f 650 // autoselect the compression method
e878aedb
DK
651 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
652 if (types.empty() == true)
653 comprExt = "plain";
654 else
655 comprExt = "." + types[0];
b3d44315 656 }
e85b4cd5
DK
657 CompressionExtension = ((comprExt == "plain" || comprExt == ".") ? "" : comprExt);
658
27d5a204 659 Desc.URI = URI + CompressionExtension;
b3d44315 660
b2e465d6 661 Desc.Description = URIDesc;
8267fe24 662 Desc.Owner = this;
b2e465d6 663 Desc.ShortDesc = ShortDesc;
8267fe24
AL
664
665 QueueURI(Desc);
0118833a
AL
666}
667 /*}}}*/
0a8a80e5 668// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
0118833a 669// ---------------------------------------------------------------------
0a8a80e5
AL
670/* The only header we use is the last-modified header. */
671string pkgAcqIndex::Custom600Headers()
0118833a 672{
0a8a80e5 673 string Final = _config->FindDir("Dir::State::lists");
b2e465d6 674 Final += URItoFileName(RealURI);
01606def 675 if (_config->FindB("Acquire::GzipIndexes",false))
676 Final += ".gz";
0a8a80e5
AL
677
678 struct stat Buf;
679 if (stat(Final.c_str(),&Buf) != 0)
a72ace20 680 return "\nIndex-File: true";
a72ace20 681 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
0118833a
AL
682}
683 /*}}}*/
92fcbfc1 684void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
debc84b2 685{
e878aedb 686 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
e85b4cd5 687
e878aedb
DK
688 for (std::vector<std::string>::const_iterator t = types.begin();
689 t != types.end(); t++)
e85b4cd5 690 {
e878aedb
DK
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)
e85b4cd5
DK
694 continue;
695
6abe2699
MV
696 // we want to try it with the next extension (and make sure to
697 // not skip over the end)
e878aedb 698 t++;
6abe2699
MV
699 if (t == types.end())
700 break;
e85b4cd5 701
6abe2699
MV
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));
706
707 Status = StatDone;
708 Complete = false;
709 Dequeue();
710 return;
0d7a243d
EL
711 }
712
17ff0930
MV
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);
717 unlink(s.c_str());
debc84b2
MZ
718 }
719
debc84b2
MZ
720 Item::Failed(Message,Cnf);
721}
92fcbfc1 722 /*}}}*/
8b89e57f
AL
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. */
495e5cb2 730void pkgAcqIndex::Done(string Message,unsigned long Size,string Hash,
459681d3 731 pkgAcquire::MethodConfig *Cfg)
8b89e57f 732{
495e5cb2 733 Item::Done(Message,Size,Hash,Cfg);
8b89e57f
AL
734
735 if (Decompression == true)
736 {
b3d44315
MV
737 if (_config->FindB("Debug::pkgAcquire::Auth", false))
738 {
495e5cb2
MV
739 std::cerr << std::endl << RealURI << ": Computed Hash: " << Hash;
740 std::cerr << " Expected Hash: " << ExpectedHash.toStr() << std::endl;
b3d44315
MV
741 }
742
8a8feb29 743 if (!ExpectedHash.empty() && ExpectedHash.toStr() != Hash)
b3d44315
MV
744 {
745 Status = StatAuthError;
9498d182 746 ErrorText = _("Hash Sum mismatch");
b3d44315 747 Rename(DestFile,DestFile + ".FAILED");
59271f62 748 ReportMirrorFailure("HashChecksumFailure");
b3d44315
MV
749 return;
750 }
8b89e57f
AL
751 // Done, move it into position
752 string FinalFile = _config->FindDir("Dir::State::lists");
b2e465d6 753 FinalFile += URItoFileName(RealURI);
8b89e57f 754 Rename(DestFile,FinalFile);
7a3c2ab0 755 chmod(FinalFile.c_str(),0644);
bfd22fc0 756
7a7fa5f0
AL
757 /* We restore the original name to DestFile so that the clean operation
758 will work OK */
759 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
b2e465d6 760 DestFile += URItoFileName(RealURI);
7a7fa5f0 761
bfd22fc0
AL
762 // Remove the compressed version.
763 if (Erase == true)
bfd22fc0 764 unlink(DestFile.c_str());
8b89e57f
AL
765 return;
766 }
bfd22fc0
AL
767
768 Erase = false;
8267fe24 769 Complete = true;
bfd22fc0 770
8b89e57f
AL
771 // Handle the unzipd case
772 string FileName = LookupTag(Message,"Alt-Filename");
773 if (FileName.empty() == false)
774 {
775 // The files timestamp matches
776 if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
777 return;
8b89e57f 778 Decompression = true;
a6568219 779 Local = true;
8b89e57f 780 DestFile += ".decomp";
8267fe24
AL
781 Desc.URI = "copy:" + FileName;
782 QueueURI(Desc);
b98f2859 783 Mode = "copy";
8b89e57f
AL
784 return;
785 }
786
787 FileName = LookupTag(Message,"Filename");
788 if (FileName.empty() == true)
789 {
790 Status = StatError;
791 ErrorText = "Method gave a blank filename";
792 }
793
0b9032b1 794 string compExt = flExtension(flNotDir(URI(Desc.URI).Path));
795
8b89e57f 796 // The files timestamp matches
0b9032b1 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
800 DestFile += ".gz";
8b89e57f 801 return;
0b9032b1 802 }
bfd22fc0
AL
803
804 if (FileName == DestFile)
805 Erase = true;
8267fe24 806 else
a6568219 807 Local = true;
8b89e57f 808
e85b4cd5
DK
809 string decompProg;
810
bb109d0b 811 // If we enable compressed indexes and already have gzip, keep it
7aeee365 812 if (_config->FindB("Acquire::GzipIndexes",false) && compExt == "gz" && !Local) {
bb109d0b 813 string FinalFile = _config->FindDir("Dir::State::lists");
814 FinalFile += URItoFileName(RealURI) + ".gz";
bb109d0b 815 Rename(DestFile,FinalFile);
816 chmod(FinalFile.c_str(),0644);
817
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";
821 return;
822 }
823
e85b4cd5
DK
824 // get the binary name for your used compression type
825 decompProg = _config->Find(string("Acquire::CompressionTypes::").append(compExt),"");
826 if(decompProg.empty() == false);
708ead3a 827 // flExtensions returns the full name if no extension is found
1c8ab720
MV
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 ""
708ead3a 832 else if(compExt == flNotDir(URI(Desc.URI).Path))
0d7a243d 833 decompProg = "copy";
debc84b2
MZ
834 else {
835 _error->Error("Unsupported extension: %s", compExt.c_str());
836 return;
837 }
838
8b89e57f
AL
839 Decompression = true;
840 DestFile += ".decomp";
e85b4cd5 841 Desc.URI = decompProg + ":" + FileName;
8267fe24 842 QueueURI(Desc);
e85b4cd5 843 Mode = decompProg.c_str();
8b89e57f 844}
92fcbfc1 845 /*}}}*/
a52f938b
OS
846// AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
847// ---------------------------------------------------------------------
848/* The Translation file is added to the queue */
849pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner,
495e5cb2
MV
850 string URI,string URIDesc,string ShortDesc)
851 : pkgAcqIndex(Owner, URI, URIDesc, ShortDesc, HashString(), "")
a52f938b 852{
963b16dc
MV
853}
854 /*}}}*/
855// AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
856// ---------------------------------------------------------------------
857string pkgAcqIndexTrans::Custom600Headers()
858{
c91d9a63
DK
859 string Final = _config->FindDir("Dir::State::lists");
860 Final += URItoFileName(RealURI);
861
862 struct stat Buf;
863 if (stat(Final.c_str(),&Buf) != 0)
864 return "\nFail-Ignore: true";
865 return "\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
a52f938b 866}
a52f938b
OS
867 /*}}}*/
868// AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
869// ---------------------------------------------------------------------
870/* */
871void pkgAcqIndexTrans::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
872{
873 if (Cnf->LocalOnly == true ||
874 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
875 {
876 // Ignore this
877 Status = StatDone;
878 Complete = false;
879 Dequeue();
880 return;
881 }
882
883 Item::Failed(Message,Cnf);
884}
885 /*}}}*/
92fcbfc1 886pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner, /*{{{*/
b3d44315
MV
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),
46e00f9d
MV
893 MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
894 MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets)
0118833a 895{
0a8a80e5 896 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
b2e465d6 897 DestFile += URItoFileName(URI);
b3d44315 898
47eb38f4
MV
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
f6237efd
MV
902 unlink(DestFile.c_str());
903
8267fe24 904 // Create the item
b2e465d6 905 Desc.Description = URIDesc;
8267fe24 906 Desc.Owner = this;
b3d44315
MV
907 Desc.ShortDesc = ShortDesc;
908 Desc.URI = URI;
b3d44315
MV
909
910 string Final = _config->FindDir("Dir::State::lists");
911 Final += URItoFileName(RealURI);
912 struct stat Buf;
913 if (stat(Final.c_str(),&Buf) == 0)
914 {
ef942597
MV
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);
b3d44315 922 }
8267fe24 923
8267fe24 924 QueueURI(Desc);
0118833a
AL
925}
926 /*}}}*/
b3d44315 927// pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
0118833a 928// ---------------------------------------------------------------------
0a8a80e5 929/* The only header we use is the last-modified header. */
b3d44315 930string pkgAcqMetaSig::Custom600Headers()
0118833a 931{
0a8a80e5 932 struct stat Buf;
ef942597 933 if (stat(LastGoodSig.c_str(),&Buf) != 0)
a72ace20 934 return "\nIndex-File: true";
a789b983 935
a72ace20 936 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
0118833a 937}
b3d44315
MV
938
939void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
940 pkgAcquire::MethodConfig *Cfg)
c88edf1d 941{
459681d3 942 Item::Done(Message,Size,MD5,Cfg);
c88edf1d
AL
943
944 string FileName = LookupTag(Message,"Filename");
945 if (FileName.empty() == true)
946 {
947 Status = StatError;
948 ErrorText = "Method gave a blank filename";
8b89e57f 949 return;
c88edf1d 950 }
8b89e57f 951
c88edf1d
AL
952 if (FileName != DestFile)
953 {
b3d44315 954 // We have to copy it into place
a6568219 955 Local = true;
8267fe24
AL
956 Desc.URI = "copy:" + FileName;
957 QueueURI(Desc);
c88edf1d
AL
958 return;
959 }
b3d44315
MV
960
961 Complete = true;
962
ef942597
MV
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);
968
b3d44315 969 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
fce72602
MV
970 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc,
971 MetaIndexShortDesc, DestFile, IndexTargets,
972 MetaIndexParser);
b3d44315 973
c88edf1d
AL
974}
975 /*}}}*/
92fcbfc1 976void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)/*{{{*/
681d76d0 977{
47eb38f4 978 string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
a789b983 979
75dd8af1 980 // if we get a network error we fail gracefully
7e5f33eb
MV
981 if(Status == StatTransientNetworkError)
982 {
24057ad6 983 Item::Failed(Message,Cnf);
484dbb81 984 // move the sigfile back on transient network failures
7730e095 985 if(FileExists(LastGoodSig))
ef942597 986 Rename(LastGoodSig,Final);
7e5f33eb
MV
987
988 // set the status back to , Item::Failed likes to reset it
989 Status = pkgAcquire::Item::StatTransientNetworkError;
24057ad6
MV
990 return;
991 }
992
75dd8af1 993 // Delete any existing sigfile when the acquire failed
75dd8af1
MV
994 unlink(Final.c_str());
995
b3d44315
MV
996 // queue a pkgAcqMetaIndex with no sigfile
997 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
998 "", IndexTargets, MetaIndexParser);
999
681d76d0
AL
1000 if (Cnf->LocalOnly == true ||
1001 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
1002 {
2b154e53
AL
1003 // Ignore this
1004 Status = StatDone;
1005 Complete = false;
681d76d0
AL
1006 Dequeue();
1007 return;
1008 }
1009
1010 Item::Failed(Message,Cnf);
1011}
92fcbfc1
DK
1012 /*}}}*/
1013pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner, /*{{{*/
b3d44315
MV
1014 string URI,string URIDesc,string ShortDesc,
1015 string SigFile,
1016 const vector<struct IndexTarget*>* IndexTargets,
1017 indexRecords* MetaIndexParser) :
21fd1746
OS
1018 Item(Owner), RealURI(URI), SigFile(SigFile), IndexTargets(IndexTargets),
1019 MetaIndexParser(MetaIndexParser), AuthPass(false), IMSHit(false)
b3d44315 1020{
b3d44315
MV
1021 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
1022 DestFile += URItoFileName(URI);
1023
1024 // Create the item
1025 Desc.Description = URIDesc;
1026 Desc.Owner = this;
1027 Desc.ShortDesc = ShortDesc;
1028 Desc.URI = URI;
1029
1030 QueueURI(Desc);
1031}
b3d44315
MV
1032 /*}}}*/
1033// pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
1034// ---------------------------------------------------------------------
1035/* The only header we use is the last-modified header. */
1036string pkgAcqMetaIndex::Custom600Headers()
1037{
1038 string Final = _config->FindDir("Dir::State::lists");
1039 Final += URItoFileName(RealURI);
1040
1041 struct stat Buf;
1042 if (stat(Final.c_str(),&Buf) != 0)
1043 return "\nIndex-File: true";
1044
1045 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
1046}
92fcbfc1
DK
1047 /*}}}*/
1048void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string Hash, /*{{{*/
b3d44315
MV
1049 pkgAcquire::MethodConfig *Cfg)
1050{
495e5cb2 1051 Item::Done(Message,Size,Hash,Cfg);
b3d44315
MV
1052
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
1056
1057 if (AuthPass == true)
1058 {
1059 AuthDone(Message);
fce72602
MV
1060
1061 // all cool, move Release file into place
1062 Complete = true;
1063
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;
b3d44315
MV
1069 }
1070 else
1071 {
1072 RetrievalDone(Message);
1073 if (!Complete)
1074 // Still more retrieving to do
1075 return;
1076
1077 if (SigFile == "")
1078 {
1079 // There was no signature file, so we are finished. Download
1080 // the indexes without verification.
1081 QueueIndexes(false);
1082 }
1083 else
1084 {
1085 // There was a signature file, so pass it to gpgv for
1086 // verification
1087
1088 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1089 std::cerr << "Metaindex acquired, queueing gpg verification ("
1090 << SigFile << "," << DestFile << ")\n";
1091 AuthPass = true;
1092 Desc.URI = "gpgv:" + SigFile;
1093 QueueURI(Desc);
1094 Mode = "gpgv";
1095 }
1096 }
1097}
92fcbfc1
DK
1098 /*}}}*/
1099void pkgAcqMetaIndex::RetrievalDone(string Message) /*{{{*/
b3d44315
MV
1100{
1101 // We have just finished downloading a Release file (it is not
1102 // verified yet)
1103
1104 string FileName = LookupTag(Message,"Filename");
1105 if (FileName.empty() == true)
1106 {
1107 Status = StatError;
1108 ErrorText = "Method gave a blank filename";
1109 return;
1110 }
1111
1112 if (FileName != DestFile)
1113 {
1114 Local = true;
1115 Desc.URI = "copy:" + FileName;
1116 QueueURI(Desc);
1117 return;
1118 }
1119
fce72602 1120 // make sure to verify against the right file on I-M-S hit
f381d68d 1121 IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false);
fce72602
MV
1122 if(IMSHit)
1123 {
1124 string FinalFile = _config->FindDir("Dir::State::lists");
1125 FinalFile += URItoFileName(RealURI);
1126 DestFile = FinalFile;
1127 }
b3d44315 1128 Complete = true;
b3d44315 1129}
92fcbfc1
DK
1130 /*}}}*/
1131void pkgAcqMetaIndex::AuthDone(string Message) /*{{{*/
b3d44315
MV
1132{
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
1137
1138 if (!MetaIndexParser->Load(DestFile))
1139 {
1140 Status = StatAuthError;
1141 ErrorText = MetaIndexParser->ErrorText;
1142 return;
1143 }
1144
ce424cd4 1145 if (!VerifyVendor(Message))
b3d44315
MV
1146 {
1147 return;
1148 }
1149
1150 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1151 std::cerr << "Signature verification succeeded: "
1152 << DestFile << std::endl;
1153
1154 // Download further indexes with verification
1155 QueueIndexes(true);
1156
1157 // Done, move signature file into position
b3d44315
MV
1158 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
1159 URItoFileName(RealURI) + ".gpg";
1160 Rename(SigFile,VerifiedSigFile);
1161 chmod(VerifiedSigFile.c_str(),0644);
1162}
92fcbfc1
DK
1163 /*}}}*/
1164void pkgAcqMetaIndex::QueueIndexes(bool verify) /*{{{*/
b3d44315
MV
1165{
1166 for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
1167 Target != IndexTargets->end();
1168 Target++)
1169 {
495e5cb2 1170 HashString ExpectedIndexHash;
b3d44315
MV
1171 if (verify)
1172 {
1173 const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
1174 if (!Record)
1175 {
1176 Status = StatAuthError;
1177 ErrorText = "Unable to find expected entry "
1178 + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
1179 return;
1180 }
495e5cb2 1181 ExpectedIndexHash = Record->Hash;
b3d44315
MV
1182 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1183 {
1184 std::cerr << "Queueing: " << (*Target)->URI << std::endl;
495e5cb2 1185 std::cerr << "Expected Hash: " << ExpectedIndexHash.toStr() << std::endl;
b3d44315 1186 }
495e5cb2 1187 if (ExpectedIndexHash.empty())
b3d44315
MV
1188 {
1189 Status = StatAuthError;
495e5cb2 1190 ErrorText = "Unable to find hash sum for "
b3d44315
MV
1191 + (*Target)->MetaKey + " in Meta-index file";
1192 return;
1193 }
1194 }
e1430400
DK
1195
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))
2ac3eeb6 1202 new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
495e5cb2 1203 (*Target)->ShortDesc, ExpectedIndexHash);
e1430400 1204 else
81fcf9e2 1205 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
495e5cb2 1206 (*Target)->ShortDesc, ExpectedIndexHash);
b3d44315
MV
1207 }
1208}
92fcbfc1
DK
1209 /*}}}*/
1210bool pkgAcqMetaIndex::VerifyVendor(string Message) /*{{{*/
b3d44315
MV
1211{
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();
1216
1217// const Vendor* Vndr = NULL;
1218// for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1219// {
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
1223// << std::endl;
1224// if (pos != std::string::npos)
1225// {
1226// string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1227// if (_config->FindB("Debug::Vendor", false))
1228// std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1229// std::endl;
1230// Vndr = List.FindVendor(Fingerprint) != "";
1231// if (Vndr != NULL);
1232// break;
1233// }
1234// }
ce424cd4
MV
1235 string::size_type pos;
1236
1237 // check for missing sigs (that where not fatal because otherwise we had
1238 // bombed earlier)
1239 string missingkeys;
400ad7a4 1240 string msg = _("There is no public key available for the "
ce424cd4
MV
1241 "following key IDs:\n");
1242 pos = Message.find("NO_PUBKEY ");
1243 if (pos != std::string::npos)
1244 {
1245 string::size_type start = pos+strlen("NO_PUBKEY ");
1246 string Fingerprint = Message.substr(start, Message.find("\n")-start);
1247 missingkeys += (Fingerprint);
1248 }
1249 if(!missingkeys.empty())
1250 _error->Warning("%s", string(msg+missingkeys).c_str());
b3d44315
MV
1251
1252 string Transformed = MetaIndexParser->GetExpectedDist();
1253
1254 if (Transformed == "../project/experimental")
1255 {
1256 Transformed = "experimental";
1257 }
1258
ce424cd4 1259 pos = Transformed.rfind('/');
b3d44315
MV
1260 if (pos != string::npos)
1261 {
1262 Transformed = Transformed.substr(0, pos);
1263 }
1264
1265 if (Transformed == ".")
1266 {
1267 Transformed = "";
1268 }
1269
0323317c
DK
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());
1ddb8596
DK
1279 }
1280
b3d44315
MV
1281 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1282 {
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;
1286 }
1287
1288 if (MetaIndexParser->CheckDist(Transformed) == false)
1289 {
1290 // This might become fatal one day
1291// Status = StatAuthError;
1292// ErrorText = "Conflicting distribution; expected "
1293// + MetaIndexParser->GetExpectedDist() + " but got "
1294// + MetaIndexParser->GetDist();
1295// return false;
1296 if (!Transformed.empty())
1297 {
1ddb8596 1298 _error->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
b3d44315
MV
1299 Desc.Description.c_str(),
1300 Transformed.c_str(),
1301 MetaIndexParser->GetDist().c_str());
1302 }
1303 }
1304
1305 return true;
1306}
92fcbfc1
DK
1307 /*}}}*/
1308// pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
b3d44315
MV
1309// ---------------------------------------------------------------------
1310/* */
1311void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1312{
1313 if (AuthPass == true)
1314 {
fce72602
MV
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))
f381d68d 1319 {
fce72602
MV
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."
76b8e5a5 1327 "GPG error: %s: %s\n"),
fce72602
MV
1328 Desc.Description.c_str(),
1329 LookupTag(Message,"Message").c_str());
5d149bfc 1330 RunScripts("APT::Update::Auth-Failure");
f381d68d 1331 return;
fce72602
MV
1332 } else {
1333 _error->Warning(_("GPG error: %s: %s"),
1334 Desc.Description.c_str(),
1335 LookupTag(Message,"Message").c_str());
f381d68d 1336 }
f381d68d 1337 // gpgv method failed
59271f62 1338 ReportMirrorFailure("GPGFailure");
b3d44315
MV
1339 }
1340
1341 // No Release file was present, or verification failed, so fall
1342 // back to queueing Packages files without verification
1343 QueueIndexes(false);
1344}
681d76d0 1345 /*}}}*/
03e39e59
AL
1346// AcqArchive::AcqArchive - Constructor /*{{{*/
1347// ---------------------------------------------------------------------
17caf1b1
AL
1348/* This just sets up the initial fetch environment and queues the first
1349 possibilitiy */
03e39e59 1350pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
30e1eab5
AL
1351 pkgRecords *Recs,pkgCache::VerIterator const &Version,
1352 string &StoreFilename) :
1353 Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
b3d44315
MV
1354 StoreFilename(StoreFilename), Vf(Version.FileList()),
1355 Trusted(false)
03e39e59 1356{
7d8afa39 1357 Retries = _config->FindI("Acquire::Retries",0);
813c8eea
AL
1358
1359 if (Version.Arch() == 0)
bdae53f1 1360 {
d1f1f6a8 1361 _error->Error(_("I wasn't able to locate a file for the %s package. "
7a3c2ab0
AL
1362 "This might mean you need to manually fix this package. "
1363 "(due to missing arch)"),
813c8eea 1364 Version.ParentPkg().Name());
bdae53f1
AL
1365 return;
1366 }
813c8eea 1367
b2e465d6
AL
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++)
1373 {
1374 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1375 continue;
1376 break;
1377 }
1378
1379 // Does not really matter here.. we are going to fail out below
1380 if (Vf.end() != true)
1381 {
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)
1385 return;
1386
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());
1392 }
b3d44315
MV
1393
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++)
1397 {
1398 pkgIndexFile *Index;
1399 if (Sources->FindIndex(i.File(),Index) == false)
1400 continue;
1401 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1402 {
1403 std::cerr << "Checking index: " << Index->Describe()
1404 << "(Trusted=" << Index->IsTrusted() << ")\n";
1405 }
1406 if (Index->IsTrusted()) {
1407 Trusted = true;
1408 break;
1409 }
1410 }
1411
a3371852
MV
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)
1416 Trusted = false;
1417
03e39e59 1418 // Select a source
b185acc2 1419 if (QueueNext() == false && _error->PendingError() == false)
b2e465d6
AL
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."),
b185acc2
AL
1422 Version.ParentPkg().Name());
1423}
1424 /*}}}*/
1425// AcqArchive::QueueNext - Queue the next file source /*{{{*/
1426// ---------------------------------------------------------------------
17caf1b1
AL
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
1429 checking later. */
b185acc2 1430bool pkgAcqArchive::QueueNext()
a722b2c5
DK
1431{
1432 string const ForceHash = _config->Find("Acquire::ForceHash");
03e39e59
AL
1433 for (; Vf.end() == false; Vf++)
1434 {
1435 // Ignore not source sources
1436 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1437 continue;
1438
1439 // Try to cross match against the source list
b2e465d6
AL
1440 pkgIndexFile *Index;
1441 if (Sources->FindIndex(Vf.File(),Index) == false)
1442 continue;
03e39e59 1443
b3d44315
MV
1444 // only try to get a trusted package from another source if that source
1445 // is also trusted
1446 if(Trusted && !Index->IsTrusted())
1447 continue;
1448
03e39e59
AL
1449 // Grab the text package record
1450 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
1451 if (_error->PendingError() == true)
b185acc2 1452 return false;
03e39e59 1453
b2e465d6 1454 string PkgFile = Parse.FileName();
a722b2c5
DK
1455 if (ForceHash.empty() == false)
1456 {
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());
1461 else
1462 ExpectedHash = HashString("MD5Sum", Parse.MD5Hash());
1463 }
1464 else
1465 {
1466 string Hash;
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);
1471 else
1472 ExpectedHash = HashString("MD5Sum", Parse.MD5Hash());
1473 }
03e39e59 1474 if (PkgFile.empty() == true)
b2e465d6
AL
1475 return _error->Error(_("The package index files are corrupted. No Filename: "
1476 "field for package %s."),
1477 Version.ParentPkg().Name());
a6568219 1478
b3d44315
MV
1479 Desc.URI = Index->ArchiveURI(PkgFile);
1480 Desc.Description = Index->ArchiveInfo(Version);
1481 Desc.Owner = this;
1482 Desc.ShortDesc = Version.ParentPkg().Name();
1483
17caf1b1 1484 // See if we already have the file. (Legacy filenames)
a6568219
AL
1485 FileSize = Version->Size;
1486 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
1487 struct stat Buf;
1488 if (stat(FinalFile.c_str(),&Buf) == 0)
1489 {
1490 // Make sure the size matches
1491 if ((unsigned)Buf.st_size == Version->Size)
1492 {
1493 Complete = true;
1494 Local = true;
1495 Status = StatDone;
30e1eab5 1496 StoreFilename = DestFile = FinalFile;
b185acc2 1497 return true;
a6568219
AL
1498 }
1499
6b1ff003
AL
1500 /* Hmm, we have a file and its size does not match, this means it is
1501 an old style mismatched arch */
a6568219
AL
1502 unlink(FinalFile.c_str());
1503 }
17caf1b1
AL
1504
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)
1508 {
1509 // Make sure the size matches
1510 if ((unsigned)Buf.st_size == Version->Size)
1511 {
1512 Complete = true;
1513 Local = true;
1514 Status = StatDone;
1515 StoreFilename = DestFile = FinalFile;
1516 return true;
1517 }
1518
1519 /* Hmm, we have a file and its size does not match, this shouldnt
1520 happen.. */
1521 unlink(FinalFile.c_str());
1522 }
1523
1524 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
6b1ff003
AL
1525
1526 // Check the destination file
1527 if (stat(DestFile.c_str(),&Buf) == 0)
1528 {
1529 // Hmm, the partial file is too big, erase it
1530 if ((unsigned)Buf.st_size > Version->Size)
1531 unlink(DestFile.c_str());
1532 else
1533 PartialSize = Buf.st_size;
1534 }
1535
03e39e59 1536 // Create the item
b2e465d6
AL
1537 Local = false;
1538 Desc.URI = Index->ArchiveURI(PkgFile);
1539 Desc.Description = Index->ArchiveInfo(Version);
03e39e59
AL
1540 Desc.Owner = this;
1541 Desc.ShortDesc = Version.ParentPkg().Name();
1542 QueueURI(Desc);
b185acc2
AL
1543
1544 Vf++;
1545 return true;
03e39e59 1546 }
b185acc2
AL
1547 return false;
1548}
03e39e59
AL
1549 /*}}}*/
1550// AcqArchive::Done - Finished fetching /*{{{*/
1551// ---------------------------------------------------------------------
1552/* */
495e5cb2 1553void pkgAcqArchive::Done(string Message,unsigned long Size,string CalcHash,
459681d3 1554 pkgAcquire::MethodConfig *Cfg)
03e39e59 1555{
495e5cb2 1556 Item::Done(Message,Size,CalcHash,Cfg);
03e39e59
AL
1557
1558 // Check the size
1559 if (Size != Version->Size)
1560 {
bdae53f1 1561 Status = StatError;
b2e465d6 1562 ErrorText = _("Size mismatch");
03e39e59
AL
1563 return;
1564 }
1565
495e5cb2 1566 // Check the hash
8a8feb29 1567 if(ExpectedHash.toStr() != CalcHash)
03e39e59 1568 {
495e5cb2 1569 Status = StatError;
9498d182 1570 ErrorText = _("Hash Sum mismatch");
495e5cb2
MV
1571 if(FileExists(DestFile))
1572 Rename(DestFile,DestFile + ".FAILED");
1573 return;
03e39e59 1574 }
a6568219
AL
1575
1576 // Grab the output filename
03e39e59
AL
1577 string FileName = LookupTag(Message,"Filename");
1578 if (FileName.empty() == true)
1579 {
1580 Status = StatError;
1581 ErrorText = "Method gave a blank filename";
1582 return;
1583 }
a6568219
AL
1584
1585 Complete = true;
30e1eab5
AL
1586
1587 // Reference filename
a6568219
AL
1588 if (FileName != DestFile)
1589 {
30e1eab5 1590 StoreFilename = DestFile = FileName;
a6568219
AL
1591 Local = true;
1592 return;
1593 }
1594
1595 // Done, move it into position
1596 string FinalFile = _config->FindDir("Dir::Cache::Archives");
17caf1b1 1597 FinalFile += flNotDir(StoreFilename);
a6568219 1598 Rename(DestFile,FinalFile);
03e39e59 1599
30e1eab5 1600 StoreFilename = DestFile = FinalFile;
03e39e59
AL
1601 Complete = true;
1602}
1603 /*}}}*/
db890fdb
AL
1604// AcqArchive::Failed - Failure handler /*{{{*/
1605// ---------------------------------------------------------------------
1606/* Here we try other sources */
7d8afa39 1607void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
db890fdb
AL
1608{
1609 ErrorText = LookupTag(Message,"Message");
b2e465d6
AL
1610
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
1613 recorded. */
1614 if (Cnf->Removable == true &&
1615 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1616 {
1617 // Vf = Version.FileList();
1618 while (Vf.end() == false) Vf++;
1619 StoreFilename = string();
1620 Item::Failed(Message,Cnf);
1621 return;
1622 }
1623
db890fdb 1624 if (QueueNext() == false)
7d8afa39
AL
1625 {
1626 // This is the retry counter
1627 if (Retries != 0 &&
1628 Cnf->LocalOnly == false &&
1629 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1630 {
1631 Retries--;
1632 Vf = Version.FileList();
1633 if (QueueNext() == true)
1634 return;
1635 }
1636
9dbb421f 1637 StoreFilename = string();
7d8afa39
AL
1638 Item::Failed(Message,Cnf);
1639 }
db890fdb
AL
1640}
1641 /*}}}*/
92fcbfc1 1642// AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
b3d44315
MV
1643// ---------------------------------------------------------------------
1644bool pkgAcqArchive::IsTrusted()
1645{
1646 return Trusted;
1647}
92fcbfc1 1648 /*}}}*/
ab559b35
AL
1649// AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1650// ---------------------------------------------------------------------
1651/* */
1652void pkgAcqArchive::Finished()
1653{
1654 if (Status == pkgAcquire::Item::StatDone &&
1655 Complete == true)
1656 return;
1657 StoreFilename = string();
1658}
1659 /*}}}*/
36375005
AL
1660// AcqFile::pkgAcqFile - Constructor /*{{{*/
1661// ---------------------------------------------------------------------
1662/* The file is added to the queue */
8a8feb29 1663pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string Hash,
46e00f9d 1664 unsigned long Size,string Dsc,string ShortDesc,
77278c2b
MV
1665 const string &DestDir, const string &DestFilename,
1666 bool IsIndexFile) :
1667 Item(Owner), ExpectedHash(Hash), IsIndexFile(IsIndexFile)
36375005 1668{
08cfc005
AL
1669 Retries = _config->FindI("Acquire::Retries",0);
1670
46e00f9d
MV
1671 if(!DestFilename.empty())
1672 DestFile = DestFilename;
1673 else if(!DestDir.empty())
1674 DestFile = DestDir + "/" + flNotDir(URI);
1675 else
1676 DestFile = flNotDir(URI);
1677
36375005
AL
1678 // Create the item
1679 Desc.URI = URI;
1680 Desc.Description = Dsc;
1681 Desc.Owner = this;
1682
1683 // Set the short description to the archive component
1684 Desc.ShortDesc = ShortDesc;
1685
1686 // Get the transfer sizes
1687 FileSize = Size;
1688 struct stat Buf;
1689 if (stat(DestFile.c_str(),&Buf) == 0)
1690 {
1691 // Hmm, the partial file is too big, erase it
1692 if ((unsigned)Buf.st_size > Size)
1693 unlink(DestFile.c_str());
1694 else
1695 PartialSize = Buf.st_size;
1696 }
092ae175 1697
36375005
AL
1698 QueueURI(Desc);
1699}
1700 /*}}}*/
1701// AcqFile::Done - Item downloaded OK /*{{{*/
1702// ---------------------------------------------------------------------
1703/* */
495e5cb2 1704void pkgAcqFile::Done(string Message,unsigned long Size,string CalcHash,
459681d3 1705 pkgAcquire::MethodConfig *Cnf)
36375005 1706{
495e5cb2
MV
1707 Item::Done(Message,Size,CalcHash,Cnf);
1708
8a8feb29 1709 // Check the hash
2c941d89 1710 if(!ExpectedHash.empty() && ExpectedHash.toStr() != CalcHash)
b3c39978 1711 {
495e5cb2 1712 Status = StatError;
f5eb830c 1713 ErrorText = _("Hash Sum mismatch");
495e5cb2
MV
1714 Rename(DestFile,DestFile + ".FAILED");
1715 return;
b3c39978
AL
1716 }
1717
36375005
AL
1718 string FileName = LookupTag(Message,"Filename");
1719 if (FileName.empty() == true)
1720 {
1721 Status = StatError;
1722 ErrorText = "Method gave a blank filename";
1723 return;
1724 }
1725
1726 Complete = true;
1727
1728 // The files timestamp matches
1729 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
1730 return;
1731
1732 // We have to copy it into place
1733 if (FileName != DestFile)
1734 {
1735 Local = true;
459681d3
AL
1736 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
1737 Cnf->Removable == true)
917ae805
AL
1738 {
1739 Desc.URI = "copy:" + FileName;
1740 QueueURI(Desc);
1741 return;
1742 }
1743
83ab33fc
AL
1744 // Erase the file if it is a symlink so we can overwrite it
1745 struct stat St;
1746 if (lstat(DestFile.c_str(),&St) == 0)
1747 {
1748 if (S_ISLNK(St.st_mode) != 0)
1749 unlink(DestFile.c_str());
1750 }
1751
1752 // Symlink the file
917ae805
AL
1753 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
1754 {
83ab33fc 1755 ErrorText = "Link to " + DestFile + " failure ";
917ae805
AL
1756 Status = StatError;
1757 Complete = false;
1758 }
36375005
AL
1759 }
1760}
1761 /*}}}*/
08cfc005
AL
1762// AcqFile::Failed - Failure handler /*{{{*/
1763// ---------------------------------------------------------------------
1764/* Here we try other sources */
1765void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1766{
1767 ErrorText = LookupTag(Message,"Message");
1768
1769 // This is the retry counter
1770 if (Retries != 0 &&
1771 Cnf->LocalOnly == false &&
1772 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1773 {
1774 Retries--;
1775 QueueURI(Desc);
1776 return;
1777 }
1778
1779 Item::Failed(Message,Cnf);
1780}
1781 /*}}}*/
77278c2b
MV
1782// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
1783// ---------------------------------------------------------------------
1784/* The only header we use is the last-modified header. */
1785string pkgAcqFile::Custom600Headers()
1786{
1787 if (IsIndexFile)
1788 return "\nIndex-File: true";
61a07c57 1789 return "";
77278c2b
MV
1790}
1791 /*}}}*/