]>
Commit | Line | Data |
---|---|---|
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 | 37 | using namespace std; |
5819a761 | 38 | |
0118833a AL |
39 | // Acquire::Item::Item - Constructor /*{{{*/ |
40 | // --------------------------------------------------------------------- | |
41 | /* */ | |
8267fe24 | 42 | pkgAcquire::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 | /* */ | |
53 | pkgAcquire::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 | 62 | void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf) |
c88edf1d | 63 | { |
93bf083d | 64 | Status = StatIdle; |
db890fdb | 65 | ErrorText = LookupTag(Message,"Message"); |
361593e9 | 66 | UsedMirror = LookupTag(Message,"UsedMirror"); |
c88edf1d | 67 | if (QueueCounter <= 1) |
93bf083d | 68 | { |
a72ace20 | 69 | /* This indicates that the file is not available right now but might |
7d8afa39 | 70 | be sometime later. If we do a retry cycle then this should be |
17caf1b1 | 71 | retried [CDROMs] */ |
7d8afa39 AL |
72 | if (Cnf->LocalOnly == true && |
73 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == true) | |
a72ace20 AL |
74 | { |
75 | Status = StatIdle; | |
681d76d0 | 76 | Dequeue(); |
a72ace20 AL |
77 | return; |
78 | } | |
7e5f33eb | 79 | |
93bf083d | 80 | Status = StatError; |
681d76d0 | 81 | Dequeue(); |
93bf083d | 82 | } |
36280399 MV |
83 | |
84 | // report mirror failure back to LP if we actually use a mirror | |
f0b509cd MV |
85 | string FailReason = LookupTag(Message, "FailReason"); |
86 | if(FailReason.size() != 0) | |
87 | ReportMirrorFailure(FailReason); | |
88 | else | |
89 | ReportMirrorFailure(ErrorText); | |
c88edf1d AL |
90 | } |
91 | /*}}}*/ | |
8267fe24 AL |
92 | // Acquire::Item::Start - Item has begun to download /*{{{*/ |
93 | // --------------------------------------------------------------------- | |
17caf1b1 AL |
94 | /* Stash status and the file size. Note that setting Complete means |
95 | sub-phases of the acquire process such as decompresion are operating */ | |
727f18af | 96 | void pkgAcquire::Item::Start(string /*Message*/,unsigned long Size) |
8267fe24 AL |
97 | { |
98 | Status = StatFetching; | |
99 | if (FileSize == 0 && Complete == false) | |
100 | FileSize = Size; | |
101 | } | |
102 | /*}}}*/ | |
c88edf1d AL |
103 | // Acquire::Item::Done - Item downloaded OK /*{{{*/ |
104 | // --------------------------------------------------------------------- | |
105 | /* */ | |
495e5cb2 | 106 | void pkgAcquire::Item::Done(string Message,unsigned long Size,string Hash, |
459681d3 | 107 | pkgAcquire::MethodConfig *Cnf) |
c88edf1d | 108 | { |
b98f2859 AL |
109 | // We just downloaded something.. |
110 | string FileName = LookupTag(Message,"Filename"); | |
36280399 | 111 | UsedMirror = LookupTag(Message,"UsedMirror"); |
8f30ca30 | 112 | if (Complete == false && !Local && FileName == DestFile) |
b98f2859 AL |
113 | { |
114 | if (Owner->Log != 0) | |
115 | Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str())); | |
116 | } | |
aa0e1101 AL |
117 | |
118 | if (FileSize == 0) | |
119 | FileSize= Size; | |
c88edf1d AL |
120 | Status = StatDone; |
121 | ErrorText = string(); | |
122 | Owner->Dequeue(this); | |
123 | } | |
124 | /*}}}*/ | |
8b89e57f AL |
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]; | |
0fcd01de | 134 | snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno), |
8b89e57f AL |
135 | From.c_str(),To.c_str()); |
136 | Status = StatError; | |
137 | ErrorText = S; | |
7a3c2ab0 | 138 | } |
8b89e57f AL |
139 | } |
140 | /*}}}*/ | |
0118833a | 141 | |
36280399 MV |
142 | void pkgAcquire::Item::ReportMirrorFailure(string FailCode) |
143 | { | |
59271f62 MV |
144 | // we only act if a mirror was used at all |
145 | if(UsedMirror.empty()) | |
146 | return; | |
36280399 MV |
147 | #if 0 |
148 | std::cerr << "\nReportMirrorFailure: " | |
149 | << UsedMirror | |
59271f62 | 150 | << " Uri: " << DescURI() |
36280399 MV |
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", | |
3f599bb7 | 157 | "/usr/lib/apt/apt-report-mirror-failure"); |
36280399 MV |
158 | if(!FileExists(report)) |
159 | return; | |
160 | Args[i++] = report.c_str(); | |
161 | Args[i++] = UsedMirror.c_str(); | |
f0b509cd | 162 | Args[i++] = DescURI().c_str(); |
36280399 | 163 | Args[i++] = FailCode.c_str(); |
361593e9 | 164 | Args[i++] = NULL; |
36280399 MV |
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 | { | |
361593e9 MV |
173 | execvp(Args[0], (char**)Args); |
174 | std::cerr << "Could not exec " << Args[0] << std::endl; | |
175 | _exit(100); | |
36280399 MV |
176 | } |
177 | if(!ExecWait(pid, "report-mirror-failure")) | |
178 | { | |
179 | _error->Warning("Couldn't report problem to '%s'", | |
361593e9 | 180 | _config->Find("Methods::Mirror::ProblemReporting").c_str()); |
36280399 MV |
181 | } |
182 | } | |
183 | ||
184 | ||
2237bd01 MV |
185 | |
186 | // AcqDiffIndex::AcqDiffIndex - Constructor | |
ac5b205a | 187 | // --------------------------------------------------------------------- |
2237bd01 MV |
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 | |
ac5b205a | 192 | */ |
2237bd01 MV |
193 | pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner, |
194 | string URI,string URIDesc,string ShortDesc, | |
495e5cb2 MV |
195 | HashString ExpectedHash) |
196 | : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash), | |
197 | Description(URIDesc) | |
ac5b205a MV |
198 | { |
199 | ||
ac5b205a MV |
200 | Debug = _config->FindB("Debug::pkgAcquire::Diffs",false); |
201 | ||
2237bd01 | 202 | Desc.Description = URIDesc + "/DiffIndex"; |
ac5b205a MV |
203 | Desc.Owner = this; |
204 | Desc.ShortDesc = ShortDesc; | |
2237bd01 MV |
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; | |
ac5b205a | 212 | |
2237bd01 | 213 | // look for the current package file |
ac5b205a MV |
214 | CurrentPackagesFile = _config->FindDir("Dir::State::lists"); |
215 | CurrentPackagesFile += URItoFileName(RealURI); | |
216 | ||
b4e57d2d MV |
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 | |
ac5b205a | 220 | if(!FileExists(CurrentPackagesFile) || |
81fcf9e2 | 221 | Desc.URI.substr(0,strlen("file:/")) == "file:/") |
2ac3eeb6 | 222 | { |
ac5b205a MV |
223 | // we don't have a pkg file or we don't want to queue |
224 | if(Debug) | |
b4e57d2d | 225 | std::clog << "No index file, local or canceld by user" << std::endl; |
ac5b205a MV |
226 | Failed("", NULL); |
227 | return; | |
228 | } | |
229 | ||
2ac3eeb6 | 230 | if(Debug) |
2237bd01 MV |
231 | std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): " |
232 | << CurrentPackagesFile << std::endl; | |
2ac3eeb6 | 233 | |
ac5b205a | 234 | QueueURI(Desc); |
2237bd01 | 235 | |
ac5b205a MV |
236 | } |
237 | ||
6cb30d01 MV |
238 | // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/ |
239 | // --------------------------------------------------------------------- | |
240 | /* The only header we use is the last-modified header. */ | |
2237bd01 | 241 | string pkgAcqDiffIndex::Custom600Headers() |
6cb30d01 | 242 | { |
6cb30d01 MV |
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 | ||
2237bd01 MV |
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; | |
002d9943 MV |
275 | bool found = false; |
276 | DiffInfo d; | |
277 | string size; | |
278 | ||
2237bd01 MV |
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 | ||
2ac3eeb6 MV |
288 | if(local_sha1 == ServerSha1) |
289 | { | |
290 | // we have the same sha1 as the server | |
2237bd01 MV |
291 | if(Debug) |
292 | std::clog << "Package file is up-to-date" << std::endl; | |
002d9943 MV |
293 | // set found to true, this will queue a pkgAcqIndexDiffs with |
294 | // a empty availabe_patches | |
295 | found = true; | |
2ac3eeb6 MV |
296 | } |
297 | else | |
298 | { | |
002d9943 MV |
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); | |
2ac3eeb6 MV |
305 | while(hist >> d.sha1 >> size >> d.file) |
306 | { | |
002d9943 MV |
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 | |
2ac3eeb6 MV |
312 | if(found) |
313 | { | |
002d9943 MV |
314 | if(Debug) |
315 | std::clog << "Need to get diff: " << d.file << std::endl; | |
316 | available_patches.push_back(d); | |
317 | } | |
2237bd01 MV |
318 | } |
319 | } | |
320 | ||
05aab406 MV |
321 | // we have something, queue the next diff |
322 | if(found) | |
2ac3eeb6 | 323 | { |
2237bd01 | 324 | // queue the diffs |
495e5cb2 | 325 | string::size_type last_space = Description.rfind(" "); |
05aab406 MV |
326 | if(last_space != string::npos) |
327 | Description.erase(last_space, Description.size()-last_space); | |
2237bd01 | 328 | new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc, |
495e5cb2 | 329 | ExpectedHash, available_patches); |
2237bd01 MV |
330 | Complete = false; |
331 | Status = StatDone; | |
332 | Dequeue(); | |
333 | return true; | |
334 | } | |
335 | } | |
05aab406 MV |
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; | |
2237bd01 MV |
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 | ||
002d9943 | 351 | new pkgAcqIndex(Owner, RealURI, Description, Desc.ShortDesc, |
495e5cb2 | 352 | ExpectedHash); |
2237bd01 MV |
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, | |
1c7d52b7 | 398 | HashString ExpectedHash, |
495e5cb2 MV |
399 | vector<DiffInfo> diffs) |
400 | : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash), | |
2237bd01 MV |
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 | ||
05aab406 | 409 | Description = URIDesc; |
2237bd01 MV |
410 | Desc.Owner = this; |
411 | Desc.ShortDesc = ShortDesc; | |
412 | ||
2ac3eeb6 MV |
413 | if(available_patches.size() == 0) |
414 | { | |
2237bd01 MV |
415 | // we are done (yeah!) |
416 | Finish(true); | |
2ac3eeb6 MV |
417 | } |
418 | else | |
419 | { | |
2237bd01 MV |
420 | // get the next diff |
421 | State = StateFetchDiff; | |
422 | QueueNextDiff(); | |
423 | } | |
424 | } | |
425 | ||
426 | ||
ac5b205a MV |
427 | void pkgAcqIndexDiffs::Failed(string Message,pkgAcquire::MethodConfig *Cnf) |
428 | { | |
2237bd01 MV |
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, | |
495e5cb2 | 433 | ExpectedHash); |
ac5b205a MV |
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 | |
2ac3eeb6 MV |
443 | if(allDone) |
444 | { | |
ac5b205a MV |
445 | DestFile = _config->FindDir("Dir::State::lists"); |
446 | DestFile += URItoFileName(RealURI); | |
2d4722e2 | 447 | |
495e5cb2 | 448 | if(!ExpectedHash.empty() && !ExpectedHash.VerifyFile(DestFile)) |
2d4722e2 MV |
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 | |
ac5b205a | 458 | Complete = true; |
cffc2ddd | 459 | Status = StatDone; |
ac5b205a MV |
460 | Dequeue(); |
461 | if(Debug) | |
462 | std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl; | |
463 | return; | |
ac5b205a MV |
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 | ||
ac5b205a MV |
475 | |
476 | bool pkgAcqIndexDiffs::QueueNextDiff() | |
477 | { | |
3de9ff77 | 478 | |
94dc9d7d MV |
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()); | |
3de9ff77 MV |
487 | if(Debug) |
488 | std::clog << "QueueNextDiff: " | |
489 | << FinalFile << " (" << local_sha1 << ")"<<std::endl; | |
94dc9d7d | 490 | |
26d27645 MV |
491 | // remove all patches until the next matching patch is found |
492 | // this requires the Index file to be ordered | |
94dc9d7d | 493 | for(vector<DiffInfo>::iterator I=available_patches.begin(); |
2ac3eeb6 MV |
494 | available_patches.size() > 0 && |
495 | I != available_patches.end() && | |
496 | (*I).sha1 != local_sha1; | |
497 | I++) | |
498 | { | |
26d27645 | 499 | available_patches.erase(I); |
59a704f0 | 500 | } |
94dc9d7d MV |
501 | |
502 | // error checking and falling back if no patch was found | |
2ac3eeb6 MV |
503 | if(available_patches.size() == 0) |
504 | { | |
94dc9d7d MV |
505 | Failed("", NULL); |
506 | return false; | |
507 | } | |
6cb30d01 | 508 | |
94dc9d7d | 509 | // queue the right diff |
2237bd01 | 510 | Desc.URI = string(RealURI) + ".diff/" + available_patches[0].file + ".gz"; |
05aab406 | 511 | Desc.Description = Description + " " + available_patches[0].file + string(".pdiff"); |
ac5b205a | 512 | DestFile = _config->FindDir("Dir::State::lists") + "partial/"; |
2237bd01 | 513 | DestFile += URItoFileName(RealURI + ".diff/" + available_patches[0].file); |
ac5b205a MV |
514 | |
515 | if(Debug) | |
516 | std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl; | |
517 | ||
518 | QueueURI(Desc); | |
519 | ||
520 | return true; | |
521 | } | |
522 | ||
ac5b205a MV |
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 | ||
4a0a786f MV |
533 | string FinalFile; |
534 | FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI); | |
6cb30d01 | 535 | |
4a0a786f MV |
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; | |
b7347826 | 545 | Local = true; |
4a0a786f MV |
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; | |
b7347826 | 564 | Local = true; |
4a0a786f MV |
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 | |
94dc9d7d | 576 | available_patches.erase(available_patches.begin()); |
ac5b205a | 577 | |
4a0a786f | 578 | // move into place |
59a704f0 MV |
579 | if(Debug) |
580 | { | |
4a0a786f MV |
581 | std::clog << "Moving patched file in place: " << std::endl |
582 | << DestFile << " -> " << FinalFile << std::endl; | |
59a704f0 | 583 | } |
4a0a786f | 584 | Rename(DestFile,FinalFile); |
1790e0cf | 585 | chmod(FinalFile.c_str(),0644); |
4a0a786f MV |
586 | |
587 | // see if there is more to download | |
94dc9d7d | 588 | if(available_patches.size() > 0) { |
ac5b205a | 589 | new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc, |
495e5cb2 | 590 | ExpectedHash, available_patches); |
4a0a786f MV |
591 | return Finish(); |
592 | } else | |
593 | return Finish(true); | |
ac5b205a | 594 | } |
ac5b205a MV |
595 | } |
596 | ||
597 | ||
0118833a AL |
598 | // AcqIndex::AcqIndex - Constructor /*{{{*/ |
599 | // --------------------------------------------------------------------- | |
600 | /* The package file is added to the queue and a second class is | |
b2e465d6 AL |
601 | instantiated to fetch the revision file */ |
602 | pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner, | |
b3d44315 | 603 | string URI,string URIDesc,string ShortDesc, |
495e5cb2 MV |
604 | HashString ExpectedHash, string comprExt) |
605 | : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash) | |
0118833a | 606 | { |
8b89e57f | 607 | Decompression = false; |
bfd22fc0 | 608 | Erase = false; |
13e8426f | 609 | |
0a8a80e5 | 610 | DestFile = _config->FindDir("Dir::State::lists") + "partial/"; |
b2e465d6 | 611 | DestFile += URItoFileName(URI); |
8267fe24 | 612 | |
b3d44315 MV |
613 | if(comprExt.empty()) |
614 | { | |
13e8426f | 615 | // autoselect the compression method |
4577fda2 | 616 | if(FileExists("/bin/bzip2")) |
13e8426f MV |
617 | CompressionExtension = ".bz2"; |
618 | else | |
619 | CompressionExtension = ".gz"; | |
b3d44315 | 620 | } else { |
13e8426f | 621 | CompressionExtension = comprExt; |
b3d44315 | 622 | } |
13e8426f | 623 | Desc.URI = URI + CompressionExtension; |
b3d44315 | 624 | |
b2e465d6 | 625 | Desc.Description = URIDesc; |
8267fe24 | 626 | Desc.Owner = this; |
b2e465d6 | 627 | Desc.ShortDesc = ShortDesc; |
8267fe24 AL |
628 | |
629 | QueueURI(Desc); | |
0118833a AL |
630 | } |
631 | /*}}}*/ | |
0a8a80e5 | 632 | // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/ |
0118833a | 633 | // --------------------------------------------------------------------- |
0a8a80e5 AL |
634 | /* The only header we use is the last-modified header. */ |
635 | string pkgAcqIndex::Custom600Headers() | |
0118833a | 636 | { |
0a8a80e5 | 637 | string Final = _config->FindDir("Dir::State::lists"); |
b2e465d6 | 638 | Final += URItoFileName(RealURI); |
0a8a80e5 AL |
639 | |
640 | struct stat Buf; | |
641 | if (stat(Final.c_str(),&Buf) != 0) | |
a72ace20 | 642 | return "\nIndex-File: true"; |
a72ace20 | 643 | return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime); |
0118833a AL |
644 | } |
645 | /*}}}*/ | |
debc84b2 MZ |
646 | |
647 | void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) | |
648 | { | |
649 | // no .bz2 found, retry with .gz | |
46e00f9d | 650 | if(Desc.URI.substr(Desc.URI.size()-3) == "bz2") { |
debc84b2 | 651 | Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz"; |
b3d44315 MV |
652 | |
653 | // retry with a gzip one | |
654 | new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc, | |
495e5cb2 | 655 | ExpectedHash, string(".gz")); |
b3d44315 MV |
656 | Status = StatDone; |
657 | Complete = false; | |
658 | Dequeue(); | |
debc84b2 | 659 | return; |
17ff0930 MV |
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()); | |
debc84b2 MZ |
667 | } |
668 | ||
debc84b2 MZ |
669 | Item::Failed(Message,Cnf); |
670 | } | |
671 | ||
672 | ||
8b89e57f AL |
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. */ | |
495e5cb2 | 680 | void pkgAcqIndex::Done(string Message,unsigned long Size,string Hash, |
459681d3 | 681 | pkgAcquire::MethodConfig *Cfg) |
8b89e57f | 682 | { |
495e5cb2 | 683 | Item::Done(Message,Size,Hash,Cfg); |
8b89e57f AL |
684 | |
685 | if (Decompression == true) | |
686 | { | |
b3d44315 MV |
687 | if (_config->FindB("Debug::pkgAcquire::Auth", false)) |
688 | { | |
495e5cb2 MV |
689 | std::cerr << std::endl << RealURI << ": Computed Hash: " << Hash; |
690 | std::cerr << " Expected Hash: " << ExpectedHash.toStr() << std::endl; | |
b3d44315 MV |
691 | } |
692 | ||
8a8feb29 | 693 | if (!ExpectedHash.empty() && ExpectedHash.toStr() != Hash) |
b3d44315 MV |
694 | { |
695 | Status = StatAuthError; | |
9498d182 | 696 | ErrorText = _("Hash Sum mismatch"); |
b3d44315 | 697 | Rename(DestFile,DestFile + ".FAILED"); |
59271f62 | 698 | ReportMirrorFailure("HashChecksumFailure"); |
b3d44315 MV |
699 | return; |
700 | } | |
8b89e57f AL |
701 | // Done, move it into position |
702 | string FinalFile = _config->FindDir("Dir::State::lists"); | |
b2e465d6 | 703 | FinalFile += URItoFileName(RealURI); |
8b89e57f | 704 | Rename(DestFile,FinalFile); |
7a3c2ab0 | 705 | chmod(FinalFile.c_str(),0644); |
bfd22fc0 | 706 | |
7a7fa5f0 AL |
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/"; | |
b2e465d6 | 710 | DestFile += URItoFileName(RealURI); |
7a7fa5f0 | 711 | |
bfd22fc0 AL |
712 | // Remove the compressed version. |
713 | if (Erase == true) | |
bfd22fc0 | 714 | unlink(DestFile.c_str()); |
8b89e57f AL |
715 | return; |
716 | } | |
bfd22fc0 AL |
717 | |
718 | Erase = false; | |
8267fe24 | 719 | Complete = true; |
bfd22fc0 | 720 | |
8b89e57f AL |
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; | |
8b89e57f | 728 | Decompression = true; |
a6568219 | 729 | Local = true; |
8b89e57f | 730 | DestFile += ".decomp"; |
8267fe24 AL |
731 | Desc.URI = "copy:" + FileName; |
732 | QueueURI(Desc); | |
b98f2859 | 733 | Mode = "copy"; |
8b89e57f AL |
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; | |
bfd22fc0 AL |
747 | |
748 | if (FileName == DestFile) | |
749 | Erase = true; | |
8267fe24 | 750 | else |
a6568219 | 751 | Local = true; |
8b89e57f | 752 | |
46e00f9d | 753 | string compExt = Desc.URI.substr(Desc.URI.size()-3); |
1450dbaa | 754 | const char *decompProg; |
debc84b2 MZ |
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 | ||
8b89e57f AL |
764 | Decompression = true; |
765 | DestFile += ".decomp"; | |
debc84b2 | 766 | Desc.URI = string(decompProg) + ":" + FileName; |
8267fe24 | 767 | QueueURI(Desc); |
debc84b2 | 768 | Mode = decompProg; |
8b89e57f | 769 | } |
8b89e57f | 770 | |
a52f938b OS |
771 | // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/ |
772 | // --------------------------------------------------------------------- | |
773 | /* The Translation file is added to the queue */ | |
774 | pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner, | |
495e5cb2 MV |
775 | string URI,string URIDesc,string ShortDesc) |
776 | : pkgAcqIndex(Owner, URI, URIDesc, ShortDesc, HashString(), "") | |
a52f938b OS |
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 | ||
b3d44315 MV |
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), | |
46e00f9d MV |
807 | MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc), |
808 | MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets) | |
0118833a | 809 | { |
0a8a80e5 | 810 | DestFile = _config->FindDir("Dir::State::lists") + "partial/"; |
b2e465d6 | 811 | DestFile += URItoFileName(URI); |
b3d44315 | 812 | |
47eb38f4 MV |
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 | |
f6237efd MV |
816 | unlink(DestFile.c_str()); |
817 | ||
8267fe24 | 818 | // Create the item |
b2e465d6 | 819 | Desc.Description = URIDesc; |
8267fe24 | 820 | Desc.Owner = this; |
b3d44315 MV |
821 | Desc.ShortDesc = ShortDesc; |
822 | Desc.URI = URI; | |
b3d44315 MV |
823 | |
824 | string Final = _config->FindDir("Dir::State::lists"); | |
825 | Final += URItoFileName(RealURI); | |
826 | struct stat Buf; | |
827 | if (stat(Final.c_str(),&Buf) == 0) | |
828 | { | |
ef942597 MV |
829 | // File was already in place. It needs to be re-downloaded/verified |
830 | // because Release might have changed, we do give it a differnt | |
831 | // name than DestFile because otherwise the http method will | |
832 | // send If-Range requests and there are too many broken servers | |
833 | // out there that do not understand them | |
834 | LastGoodSig = DestFile+".reverify"; | |
835 | Rename(Final,LastGoodSig); | |
b3d44315 | 836 | } |
8267fe24 | 837 | |
8267fe24 | 838 | QueueURI(Desc); |
0118833a AL |
839 | } |
840 | /*}}}*/ | |
b3d44315 | 841 | // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/ |
0118833a | 842 | // --------------------------------------------------------------------- |
0a8a80e5 | 843 | /* The only header we use is the last-modified header. */ |
b3d44315 | 844 | string pkgAcqMetaSig::Custom600Headers() |
0118833a | 845 | { |
0a8a80e5 | 846 | struct stat Buf; |
ef942597 | 847 | if (stat(LastGoodSig.c_str(),&Buf) != 0) |
a72ace20 | 848 | return "\nIndex-File: true"; |
a789b983 | 849 | |
a72ace20 | 850 | return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime); |
0118833a | 851 | } |
b3d44315 MV |
852 | |
853 | void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5, | |
854 | pkgAcquire::MethodConfig *Cfg) | |
c88edf1d | 855 | { |
459681d3 | 856 | Item::Done(Message,Size,MD5,Cfg); |
c88edf1d AL |
857 | |
858 | string FileName = LookupTag(Message,"Filename"); | |
859 | if (FileName.empty() == true) | |
860 | { | |
861 | Status = StatError; | |
862 | ErrorText = "Method gave a blank filename"; | |
8b89e57f | 863 | return; |
c88edf1d | 864 | } |
8b89e57f | 865 | |
c88edf1d AL |
866 | if (FileName != DestFile) |
867 | { | |
b3d44315 | 868 | // We have to copy it into place |
a6568219 | 869 | Local = true; |
8267fe24 AL |
870 | Desc.URI = "copy:" + FileName; |
871 | QueueURI(Desc); | |
c88edf1d AL |
872 | return; |
873 | } | |
b3d44315 MV |
874 | |
875 | Complete = true; | |
876 | ||
ef942597 MV |
877 | // put the last known good file back on i-m-s hit (it will |
878 | // be re-verified again) | |
879 | // Else do nothing, we have the new file in DestFile then | |
880 | if(StringToBool(LookupTag(Message,"IMS-Hit"),false) == true) | |
881 | Rename(LastGoodSig, DestFile); | |
882 | ||
b3d44315 | 883 | // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved |
fce72602 MV |
884 | new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, |
885 | MetaIndexShortDesc, DestFile, IndexTargets, | |
886 | MetaIndexParser); | |
b3d44315 | 887 | |
c88edf1d AL |
888 | } |
889 | /*}}}*/ | |
b3d44315 | 890 | void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf) |
681d76d0 | 891 | { |
47eb38f4 | 892 | string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI); |
a789b983 | 893 | |
75dd8af1 | 894 | // if we get a network error we fail gracefully |
7e5f33eb MV |
895 | if(Status == StatTransientNetworkError) |
896 | { | |
24057ad6 | 897 | Item::Failed(Message,Cnf); |
484dbb81 | 898 | // move the sigfile back on transient network failures |
47eb38f4 | 899 | if(FileExists(DestFile)) |
ef942597 | 900 | Rename(LastGoodSig,Final); |
7e5f33eb MV |
901 | |
902 | // set the status back to , Item::Failed likes to reset it | |
903 | Status = pkgAcquire::Item::StatTransientNetworkError; | |
24057ad6 MV |
904 | return; |
905 | } | |
906 | ||
75dd8af1 | 907 | // Delete any existing sigfile when the acquire failed |
75dd8af1 MV |
908 | unlink(Final.c_str()); |
909 | ||
b3d44315 MV |
910 | // queue a pkgAcqMetaIndex with no sigfile |
911 | new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc, | |
912 | "", IndexTargets, MetaIndexParser); | |
913 | ||
681d76d0 AL |
914 | if (Cnf->LocalOnly == true || |
915 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == false) | |
916 | { | |
2b154e53 AL |
917 | // Ignore this |
918 | Status = StatDone; | |
919 | Complete = false; | |
681d76d0 AL |
920 | Dequeue(); |
921 | return; | |
922 | } | |
923 | ||
924 | Item::Failed(Message,Cnf); | |
925 | } | |
b3d44315 MV |
926 | |
927 | pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner, | |
928 | string URI,string URIDesc,string ShortDesc, | |
929 | string SigFile, | |
930 | const vector<struct IndexTarget*>* IndexTargets, | |
931 | indexRecords* MetaIndexParser) : | |
21fd1746 OS |
932 | Item(Owner), RealURI(URI), SigFile(SigFile), IndexTargets(IndexTargets), |
933 | MetaIndexParser(MetaIndexParser), AuthPass(false), IMSHit(false) | |
b3d44315 | 934 | { |
b3d44315 MV |
935 | DestFile = _config->FindDir("Dir::State::lists") + "partial/"; |
936 | DestFile += URItoFileName(URI); | |
937 | ||
938 | // Create the item | |
939 | Desc.Description = URIDesc; | |
940 | Desc.Owner = this; | |
941 | Desc.ShortDesc = ShortDesc; | |
942 | Desc.URI = URI; | |
943 | ||
944 | QueueURI(Desc); | |
945 | } | |
946 | ||
947 | /*}}}*/ | |
948 | // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/ | |
949 | // --------------------------------------------------------------------- | |
950 | /* The only header we use is the last-modified header. */ | |
951 | string pkgAcqMetaIndex::Custom600Headers() | |
952 | { | |
953 | string Final = _config->FindDir("Dir::State::lists"); | |
954 | Final += URItoFileName(RealURI); | |
955 | ||
956 | struct stat Buf; | |
957 | if (stat(Final.c_str(),&Buf) != 0) | |
958 | return "\nIndex-File: true"; | |
959 | ||
960 | return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime); | |
961 | } | |
962 | ||
495e5cb2 | 963 | void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string Hash, |
b3d44315 MV |
964 | pkgAcquire::MethodConfig *Cfg) |
965 | { | |
495e5cb2 | 966 | Item::Done(Message,Size,Hash,Cfg); |
b3d44315 MV |
967 | |
968 | // MetaIndexes are done in two passes: one to download the | |
969 | // metaindex with an appropriate method, and a second to verify it | |
970 | // with the gpgv method | |
971 | ||
972 | if (AuthPass == true) | |
973 | { | |
974 | AuthDone(Message); | |
fce72602 MV |
975 | |
976 | // all cool, move Release file into place | |
977 | Complete = true; | |
978 | ||
979 | string FinalFile = _config->FindDir("Dir::State::lists"); | |
980 | FinalFile += URItoFileName(RealURI); | |
981 | Rename(DestFile,FinalFile); | |
982 | chmod(FinalFile.c_str(),0644); | |
983 | DestFile = FinalFile; | |
b3d44315 MV |
984 | } |
985 | else | |
986 | { | |
987 | RetrievalDone(Message); | |
988 | if (!Complete) | |
989 | // Still more retrieving to do | |
990 | return; | |
991 | ||
992 | if (SigFile == "") | |
993 | { | |
994 | // There was no signature file, so we are finished. Download | |
995 | // the indexes without verification. | |
996 | QueueIndexes(false); | |
997 | } | |
998 | else | |
999 | { | |
1000 | // There was a signature file, so pass it to gpgv for | |
1001 | // verification | |
1002 | ||
1003 | if (_config->FindB("Debug::pkgAcquire::Auth", false)) | |
1004 | std::cerr << "Metaindex acquired, queueing gpg verification (" | |
1005 | << SigFile << "," << DestFile << ")\n"; | |
1006 | AuthPass = true; | |
1007 | Desc.URI = "gpgv:" + SigFile; | |
1008 | QueueURI(Desc); | |
1009 | Mode = "gpgv"; | |
1010 | } | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | void pkgAcqMetaIndex::RetrievalDone(string Message) | |
1015 | { | |
1016 | // We have just finished downloading a Release file (it is not | |
1017 | // verified yet) | |
1018 | ||
1019 | string FileName = LookupTag(Message,"Filename"); | |
1020 | if (FileName.empty() == true) | |
1021 | { | |
1022 | Status = StatError; | |
1023 | ErrorText = "Method gave a blank filename"; | |
1024 | return; | |
1025 | } | |
1026 | ||
1027 | if (FileName != DestFile) | |
1028 | { | |
1029 | Local = true; | |
1030 | Desc.URI = "copy:" + FileName; | |
1031 | QueueURI(Desc); | |
1032 | return; | |
1033 | } | |
1034 | ||
fce72602 | 1035 | // make sure to verify against the right file on I-M-S hit |
f381d68d | 1036 | IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false); |
fce72602 MV |
1037 | if(IMSHit) |
1038 | { | |
1039 | string FinalFile = _config->FindDir("Dir::State::lists"); | |
1040 | FinalFile += URItoFileName(RealURI); | |
1041 | DestFile = FinalFile; | |
1042 | } | |
b3d44315 | 1043 | Complete = true; |
b3d44315 MV |
1044 | } |
1045 | ||
1046 | void pkgAcqMetaIndex::AuthDone(string Message) | |
1047 | { | |
1048 | // At this point, the gpgv method has succeeded, so there is a | |
1049 | // valid signature from a key in the trusted keyring. We | |
1050 | // perform additional verification of its contents, and use them | |
1051 | // to verify the indexes we are about to download | |
1052 | ||
1053 | if (!MetaIndexParser->Load(DestFile)) | |
1054 | { | |
1055 | Status = StatAuthError; | |
1056 | ErrorText = MetaIndexParser->ErrorText; | |
1057 | return; | |
1058 | } | |
1059 | ||
ce424cd4 | 1060 | if (!VerifyVendor(Message)) |
b3d44315 MV |
1061 | { |
1062 | return; | |
1063 | } | |
1064 | ||
1065 | if (_config->FindB("Debug::pkgAcquire::Auth", false)) | |
1066 | std::cerr << "Signature verification succeeded: " | |
1067 | << DestFile << std::endl; | |
1068 | ||
1069 | // Download further indexes with verification | |
1070 | QueueIndexes(true); | |
1071 | ||
1072 | // Done, move signature file into position | |
b3d44315 MV |
1073 | string VerifiedSigFile = _config->FindDir("Dir::State::lists") + |
1074 | URItoFileName(RealURI) + ".gpg"; | |
1075 | Rename(SigFile,VerifiedSigFile); | |
1076 | chmod(VerifiedSigFile.c_str(),0644); | |
1077 | } | |
1078 | ||
1079 | void pkgAcqMetaIndex::QueueIndexes(bool verify) | |
1080 | { | |
1081 | for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin(); | |
1082 | Target != IndexTargets->end(); | |
1083 | Target++) | |
1084 | { | |
495e5cb2 | 1085 | HashString ExpectedIndexHash; |
b3d44315 MV |
1086 | if (verify) |
1087 | { | |
1088 | const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey); | |
1089 | if (!Record) | |
1090 | { | |
1091 | Status = StatAuthError; | |
1092 | ErrorText = "Unable to find expected entry " | |
1093 | + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)"; | |
1094 | return; | |
1095 | } | |
495e5cb2 | 1096 | ExpectedIndexHash = Record->Hash; |
b3d44315 MV |
1097 | if (_config->FindB("Debug::pkgAcquire::Auth", false)) |
1098 | { | |
1099 | std::cerr << "Queueing: " << (*Target)->URI << std::endl; | |
495e5cb2 | 1100 | std::cerr << "Expected Hash: " << ExpectedIndexHash.toStr() << std::endl; |
b3d44315 | 1101 | } |
495e5cb2 | 1102 | if (ExpectedIndexHash.empty()) |
b3d44315 MV |
1103 | { |
1104 | Status = StatAuthError; | |
495e5cb2 | 1105 | ErrorText = "Unable to find hash sum for " |
b3d44315 MV |
1106 | + (*Target)->MetaKey + " in Meta-index file"; |
1107 | return; | |
1108 | } | |
1109 | } | |
1110 | ||
2ac3eeb6 MV |
1111 | // Queue Packages file (either diff or full packages files, depending |
1112 | // on the users option) | |
702c84fb | 1113 | if(_config->FindB("Acquire::PDiffs",false) == true) |
2ac3eeb6 | 1114 | new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description, |
495e5cb2 | 1115 | (*Target)->ShortDesc, ExpectedIndexHash); |
2ac3eeb6 | 1116 | else |
81fcf9e2 | 1117 | new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description, |
495e5cb2 | 1118 | (*Target)->ShortDesc, ExpectedIndexHash); |
b3d44315 MV |
1119 | } |
1120 | } | |
1121 | ||
ce424cd4 | 1122 | bool pkgAcqMetaIndex::VerifyVendor(string Message) |
b3d44315 MV |
1123 | { |
1124 | // // Maybe this should be made available from above so we don't have | |
1125 | // // to read and parse it every time? | |
1126 | // pkgVendorList List; | |
1127 | // List.ReadMainList(); | |
1128 | ||
1129 | // const Vendor* Vndr = NULL; | |
1130 | // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++) | |
1131 | // { | |
1132 | // string::size_type pos = (*I).find("VALIDSIG "); | |
1133 | // if (_config->FindB("Debug::Vendor", false)) | |
1134 | // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos | |
1135 | // << std::endl; | |
1136 | // if (pos != std::string::npos) | |
1137 | // { | |
1138 | // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG")); | |
1139 | // if (_config->FindB("Debug::Vendor", false)) | |
1140 | // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." << | |
1141 | // std::endl; | |
1142 | // Vndr = List.FindVendor(Fingerprint) != ""; | |
1143 | // if (Vndr != NULL); | |
1144 | // break; | |
1145 | // } | |
1146 | // } | |
ce424cd4 MV |
1147 | string::size_type pos; |
1148 | ||
1149 | // check for missing sigs (that where not fatal because otherwise we had | |
1150 | // bombed earlier) | |
1151 | string missingkeys; | |
400ad7a4 | 1152 | string msg = _("There is no public key available for the " |
ce424cd4 MV |
1153 | "following key IDs:\n"); |
1154 | pos = Message.find("NO_PUBKEY "); | |
1155 | if (pos != std::string::npos) | |
1156 | { | |
1157 | string::size_type start = pos+strlen("NO_PUBKEY "); | |
1158 | string Fingerprint = Message.substr(start, Message.find("\n")-start); | |
1159 | missingkeys += (Fingerprint); | |
1160 | } | |
1161 | if(!missingkeys.empty()) | |
1162 | _error->Warning("%s", string(msg+missingkeys).c_str()); | |
b3d44315 MV |
1163 | |
1164 | string Transformed = MetaIndexParser->GetExpectedDist(); | |
1165 | ||
1166 | if (Transformed == "../project/experimental") | |
1167 | { | |
1168 | Transformed = "experimental"; | |
1169 | } | |
1170 | ||
ce424cd4 | 1171 | pos = Transformed.rfind('/'); |
b3d44315 MV |
1172 | if (pos != string::npos) |
1173 | { | |
1174 | Transformed = Transformed.substr(0, pos); | |
1175 | } | |
1176 | ||
1177 | if (Transformed == ".") | |
1178 | { | |
1179 | Transformed = ""; | |
1180 | } | |
1181 | ||
1182 | if (_config->FindB("Debug::pkgAcquire::Auth", false)) | |
1183 | { | |
1184 | std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl; | |
1185 | std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl; | |
1186 | std::cerr << "Transformed Dist: " << Transformed << std::endl; | |
1187 | } | |
1188 | ||
1189 | if (MetaIndexParser->CheckDist(Transformed) == false) | |
1190 | { | |
1191 | // This might become fatal one day | |
1192 | // Status = StatAuthError; | |
1193 | // ErrorText = "Conflicting distribution; expected " | |
1194 | // + MetaIndexParser->GetExpectedDist() + " but got " | |
1195 | // + MetaIndexParser->GetDist(); | |
1196 | // return false; | |
1197 | if (!Transformed.empty()) | |
1198 | { | |
1199 | _error->Warning("Conflicting distribution: %s (expected %s but got %s)", | |
1200 | Desc.Description.c_str(), | |
1201 | Transformed.c_str(), | |
1202 | MetaIndexParser->GetDist().c_str()); | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | return true; | |
1207 | } | |
1208 | /*}}}*/ | |
1209 | // pkgAcqMetaIndex::Failed - no Release file present or no signature | |
1210 | // file present /*{{{*/ | |
1211 | // --------------------------------------------------------------------- | |
1212 | /* */ | |
1213 | void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) | |
1214 | { | |
1215 | if (AuthPass == true) | |
1216 | { | |
fce72602 MV |
1217 | // gpgv method failed, if we have a good signature |
1218 | string LastGoodSigFile = _config->FindDir("Dir::State::lists") + | |
1219 | "partial/" + URItoFileName(RealURI) + ".gpg.reverify"; | |
1220 | if(FileExists(LastGoodSigFile)) | |
f381d68d | 1221 | { |
fce72602 MV |
1222 | string VerifiedSigFile = _config->FindDir("Dir::State::lists") + |
1223 | URItoFileName(RealURI) + ".gpg"; | |
1224 | Rename(LastGoodSigFile,VerifiedSigFile); | |
1225 | Status = StatTransientNetworkError; | |
1226 | _error->Warning(_("A error occurred during the signature " | |
1227 | "verification. The repository is not updated " | |
1228 | "and the previous index files will be used." | |
76b8e5a5 | 1229 | "GPG error: %s: %s\n"), |
fce72602 MV |
1230 | Desc.Description.c_str(), |
1231 | LookupTag(Message,"Message").c_str()); | |
f381d68d | 1232 | return; |
fce72602 MV |
1233 | } else { |
1234 | _error->Warning(_("GPG error: %s: %s"), | |
1235 | Desc.Description.c_str(), | |
1236 | LookupTag(Message,"Message").c_str()); | |
f381d68d | 1237 | } |
f381d68d | 1238 | // gpgv method failed |
59271f62 | 1239 | ReportMirrorFailure("GPGFailure"); |
b3d44315 MV |
1240 | } |
1241 | ||
1242 | // No Release file was present, or verification failed, so fall | |
1243 | // back to queueing Packages files without verification | |
1244 | QueueIndexes(false); | |
1245 | } | |
1246 | ||
681d76d0 | 1247 | /*}}}*/ |
03e39e59 AL |
1248 | |
1249 | // AcqArchive::AcqArchive - Constructor /*{{{*/ | |
1250 | // --------------------------------------------------------------------- | |
17caf1b1 AL |
1251 | /* This just sets up the initial fetch environment and queues the first |
1252 | possibilitiy */ | |
03e39e59 | 1253 | pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources, |
30e1eab5 AL |
1254 | pkgRecords *Recs,pkgCache::VerIterator const &Version, |
1255 | string &StoreFilename) : | |
1256 | Item(Owner), Version(Version), Sources(Sources), Recs(Recs), | |
b3d44315 MV |
1257 | StoreFilename(StoreFilename), Vf(Version.FileList()), |
1258 | Trusted(false) | |
03e39e59 | 1259 | { |
7d8afa39 | 1260 | Retries = _config->FindI("Acquire::Retries",0); |
813c8eea AL |
1261 | |
1262 | if (Version.Arch() == 0) | |
bdae53f1 | 1263 | { |
d1f1f6a8 | 1264 | _error->Error(_("I wasn't able to locate a file for the %s package. " |
7a3c2ab0 AL |
1265 | "This might mean you need to manually fix this package. " |
1266 | "(due to missing arch)"), | |
813c8eea | 1267 | Version.ParentPkg().Name()); |
bdae53f1 AL |
1268 | return; |
1269 | } | |
813c8eea | 1270 | |
b2e465d6 AL |
1271 | /* We need to find a filename to determine the extension. We make the |
1272 | assumption here that all the available sources for this version share | |
1273 | the same extension.. */ | |
1274 | // Skip not source sources, they do not have file fields. | |
1275 | for (; Vf.end() == false; Vf++) | |
1276 | { | |
1277 | if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0) | |
1278 | continue; | |
1279 | break; | |
1280 | } | |
1281 | ||
1282 | // Does not really matter here.. we are going to fail out below | |
1283 | if (Vf.end() != true) | |
1284 | { | |
1285 | // If this fails to get a file name we will bomb out below. | |
1286 | pkgRecords::Parser &Parse = Recs->Lookup(Vf); | |
1287 | if (_error->PendingError() == true) | |
1288 | return; | |
1289 | ||
1290 | // Generate the final file name as: package_version_arch.foo | |
1291 | StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' + | |
1292 | QuoteString(Version.VerStr(),"_:") + '_' + | |
1293 | QuoteString(Version.Arch(),"_:.") + | |
1294 | "." + flExtension(Parse.FileName()); | |
1295 | } | |
b3d44315 MV |
1296 | |
1297 | // check if we have one trusted source for the package. if so, switch | |
1298 | // to "TrustedOnly" mode | |
1299 | for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++) | |
1300 | { | |
1301 | pkgIndexFile *Index; | |
1302 | if (Sources->FindIndex(i.File(),Index) == false) | |
1303 | continue; | |
1304 | if (_config->FindB("Debug::pkgAcquire::Auth", false)) | |
1305 | { | |
1306 | std::cerr << "Checking index: " << Index->Describe() | |
1307 | << "(Trusted=" << Index->IsTrusted() << ")\n"; | |
1308 | } | |
1309 | if (Index->IsTrusted()) { | |
1310 | Trusted = true; | |
1311 | break; | |
1312 | } | |
1313 | } | |
1314 | ||
a3371852 MV |
1315 | // "allow-unauthenticated" restores apts old fetching behaviour |
1316 | // that means that e.g. unauthenticated file:// uris are higher | |
1317 | // priority than authenticated http:// uris | |
1318 | if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true) | |
1319 | Trusted = false; | |
1320 | ||
03e39e59 | 1321 | // Select a source |
b185acc2 | 1322 | if (QueueNext() == false && _error->PendingError() == false) |
b2e465d6 AL |
1323 | _error->Error(_("I wasn't able to locate file for the %s package. " |
1324 | "This might mean you need to manually fix this package."), | |
b185acc2 AL |
1325 | Version.ParentPkg().Name()); |
1326 | } | |
1327 | /*}}}*/ | |
1328 | // AcqArchive::QueueNext - Queue the next file source /*{{{*/ | |
1329 | // --------------------------------------------------------------------- | |
17caf1b1 AL |
1330 | /* This queues the next available file version for download. It checks if |
1331 | the archive is already available in the cache and stashs the MD5 for | |
1332 | checking later. */ | |
b185acc2 | 1333 | bool pkgAcqArchive::QueueNext() |
b2e465d6 | 1334 | { |
03e39e59 AL |
1335 | for (; Vf.end() == false; Vf++) |
1336 | { | |
1337 | // Ignore not source sources | |
1338 | if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0) | |
1339 | continue; | |
1340 | ||
1341 | // Try to cross match against the source list | |
b2e465d6 AL |
1342 | pkgIndexFile *Index; |
1343 | if (Sources->FindIndex(Vf.File(),Index) == false) | |
1344 | continue; | |
03e39e59 | 1345 | |
b3d44315 MV |
1346 | // only try to get a trusted package from another source if that source |
1347 | // is also trusted | |
1348 | if(Trusted && !Index->IsTrusted()) | |
1349 | continue; | |
1350 | ||
03e39e59 AL |
1351 | // Grab the text package record |
1352 | pkgRecords::Parser &Parse = Recs->Lookup(Vf); | |
1353 | if (_error->PendingError() == true) | |
b185acc2 | 1354 | return false; |
03e39e59 | 1355 | |
b2e465d6 | 1356 | string PkgFile = Parse.FileName(); |
495e5cb2 | 1357 | if(Parse.SHA256Hash() != "") |
8a8feb29 | 1358 | ExpectedHash = HashString("SHA256", Parse.SHA256Hash()); |
495e5cb2 | 1359 | else if (Parse.SHA1Hash() != "") |
8a8feb29 | 1360 | ExpectedHash = HashString("SHA1", Parse.SHA1Hash()); |
495e5cb2 | 1361 | else |
8a8feb29 | 1362 | ExpectedHash = HashString("MD5Sum", Parse.MD5Hash()); |
03e39e59 | 1363 | if (PkgFile.empty() == true) |
b2e465d6 AL |
1364 | return _error->Error(_("The package index files are corrupted. No Filename: " |
1365 | "field for package %s."), | |
1366 | Version.ParentPkg().Name()); | |
a6568219 | 1367 | |
b3d44315 MV |
1368 | Desc.URI = Index->ArchiveURI(PkgFile); |
1369 | Desc.Description = Index->ArchiveInfo(Version); | |
1370 | Desc.Owner = this; | |
1371 | Desc.ShortDesc = Version.ParentPkg().Name(); | |
1372 | ||
17caf1b1 | 1373 | // See if we already have the file. (Legacy filenames) |
a6568219 AL |
1374 | FileSize = Version->Size; |
1375 | string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile); | |
1376 | struct stat Buf; | |
1377 | if (stat(FinalFile.c_str(),&Buf) == 0) | |
1378 | { | |
1379 | // Make sure the size matches | |
1380 | if ((unsigned)Buf.st_size == Version->Size) | |
1381 | { | |
1382 | Complete = true; | |
1383 | Local = true; | |
1384 | Status = StatDone; | |
30e1eab5 | 1385 | StoreFilename = DestFile = FinalFile; |
b185acc2 | 1386 | return true; |
a6568219 AL |
1387 | } |
1388 | ||
6b1ff003 AL |
1389 | /* Hmm, we have a file and its size does not match, this means it is |
1390 | an old style mismatched arch */ | |
a6568219 AL |
1391 | unlink(FinalFile.c_str()); |
1392 | } | |
17caf1b1 AL |
1393 | |
1394 | // Check it again using the new style output filenames | |
1395 | FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename); | |
1396 | if (stat(FinalFile.c_str(),&Buf) == 0) | |
1397 | { | |
1398 | // Make sure the size matches | |
1399 | if ((unsigned)Buf.st_size == Version->Size) | |
1400 | { | |
1401 | Complete = true; | |
1402 | Local = true; | |
1403 | Status = StatDone; | |
1404 | StoreFilename = DestFile = FinalFile; | |
1405 | return true; | |
1406 | } | |
1407 | ||
1408 | /* Hmm, we have a file and its size does not match, this shouldnt | |
1409 | happen.. */ | |
1410 | unlink(FinalFile.c_str()); | |
1411 | } | |
1412 | ||
1413 | DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename); | |
6b1ff003 AL |
1414 | |
1415 | // Check the destination file | |
1416 | if (stat(DestFile.c_str(),&Buf) == 0) | |
1417 | { | |
1418 | // Hmm, the partial file is too big, erase it | |
1419 | if ((unsigned)Buf.st_size > Version->Size) | |
1420 | unlink(DestFile.c_str()); | |
1421 | else | |
1422 | PartialSize = Buf.st_size; | |
1423 | } | |
1424 | ||
03e39e59 | 1425 | // Create the item |
b2e465d6 AL |
1426 | Local = false; |
1427 | Desc.URI = Index->ArchiveURI(PkgFile); | |
1428 | Desc.Description = Index->ArchiveInfo(Version); | |
03e39e59 AL |
1429 | Desc.Owner = this; |
1430 | Desc.ShortDesc = Version.ParentPkg().Name(); | |
1431 | QueueURI(Desc); | |
b185acc2 AL |
1432 | |
1433 | Vf++; | |
1434 | return true; | |
03e39e59 | 1435 | } |
b185acc2 AL |
1436 | return false; |
1437 | } | |
03e39e59 AL |
1438 | /*}}}*/ |
1439 | // AcqArchive::Done - Finished fetching /*{{{*/ | |
1440 | // --------------------------------------------------------------------- | |
1441 | /* */ | |
495e5cb2 | 1442 | void pkgAcqArchive::Done(string Message,unsigned long Size,string CalcHash, |
459681d3 | 1443 | pkgAcquire::MethodConfig *Cfg) |
03e39e59 | 1444 | { |
495e5cb2 | 1445 | Item::Done(Message,Size,CalcHash,Cfg); |
03e39e59 AL |
1446 | |
1447 | // Check the size | |
1448 | if (Size != Version->Size) | |
1449 | { | |
bdae53f1 | 1450 | Status = StatError; |
b2e465d6 | 1451 | ErrorText = _("Size mismatch"); |
03e39e59 AL |
1452 | return; |
1453 | } | |
1454 | ||
495e5cb2 | 1455 | // Check the hash |
8a8feb29 | 1456 | if(ExpectedHash.toStr() != CalcHash) |
03e39e59 | 1457 | { |
495e5cb2 | 1458 | Status = StatError; |
9498d182 | 1459 | ErrorText = _("Hash Sum mismatch"); |
495e5cb2 MV |
1460 | if(FileExists(DestFile)) |
1461 | Rename(DestFile,DestFile + ".FAILED"); | |
1462 | return; | |
03e39e59 | 1463 | } |
a6568219 AL |
1464 | |
1465 | // Grab the output filename | |
03e39e59 AL |
1466 | string FileName = LookupTag(Message,"Filename"); |
1467 | if (FileName.empty() == true) | |
1468 | { | |
1469 | Status = StatError; | |
1470 | ErrorText = "Method gave a blank filename"; | |
1471 | return; | |
1472 | } | |
a6568219 AL |
1473 | |
1474 | Complete = true; | |
30e1eab5 AL |
1475 | |
1476 | // Reference filename | |
a6568219 AL |
1477 | if (FileName != DestFile) |
1478 | { | |
30e1eab5 | 1479 | StoreFilename = DestFile = FileName; |
a6568219 AL |
1480 | Local = true; |
1481 | return; | |
1482 | } | |
1483 | ||
1484 | // Done, move it into position | |
1485 | string FinalFile = _config->FindDir("Dir::Cache::Archives"); | |
17caf1b1 | 1486 | FinalFile += flNotDir(StoreFilename); |
a6568219 | 1487 | Rename(DestFile,FinalFile); |
03e39e59 | 1488 | |
30e1eab5 | 1489 | StoreFilename = DestFile = FinalFile; |
03e39e59 AL |
1490 | Complete = true; |
1491 | } | |
1492 | /*}}}*/ | |
db890fdb AL |
1493 | // AcqArchive::Failed - Failure handler /*{{{*/ |
1494 | // --------------------------------------------------------------------- | |
1495 | /* Here we try other sources */ | |
7d8afa39 | 1496 | void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf) |
db890fdb AL |
1497 | { |
1498 | ErrorText = LookupTag(Message,"Message"); | |
b2e465d6 AL |
1499 | |
1500 | /* We don't really want to retry on failed media swaps, this prevents | |
1501 | that. An interesting observation is that permanent failures are not | |
1502 | recorded. */ | |
1503 | if (Cnf->Removable == true && | |
1504 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == true) | |
1505 | { | |
1506 | // Vf = Version.FileList(); | |
1507 | while (Vf.end() == false) Vf++; | |
1508 | StoreFilename = string(); | |
1509 | Item::Failed(Message,Cnf); | |
1510 | return; | |
1511 | } | |
1512 | ||
db890fdb | 1513 | if (QueueNext() == false) |
7d8afa39 AL |
1514 | { |
1515 | // This is the retry counter | |
1516 | if (Retries != 0 && | |
1517 | Cnf->LocalOnly == false && | |
1518 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == true) | |
1519 | { | |
1520 | Retries--; | |
1521 | Vf = Version.FileList(); | |
1522 | if (QueueNext() == true) | |
1523 | return; | |
1524 | } | |
1525 | ||
9dbb421f | 1526 | StoreFilename = string(); |
7d8afa39 AL |
1527 | Item::Failed(Message,Cnf); |
1528 | } | |
db890fdb AL |
1529 | } |
1530 | /*}}}*/ | |
b3d44315 MV |
1531 | // AcqArchive::IsTrusted - Determine whether this archive comes from a |
1532 | // trusted source /*{{{*/ | |
1533 | // --------------------------------------------------------------------- | |
1534 | bool pkgAcqArchive::IsTrusted() | |
1535 | { | |
1536 | return Trusted; | |
1537 | } | |
1538 | ||
ab559b35 AL |
1539 | // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/ |
1540 | // --------------------------------------------------------------------- | |
1541 | /* */ | |
1542 | void pkgAcqArchive::Finished() | |
1543 | { | |
1544 | if (Status == pkgAcquire::Item::StatDone && | |
1545 | Complete == true) | |
1546 | return; | |
1547 | StoreFilename = string(); | |
1548 | } | |
1549 | /*}}}*/ | |
36375005 AL |
1550 | |
1551 | // AcqFile::pkgAcqFile - Constructor /*{{{*/ | |
1552 | // --------------------------------------------------------------------- | |
1553 | /* The file is added to the queue */ | |
8a8feb29 | 1554 | pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string Hash, |
46e00f9d MV |
1555 | unsigned long Size,string Dsc,string ShortDesc, |
1556 | const string &DestDir, const string &DestFilename) : | |
8a8feb29 | 1557 | Item(Owner), ExpectedHash(Hash) |
36375005 | 1558 | { |
08cfc005 AL |
1559 | Retries = _config->FindI("Acquire::Retries",0); |
1560 | ||
46e00f9d MV |
1561 | if(!DestFilename.empty()) |
1562 | DestFile = DestFilename; | |
1563 | else if(!DestDir.empty()) | |
1564 | DestFile = DestDir + "/" + flNotDir(URI); | |
1565 | else | |
1566 | DestFile = flNotDir(URI); | |
1567 | ||
36375005 AL |
1568 | // Create the item |
1569 | Desc.URI = URI; | |
1570 | Desc.Description = Dsc; | |
1571 | Desc.Owner = this; | |
1572 | ||
1573 | // Set the short description to the archive component | |
1574 | Desc.ShortDesc = ShortDesc; | |
1575 | ||
1576 | // Get the transfer sizes | |
1577 | FileSize = Size; | |
1578 | struct stat Buf; | |
1579 | if (stat(DestFile.c_str(),&Buf) == 0) | |
1580 | { | |
1581 | // Hmm, the partial file is too big, erase it | |
1582 | if ((unsigned)Buf.st_size > Size) | |
1583 | unlink(DestFile.c_str()); | |
1584 | else | |
1585 | PartialSize = Buf.st_size; | |
1586 | } | |
092ae175 | 1587 | |
36375005 AL |
1588 | QueueURI(Desc); |
1589 | } | |
1590 | /*}}}*/ | |
1591 | // AcqFile::Done - Item downloaded OK /*{{{*/ | |
1592 | // --------------------------------------------------------------------- | |
1593 | /* */ | |
495e5cb2 | 1594 | void pkgAcqFile::Done(string Message,unsigned long Size,string CalcHash, |
459681d3 | 1595 | pkgAcquire::MethodConfig *Cnf) |
36375005 | 1596 | { |
495e5cb2 MV |
1597 | Item::Done(Message,Size,CalcHash,Cnf); |
1598 | ||
8a8feb29 | 1599 | // Check the hash |
2c941d89 | 1600 | if(!ExpectedHash.empty() && ExpectedHash.toStr() != CalcHash) |
b3c39978 | 1601 | { |
495e5cb2 | 1602 | Status = StatError; |
9498d182 | 1603 | ErrorText = "Hash Sum mismatch"; |
495e5cb2 MV |
1604 | Rename(DestFile,DestFile + ".FAILED"); |
1605 | return; | |
b3c39978 AL |
1606 | } |
1607 | ||
36375005 AL |
1608 | string FileName = LookupTag(Message,"Filename"); |
1609 | if (FileName.empty() == true) | |
1610 | { | |
1611 | Status = StatError; | |
1612 | ErrorText = "Method gave a blank filename"; | |
1613 | return; | |
1614 | } | |
1615 | ||
1616 | Complete = true; | |
1617 | ||
1618 | // The files timestamp matches | |
1619 | if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true) | |
1620 | return; | |
1621 | ||
1622 | // We have to copy it into place | |
1623 | if (FileName != DestFile) | |
1624 | { | |
1625 | Local = true; | |
459681d3 AL |
1626 | if (_config->FindB("Acquire::Source-Symlinks",true) == false || |
1627 | Cnf->Removable == true) | |
917ae805 AL |
1628 | { |
1629 | Desc.URI = "copy:" + FileName; | |
1630 | QueueURI(Desc); | |
1631 | return; | |
1632 | } | |
1633 | ||
83ab33fc AL |
1634 | // Erase the file if it is a symlink so we can overwrite it |
1635 | struct stat St; | |
1636 | if (lstat(DestFile.c_str(),&St) == 0) | |
1637 | { | |
1638 | if (S_ISLNK(St.st_mode) != 0) | |
1639 | unlink(DestFile.c_str()); | |
1640 | } | |
1641 | ||
1642 | // Symlink the file | |
917ae805 AL |
1643 | if (symlink(FileName.c_str(),DestFile.c_str()) != 0) |
1644 | { | |
83ab33fc | 1645 | ErrorText = "Link to " + DestFile + " failure "; |
917ae805 AL |
1646 | Status = StatError; |
1647 | Complete = false; | |
1648 | } | |
36375005 AL |
1649 | } |
1650 | } | |
1651 | /*}}}*/ | |
08cfc005 AL |
1652 | // AcqFile::Failed - Failure handler /*{{{*/ |
1653 | // --------------------------------------------------------------------- | |
1654 | /* Here we try other sources */ | |
1655 | void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf) | |
1656 | { | |
1657 | ErrorText = LookupTag(Message,"Message"); | |
1658 | ||
1659 | // This is the retry counter | |
1660 | if (Retries != 0 && | |
1661 | Cnf->LocalOnly == false && | |
1662 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == true) | |
1663 | { | |
1664 | Retries--; | |
1665 | QueueURI(Desc); | |
1666 | return; | |
1667 | } | |
1668 | ||
1669 | Item::Failed(Message,Cnf); | |
1670 | } | |
1671 | /*}}}*/ |