]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-item.cc
Fixed weird to-configure inconsitency and added apt-cac...
[apt.git] / apt-pkg / acquire-item.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire-item.cc,v 1.24 1999/02/19 07:56:06 jgg Exp $
4 /* ######################################################################
5
6 Acquire Item - Item to acquire
7
8 Each item can download to exactly one file at a time. This means you
9 cannot create an item that fetches two uri's to two files at the same
10 time. The pkgAcqIndex class creates a second class upon instantiation
11 to fetch the other index files because of this.
12
13 ##################################################################### */
14 /*}}}*/
15 // Include Files /*{{{*/
16 #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>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/strutl.h>
23
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdio.h>
29 /*}}}*/
30
31 // Acquire::Item::Item - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
33 /* */
34 pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
35 Mode(0), ID(0), Complete(false), Local(false),
36 QueueCounter(0)
37 {
38 Owner->Add(this);
39 Status = StatIdle;
40 }
41 /*}}}*/
42 // Acquire::Item::~Item - Destructor /*{{{*/
43 // ---------------------------------------------------------------------
44 /* */
45 pkgAcquire::Item::~Item()
46 {
47 Owner->Remove(this);
48 }
49 /*}}}*/
50 // Acquire::Item::Failed - Item failed to download /*{{{*/
51 // ---------------------------------------------------------------------
52 /* We return to an idle state if there are still other queues that could
53 fetch this object */
54 void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
55 {
56 Status = StatIdle;
57 ErrorText = LookupTag(Message,"Message");
58 if (QueueCounter <= 1)
59 {
60 /* This indicates that the file is not available right now but might
61 be sometime later. If we do a retry cycle then this should be
62 retried [CDROMs] */
63 if (Cnf->LocalOnly == true &&
64 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
65 {
66 Status = StatIdle;
67 Dequeue();
68 return;
69 }
70
71 Status = StatError;
72 Dequeue();
73 }
74 }
75 /*}}}*/
76 // Acquire::Item::Start - Item has begun to download /*{{{*/
77 // ---------------------------------------------------------------------
78 /* Stash status and the file size. Note that setting Complete means
79 sub-phases of the acquire process such as decompresion are operating */
80 void pkgAcquire::Item::Start(string Message,unsigned long Size)
81 {
82 Status = StatFetching;
83 if (FileSize == 0 && Complete == false)
84 FileSize = Size;
85 }
86 /*}}}*/
87 // Acquire::Item::Done - Item downloaded OK /*{{{*/
88 // ---------------------------------------------------------------------
89 /* */
90 void pkgAcquire::Item::Done(string Message,unsigned long Size,string)
91 {
92 // We just downloaded something..
93 string FileName = LookupTag(Message,"Filename");
94 if (Complete == false && FileName == DestFile)
95 {
96 if (Owner->Log != 0)
97 Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
98 }
99
100 Status = StatDone;
101 ErrorText = string();
102 Owner->Dequeue(this);
103 }
104 /*}}}*/
105 // Acquire::Item::Rename - Rename a file /*{{{*/
106 // ---------------------------------------------------------------------
107 /* This helper function is used by alot of item methods as thier final
108 step */
109 void pkgAcquire::Item::Rename(string From,string To)
110 {
111 if (rename(From.c_str(),To.c_str()) != 0)
112 {
113 char S[300];
114 sprintf(S,"rename failed, %s (%s -> %s).",strerror(errno),
115 From.c_str(),To.c_str());
116 Status = StatError;
117 ErrorText = S;
118 }
119 }
120 /*}}}*/
121
122 // AcqIndex::AcqIndex - Constructor /*{{{*/
123 // ---------------------------------------------------------------------
124 /* The package file is added to the queue and a second class is
125 instantiated to fetch the revision file */
126 pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,const pkgSourceList::Item *Location) :
127 Item(Owner), Location(Location)
128 {
129 Decompression = false;
130 Erase = false;
131
132 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
133 DestFile += URItoFileName(Location->PackagesURI());
134
135 // Create the item
136 Desc.URI = Location->PackagesURI() + ".gz";
137 Desc.Description = Location->PackagesInfo();
138 Desc.Owner = this;
139
140 // Set the short description to the archive component
141 if (Location->Dist[Location->Dist.size() - 1] == '/')
142 Desc.ShortDesc = Location->Dist;
143 else
144 Desc.ShortDesc = Location->Dist + '/' + Location->Section;
145
146 QueueURI(Desc);
147
148 // Create the Release fetch class
149 new pkgAcqIndexRel(Owner,Location);
150 }
151 /*}}}*/
152 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
153 // ---------------------------------------------------------------------
154 /* The only header we use is the last-modified header. */
155 string pkgAcqIndex::Custom600Headers()
156 {
157 string Final = _config->FindDir("Dir::State::lists");
158 Final += URItoFileName(Location->PackagesURI());
159
160 struct stat Buf;
161 if (stat(Final.c_str(),&Buf) != 0)
162 return "\nIndex-File: true";
163
164 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
165 }
166 /*}}}*/
167 // AcqIndex::Done - Finished a fetch /*{{{*/
168 // ---------------------------------------------------------------------
169 /* This goes through a number of states.. On the initial fetch the
170 method could possibly return an alternate filename which points
171 to the uncompressed version of the file. If this is so the file
172 is copied into the partial directory. In all other cases the file
173 is decompressed with a gzip uri. */
174 void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5)
175 {
176 Item::Done(Message,Size,MD5);
177
178 if (Decompression == true)
179 {
180 // Done, move it into position
181 string FinalFile = _config->FindDir("Dir::State::lists");
182 FinalFile += URItoFileName(Location->PackagesURI());
183 Rename(DestFile,FinalFile);
184
185 /* We restore the original name to DestFile so that the clean operation
186 will work OK */
187 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
188 DestFile += URItoFileName(Location->PackagesURI());
189
190 // Remove the compressed version.
191 if (Erase == true)
192 unlink(DestFile.c_str());
193 return;
194 }
195
196 Erase = false;
197 Complete = true;
198
199 // Handle the unzipd case
200 string FileName = LookupTag(Message,"Alt-Filename");
201 if (FileName.empty() == false)
202 {
203 // The files timestamp matches
204 if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
205 return;
206
207 Decompression = true;
208 Local = true;
209 DestFile += ".decomp";
210 Desc.URI = "copy:" + FileName;
211 QueueURI(Desc);
212 Mode = "copy";
213 return;
214 }
215
216 FileName = LookupTag(Message,"Filename");
217 if (FileName.empty() == true)
218 {
219 Status = StatError;
220 ErrorText = "Method gave a blank filename";
221 }
222
223 // The files timestamp matches
224 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
225 return;
226
227 if (FileName == DestFile)
228 Erase = true;
229 else
230 Local = true;
231
232 Decompression = true;
233 DestFile += ".decomp";
234 Desc.URI = "gzip:" + FileName,Location->PackagesInfo();
235 QueueURI(Desc);
236 Mode = "gzip";
237 }
238 /*}}}*/
239 // AcqIndex::Describe - Describe the Item /*{{{*/
240 // ---------------------------------------------------------------------
241 /* */
242 string pkgAcqIndex::Describe()
243 {
244 return Location->PackagesURI();
245 }
246 /*}}}*/
247
248 // AcqIndexRel::pkgAcqIndexRel - Constructor /*{{{*/
249 // ---------------------------------------------------------------------
250 /* The Release file is added to the queue */
251 pkgAcqIndexRel::pkgAcqIndexRel(pkgAcquire *Owner,
252 const pkgSourceList::Item *Location) :
253 Item(Owner), Location(Location)
254 {
255 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
256 DestFile += URItoFileName(Location->ReleaseURI());
257
258 // Create the item
259 Desc.URI = Location->ReleaseURI();
260 Desc.Description = Location->ReleaseInfo();
261 Desc.Owner = this;
262
263 // Set the short description to the archive component
264 if (Location->Dist[Location->Dist.size() - 1] == '/')
265 Desc.ShortDesc = Location->Dist;
266 else
267 Desc.ShortDesc = Location->Dist + '/' + Location->Section;
268
269 QueueURI(Desc);
270 }
271 /*}}}*/
272 // AcqIndexRel::Custom600Headers - Insert custom request headers /*{{{*/
273 // ---------------------------------------------------------------------
274 /* The only header we use is the last-modified header. */
275 string pkgAcqIndexRel::Custom600Headers()
276 {
277 string Final = _config->FindDir("Dir::State::lists");
278 Final += URItoFileName(Location->ReleaseURI());
279
280 struct stat Buf;
281 if (stat(Final.c_str(),&Buf) != 0)
282 return "\nIndex-File: true";
283
284 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
285 }
286 /*}}}*/
287 // AcqIndexRel::Done - Item downloaded OK /*{{{*/
288 // ---------------------------------------------------------------------
289 /* The release file was not placed into the download directory then
290 a copy URI is generated and it is copied there otherwise the file
291 in the partial directory is moved into .. and the URI is finished. */
292 void pkgAcqIndexRel::Done(string Message,unsigned long Size,string MD5)
293 {
294 Item::Done(Message,Size,MD5);
295
296 string FileName = LookupTag(Message,"Filename");
297 if (FileName.empty() == true)
298 {
299 Status = StatError;
300 ErrorText = "Method gave a blank filename";
301 return;
302 }
303
304 Complete = true;
305
306 // The files timestamp matches
307 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
308 return;
309
310 // We have to copy it into place
311 if (FileName != DestFile)
312 {
313 Local = true;
314 Desc.URI = "copy:" + FileName;
315 QueueURI(Desc);
316 return;
317 }
318
319 // Done, move it into position
320 string FinalFile = _config->FindDir("Dir::State::lists");
321 FinalFile += URItoFileName(Location->ReleaseURI());
322 Rename(DestFile,FinalFile);
323 }
324 /*}}}*/
325 // AcqIndexRel::Describe - Describe the Item /*{{{*/
326 // ---------------------------------------------------------------------
327 /* */
328 string pkgAcqIndexRel::Describe()
329 {
330 return Location->ReleaseURI();
331 }
332 /*}}}*/
333 // AcqIndexRel::Failed - Silence failure messages for missing rel files /*{{{*/
334 // ---------------------------------------------------------------------
335 /* */
336 void pkgAcqIndexRel::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
337 {
338 // This is the retry counter
339 if (Cnf->LocalOnly == true ||
340 StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
341 {
342 Status = StatIdle;
343 Dequeue();
344 return;
345 }
346
347 Item::Failed(Message,Cnf);
348 }
349 /*}}}*/
350
351 // AcqArchive::AcqArchive - Constructor /*{{{*/
352 // ---------------------------------------------------------------------
353 /* This just sets up the initial fetch environment and queues the first
354 possibilitiy */
355 pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
356 pkgRecords *Recs,pkgCache::VerIterator const &Version,
357 string &StoreFilename) :
358 Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
359 StoreFilename(StoreFilename), Vf(Version.FileList())
360 {
361 Retries = _config->FindI("Acquire::Retries",0);
362
363 // Generate the final file name as: package_version_arch.deb
364 StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
365 QuoteString(Version.VerStr(),"_:") + '_' +
366 QuoteString(Version.Arch(),"_:.") + ".deb";
367
368 // Select a source
369 if (QueueNext() == false && _error->PendingError() == false)
370 _error->Error("I wasn't able to locate file for the %s package. "
371 "This might mean you need to manually fix this package.",
372 Version.ParentPkg().Name());
373 }
374 /*}}}*/
375 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
376 // ---------------------------------------------------------------------
377 /* This queues the next available file version for download. It checks if
378 the archive is already available in the cache and stashs the MD5 for
379 checking later. */
380 bool pkgAcqArchive::QueueNext()
381 {
382 for (; Vf.end() == false; Vf++)
383 {
384 // Ignore not source sources
385 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
386 continue;
387
388 // Try to cross match against the source list
389 string PkgFile = flNotDir(Vf.File().FileName());
390 pkgSourceList::const_iterator Location;
391 for (Location = Sources->begin(); Location != Sources->end(); Location++)
392 if (PkgFile == URItoFileName(Location->PackagesURI()))
393 break;
394
395 if (Location == Sources->end())
396 continue;
397
398 // Grab the text package record
399 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
400 if (_error->PendingError() == true)
401 return false;
402
403 PkgFile = Parse.FileName();
404 MD5 = Parse.MD5Hash();
405 if (PkgFile.empty() == true)
406 return _error->Error("The package index files are corrupted. No Filename: "
407 "field for package %s."
408 ,Version.ParentPkg().Name());
409
410 // See if we already have the file. (Legacy filenames)
411 FileSize = Version->Size;
412 string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
413 struct stat Buf;
414 if (stat(FinalFile.c_str(),&Buf) == 0)
415 {
416 // Make sure the size matches
417 if ((unsigned)Buf.st_size == Version->Size)
418 {
419 Complete = true;
420 Local = true;
421 Status = StatDone;
422 StoreFilename = DestFile = FinalFile;
423 return true;
424 }
425
426 /* Hmm, we have a file and its size does not match, this shouldnt
427 happen.. */
428 unlink(FinalFile.c_str());
429 }
430
431 // Check it again using the new style output filenames
432 FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
433 if (stat(FinalFile.c_str(),&Buf) == 0)
434 {
435 // Make sure the size matches
436 if ((unsigned)Buf.st_size == Version->Size)
437 {
438 Complete = true;
439 Local = true;
440 Status = StatDone;
441 StoreFilename = DestFile = FinalFile;
442 return true;
443 }
444
445 /* Hmm, we have a file and its size does not match, this shouldnt
446 happen.. */
447 unlink(FinalFile.c_str());
448 }
449
450 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
451
452 // Create the item
453 Desc.URI = Location->ArchiveURI(PkgFile);
454 Desc.Description = Location->ArchiveInfo(Version);
455 Desc.Owner = this;
456 Desc.ShortDesc = Version.ParentPkg().Name();
457 QueueURI(Desc);
458
459 Vf++;
460 return true;
461 }
462 return false;
463 }
464 /*}}}*/
465 // AcqArchive::Done - Finished fetching /*{{{*/
466 // ---------------------------------------------------------------------
467 /* */
468 void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash)
469 {
470 Item::Done(Message,Size,Md5Hash);
471
472 // Check the size
473 if (Size != Version->Size)
474 {
475 _error->Error("Size mismatch for package %s",Version.ParentPkg().Name());
476 return;
477 }
478
479 // Check the md5
480 if (Md5Hash.empty() == false && MD5.empty() == false)
481 {
482 if (Md5Hash != MD5)
483 {
484 _error->Error("MD5Sum mismatch for package %s",Version.ParentPkg().Name());
485 return;
486 }
487 }
488
489 // Grab the output filename
490 string FileName = LookupTag(Message,"Filename");
491 if (FileName.empty() == true)
492 {
493 Status = StatError;
494 ErrorText = "Method gave a blank filename";
495 return;
496 }
497
498 Complete = true;
499
500 // Reference filename
501 if (FileName != DestFile)
502 {
503 StoreFilename = DestFile = FileName;
504 Local = true;
505 return;
506 }
507
508 // Done, move it into position
509 string FinalFile = _config->FindDir("Dir::Cache::Archives");
510 FinalFile += flNotDir(StoreFilename);
511 Rename(DestFile,FinalFile);
512
513 StoreFilename = DestFile = FinalFile;
514 Complete = true;
515 }
516 /*}}}*/
517 // AcqArchive::Describe - Describe the Item /*{{{*/
518 // ---------------------------------------------------------------------
519 /* */
520 string pkgAcqArchive::Describe()
521 {
522 return Desc.URI;
523 }
524 /*}}}*/
525 // AcqArchive::Failed - Failure handler /*{{{*/
526 // ---------------------------------------------------------------------
527 /* Here we try other sources */
528 void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
529 {
530 ErrorText = LookupTag(Message,"Message");
531 if (QueueNext() == false)
532 {
533 // This is the retry counter
534 if (Retries != 0 &&
535 Cnf->LocalOnly == false &&
536 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
537 {
538 Retries--;
539 Vf = Version.FileList();
540 if (QueueNext() == true)
541 return;
542 }
543
544 StoreFilename = string();
545 Item::Failed(Message,Cnf);
546 }
547 }
548 /*}}}*/