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