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