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