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