]> git.saurik.com Git - apt.git/blame - apt-pkg/acquire-item.cc
put fetch errors in 'source' on our errorstack
[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 /*{{{*/
ea542140
DK
16#include <config.h>
17
0118833a
AL
18#include <apt-pkg/acquire-item.h>
19#include <apt-pkg/configuration.h>
e878aedb 20#include <apt-pkg/aptconfiguration.h>
b2e465d6 21#include <apt-pkg/sourcelist.h>
03e39e59 22#include <apt-pkg/error.h>
cdcc6d34 23#include <apt-pkg/strutl.h>
36375005 24#include <apt-pkg/fileutl.h>
b3d44315 25#include <apt-pkg/md5.h>
ac5b205a
MV
26#include <apt-pkg/sha1.h>
27#include <apt-pkg/tagfile.h>
472ff00e
DK
28#include <apt-pkg/indexrecords.h>
29#include <apt-pkg/metaindex.h>
0a8a80e5
AL
30
31#include <sys/stat.h>
32#include <unistd.h>
c88edf1d 33#include <errno.h>
5819a761 34#include <string>
ac5b205a 35#include <sstream>
c88edf1d 36#include <stdio.h>
1ddb8596 37#include <ctime>
ea542140
DK
38
39#include <apti18n.h>
0118833a
AL
40 /*}}}*/
41
b3d44315 42using namespace std;
5819a761 43
0118833a
AL
44// Acquire::Item::Item - Constructor /*{{{*/
45// ---------------------------------------------------------------------
46/* */
8267fe24 47pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
6b1ff003
AL
48 PartialSize(0), Mode(0), ID(0), Complete(false),
49 Local(false), QueueCounter(0)
0118833a
AL
50{
51 Owner->Add(this);
c88edf1d 52 Status = StatIdle;
0118833a
AL
53}
54 /*}}}*/
55// Acquire::Item::~Item - Destructor /*{{{*/
56// ---------------------------------------------------------------------
57/* */
58pkgAcquire::Item::~Item()
59{
60 Owner->Remove(this);
61}
62 /*}}}*/
c88edf1d
AL
63// Acquire::Item::Failed - Item failed to download /*{{{*/
64// ---------------------------------------------------------------------
93bf083d
AL
65/* We return to an idle state if there are still other queues that could
66 fetch this object */
7d8afa39 67void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
c88edf1d 68{
93bf083d 69 Status = StatIdle;
db890fdb 70 ErrorText = LookupTag(Message,"Message");
361593e9 71 UsedMirror = LookupTag(Message,"UsedMirror");
c88edf1d 72 if (QueueCounter <= 1)
93bf083d 73 {
a72ace20 74 /* This indicates that the file is not available right now but might
7d8afa39 75 be sometime later. If we do a retry cycle then this should be
17caf1b1 76 retried [CDROMs] */
7d8afa39
AL
77 if (Cnf->LocalOnly == true &&
78 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
a72ace20
AL
79 {
80 Status = StatIdle;
681d76d0 81 Dequeue();
a72ace20
AL
82 return;
83 }
7e5f33eb 84
93bf083d 85 Status = StatError;
681d76d0 86 Dequeue();
93bf083d 87 }
23c5897c 88
36280399 89 // report mirror failure back to LP if we actually use a mirror
f0b509cd
MV
90 string FailReason = LookupTag(Message, "FailReason");
91 if(FailReason.size() != 0)
92 ReportMirrorFailure(FailReason);
93 else
94 ReportMirrorFailure(ErrorText);
c88edf1d
AL
95}
96 /*}}}*/
8267fe24
AL
97// Acquire::Item::Start - Item has begun to download /*{{{*/
98// ---------------------------------------------------------------------
17caf1b1
AL
99/* Stash status and the file size. Note that setting Complete means
100 sub-phases of the acquire process such as decompresion are operating */
73da43e9 101void pkgAcquire::Item::Start(string /*Message*/,unsigned long long Size)
8267fe24
AL
102{
103 Status = StatFetching;
104 if (FileSize == 0 && Complete == false)
105 FileSize = Size;
106}
107 /*}}}*/
c88edf1d
AL
108// Acquire::Item::Done - Item downloaded OK /*{{{*/
109// ---------------------------------------------------------------------
110/* */
73da43e9 111void pkgAcquire::Item::Done(string Message,unsigned long long Size,string Hash,
459681d3 112 pkgAcquire::MethodConfig *Cnf)
c88edf1d 113{
b98f2859
AL
114 // We just downloaded something..
115 string FileName = LookupTag(Message,"Filename");
36280399 116 UsedMirror = LookupTag(Message,"UsedMirror");
8f30ca30 117 if (Complete == false && !Local && FileName == DestFile)
b98f2859
AL
118 {
119 if (Owner->Log != 0)
120 Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
121 }
aa0e1101
AL
122
123 if (FileSize == 0)
124 FileSize= Size;
c88edf1d
AL
125 Status = StatDone;
126 ErrorText = string();
127 Owner->Dequeue(this);
128}
129 /*}}}*/
8b89e57f
AL
130// Acquire::Item::Rename - Rename a file /*{{{*/
131// ---------------------------------------------------------------------
132/* This helper function is used by alot of item methods as thier final
133 step */
134void pkgAcquire::Item::Rename(string From,string To)
135{
136 if (rename(From.c_str(),To.c_str()) != 0)
137 {
138 char S[300];
0fcd01de 139 snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
8b89e57f
AL
140 From.c_str(),To.c_str());
141 Status = StatError;
142 ErrorText = S;
7a3c2ab0 143 }
8b89e57f
AL
144}
145 /*}}}*/
3c8030a4
DK
146bool pkgAcquire::Item::RenameOnError(pkgAcquire::Item::RenameOnErrorState const error)/*{{{*/
147{
148 if(FileExists(DestFile))
149 Rename(DestFile, DestFile + ".FAILED");
150
151 switch (error)
152 {
153 case HashSumMismatch:
154 ErrorText = _("Hash Sum mismatch");
155 Status = StatAuthError;
156 ReportMirrorFailure("HashChecksumFailure");
157 break;
158 case SizeMismatch:
159 ErrorText = _("Size mismatch");
160 Status = StatAuthError;
161 ReportMirrorFailure("SizeFailure");
162 break;
163 case InvalidFormat:
164 ErrorText = _("Invalid file format");
165 Status = StatError;
166 // do not report as usually its not the mirrors fault, but Portal/Proxy
167 break;
168 }
169 return false;
170}
171 /*}}}*/
c91d9a63
DK
172// Acquire::Item::ReportMirrorFailure /*{{{*/
173// ---------------------------------------------------------------------
36280399
MV
174void pkgAcquire::Item::ReportMirrorFailure(string FailCode)
175{
59271f62
MV
176 // we only act if a mirror was used at all
177 if(UsedMirror.empty())
178 return;
36280399
MV
179#if 0
180 std::cerr << "\nReportMirrorFailure: "
181 << UsedMirror
59271f62 182 << " Uri: " << DescURI()
36280399
MV
183 << " FailCode: "
184 << FailCode << std::endl;
185#endif
186 const char *Args[40];
187 unsigned int i = 0;
188 string report = _config->Find("Methods::Mirror::ProblemReporting",
3f599bb7 189 "/usr/lib/apt/apt-report-mirror-failure");
36280399
MV
190 if(!FileExists(report))
191 return;
192 Args[i++] = report.c_str();
193 Args[i++] = UsedMirror.c_str();
f0b509cd 194 Args[i++] = DescURI().c_str();
36280399 195 Args[i++] = FailCode.c_str();
361593e9 196 Args[i++] = NULL;
36280399
MV
197 pid_t pid = ExecFork();
198 if(pid < 0)
199 {
200 _error->Error("ReportMirrorFailure Fork failed");
201 return;
202 }
203 else if(pid == 0)
204 {
361593e9
MV
205 execvp(Args[0], (char**)Args);
206 std::cerr << "Could not exec " << Args[0] << std::endl;
207 _exit(100);
36280399
MV
208 }
209 if(!ExecWait(pid, "report-mirror-failure"))
210 {
211 _error->Warning("Couldn't report problem to '%s'",
361593e9 212 _config->Find("Methods::Mirror::ProblemReporting").c_str());
36280399
MV
213 }
214}
c91d9a63 215 /*}}}*/
ab53c018
DK
216// AcqSubIndex::AcqSubIndex - Constructor /*{{{*/
217// ---------------------------------------------------------------------
8e3900d0
DK
218/* Get a sub-index file based on checksums from a 'master' file and
219 possibly query additional files */
ab53c018
DK
220pkgAcqSubIndex::pkgAcqSubIndex(pkgAcquire *Owner, string const &URI,
221 string const &URIDesc, string const &ShortDesc,
222 HashString const &ExpectedHash)
223 : Item(Owner), ExpectedHash(ExpectedHash)
224{
8e3900d0 225 /* XXX: Beware: Currently this class does nothing (of value) anymore ! */
ab53c018
DK
226 Debug = _config->FindB("Debug::pkgAcquire::SubIndex",false);
227
228 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
229 DestFile += URItoFileName(URI);
230
231 Desc.URI = URI;
232 Desc.Description = URIDesc;
233 Desc.Owner = this;
234 Desc.ShortDesc = ShortDesc;
235
236 QueueURI(Desc);
237
238 if(Debug)
239 std::clog << "pkgAcqSubIndex: " << Desc.URI << std::endl;
240}
241 /*}}}*/
242// AcqSubIndex::Custom600Headers - Insert custom request headers /*{{{*/
243// ---------------------------------------------------------------------
244/* The only header we use is the last-modified header. */
245string pkgAcqSubIndex::Custom600Headers()
246{
247 string Final = _config->FindDir("Dir::State::lists");
248 Final += URItoFileName(Desc.URI);
249
250 struct stat Buf;
251 if (stat(Final.c_str(),&Buf) != 0)
97b65b10
MV
252 return "\nIndex-File: true\nFail-Ignore: true\n";
253 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
ab53c018
DK
254}
255 /*}}}*/
256void pkgAcqSubIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
257{
258 if(Debug)
259 std::clog << "pkgAcqSubIndex failed: " << Desc.URI << std::endl;
260
261 Complete = false;
262 Status = StatDone;
263 Dequeue();
264
8e3900d0 265 // No good Index is provided
ab53c018
DK
266}
267 /*}}}*/
73da43e9 268void pkgAcqSubIndex::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
ab53c018
DK
269 pkgAcquire::MethodConfig *Cnf)
270{
271 if(Debug)
272 std::clog << "pkgAcqSubIndex::Done(): " << Desc.URI << std::endl;
273
274 string FileName = LookupTag(Message,"Filename");
275 if (FileName.empty() == true)
276 {
277 Status = StatError;
278 ErrorText = "Method gave a blank filename";
279 return;
280 }
281
282 if (FileName != DestFile)
283 {
284 Local = true;
285 Desc.URI = "copy:" + FileName;
286 QueueURI(Desc);
287 return;
288 }
289
290 Item::Done(Message,Size,Md5Hash,Cnf);
291
292 string FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(Desc.URI);
293
4fdb6123 294 /* Downloaded invalid transindex => Error (LP: #346386) (Closes: #627642) */
0901c5d0
JAK
295 indexRecords SubIndexParser;
296 if (FileExists(DestFile) == true && !SubIndexParser.Load(DestFile)) {
297 Status = StatError;
298 ErrorText = SubIndexParser.ErrorText;
299 return;
300 }
301
ab53c018
DK
302 // sucess in downloading the index
303 // rename the index
304 if(Debug)
305 std::clog << "Renaming: " << DestFile << " -> " << FinalFile << std::endl;
306 Rename(DestFile,FinalFile);
307 chmod(FinalFile.c_str(),0644);
308 DestFile = FinalFile;
309
310 if(ParseIndex(DestFile) == false)
311 return Failed("", NULL);
312
313 Complete = true;
314 Status = StatDone;
315 Dequeue();
316 return;
317}
318 /*}}}*/
319bool pkgAcqSubIndex::ParseIndex(string const &IndexFile) /*{{{*/
320{
321 indexRecords SubIndexParser;
322 if (FileExists(IndexFile) == false || SubIndexParser.Load(IndexFile) == false)
323 return false;
8e3900d0 324 // so something with the downloaded index
ab53c018
DK
325 return true;
326}
327 /*}}}*/
92fcbfc1 328// AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
ac5b205a 329// ---------------------------------------------------------------------
2237bd01
MV
330/* Get the DiffIndex file first and see if there are patches availabe
331 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
332 * patches. If anything goes wrong in that process, it will fall back to
333 * the original packages file
ac5b205a 334 */
2237bd01
MV
335pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
336 string URI,string URIDesc,string ShortDesc,
495e5cb2
MV
337 HashString ExpectedHash)
338 : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash),
339 Description(URIDesc)
ac5b205a
MV
340{
341
ac5b205a
MV
342 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
343
2237bd01 344 Desc.Description = URIDesc + "/DiffIndex";
ac5b205a
MV
345 Desc.Owner = this;
346 Desc.ShortDesc = ShortDesc;
2237bd01
MV
347 Desc.URI = URI + ".diff/Index";
348
349 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
350 DestFile += URItoFileName(URI) + string(".DiffIndex");
351
352 if(Debug)
353 std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
ac5b205a 354
2237bd01 355 // look for the current package file
ac5b205a
MV
356 CurrentPackagesFile = _config->FindDir("Dir::State::lists");
357 CurrentPackagesFile += URItoFileName(RealURI);
358
b4e57d2d
MV
359 // FIXME: this file:/ check is a hack to prevent fetching
360 // from local sources. this is really silly, and
361 // should be fixed cleanly as soon as possible
ac5b205a 362 if(!FileExists(CurrentPackagesFile) ||
81fcf9e2 363 Desc.URI.substr(0,strlen("file:/")) == "file:/")
2ac3eeb6 364 {
ac5b205a
MV
365 // we don't have a pkg file or we don't want to queue
366 if(Debug)
b4e57d2d 367 std::clog << "No index file, local or canceld by user" << std::endl;
ac5b205a
MV
368 Failed("", NULL);
369 return;
370 }
371
2ac3eeb6 372 if(Debug)
2237bd01
MV
373 std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
374 << CurrentPackagesFile << std::endl;
2ac3eeb6 375
ac5b205a 376 QueueURI(Desc);
2237bd01 377
ac5b205a 378}
92fcbfc1 379 /*}}}*/
6cb30d01
MV
380// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
381// ---------------------------------------------------------------------
382/* The only header we use is the last-modified header. */
2237bd01 383string pkgAcqDiffIndex::Custom600Headers()
6cb30d01 384{
6cb30d01
MV
385 string Final = _config->FindDir("Dir::State::lists");
386 Final += URItoFileName(RealURI) + string(".IndexDiff");
387
388 if(Debug)
389 std::clog << "Custom600Header-IMS: " << Final << std::endl;
390
391 struct stat Buf;
392 if (stat(Final.c_str(),&Buf) != 0)
393 return "\nIndex-File: true";
394
395 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
396}
92fcbfc1
DK
397 /*}}}*/
398bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile) /*{{{*/
2237bd01
MV
399{
400 if(Debug)
401 std::clog << "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
402 << std::endl;
403
404 pkgTagSection Tags;
405 string ServerSha1;
406 vector<DiffInfo> available_patches;
407
408 FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
409 pkgTagFile TF(&Fd);
410 if (_error->PendingError() == true)
411 return false;
412
413 if(TF.Step(Tags) == true)
414 {
002d9943
MV
415 bool found = false;
416 DiffInfo d;
417 string size;
418
02dceb31 419 string const tmp = Tags.FindS("SHA1-Current");
2237bd01 420 std::stringstream ss(tmp);
02dceb31
DK
421 ss >> ServerSha1 >> size;
422 unsigned long const ServerSize = atol(size.c_str());
2237bd01 423
f213b6ea 424 FileFd fd(CurrentPackagesFile, FileFd::ReadOnly);
2237bd01 425 SHA1Summation SHA1;
109eb151 426 SHA1.AddFD(fd);
02dceb31 427 string const local_sha1 = SHA1.Result();
2237bd01 428
2ac3eeb6
MV
429 if(local_sha1 == ServerSha1)
430 {
431 // we have the same sha1 as the server
2237bd01
MV
432 if(Debug)
433 std::clog << "Package file is up-to-date" << std::endl;
002d9943
MV
434 // set found to true, this will queue a pkgAcqIndexDiffs with
435 // a empty availabe_patches
436 found = true;
2ac3eeb6
MV
437 }
438 else
439 {
002d9943 440 if(Debug)
f213b6ea 441 std::clog << "SHA1-Current: " << ServerSha1 << " and we start at "<< fd.Name() << " " << fd.Size() << " " << local_sha1 << std::endl;
002d9943
MV
442
443 // check the historie and see what patches we need
02dceb31 444 string const history = Tags.FindS("SHA1-History");
002d9943 445 std::stringstream hist(history);
02dceb31 446 while(hist >> d.sha1 >> size >> d.file)
2ac3eeb6 447 {
002d9943 448 // read until the first match is found
02dceb31 449 // from that point on, we probably need all diffs
002d9943
MV
450 if(d.sha1 == local_sha1)
451 found=true;
02dceb31
DK
452 else if (found == false)
453 continue;
454
455 if(Debug)
456 std::clog << "Need to get diff: " << d.file << std::endl;
457 available_patches.push_back(d);
458 }
459
460 if (available_patches.empty() == false)
461 {
462 // patching with too many files is rather slow compared to a fast download
463 unsigned long const fileLimit = _config->FindI("Acquire::PDiffs::FileLimit", 0);
464 if (fileLimit != 0 && fileLimit < available_patches.size())
465 {
466 if (Debug)
467 std::clog << "Need " << available_patches.size() << " diffs (Limit is " << fileLimit
468 << ") so fallback to complete download" << std::endl;
469 return false;
470 }
471
472 // see if the patches are too big
473 found = false; // it was true and it will be true again at the end
474 d = *available_patches.begin();
475 string const firstPatch = d.file;
476 unsigned long patchesSize = 0;
477 std::stringstream patches(Tags.FindS("SHA1-Patches"));
478 while(patches >> d.sha1 >> size >> d.file)
479 {
480 if (firstPatch == d.file)
481 found = true;
482 else if (found == false)
483 continue;
484
485 patchesSize += atol(size.c_str());
486 }
487 unsigned long const sizeLimit = ServerSize * _config->FindI("Acquire::PDiffs::SizeLimit", 100);
488 if (sizeLimit > 0 && (sizeLimit/100) < patchesSize)
2ac3eeb6 489 {
02dceb31
DK
490 if (Debug)
491 std::clog << "Need " << patchesSize << " bytes (Limit is " << sizeLimit/100
492 << ") so fallback to complete download" << std::endl;
493 return false;
002d9943 494 }
2237bd01
MV
495 }
496 }
497
05aab406
MV
498 // we have something, queue the next diff
499 if(found)
2ac3eeb6 500 {
2237bd01 501 // queue the diffs
02dceb31 502 string::size_type const last_space = Description.rfind(" ");
05aab406
MV
503 if(last_space != string::npos)
504 Description.erase(last_space, Description.size()-last_space);
2237bd01 505 new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
8a3207f4 506 ExpectedHash, ServerSha1, available_patches);
2237bd01
MV
507 Complete = false;
508 Status = StatDone;
509 Dequeue();
510 return true;
511 }
512 }
05aab406
MV
513
514 // Nothing found, report and return false
515 // Failing here is ok, if we return false later, the full
516 // IndexFile is queued
517 if(Debug)
518 std::clog << "Can't find a patch in the index file" << std::endl;
2237bd01
MV
519 return false;
520}
92fcbfc1
DK
521 /*}}}*/
522void pkgAcqDiffIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
2237bd01
MV
523{
524 if(Debug)
525 std::clog << "pkgAcqDiffIndex failed: " << Desc.URI << std::endl
526 << "Falling back to normal index file aquire" << std::endl;
527
002d9943 528 new pkgAcqIndex(Owner, RealURI, Description, Desc.ShortDesc,
495e5cb2 529 ExpectedHash);
2237bd01
MV
530
531 Complete = false;
532 Status = StatDone;
533 Dequeue();
534}
92fcbfc1 535 /*}}}*/
73da43e9 536void pkgAcqDiffIndex::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
2237bd01
MV
537 pkgAcquire::MethodConfig *Cnf)
538{
539 if(Debug)
540 std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl;
541
542 Item::Done(Message,Size,Md5Hash,Cnf);
543
544 string FinalFile;
545 FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
546
547 // sucess in downloading the index
548 // rename the index
549 FinalFile += string(".IndexDiff");
550 if(Debug)
551 std::clog << "Renaming: " << DestFile << " -> " << FinalFile
552 << std::endl;
553 Rename(DestFile,FinalFile);
554 chmod(FinalFile.c_str(),0644);
555 DestFile = FinalFile;
556
557 if(!ParseDiffIndex(DestFile))
558 return Failed("", NULL);
559
560 Complete = true;
561 Status = StatDone;
562 Dequeue();
563 return;
564}
92fcbfc1
DK
565 /*}}}*/
566// AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
2237bd01
MV
567// ---------------------------------------------------------------------
568/* The package diff is added to the queue. one object is constructed
569 * for each diff and the index
570 */
571pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
572 string URI,string URIDesc,string ShortDesc,
59d5cc74 573 HashString ExpectedHash,
8a3207f4 574 string ServerSha1,
495e5cb2
MV
575 vector<DiffInfo> diffs)
576 : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash),
8a3207f4 577 available_patches(diffs), ServerSha1(ServerSha1)
2237bd01
MV
578{
579
580 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
581 DestFile += URItoFileName(URI);
582
583 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
584
05aab406 585 Description = URIDesc;
2237bd01
MV
586 Desc.Owner = this;
587 Desc.ShortDesc = ShortDesc;
588
69c2ecbd 589 if(available_patches.empty() == true)
2ac3eeb6 590 {
2237bd01
MV
591 // we are done (yeah!)
592 Finish(true);
2ac3eeb6
MV
593 }
594 else
595 {
2237bd01
MV
596 // get the next diff
597 State = StateFetchDiff;
598 QueueNextDiff();
599 }
600}
92fcbfc1
DK
601 /*}}}*/
602void pkgAcqIndexDiffs::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
ac5b205a 603{
2237bd01
MV
604 if(Debug)
605 std::clog << "pkgAcqIndexDiffs failed: " << Desc.URI << std::endl
606 << "Falling back to normal index file aquire" << std::endl;
607 new pkgAcqIndex(Owner, RealURI, Description,Desc.ShortDesc,
495e5cb2 608 ExpectedHash);
ac5b205a
MV
609 Finish();
610}
92fcbfc1
DK
611 /*}}}*/
612// Finish - helper that cleans the item out of the fetcher queue /*{{{*/
ac5b205a
MV
613void pkgAcqIndexDiffs::Finish(bool allDone)
614{
615 // we restore the original name, this is required, otherwise
616 // the file will be cleaned
2ac3eeb6
MV
617 if(allDone)
618 {
ac5b205a
MV
619 DestFile = _config->FindDir("Dir::State::lists");
620 DestFile += URItoFileName(RealURI);
2d4722e2 621
495e5cb2 622 if(!ExpectedHash.empty() && !ExpectedHash.VerifyFile(DestFile))
2d4722e2 623 {
3c8030a4 624 RenameOnError(HashSumMismatch);
2d4722e2
MV
625 Dequeue();
626 return;
627 }
628
629 // this is for the "real" finish
ac5b205a 630 Complete = true;
cffc2ddd 631 Status = StatDone;
ac5b205a
MV
632 Dequeue();
633 if(Debug)
634 std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
635 return;
ac5b205a
MV
636 }
637
638 if(Debug)
639 std::clog << "Finishing: " << Desc.URI << std::endl;
640 Complete = false;
641 Status = StatDone;
642 Dequeue();
643 return;
644}
92fcbfc1
DK
645 /*}}}*/
646bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
ac5b205a 647{
3de9ff77 648
94dc9d7d
MV
649 // calc sha1 of the just patched file
650 string FinalFile = _config->FindDir("Dir::State::lists");
651 FinalFile += URItoFileName(RealURI);
652
f213b6ea 653 FileFd fd(FinalFile, FileFd::ReadOnly);
94dc9d7d 654 SHA1Summation SHA1;
109eb151 655 SHA1.AddFD(fd);
94dc9d7d 656 string local_sha1 = string(SHA1.Result());
3de9ff77
MV
657 if(Debug)
658 std::clog << "QueueNextDiff: "
659 << FinalFile << " (" << local_sha1 << ")"<<std::endl;
94dc9d7d 660
8a3207f4
DK
661 // final file reached before all patches are applied
662 if(local_sha1 == ServerSha1)
663 {
664 Finish(true);
665 return true;
666 }
667
26d27645
MV
668 // remove all patches until the next matching patch is found
669 // this requires the Index file to be ordered
94dc9d7d 670 for(vector<DiffInfo>::iterator I=available_patches.begin();
f7f0d6c7 671 available_patches.empty() == false &&
2ac3eeb6 672 I != available_patches.end() &&
f7f0d6c7
DK
673 I->sha1 != local_sha1;
674 ++I)
2ac3eeb6 675 {
26d27645 676 available_patches.erase(I);
59a704f0 677 }
94dc9d7d
MV
678
679 // error checking and falling back if no patch was found
f7f0d6c7
DK
680 if(available_patches.empty() == true)
681 {
94dc9d7d
MV
682 Failed("", NULL);
683 return false;
684 }
6cb30d01 685
94dc9d7d 686 // queue the right diff
2237bd01 687 Desc.URI = string(RealURI) + ".diff/" + available_patches[0].file + ".gz";
05aab406 688 Desc.Description = Description + " " + available_patches[0].file + string(".pdiff");
ac5b205a 689 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
2237bd01 690 DestFile += URItoFileName(RealURI + ".diff/" + available_patches[0].file);
ac5b205a
MV
691
692 if(Debug)
693 std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
694
695 QueueURI(Desc);
696
697 return true;
698}
92fcbfc1 699 /*}}}*/
73da43e9 700void pkgAcqIndexDiffs::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
ac5b205a
MV
701 pkgAcquire::MethodConfig *Cnf)
702{
703 if(Debug)
704 std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl;
705
706 Item::Done(Message,Size,Md5Hash,Cnf);
707
4a0a786f
MV
708 string FinalFile;
709 FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
6cb30d01 710
4a0a786f 711 // sucess in downloading a diff, enter ApplyDiff state
caffd480 712 if(State == StateFetchDiff)
4a0a786f
MV
713 {
714
715 // rred excepts the patch as $FinalFile.ed
716 Rename(DestFile,FinalFile+".ed");
717
718 if(Debug)
719 std::clog << "Sending to rred method: " << FinalFile << std::endl;
720
721 State = StateApplyDiff;
b7347826 722 Local = true;
4a0a786f
MV
723 Desc.URI = "rred:" + FinalFile;
724 QueueURI(Desc);
725 Mode = "rred";
726 return;
727 }
728
729
730 // success in download/apply a diff, queue next (if needed)
731 if(State == StateApplyDiff)
732 {
733 // remove the just applied patch
94dc9d7d 734 available_patches.erase(available_patches.begin());
ac5b205a 735
4a0a786f 736 // move into place
59a704f0
MV
737 if(Debug)
738 {
4a0a786f
MV
739 std::clog << "Moving patched file in place: " << std::endl
740 << DestFile << " -> " << FinalFile << std::endl;
59a704f0 741 }
4a0a786f 742 Rename(DestFile,FinalFile);
1790e0cf 743 chmod(FinalFile.c_str(),0644);
4a0a786f
MV
744
745 // see if there is more to download
f7f0d6c7 746 if(available_patches.empty() == false) {
ac5b205a 747 new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
8a3207f4 748 ExpectedHash, ServerSha1, available_patches);
4a0a786f
MV
749 return Finish();
750 } else
751 return Finish(true);
ac5b205a 752 }
ac5b205a 753}
92fcbfc1 754 /*}}}*/
0118833a
AL
755// AcqIndex::AcqIndex - Constructor /*{{{*/
756// ---------------------------------------------------------------------
757/* The package file is added to the queue and a second class is
b2e465d6
AL
758 instantiated to fetch the revision file */
759pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
b3d44315 760 string URI,string URIDesc,string ShortDesc,
495e5cb2
MV
761 HashString ExpectedHash, string comprExt)
762 : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash)
0118833a 763{
5d885723
DK
764 if(comprExt.empty() == true)
765 {
766 // autoselect the compression method
767 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
768 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
769 comprExt.append(*t).append(" ");
770 if (comprExt.empty() == false)
771 comprExt.erase(comprExt.end()-1);
772 }
773 CompressionExtension = comprExt;
774
775 Init(URI, URIDesc, ShortDesc);
776}
777pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner, IndexTarget const *Target,
778 HashString const &ExpectedHash, indexRecords const *MetaIndexParser)
779 : Item(Owner), RealURI(Target->URI), ExpectedHash(ExpectedHash)
780{
781 // autoselect the compression method
782 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
783 CompressionExtension = "";
784 if (ExpectedHash.empty() == false)
785 {
786 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
787 if (*t == "uncompressed" || MetaIndexParser->Exists(string(Target->MetaKey).append(".").append(*t)) == true)
788 CompressionExtension.append(*t).append(" ");
789 }
790 else
791 {
792 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
793 CompressionExtension.append(*t).append(" ");
794 }
795 if (CompressionExtension.empty() == false)
796 CompressionExtension.erase(CompressionExtension.end()-1);
797
97efc27f
MV
798 // only verify non-optional targets, see acquire-item.h for a FIXME
799 // to make this more flexible
c5f661b7
MV
800 if (Target->IsOptional())
801 Verify = false;
97efc27f
MV
802 else
803 Verify = true;
c5f661b7 804
5d885723
DK
805 Init(Target->URI, Target->Description, Target->ShortDesc);
806}
807 /*}}}*/
808// AcqIndex::Init - defered Constructor /*{{{*/
809void pkgAcqIndex::Init(string const &URI, string const &URIDesc, string const &ShortDesc) {
8b89e57f 810 Decompression = false;
bfd22fc0 811 Erase = false;
13e8426f 812
0a8a80e5 813 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
b2e465d6 814 DestFile += URItoFileName(URI);
8267fe24 815
5d885723
DK
816 std::string const comprExt = CompressionExtension.substr(0, CompressionExtension.find(' '));
817 if (comprExt == "uncompressed")
818 Desc.URI = URI;
819 else
820 Desc.URI = URI + '.' + comprExt;
b3d44315 821
b2e465d6 822 Desc.Description = URIDesc;
8267fe24 823 Desc.Owner = this;
b2e465d6 824 Desc.ShortDesc = ShortDesc;
5d885723 825
8267fe24 826 QueueURI(Desc);
0118833a
AL
827}
828 /*}}}*/
0a8a80e5 829// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
0118833a 830// ---------------------------------------------------------------------
0a8a80e5
AL
831/* The only header we use is the last-modified header. */
832string pkgAcqIndex::Custom600Headers()
0118833a 833{
0a8a80e5 834 string Final = _config->FindDir("Dir::State::lists");
b2e465d6 835 Final += URItoFileName(RealURI);
01606def 836 if (_config->FindB("Acquire::GzipIndexes",false))
837 Final += ".gz";
0a8a80e5 838
97b65b10
MV
839 string msg = "\nIndex-File: true";
840 // FIXME: this really should use "IndexTarget::IsOptional()" but that
841 // seems to be difficult without breaking ABI
842 if (ShortDesc().find("Translation") != 0)
843 msg += "\nFail-Ignore: true";
0a8a80e5 844 struct stat Buf;
3a1f49c4 845 if (stat(Final.c_str(),&Buf) == 0)
97b65b10
MV
846 msg += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
847
848 return msg;
0118833a
AL
849}
850 /*}}}*/
92fcbfc1 851void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
debc84b2 852{
5d885723
DK
853 size_t const nextExt = CompressionExtension.find(' ');
854 if (nextExt != std::string::npos)
e85b4cd5 855 {
5d885723
DK
856 CompressionExtension = CompressionExtension.substr(nextExt+1);
857 Init(RealURI, Desc.Description, Desc.ShortDesc);
6abe2699 858 return;
0d7a243d
EL
859 }
860
17ff0930 861 // on decompression failure, remove bad versions in partial/
5d885723 862 if (Decompression && Erase) {
17ff0930 863 string s = _config->FindDir("Dir::State::lists") + "partial/";
5d885723 864 s.append(URItoFileName(RealURI));
17ff0930 865 unlink(s.c_str());
debc84b2
MZ
866 }
867
debc84b2
MZ
868 Item::Failed(Message,Cnf);
869}
92fcbfc1 870 /*}}}*/
8b89e57f
AL
871// AcqIndex::Done - Finished a fetch /*{{{*/
872// ---------------------------------------------------------------------
873/* This goes through a number of states.. On the initial fetch the
874 method could possibly return an alternate filename which points
875 to the uncompressed version of the file. If this is so the file
876 is copied into the partial directory. In all other cases the file
877 is decompressed with a gzip uri. */
73da43e9 878void pkgAcqIndex::Done(string Message,unsigned long long Size,string Hash,
459681d3 879 pkgAcquire::MethodConfig *Cfg)
8b89e57f 880{
495e5cb2 881 Item::Done(Message,Size,Hash,Cfg);
8b89e57f
AL
882
883 if (Decompression == true)
884 {
b3d44315
MV
885 if (_config->FindB("Debug::pkgAcquire::Auth", false))
886 {
495e5cb2
MV
887 std::cerr << std::endl << RealURI << ": Computed Hash: " << Hash;
888 std::cerr << " Expected Hash: " << ExpectedHash.toStr() << std::endl;
b3d44315
MV
889 }
890
8a8feb29 891 if (!ExpectedHash.empty() && ExpectedHash.toStr() != Hash)
b3d44315 892 {
3c8030a4 893 RenameOnError(HashSumMismatch);
b3d44315
MV
894 return;
895 }
0901c5d0
JAK
896
897 /* Verify the index file for correctness (all indexes must
4fdb6123 898 * have a Package field) (LP: #346386) (Closes: #627642) */
c5f661b7 899 if (Verify == true)
0901c5d0
JAK
900 {
901 FileFd fd(DestFile, FileFd::ReadOnly);
3c8030a4
DK
902 // Only test for correctness if the file is not empty (empty is ok)
903 if (fd.FileSize() > 0)
904 {
905 pkgTagSection sec;
906 pkgTagFile tag(&fd);
907
908 // all our current indexes have a field 'Package' in each section
909 if (_error->PendingError() == true || tag.Step(sec) == false || sec.Exists("Package") == false)
910 {
911 RenameOnError(InvalidFormat);
912 return;
913 }
a0c3110e 914 }
0901c5d0
JAK
915 }
916
8b89e57f
AL
917 // Done, move it into position
918 string FinalFile = _config->FindDir("Dir::State::lists");
b2e465d6 919 FinalFile += URItoFileName(RealURI);
8b89e57f 920 Rename(DestFile,FinalFile);
7a3c2ab0 921 chmod(FinalFile.c_str(),0644);
bfd22fc0 922
7a7fa5f0
AL
923 /* We restore the original name to DestFile so that the clean operation
924 will work OK */
925 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
b2e465d6 926 DestFile += URItoFileName(RealURI);
7a7fa5f0 927
bfd22fc0
AL
928 // Remove the compressed version.
929 if (Erase == true)
bfd22fc0 930 unlink(DestFile.c_str());
8b89e57f
AL
931 return;
932 }
bfd22fc0
AL
933
934 Erase = false;
8267fe24 935 Complete = true;
bfd22fc0 936
8b89e57f
AL
937 // Handle the unzipd case
938 string FileName = LookupTag(Message,"Alt-Filename");
939 if (FileName.empty() == false)
940 {
941 // The files timestamp matches
942 if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
943 return;
8b89e57f 944 Decompression = true;
a6568219 945 Local = true;
8b89e57f 946 DestFile += ".decomp";
8267fe24
AL
947 Desc.URI = "copy:" + FileName;
948 QueueURI(Desc);
b98f2859 949 Mode = "copy";
8b89e57f
AL
950 return;
951 }
952
953 FileName = LookupTag(Message,"Filename");
954 if (FileName.empty() == true)
955 {
956 Status = StatError;
957 ErrorText = "Method gave a blank filename";
958 }
5d885723
DK
959
960 std::string const compExt = CompressionExtension.substr(0, CompressionExtension.find(' '));
0b9032b1 961
8b89e57f 962 // The files timestamp matches
0b9032b1 963 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true) {
964 if (_config->FindB("Acquire::GzipIndexes",false) && compExt == "gz")
965 // Update DestFile for .gz suffix so that the clean operation keeps it
966 DestFile += ".gz";
8b89e57f 967 return;
0b9032b1 968 }
bfd22fc0
AL
969
970 if (FileName == DestFile)
971 Erase = true;
8267fe24 972 else
a6568219 973 Local = true;
8b89e57f 974
e85b4cd5
DK
975 string decompProg;
976
bb109d0b 977 // If we enable compressed indexes and already have gzip, keep it
7aeee365 978 if (_config->FindB("Acquire::GzipIndexes",false) && compExt == "gz" && !Local) {
bb109d0b 979 string FinalFile = _config->FindDir("Dir::State::lists");
980 FinalFile += URItoFileName(RealURI) + ".gz";
bb109d0b 981 Rename(DestFile,FinalFile);
982 chmod(FinalFile.c_str(),0644);
983
984 // Update DestFile for .gz suffix so that the clean operation keeps it
985 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
986 DestFile += URItoFileName(RealURI) + ".gz";
987 return;
988 }
989
e85b4cd5
DK
990 // get the binary name for your used compression type
991 decompProg = _config->Find(string("Acquire::CompressionTypes::").append(compExt),"");
992 if(decompProg.empty() == false);
5d885723 993 else if(compExt == "uncompressed")
0d7a243d 994 decompProg = "copy";
debc84b2
MZ
995 else {
996 _error->Error("Unsupported extension: %s", compExt.c_str());
997 return;
998 }
999
8b89e57f
AL
1000 Decompression = true;
1001 DestFile += ".decomp";
e85b4cd5 1002 Desc.URI = decompProg + ":" + FileName;
8267fe24 1003 QueueURI(Desc);
70e0c168
MV
1004
1005 // FIXME: this points to a c++ string that goes out of scope
e85b4cd5 1006 Mode = decompProg.c_str();
8b89e57f 1007}
92fcbfc1 1008 /*}}}*/
a52f938b
OS
1009// AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
1010// ---------------------------------------------------------------------
1011/* The Translation file is added to the queue */
1012pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner,
495e5cb2
MV
1013 string URI,string URIDesc,string ShortDesc)
1014 : pkgAcqIndex(Owner, URI, URIDesc, ShortDesc, HashString(), "")
a52f938b 1015{
ab53c018
DK
1016}
1017pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner, IndexTarget const *Target,
1018 HashString const &ExpectedHash, indexRecords const *MetaIndexParser)
1019 : pkgAcqIndex(Owner, Target, ExpectedHash, MetaIndexParser)
1020{
963b16dc
MV
1021}
1022 /*}}}*/
1023// AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
1024// ---------------------------------------------------------------------
1025string pkgAcqIndexTrans::Custom600Headers()
1026{
c91d9a63
DK
1027 string Final = _config->FindDir("Dir::State::lists");
1028 Final += URItoFileName(RealURI);
1029
1030 struct stat Buf;
1031 if (stat(Final.c_str(),&Buf) != 0)
a3f7fff8
MV
1032 return "\nFail-Ignore: true\nIndex-File: true";
1033 return "\nFail-Ignore: true\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
a52f938b 1034}
a52f938b
OS
1035 /*}}}*/
1036// AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
1037// ---------------------------------------------------------------------
1038/* */
1039void pkgAcqIndexTrans::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1040{
5d885723
DK
1041 size_t const nextExt = CompressionExtension.find(' ');
1042 if (nextExt != std::string::npos)
1043 {
1044 CompressionExtension = CompressionExtension.substr(nextExt+1);
1045 Init(RealURI, Desc.Description, Desc.ShortDesc);
1046 Status = StatIdle;
1047 return;
1048 }
1049
a52f938b
OS
1050 if (Cnf->LocalOnly == true ||
1051 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
1052 {
1053 // Ignore this
1054 Status = StatDone;
1055 Complete = false;
1056 Dequeue();
1057 return;
1058 }
5d885723 1059
a52f938b
OS
1060 Item::Failed(Message,Cnf);
1061}
1062 /*}}}*/
92fcbfc1 1063pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner, /*{{{*/
b3d44315
MV
1064 string URI,string URIDesc,string ShortDesc,
1065 string MetaIndexURI, string MetaIndexURIDesc,
1066 string MetaIndexShortDesc,
1067 const vector<IndexTarget*>* IndexTargets,
1068 indexRecords* MetaIndexParser) :
1069 Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
46e00f9d
MV
1070 MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
1071 MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets)
0118833a 1072{
0a8a80e5 1073 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
b2e465d6 1074 DestFile += URItoFileName(URI);
b3d44315 1075
47eb38f4
MV
1076 // remove any partial downloaded sig-file in partial/.
1077 // it may confuse proxies and is too small to warrant a
1078 // partial download anyway
f6237efd
MV
1079 unlink(DestFile.c_str());
1080
8267fe24 1081 // Create the item
b2e465d6 1082 Desc.Description = URIDesc;
8267fe24 1083 Desc.Owner = this;
b3d44315
MV
1084 Desc.ShortDesc = ShortDesc;
1085 Desc.URI = URI;
b3d44315
MV
1086
1087 string Final = _config->FindDir("Dir::State::lists");
1088 Final += URItoFileName(RealURI);
ffcccd62 1089 if (RealFileExists(Final) == true)
b3d44315 1090 {
ef942597
MV
1091 // File was already in place. It needs to be re-downloaded/verified
1092 // because Release might have changed, we do give it a differnt
1093 // name than DestFile because otherwise the http method will
1094 // send If-Range requests and there are too many broken servers
1095 // out there that do not understand them
1096 LastGoodSig = DestFile+".reverify";
1097 Rename(Final,LastGoodSig);
b3d44315 1098 }
8267fe24 1099
8267fe24 1100 QueueURI(Desc);
ffcccd62
DK
1101}
1102 /*}}}*/
1103pkgAcqMetaSig::~pkgAcqMetaSig() /*{{{*/
1104{
1105 // if the file was never queued undo file-changes done in the constructor
1106 if (QueueCounter == 1 && Status == StatIdle && FileSize == 0 && Complete == false &&
1107 LastGoodSig.empty() == false)
1108 {
1109 string const Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
1110 if (RealFileExists(Final) == false && RealFileExists(LastGoodSig) == true)
1111 Rename(LastGoodSig, Final);
1112 }
1113
0118833a
AL
1114}
1115 /*}}}*/
b3d44315 1116// pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
0118833a 1117// ---------------------------------------------------------------------
0a8a80e5 1118/* The only header we use is the last-modified header. */
b3d44315 1119string pkgAcqMetaSig::Custom600Headers()
0118833a 1120{
0a8a80e5 1121 struct stat Buf;
ef942597 1122 if (stat(LastGoodSig.c_str(),&Buf) != 0)
a72ace20 1123 return "\nIndex-File: true";
a789b983 1124
a72ace20 1125 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
0118833a 1126}
b3d44315 1127
73da43e9 1128void pkgAcqMetaSig::Done(string Message,unsigned long long Size,string MD5,
b3d44315 1129 pkgAcquire::MethodConfig *Cfg)
c88edf1d 1130{
459681d3 1131 Item::Done(Message,Size,MD5,Cfg);
c88edf1d
AL
1132
1133 string FileName = LookupTag(Message,"Filename");
1134 if (FileName.empty() == true)
1135 {
1136 Status = StatError;
1137 ErrorText = "Method gave a blank filename";
8b89e57f 1138 return;
c88edf1d 1139 }
8b89e57f 1140
c88edf1d
AL
1141 if (FileName != DestFile)
1142 {
b3d44315 1143 // We have to copy it into place
a6568219 1144 Local = true;
8267fe24
AL
1145 Desc.URI = "copy:" + FileName;
1146 QueueURI(Desc);
c88edf1d
AL
1147 return;
1148 }
b3d44315
MV
1149
1150 Complete = true;
1151
ef942597
MV
1152 // put the last known good file back on i-m-s hit (it will
1153 // be re-verified again)
1154 // Else do nothing, we have the new file in DestFile then
1155 if(StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
1156 Rename(LastGoodSig, DestFile);
1157
b3d44315 1158 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
fce72602
MV
1159 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc,
1160 MetaIndexShortDesc, DestFile, IndexTargets,
1161 MetaIndexParser);
b3d44315 1162
c88edf1d
AL
1163}
1164 /*}}}*/
92fcbfc1 1165void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)/*{{{*/
681d76d0 1166{
47eb38f4 1167 string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
a789b983 1168
75dd8af1 1169 // if we get a network error we fail gracefully
7e5f33eb
MV
1170 if(Status == StatTransientNetworkError)
1171 {
24057ad6 1172 Item::Failed(Message,Cnf);
484dbb81 1173 // move the sigfile back on transient network failures
7730e095 1174 if(FileExists(LastGoodSig))
ef942597 1175 Rename(LastGoodSig,Final);
7e5f33eb
MV
1176
1177 // set the status back to , Item::Failed likes to reset it
1178 Status = pkgAcquire::Item::StatTransientNetworkError;
24057ad6
MV
1179 return;
1180 }
1181
75dd8af1 1182 // Delete any existing sigfile when the acquire failed
75dd8af1
MV
1183 unlink(Final.c_str());
1184
b3d44315
MV
1185 // queue a pkgAcqMetaIndex with no sigfile
1186 new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
1187 "", IndexTargets, MetaIndexParser);
1188
681d76d0
AL
1189 if (Cnf->LocalOnly == true ||
1190 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
1191 {
2b154e53
AL
1192 // Ignore this
1193 Status = StatDone;
1194 Complete = false;
681d76d0
AL
1195 Dequeue();
1196 return;
1197 }
1198
1199 Item::Failed(Message,Cnf);
1200}
92fcbfc1
DK
1201 /*}}}*/
1202pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner, /*{{{*/
b3d44315
MV
1203 string URI,string URIDesc,string ShortDesc,
1204 string SigFile,
1205 const vector<struct IndexTarget*>* IndexTargets,
1206 indexRecords* MetaIndexParser) :
21fd1746
OS
1207 Item(Owner), RealURI(URI), SigFile(SigFile), IndexTargets(IndexTargets),
1208 MetaIndexParser(MetaIndexParser), AuthPass(false), IMSHit(false)
b3d44315 1209{
b3d44315
MV
1210 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
1211 DestFile += URItoFileName(URI);
1212
1213 // Create the item
1214 Desc.Description = URIDesc;
1215 Desc.Owner = this;
1216 Desc.ShortDesc = ShortDesc;
1217 Desc.URI = URI;
1218
1219 QueueURI(Desc);
1220}
b3d44315
MV
1221 /*}}}*/
1222// pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
1223// ---------------------------------------------------------------------
1224/* The only header we use is the last-modified header. */
1225string pkgAcqMetaIndex::Custom600Headers()
1226{
1227 string Final = _config->FindDir("Dir::State::lists");
1228 Final += URItoFileName(RealURI);
1229
1230 struct stat Buf;
1231 if (stat(Final.c_str(),&Buf) != 0)
1232 return "\nIndex-File: true";
1233
1234 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
1235}
92fcbfc1 1236 /*}}}*/
73da43e9 1237void pkgAcqMetaIndex::Done(string Message,unsigned long long Size,string Hash, /*{{{*/
b3d44315
MV
1238 pkgAcquire::MethodConfig *Cfg)
1239{
495e5cb2 1240 Item::Done(Message,Size,Hash,Cfg);
b3d44315
MV
1241
1242 // MetaIndexes are done in two passes: one to download the
1243 // metaindex with an appropriate method, and a second to verify it
1244 // with the gpgv method
1245
1246 if (AuthPass == true)
1247 {
1248 AuthDone(Message);
fce72602
MV
1249
1250 // all cool, move Release file into place
1251 Complete = true;
b3d44315
MV
1252 }
1253 else
1254 {
1255 RetrievalDone(Message);
1256 if (!Complete)
1257 // Still more retrieving to do
1258 return;
1259
1260 if (SigFile == "")
1261 {
1262 // There was no signature file, so we are finished. Download
1207cf3f 1263 // the indexes and do only hashsum verification if possible
3568a640 1264 MetaIndexParser->Load(DestFile);
1207cf3f 1265 QueueIndexes(false);
b3d44315
MV
1266 }
1267 else
1268 {
1269 // There was a signature file, so pass it to gpgv for
1270 // verification
1271
1272 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1273 std::cerr << "Metaindex acquired, queueing gpg verification ("
1274 << SigFile << "," << DestFile << ")\n";
1275 AuthPass = true;
1276 Desc.URI = "gpgv:" + SigFile;
1277 QueueURI(Desc);
1278 Mode = "gpgv";
56bc3358 1279 return;
b3d44315
MV
1280 }
1281 }
56bc3358
DK
1282
1283 if (Complete == true)
1284 {
1285 string FinalFile = _config->FindDir("Dir::State::lists");
1286 FinalFile += URItoFileName(RealURI);
fe0f7911
DK
1287 if (SigFile == DestFile)
1288 SigFile = FinalFile;
56bc3358
DK
1289 Rename(DestFile,FinalFile);
1290 chmod(FinalFile.c_str(),0644);
1291 DestFile = FinalFile;
1292 }
b3d44315 1293}
92fcbfc1
DK
1294 /*}}}*/
1295void pkgAcqMetaIndex::RetrievalDone(string Message) /*{{{*/
b3d44315
MV
1296{
1297 // We have just finished downloading a Release file (it is not
1298 // verified yet)
1299
1300 string FileName = LookupTag(Message,"Filename");
1301 if (FileName.empty() == true)
1302 {
1303 Status = StatError;
1304 ErrorText = "Method gave a blank filename";
1305 return;
1306 }
1307
1308 if (FileName != DestFile)
1309 {
1310 Local = true;
1311 Desc.URI = "copy:" + FileName;
1312 QueueURI(Desc);
1313 return;
1314 }
1315
fce72602 1316 // make sure to verify against the right file on I-M-S hit
f381d68d 1317 IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false);
fce72602
MV
1318 if(IMSHit)
1319 {
1320 string FinalFile = _config->FindDir("Dir::State::lists");
1321 FinalFile += URItoFileName(RealURI);
fe0f7911 1322 if (SigFile == DestFile)
0aec7d5c 1323 {
fe0f7911 1324 SigFile = FinalFile;
0aec7d5c
DK
1325 // constructor of pkgAcqMetaClearSig moved it out of the way,
1326 // now move it back in on IMS hit for the 'old' file
1327 string const OldClearSig = DestFile + ".reverify";
1328 if (RealFileExists(OldClearSig) == true)
1329 Rename(OldClearSig, FinalFile);
1330 }
fce72602
MV
1331 DestFile = FinalFile;
1332 }
b3d44315 1333 Complete = true;
b3d44315 1334}
92fcbfc1
DK
1335 /*}}}*/
1336void pkgAcqMetaIndex::AuthDone(string Message) /*{{{*/
b3d44315
MV
1337{
1338 // At this point, the gpgv method has succeeded, so there is a
1339 // valid signature from a key in the trusted keyring. We
1340 // perform additional verification of its contents, and use them
1341 // to verify the indexes we are about to download
1342
1343 if (!MetaIndexParser->Load(DestFile))
1344 {
1345 Status = StatAuthError;
1346 ErrorText = MetaIndexParser->ErrorText;
1347 return;
1348 }
1349
ce424cd4 1350 if (!VerifyVendor(Message))
b3d44315
MV
1351 {
1352 return;
1353 }
1354
1355 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1356 std::cerr << "Signature verification succeeded: "
1357 << DestFile << std::endl;
1358
1359 // Download further indexes with verification
1360 QueueIndexes(true);
1361
fe0f7911
DK
1362 // is it a clearsigned MetaIndex file?
1363 if (DestFile == SigFile)
1364 return;
1365
b3d44315 1366 // Done, move signature file into position
b3d44315
MV
1367 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
1368 URItoFileName(RealURI) + ".gpg";
1369 Rename(SigFile,VerifiedSigFile);
1370 chmod(VerifiedSigFile.c_str(),0644);
1371}
92fcbfc1
DK
1372 /*}}}*/
1373void pkgAcqMetaIndex::QueueIndexes(bool verify) /*{{{*/
b3d44315 1374{
0901c5d0 1375#if 0
4fdb6123 1376 /* Reject invalid, existing Release files (LP: #346386) (Closes: #627642)
0901c5d0
JAK
1377 * FIXME: Disabled; it breaks unsigned repositories without hashes */
1378 if (!verify && FileExists(DestFile) && !MetaIndexParser->Load(DestFile))
1379 {
1380 Status = StatError;
1381 ErrorText = MetaIndexParser->ErrorText;
1382 return;
1383 }
1384#endif
8e3900d0
DK
1385 bool transInRelease = false;
1386 {
1387 std::vector<std::string> const keys = MetaIndexParser->MetaKeys();
1388 for (std::vector<std::string>::const_iterator k = keys.begin(); k != keys.end(); ++k)
1389 // FIXME: Feels wrong to check for hardcoded string here, but what should we do else…
1390 if (k->find("Translation-") != std::string::npos)
1391 {
1392 transInRelease = true;
1393 break;
1394 }
1395 }
1396
b3d44315
MV
1397 for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
1398 Target != IndexTargets->end();
f7f0d6c7 1399 ++Target)
b3d44315 1400 {
495e5cb2 1401 HashString ExpectedIndexHash;
1207cf3f 1402 const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
a5b9f489 1403 bool compressedAvailable = false;
1207cf3f 1404 if (Record == NULL)
b3d44315 1405 {
a5b9f489
DK
1406 if ((*Target)->IsOptional() == true)
1407 {
1408 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
1409 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
1410 if (MetaIndexParser->Exists(string((*Target)->MetaKey).append(".").append(*t)) == true)
1411 {
1412 compressedAvailable = true;
1413 break;
1414 }
1415 }
1416 else if (verify == true)
ab53c018 1417 {
1207cf3f
DK
1418 Status = StatAuthError;
1419 strprintf(ErrorText, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), (*Target)->MetaKey.c_str());
1420 return;
ab53c018 1421 }
1207cf3f
DK
1422 }
1423 else
1424 {
1425 ExpectedIndexHash = Record->Hash;
1426 if (_config->FindB("Debug::pkgAcquire::Auth", false))
ab53c018 1427 {
1207cf3f
DK
1428 std::cerr << "Queueing: " << (*Target)->URI << std::endl;
1429 std::cerr << "Expected Hash: " << ExpectedIndexHash.toStr() << std::endl;
1430 std::cerr << "For: " << Record->MetaKeyFilename << std::endl;
1431 }
1432 if (verify == true && ExpectedIndexHash.empty() == true && (*Target)->IsOptional() == false)
1433 {
1434 Status = StatAuthError;
1435 strprintf(ErrorText, _("Unable to find hash sum for '%s' in Release file"), (*Target)->MetaKey.c_str());
1436 return;
ab53c018
DK
1437 }
1438 }
1439
1440 if ((*Target)->IsOptional() == true)
1441 {
1442 if ((*Target)->IsSubIndex() == true)
1443 new pkgAcqSubIndex(Owner, (*Target)->URI, (*Target)->Description,
1444 (*Target)->ShortDesc, ExpectedIndexHash);
a5b9f489 1445 else if (transInRelease == false || Record != NULL || compressedAvailable == true)
8e3900d0 1446 {
f55602cb
DK
1447 if (_config->FindB("Acquire::PDiffs",true) == true && transInRelease == true &&
1448 MetaIndexParser->Exists(string((*Target)->MetaKey).append(".diff/Index")) == true)
1449 new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
1450 (*Target)->ShortDesc, ExpectedIndexHash);
1451 else
1452 new pkgAcqIndexTrans(Owner, *Target, ExpectedIndexHash, MetaIndexParser);
8e3900d0 1453 }
ab53c018 1454 continue;
b3d44315 1455 }
e1430400
DK
1456
1457 /* Queue Packages file (either diff or full packages files, depending
1458 on the users option) - we also check if the PDiff Index file is listed
1459 in the Meta-Index file. Ideal would be if pkgAcqDiffIndex would test this
1460 instead, but passing the required info to it is to much hassle */
1461 if(_config->FindB("Acquire::PDiffs",true) == true && (verify == false ||
1462 MetaIndexParser->Exists(string((*Target)->MetaKey).append(".diff/Index")) == true))
2ac3eeb6 1463 new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
495e5cb2 1464 (*Target)->ShortDesc, ExpectedIndexHash);
e1430400 1465 else
5d885723 1466 new pkgAcqIndex(Owner, *Target, ExpectedIndexHash, MetaIndexParser);
b3d44315
MV
1467 }
1468}
92fcbfc1
DK
1469 /*}}}*/
1470bool pkgAcqMetaIndex::VerifyVendor(string Message) /*{{{*/
b3d44315 1471{
ce424cd4
MV
1472 string::size_type pos;
1473
1474 // check for missing sigs (that where not fatal because otherwise we had
1475 // bombed earlier)
1476 string missingkeys;
400ad7a4 1477 string msg = _("There is no public key available for the "
ce424cd4
MV
1478 "following key IDs:\n");
1479 pos = Message.find("NO_PUBKEY ");
1480 if (pos != std::string::npos)
1481 {
1482 string::size_type start = pos+strlen("NO_PUBKEY ");
1483 string Fingerprint = Message.substr(start, Message.find("\n")-start);
1484 missingkeys += (Fingerprint);
1485 }
1486 if(!missingkeys.empty())
1487 _error->Warning("%s", string(msg+missingkeys).c_str());
b3d44315
MV
1488
1489 string Transformed = MetaIndexParser->GetExpectedDist();
1490
1491 if (Transformed == "../project/experimental")
1492 {
1493 Transformed = "experimental";
1494 }
1495
ce424cd4 1496 pos = Transformed.rfind('/');
b3d44315
MV
1497 if (pos != string::npos)
1498 {
1499 Transformed = Transformed.substr(0, pos);
1500 }
1501
1502 if (Transformed == ".")
1503 {
1504 Transformed = "";
1505 }
1506
0323317c
DK
1507 if (_config->FindB("Acquire::Check-Valid-Until", true) == true &&
1508 MetaIndexParser->GetValidUntil() > 0) {
1509 time_t const invalid_since = time(NULL) - MetaIndexParser->GetValidUntil();
1510 if (invalid_since > 0)
1511 // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
1512 // the time since then the file is invalid - formated in the same way as in
1513 // the download progress display (e.g. 7d 3h 42min 1s)
457bea86
MV
1514 return _error->Error(
1515 _("Release file for %s is expired (invalid since %s). "
1516 "Updates for this repository will not be applied."),
1517 RealURI.c_str(), TimeToStr(invalid_since).c_str());
1ddb8596
DK
1518 }
1519
b3d44315
MV
1520 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1521 {
1522 std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
1523 std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
1524 std::cerr << "Transformed Dist: " << Transformed << std::endl;
1525 }
1526
1527 if (MetaIndexParser->CheckDist(Transformed) == false)
1528 {
1529 // This might become fatal one day
1530// Status = StatAuthError;
1531// ErrorText = "Conflicting distribution; expected "
1532// + MetaIndexParser->GetExpectedDist() + " but got "
1533// + MetaIndexParser->GetDist();
1534// return false;
1535 if (!Transformed.empty())
1536 {
1ddb8596 1537 _error->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
b3d44315
MV
1538 Desc.Description.c_str(),
1539 Transformed.c_str(),
1540 MetaIndexParser->GetDist().c_str());
1541 }
1542 }
1543
1544 return true;
1545}
92fcbfc1
DK
1546 /*}}}*/
1547// pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
b3d44315
MV
1548// ---------------------------------------------------------------------
1549/* */
1550void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
1551{
1552 if (AuthPass == true)
1553 {
fce72602 1554 // gpgv method failed, if we have a good signature
39f38a81
DK
1555 string LastGoodSigFile = _config->FindDir("Dir::State::lists").append("partial/").append(URItoFileName(RealURI));
1556 if (DestFile != SigFile)
1557 LastGoodSigFile.append(".gpg");
1558 LastGoodSigFile.append(".reverify");
fe0f7911 1559
fce72602 1560 if(FileExists(LastGoodSigFile))
f381d68d 1561 {
39f38a81 1562 string VerifiedSigFile = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
fe0f7911 1563 if (DestFile != SigFile)
39f38a81
DK
1564 VerifiedSigFile.append(".gpg");
1565 Rename(LastGoodSigFile, VerifiedSigFile);
fce72602 1566 Status = StatTransientNetworkError;
b5595da9 1567 _error->Warning(_("An error occurred during the signature "
fce72602 1568 "verification. The repository is not updated "
2493f4b5 1569 "and the previous index files will be used. "
76b8e5a5 1570 "GPG error: %s: %s\n"),
fce72602
MV
1571 Desc.Description.c_str(),
1572 LookupTag(Message,"Message").c_str());
5d149bfc 1573 RunScripts("APT::Update::Auth-Failure");
f381d68d 1574 return;
0901c5d0 1575 } else if (LookupTag(Message,"Message").find("NODATA") != string::npos) {
4fdb6123 1576 /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */
0901c5d0
JAK
1577 _error->Error(_("GPG error: %s: %s"),
1578 Desc.Description.c_str(),
1579 LookupTag(Message,"Message").c_str());
1580 return;
fce72602
MV
1581 } else {
1582 _error->Warning(_("GPG error: %s: %s"),
1583 Desc.Description.c_str(),
1584 LookupTag(Message,"Message").c_str());
f381d68d 1585 }
f381d68d 1586 // gpgv method failed
59271f62 1587 ReportMirrorFailure("GPGFailure");
b3d44315
MV
1588 }
1589
7ea7ac9e
JAK
1590 /* Always move the meta index, even if gpgv failed. This ensures
1591 * that PackageFile objects are correctly filled in */
a235ddf8 1592 if (FileExists(DestFile)) {
7ea7ac9e
JAK
1593 string FinalFile = _config->FindDir("Dir::State::lists");
1594 FinalFile += URItoFileName(RealURI);
1595 /* InRelease files become Release files, otherwise
1596 * they would be considered as trusted later on */
1597 if (SigFile == DestFile) {
1598 RealURI = RealURI.replace(RealURI.rfind("InRelease"), 9,
1599 "Release");
1600 FinalFile = FinalFile.replace(FinalFile.rfind("InRelease"), 9,
1601 "Release");
1602 SigFile = FinalFile;
1603 }
1604 Rename(DestFile,FinalFile);
1605 chmod(FinalFile.c_str(),0644);
1606
1607 DestFile = FinalFile;
1608 }
1609
b3d44315
MV
1610 // No Release file was present, or verification failed, so fall
1611 // back to queueing Packages files without verification
1612 QueueIndexes(false);
1613}
681d76d0 1614 /*}}}*/
fe0f7911
DK
1615pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire *Owner, /*{{{*/
1616 string const &URI, string const &URIDesc, string const &ShortDesc,
1617 string const &MetaIndexURI, string const &MetaIndexURIDesc, string const &MetaIndexShortDesc,
1618 string const &MetaSigURI, string const &MetaSigURIDesc, string const &MetaSigShortDesc,
1619 const vector<struct IndexTarget*>* IndexTargets,
1620 indexRecords* MetaIndexParser) :
1621 pkgAcqMetaIndex(Owner, URI, URIDesc, ShortDesc, "", IndexTargets, MetaIndexParser),
1622 MetaIndexURI(MetaIndexURI), MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
1623 MetaSigURI(MetaSigURI), MetaSigURIDesc(MetaSigURIDesc), MetaSigShortDesc(MetaSigShortDesc)
1624{
1625 SigFile = DestFile;
39f38a81
DK
1626
1627 // keep the old InRelease around in case of transistent network errors
1628 string const Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
ffcccd62 1629 if (RealFileExists(Final) == true)
39f38a81
DK
1630 {
1631 string const LastGoodSig = DestFile + ".reverify";
1632 Rename(Final,LastGoodSig);
1633 }
fe0f7911
DK
1634}
1635 /*}}}*/
ffcccd62
DK
1636pkgAcqMetaClearSig::~pkgAcqMetaClearSig() /*{{{*/
1637{
1638 // if the file was never queued undo file-changes done in the constructor
1639 if (QueueCounter == 1 && Status == StatIdle && FileSize == 0 && Complete == false)
1640 {
1641 string const Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
1642 string const LastGoodSig = DestFile + ".reverify";
1643 if (RealFileExists(Final) == false && RealFileExists(LastGoodSig) == true)
1644 Rename(LastGoodSig, Final);
1645 }
1646}
1647 /*}}}*/
8d6c5839
MV
1648// pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
1649// ---------------------------------------------------------------------
1650// FIXME: this can go away once the InRelease file is used widely
1651string pkgAcqMetaClearSig::Custom600Headers()
1652{
1653 string Final = _config->FindDir("Dir::State::lists");
1654 Final += URItoFileName(RealURI);
1655
1656 struct stat Buf;
1657 if (stat(Final.c_str(),&Buf) != 0)
0aec7d5c
DK
1658 {
1659 Final = DestFile + ".reverify";
1660 if (stat(Final.c_str(),&Buf) != 0)
1661 return "\nIndex-File: true\nFail-Ignore: true\n";
1662 }
8d6c5839
MV
1663
1664 return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
1665}
1666 /*}}}*/
fe0f7911
DK
1667void pkgAcqMetaClearSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
1668{
1669 if (AuthPass == false)
1670 {
de498a52
DK
1671 // Remove the 'old' InRelease file if we try Release.gpg now as otherwise
1672 // the file will stay around and gives a false-auth impression (CVE-2012-0214)
1673 string FinalFile = _config->FindDir("Dir::State::lists");
1674 FinalFile.append(URItoFileName(RealURI));
1675 if (FileExists(FinalFile))
1676 unlink(FinalFile.c_str());
1677
fe0f7911
DK
1678 new pkgAcqMetaSig(Owner,
1679 MetaSigURI, MetaSigURIDesc, MetaSigShortDesc,
1680 MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
1681 IndexTargets, MetaIndexParser);
1682 if (Cnf->LocalOnly == true ||
1683 StringToBool(LookupTag(Message, "Transient-Failure"), false) == false)
1684 Dequeue();
1685 }
1686 else
1687 pkgAcqMetaIndex::Failed(Message, Cnf);
1688}
1689 /*}}}*/
03e39e59
AL
1690// AcqArchive::AcqArchive - Constructor /*{{{*/
1691// ---------------------------------------------------------------------
17caf1b1
AL
1692/* This just sets up the initial fetch environment and queues the first
1693 possibilitiy */
03e39e59 1694pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
30e1eab5
AL
1695 pkgRecords *Recs,pkgCache::VerIterator const &Version,
1696 string &StoreFilename) :
1697 Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
b3d44315
MV
1698 StoreFilename(StoreFilename), Vf(Version.FileList()),
1699 Trusted(false)
03e39e59 1700{
7d8afa39 1701 Retries = _config->FindI("Acquire::Retries",0);
813c8eea
AL
1702
1703 if (Version.Arch() == 0)
bdae53f1 1704 {
d1f1f6a8 1705 _error->Error(_("I wasn't able to locate a file for the %s package. "
7a3c2ab0
AL
1706 "This might mean you need to manually fix this package. "
1707 "(due to missing arch)"),
813c8eea 1708 Version.ParentPkg().Name());
bdae53f1
AL
1709 return;
1710 }
813c8eea 1711
b2e465d6
AL
1712 /* We need to find a filename to determine the extension. We make the
1713 assumption here that all the available sources for this version share
1714 the same extension.. */
1715 // Skip not source sources, they do not have file fields.
69c2ecbd 1716 for (; Vf.end() == false; ++Vf)
b2e465d6
AL
1717 {
1718 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1719 continue;
1720 break;
1721 }
1722
1723 // Does not really matter here.. we are going to fail out below
1724 if (Vf.end() != true)
1725 {
1726 // If this fails to get a file name we will bomb out below.
1727 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
1728 if (_error->PendingError() == true)
1729 return;
1730
1731 // Generate the final file name as: package_version_arch.foo
1732 StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
1733 QuoteString(Version.VerStr(),"_:") + '_' +
1734 QuoteString(Version.Arch(),"_:.") +
1735 "." + flExtension(Parse.FileName());
1736 }
b3d44315
MV
1737
1738 // check if we have one trusted source for the package. if so, switch
6c34ccca
DK
1739 // to "TrustedOnly" mode - but only if not in AllowUnauthenticated mode
1740 bool const allowUnauth = _config->FindB("APT::Get::AllowUnauthenticated", false);
1741 bool const debugAuth = _config->FindB("Debug::pkgAcquire::Auth", false);
1742 bool seenUntrusted = false;
f7f0d6c7 1743 for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; ++i)
b3d44315
MV
1744 {
1745 pkgIndexFile *Index;
1746 if (Sources->FindIndex(i.File(),Index) == false)
1747 continue;
6c34ccca
DK
1748
1749 if (debugAuth == true)
b3d44315 1750 std::cerr << "Checking index: " << Index->Describe()
6c34ccca
DK
1751 << "(Trusted=" << Index->IsTrusted() << ")" << std::endl;
1752
1753 if (Index->IsTrusted() == true)
1754 {
b3d44315 1755 Trusted = true;
6c34ccca
DK
1756 if (allowUnauth == false)
1757 break;
b3d44315 1758 }
6c34ccca
DK
1759 else
1760 seenUntrusted = true;
b3d44315
MV
1761 }
1762
a3371852
MV
1763 // "allow-unauthenticated" restores apts old fetching behaviour
1764 // that means that e.g. unauthenticated file:// uris are higher
1765 // priority than authenticated http:// uris
6c34ccca 1766 if (allowUnauth == true && seenUntrusted == true)
a3371852
MV
1767 Trusted = false;
1768
03e39e59 1769 // Select a source
b185acc2 1770 if (QueueNext() == false && _error->PendingError() == false)
2d5102e8 1771 _error->Error(_("I wasn't able to locate a file for the %s package. "
b2e465d6 1772 "This might mean you need to manually fix this package."),
b185acc2
AL
1773 Version.ParentPkg().Name());
1774}
1775 /*}}}*/
1776// AcqArchive::QueueNext - Queue the next file source /*{{{*/
1777// ---------------------------------------------------------------------
17caf1b1
AL
1778/* This queues the next available file version for download. It checks if
1779 the archive is already available in the cache and stashs the MD5 for
1780 checking later. */
b185acc2 1781bool pkgAcqArchive::QueueNext()
a722b2c5
DK
1782{
1783 string const ForceHash = _config->Find("Acquire::ForceHash");
f7f0d6c7 1784 for (; Vf.end() == false; ++Vf)
03e39e59
AL
1785 {
1786 // Ignore not source sources
1787 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
1788 continue;
1789
1790 // Try to cross match against the source list
b2e465d6
AL
1791 pkgIndexFile *Index;
1792 if (Sources->FindIndex(Vf.File(),Index) == false)
1793 continue;
03e39e59 1794
b3d44315
MV
1795 // only try to get a trusted package from another source if that source
1796 // is also trusted
1797 if(Trusted && !Index->IsTrusted())
1798 continue;
1799
03e39e59
AL
1800 // Grab the text package record
1801 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
1802 if (_error->PendingError() == true)
b185acc2 1803 return false;
03e39e59 1804
b2e465d6 1805 string PkgFile = Parse.FileName();
a722b2c5
DK
1806 if (ForceHash.empty() == false)
1807 {
d9b9e9e2
MV
1808 if(stringcasecmp(ForceHash, "sha512") == 0)
1809 ExpectedHash = HashString("SHA512", Parse.SHA512Hash());
ee5f5d25 1810 else if(stringcasecmp(ForceHash, "sha256") == 0)
a722b2c5
DK
1811 ExpectedHash = HashString("SHA256", Parse.SHA256Hash());
1812 else if (stringcasecmp(ForceHash, "sha1") == 0)
1813 ExpectedHash = HashString("SHA1", Parse.SHA1Hash());
1814 else
1815 ExpectedHash = HashString("MD5Sum", Parse.MD5Hash());
1816 }
1817 else
1818 {
1819 string Hash;
d9b9e9e2
MV
1820 if ((Hash = Parse.SHA512Hash()).empty() == false)
1821 ExpectedHash = HashString("SHA512", Hash);
1822 else if ((Hash = Parse.SHA256Hash()).empty() == false)
a722b2c5
DK
1823 ExpectedHash = HashString("SHA256", Hash);
1824 else if ((Hash = Parse.SHA1Hash()).empty() == false)
1825 ExpectedHash = HashString("SHA1", Hash);
1826 else
1827 ExpectedHash = HashString("MD5Sum", Parse.MD5Hash());
1828 }
03e39e59 1829 if (PkgFile.empty() == true)
b2e465d6
AL
1830 return _error->Error(_("The package index files are corrupted. No Filename: "
1831 "field for package %s."),
1832 Version.ParentPkg().Name());
a6568219 1833
b3d44315
MV
1834 Desc.URI = Index->ArchiveURI(PkgFile);
1835 Desc.Description = Index->ArchiveInfo(Version);
1836 Desc.Owner = this;
1837 Desc.ShortDesc = Version.ParentPkg().Name();
1838
17caf1b1 1839 // See if we already have the file. (Legacy filenames)
a6568219
AL
1840 FileSize = Version->Size;
1841 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
1842 struct stat Buf;
1843 if (stat(FinalFile.c_str(),&Buf) == 0)
1844 {
1845 // Make sure the size matches
73da43e9 1846 if ((unsigned long long)Buf.st_size == Version->Size)
a6568219
AL
1847 {
1848 Complete = true;
1849 Local = true;
1850 Status = StatDone;
30e1eab5 1851 StoreFilename = DestFile = FinalFile;
b185acc2 1852 return true;
a6568219
AL
1853 }
1854
6b1ff003
AL
1855 /* Hmm, we have a file and its size does not match, this means it is
1856 an old style mismatched arch */
a6568219
AL
1857 unlink(FinalFile.c_str());
1858 }
17caf1b1
AL
1859
1860 // Check it again using the new style output filenames
1861 FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
1862 if (stat(FinalFile.c_str(),&Buf) == 0)
1863 {
1864 // Make sure the size matches
73da43e9 1865 if ((unsigned long long)Buf.st_size == Version->Size)
17caf1b1
AL
1866 {
1867 Complete = true;
1868 Local = true;
1869 Status = StatDone;
1870 StoreFilename = DestFile = FinalFile;
1871 return true;
1872 }
1873
1874 /* Hmm, we have a file and its size does not match, this shouldnt
1875 happen.. */
1876 unlink(FinalFile.c_str());
1877 }
1878
1879 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
6b1ff003
AL
1880
1881 // Check the destination file
1882 if (stat(DestFile.c_str(),&Buf) == 0)
1883 {
1884 // Hmm, the partial file is too big, erase it
73da43e9 1885 if ((unsigned long long)Buf.st_size > Version->Size)
6b1ff003
AL
1886 unlink(DestFile.c_str());
1887 else
1888 PartialSize = Buf.st_size;
1889 }
de31189f
DK
1890
1891 // Disables download of archives - useful if no real installation follows,
1892 // e.g. if we are just interested in proposed installation order
1893 if (_config->FindB("Debug::pkgAcqArchive::NoQueue", false) == true)
1894 {
1895 Complete = true;
1896 Local = true;
1897 Status = StatDone;
1898 StoreFilename = DestFile = FinalFile;
1899 return true;
1900 }
1901
03e39e59 1902 // Create the item
b2e465d6
AL
1903 Local = false;
1904 Desc.URI = Index->ArchiveURI(PkgFile);
1905 Desc.Description = Index->ArchiveInfo(Version);
03e39e59
AL
1906 Desc.Owner = this;
1907 Desc.ShortDesc = Version.ParentPkg().Name();
1908 QueueURI(Desc);
b185acc2 1909
f7f0d6c7 1910 ++Vf;
b185acc2 1911 return true;
03e39e59 1912 }
b185acc2
AL
1913 return false;
1914}
03e39e59
AL
1915 /*}}}*/
1916// AcqArchive::Done - Finished fetching /*{{{*/
1917// ---------------------------------------------------------------------
1918/* */
73da43e9 1919void pkgAcqArchive::Done(string Message,unsigned long long Size,string CalcHash,
459681d3 1920 pkgAcquire::MethodConfig *Cfg)
03e39e59 1921{
495e5cb2 1922 Item::Done(Message,Size,CalcHash,Cfg);
03e39e59
AL
1923
1924 // Check the size
1925 if (Size != Version->Size)
1926 {
3c8030a4 1927 RenameOnError(SizeMismatch);
03e39e59
AL
1928 return;
1929 }
1930
495e5cb2 1931 // Check the hash
8a8feb29 1932 if(ExpectedHash.toStr() != CalcHash)
03e39e59 1933 {
3c8030a4 1934 RenameOnError(HashSumMismatch);
495e5cb2 1935 return;
03e39e59 1936 }
a6568219
AL
1937
1938 // Grab the output filename
03e39e59
AL
1939 string FileName = LookupTag(Message,"Filename");
1940 if (FileName.empty() == true)
1941 {
1942 Status = StatError;
1943 ErrorText = "Method gave a blank filename";
1944 return;
1945 }
a6568219
AL
1946
1947 Complete = true;
30e1eab5
AL
1948
1949 // Reference filename
a6568219
AL
1950 if (FileName != DestFile)
1951 {
30e1eab5 1952 StoreFilename = DestFile = FileName;
a6568219
AL
1953 Local = true;
1954 return;
1955 }
1956
1957 // Done, move it into position
1958 string FinalFile = _config->FindDir("Dir::Cache::Archives");
17caf1b1 1959 FinalFile += flNotDir(StoreFilename);
a6568219 1960 Rename(DestFile,FinalFile);
03e39e59 1961
30e1eab5 1962 StoreFilename = DestFile = FinalFile;
03e39e59
AL
1963 Complete = true;
1964}
1965 /*}}}*/
db890fdb
AL
1966// AcqArchive::Failed - Failure handler /*{{{*/
1967// ---------------------------------------------------------------------
1968/* Here we try other sources */
7d8afa39 1969void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
db890fdb
AL
1970{
1971 ErrorText = LookupTag(Message,"Message");
b2e465d6
AL
1972
1973 /* We don't really want to retry on failed media swaps, this prevents
1974 that. An interesting observation is that permanent failures are not
1975 recorded. */
1976 if (Cnf->Removable == true &&
1977 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1978 {
1979 // Vf = Version.FileList();
f7f0d6c7 1980 while (Vf.end() == false) ++Vf;
b2e465d6
AL
1981 StoreFilename = string();
1982 Item::Failed(Message,Cnf);
1983 return;
1984 }
1985
db890fdb 1986 if (QueueNext() == false)
7d8afa39
AL
1987 {
1988 // This is the retry counter
1989 if (Retries != 0 &&
1990 Cnf->LocalOnly == false &&
1991 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
1992 {
1993 Retries--;
1994 Vf = Version.FileList();
1995 if (QueueNext() == true)
1996 return;
1997 }
1998
9dbb421f 1999 StoreFilename = string();
7d8afa39
AL
2000 Item::Failed(Message,Cnf);
2001 }
db890fdb
AL
2002}
2003 /*}}}*/
92fcbfc1 2004// AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
b3d44315
MV
2005// ---------------------------------------------------------------------
2006bool pkgAcqArchive::IsTrusted()
2007{
2008 return Trusted;
2009}
92fcbfc1 2010 /*}}}*/
ab559b35
AL
2011// AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
2012// ---------------------------------------------------------------------
2013/* */
2014void pkgAcqArchive::Finished()
2015{
2016 if (Status == pkgAcquire::Item::StatDone &&
2017 Complete == true)
2018 return;
2019 StoreFilename = string();
2020}
2021 /*}}}*/
36375005
AL
2022// AcqFile::pkgAcqFile - Constructor /*{{{*/
2023// ---------------------------------------------------------------------
2024/* The file is added to the queue */
8a8feb29 2025pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string Hash,
73da43e9 2026 unsigned long long Size,string Dsc,string ShortDesc,
77278c2b
MV
2027 const string &DestDir, const string &DestFilename,
2028 bool IsIndexFile) :
2029 Item(Owner), ExpectedHash(Hash), IsIndexFile(IsIndexFile)
36375005 2030{
08cfc005
AL
2031 Retries = _config->FindI("Acquire::Retries",0);
2032
46e00f9d
MV
2033 if(!DestFilename.empty())
2034 DestFile = DestFilename;
2035 else if(!DestDir.empty())
2036 DestFile = DestDir + "/" + flNotDir(URI);
2037 else
2038 DestFile = flNotDir(URI);
2039
36375005
AL
2040 // Create the item
2041 Desc.URI = URI;
2042 Desc.Description = Dsc;
2043 Desc.Owner = this;
2044
2045 // Set the short description to the archive component
2046 Desc.ShortDesc = ShortDesc;
2047
2048 // Get the transfer sizes
2049 FileSize = Size;
2050 struct stat Buf;
2051 if (stat(DestFile.c_str(),&Buf) == 0)
2052 {
2053 // Hmm, the partial file is too big, erase it
73da43e9 2054 if ((unsigned long long)Buf.st_size > Size)
36375005
AL
2055 unlink(DestFile.c_str());
2056 else
2057 PartialSize = Buf.st_size;
2058 }
092ae175 2059
36375005
AL
2060 QueueURI(Desc);
2061}
2062 /*}}}*/
2063// AcqFile::Done - Item downloaded OK /*{{{*/
2064// ---------------------------------------------------------------------
2065/* */
73da43e9 2066void pkgAcqFile::Done(string Message,unsigned long long Size,string CalcHash,
459681d3 2067 pkgAcquire::MethodConfig *Cnf)
36375005 2068{
495e5cb2
MV
2069 Item::Done(Message,Size,CalcHash,Cnf);
2070
8a8feb29 2071 // Check the hash
2c941d89 2072 if(!ExpectedHash.empty() && ExpectedHash.toStr() != CalcHash)
b3c39978 2073 {
3c8030a4 2074 RenameOnError(HashSumMismatch);
495e5cb2 2075 return;
b3c39978
AL
2076 }
2077
36375005
AL
2078 string FileName = LookupTag(Message,"Filename");
2079 if (FileName.empty() == true)
2080 {
2081 Status = StatError;
2082 ErrorText = "Method gave a blank filename";
2083 return;
2084 }
2085
2086 Complete = true;
2087
2088 // The files timestamp matches
2089 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
2090 return;
2091
2092 // We have to copy it into place
2093 if (FileName != DestFile)
2094 {
2095 Local = true;
459681d3
AL
2096 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
2097 Cnf->Removable == true)
917ae805
AL
2098 {
2099 Desc.URI = "copy:" + FileName;
2100 QueueURI(Desc);
2101 return;
2102 }
2103
83ab33fc
AL
2104 // Erase the file if it is a symlink so we can overwrite it
2105 struct stat St;
2106 if (lstat(DestFile.c_str(),&St) == 0)
2107 {
2108 if (S_ISLNK(St.st_mode) != 0)
2109 unlink(DestFile.c_str());
2110 }
2111
2112 // Symlink the file
917ae805
AL
2113 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
2114 {
83ab33fc 2115 ErrorText = "Link to " + DestFile + " failure ";
917ae805
AL
2116 Status = StatError;
2117 Complete = false;
2118 }
36375005
AL
2119 }
2120}
2121 /*}}}*/
08cfc005
AL
2122// AcqFile::Failed - Failure handler /*{{{*/
2123// ---------------------------------------------------------------------
2124/* Here we try other sources */
2125void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
2126{
2127 ErrorText = LookupTag(Message,"Message");
2128
2129 // This is the retry counter
2130 if (Retries != 0 &&
2131 Cnf->LocalOnly == false &&
2132 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
2133 {
2134 Retries--;
2135 QueueURI(Desc);
2136 return;
2137 }
2138
2139 Item::Failed(Message,Cnf);
2140}
2141 /*}}}*/
77278c2b
MV
2142// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
2143// ---------------------------------------------------------------------
2144/* The only header we use is the last-modified header. */
2145string pkgAcqFile::Custom600Headers()
2146{
2147 if (IsIndexFile)
2148 return "\nIndex-File: true";
61a07c57 2149 return "";
77278c2b
MV
2150}
2151 /*}}}*/