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