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