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