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