Switch FileFd to not transparently gunzip, since that breaks code which expects the...
[ntk/apt.git] / apt-pkg / deb / debindexfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debindexfile.cc,v 1.5.2.3 2004/01/04 19:11:00 mdz Exp $
4 /* ######################################################################
5
6 Debian Specific sources.list types and the three sorts of Debian
7 index files.
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <apt-pkg/debindexfile.h>
13 #include <apt-pkg/debsrcrecords.h>
14 #include <apt-pkg/deblistparser.h>
15 #include <apt-pkg/debrecords.h>
16 #include <apt-pkg/sourcelist.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/progress.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/strutl.h>
21 #include <apt-pkg/acquire-item.h>
22 #include <apt-pkg/debmetaindex.h>
23
24 #include <sys/stat.h>
25 /*}}}*/
26
27 // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
29 /* */
30 debSourcesIndex::debSourcesIndex(string URI,string Dist,string Section,bool Trusted) :
31 pkgIndexFile(Trusted), URI(URI), Dist(Dist), Section(Section)
32 {
33 }
34 /*}}}*/
35 // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
36 // ---------------------------------------------------------------------
37 /* The result looks like:
38 http://foo/debian/ stable/main src 1.1.1 (dsc) */
39 string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
40 pkgSrcRecords::File const &File) const
41 {
42 string Res;
43 Res = ::URI::NoUserPassword(URI) + ' ';
44 if (Dist[Dist.size() - 1] == '/')
45 {
46 if (Dist != "/")
47 Res += Dist;
48 }
49 else
50 Res += Dist + '/' + Section;
51
52 Res += " ";
53 Res += Record.Package();
54 Res += " ";
55 Res += Record.Version();
56 if (File.Type.empty() == false)
57 Res += " (" + File.Type + ")";
58 return Res;
59 }
60 /*}}}*/
61 // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
62 // ---------------------------------------------------------------------
63 /* */
64 pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const
65 {
66 string SourcesURI = _config->FindDir("Dir::State::lists") +
67 URItoFileName(IndexURI("Sources"));
68 string SourcesURIgzip = SourcesURI + ".gz";
69 if (!FileExists(SourcesURI) && FileExists(SourcesURIgzip))
70 SourcesURI = SourcesURIgzip;
71
72 return new debSrcRecordParser(SourcesURI,this);
73 }
74 /*}}}*/
75 // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
76 // ---------------------------------------------------------------------
77 /* */
78 string debSourcesIndex::Describe(bool Short) const
79 {
80 char S[300];
81 if (Short == true)
82 snprintf(S,sizeof(S),"%s",Info("Sources").c_str());
83 else
84 snprintf(S,sizeof(S),"%s (%s)",Info("Sources").c_str(),
85 IndexFile("Sources").c_str());
86
87 return S;
88 }
89 /*}}}*/
90 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
91 // ---------------------------------------------------------------------
92 /* */
93 string debSourcesIndex::Info(const char *Type) const
94 {
95 string Info = ::URI::NoUserPassword(URI) + ' ';
96 if (Dist[Dist.size() - 1] == '/')
97 {
98 if (Dist != "/")
99 Info += Dist;
100 }
101 else
102 Info += Dist + '/' + Section;
103 Info += " ";
104 Info += Type;
105 return Info;
106 }
107 /*}}}*/
108 // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
109 // ---------------------------------------------------------------------
110 /* */
111 inline string debSourcesIndex::IndexFile(const char *Type) const
112 {
113 string s = URItoFileName(IndexURI(Type));
114 string sgzip = s + ".gz";
115 if (!FileExists(s) && FileExists(sgzip))
116 return sgzip;
117 else
118 return s;
119 }
120
121 string debSourcesIndex::IndexURI(const char *Type) const
122 {
123 string Res;
124 if (Dist[Dist.size() - 1] == '/')
125 {
126 if (Dist != "/")
127 Res = URI + Dist;
128 else
129 Res = URI;
130 }
131 else
132 Res = URI + "dists/" + Dist + '/' + Section +
133 "/source/";
134
135 Res += Type;
136 return Res;
137 }
138 /*}}}*/
139 // SourcesIndex::Exists - Check if the index is available /*{{{*/
140 // ---------------------------------------------------------------------
141 /* */
142 bool debSourcesIndex::Exists() const
143 {
144 return FileExists(IndexFile("Sources"));
145 }
146 /*}}}*/
147 // SourcesIndex::Size - Return the size of the index /*{{{*/
148 // ---------------------------------------------------------------------
149 /* */
150 unsigned long debSourcesIndex::Size() const
151 {
152 struct stat S;
153 if (stat(IndexFile("Sources").c_str(),&S) != 0)
154 return 0;
155 return S.st_size;
156 }
157 /*}}}*/
158
159 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
160 // ---------------------------------------------------------------------
161 /* */
162 debPackagesIndex::debPackagesIndex(string URI,string Dist,string Section,bool Trusted) :
163 pkgIndexFile(Trusted), URI(URI), Dist(Dist), Section(Section)
164 {
165 }
166 /*}}}*/
167 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
168 // ---------------------------------------------------------------------
169 /* This is a shorter version that is designed to be < 60 chars or so */
170 string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver) const
171 {
172 string Res = ::URI::NoUserPassword(URI) + ' ';
173 if (Dist[Dist.size() - 1] == '/')
174 {
175 if (Dist != "/")
176 Res += Dist;
177 }
178 else
179 Res += Dist + '/' + Section;
180
181 Res += " ";
182 Res += Ver.ParentPkg().Name();
183 Res += " ";
184 Res += Ver.VerStr();
185 return Res;
186 }
187 /*}}}*/
188 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This should help the user find the index in the sources.list and
191 in the filesystem for problem solving */
192 string debPackagesIndex::Describe(bool Short) const
193 {
194 char S[300];
195 if (Short == true)
196 snprintf(S,sizeof(S),"%s",Info("Packages").c_str());
197 else
198 snprintf(S,sizeof(S),"%s (%s)",Info("Packages").c_str(),
199 IndexFile("Packages").c_str());
200 return S;
201 }
202 /*}}}*/
203 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
204 // ---------------------------------------------------------------------
205 /* */
206 string debPackagesIndex::Info(const char *Type) const
207 {
208 string Info = ::URI::NoUserPassword(URI) + ' ';
209 if (Dist[Dist.size() - 1] == '/')
210 {
211 if (Dist != "/")
212 Info += Dist;
213 }
214 else
215 Info += Dist + '/' + Section;
216 Info += " ";
217 Info += Type;
218 return Info;
219 }
220 /*}}}*/
221 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
222 // ---------------------------------------------------------------------
223 /* */
224 inline string debPackagesIndex::IndexFile(const char *Type) const
225 {
226 string s =_config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
227 string sgzip = s + ".gz";
228 if (!FileExists(s) && FileExists(sgzip))
229 return sgzip;
230 else
231 return s;
232 }
233 string debPackagesIndex::IndexURI(const char *Type) const
234 {
235 string Res;
236 if (Dist[Dist.size() - 1] == '/')
237 {
238 if (Dist != "/")
239 Res = URI + Dist;
240 else
241 Res = URI;
242 }
243 else
244 Res = URI + "dists/" + Dist + '/' + Section +
245 "/binary-" + _config->Find("APT::Architecture") + '/';
246
247 Res += Type;
248 return Res;
249 }
250 /*}}}*/
251 // PackagesIndex::Exists - Check if the index is available /*{{{*/
252 // ---------------------------------------------------------------------
253 /* */
254 bool debPackagesIndex::Exists() const
255 {
256 return FileExists(IndexFile("Packages"));
257 }
258 /*}}}*/
259 // PackagesIndex::Size - Return the size of the index /*{{{*/
260 // ---------------------------------------------------------------------
261 /* This is really only used for progress reporting. */
262 unsigned long debPackagesIndex::Size() const
263 {
264 struct stat S;
265 if (stat(IndexFile("Packages").c_str(),&S) != 0)
266 return 0;
267 return S.st_size;
268 }
269 /*}}}*/
270 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
271 // ---------------------------------------------------------------------
272 /* */
273 bool debPackagesIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
274 {
275 string PackageFile = IndexFile("Packages");
276 FileFd Pkg(PackageFile,FileFd::ReadOnlyGzip);
277 debListParser Parser(&Pkg);
278 if (_error->PendingError() == true)
279 return _error->Error("Problem opening %s",PackageFile.c_str());
280
281 Prog.SubProgress(0,Info("Packages"));
282 ::URI Tmp(URI);
283 if (Gen.SelectFile(PackageFile,Tmp.Host,*this) == false)
284 return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
285
286 // Store the IMS information
287 pkgCache::PkgFileIterator File = Gen.GetCurFile();
288 struct stat St;
289 if (fstat(Pkg.Fd(),&St) != 0)
290 return _error->Errno("fstat","Failed to stat");
291 File->Size = St.st_size;
292 File->mtime = St.st_mtime;
293
294 if (Gen.MergeList(Parser) == false)
295 return _error->Error("Problem with MergeList %s",PackageFile.c_str());
296
297 // Check the release file
298 string ReleaseFile = debReleaseIndex(URI,Dist).MetaIndexFile("Release");
299 if (FileExists(ReleaseFile) == true)
300 {
301 FileFd Rel(ReleaseFile,FileFd::ReadOnly);
302 if (_error->PendingError() == true)
303 return false;
304 Parser.LoadReleaseInfo(File,Rel,Section);
305 }
306
307 return true;
308 }
309 /*}}}*/
310 // PackagesIndex::FindInCache - Find this index /*{{{*/
311 // ---------------------------------------------------------------------
312 /* */
313 pkgCache::PkgFileIterator debPackagesIndex::FindInCache(pkgCache &Cache) const
314 {
315 string FileName = IndexFile("Packages");
316 pkgCache::PkgFileIterator File = Cache.FileBegin();
317 for (; File.end() == false; File++)
318 {
319 if (File.FileName() == NULL || FileName != File.FileName())
320 continue;
321
322 struct stat St;
323 if (stat(File.FileName(),&St) != 0)
324 {
325 if (_config->FindB("Debug::pkgCacheGen", false))
326 std::clog << "PackagesIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
327 return pkgCache::PkgFileIterator(Cache);
328 }
329 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
330 {
331 if (_config->FindB("Debug::pkgCacheGen", false))
332 std::clog << "PackagesIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
333 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
334 << ") doesn't match for " << File.FileName() << std::endl;
335 return pkgCache::PkgFileIterator(Cache);
336 }
337 return File;
338 }
339
340 return File;
341 }
342 /*}}}*/
343
344 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
345 // ---------------------------------------------------------------------
346 /* */
347 debTranslationsIndex::debTranslationsIndex(string URI,string Dist,string Section,
348 char const * const Translation) :
349 pkgIndexFile(true), URI(URI), Dist(Dist), Section(Section),
350 Language(Translation)
351 {}
352 /*}}}*/
353 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
354 // ---------------------------------------------------------------------
355 /* */
356 inline string debTranslationsIndex::IndexFile(const char *Type) const
357 {
358 string s =_config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
359 string sgzip = s + ".gz";
360 if (!FileExists(s) && FileExists(sgzip))
361 return sgzip;
362 else
363 return s;
364 }
365 string debTranslationsIndex::IndexURI(const char *Type) const
366 {
367 string Res;
368 if (Dist[Dist.size() - 1] == '/')
369 {
370 if (Dist != "/")
371 Res = URI + Dist;
372 else
373 Res = URI;
374 }
375 else
376 Res = URI + "dists/" + Dist + '/' + Section +
377 "/i18n/Translation-";
378
379 Res += Type;
380 return Res;
381 }
382 /*}}}*/
383 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
384 // ---------------------------------------------------------------------
385 /* */
386 bool debTranslationsIndex::GetIndexes(pkgAcquire *Owner) const
387 {
388 if (TranslationsAvailable()) {
389 string const TranslationFile = string("Translation-").append(Language);
390 new pkgAcqIndexTrans(Owner, IndexURI(Language),
391 Info(TranslationFile.c_str()),
392 TranslationFile);
393 }
394
395 return true;
396 }
397 /*}}}*/
398 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
399 // ---------------------------------------------------------------------
400 /* This should help the user find the index in the sources.list and
401 in the filesystem for problem solving */
402 string debTranslationsIndex::Describe(bool Short) const
403 {
404 char S[300];
405 if (Short == true)
406 snprintf(S,sizeof(S),"%s",Info(TranslationFile().c_str()).c_str());
407 else
408 snprintf(S,sizeof(S),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
409 IndexFile(Language).c_str());
410 return S;
411 }
412 /*}}}*/
413 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
414 // ---------------------------------------------------------------------
415 /* */
416 string debTranslationsIndex::Info(const char *Type) const
417 {
418 string Info = ::URI::NoUserPassword(URI) + ' ';
419 if (Dist[Dist.size() - 1] == '/')
420 {
421 if (Dist != "/")
422 Info += Dist;
423 }
424 else
425 Info += Dist + '/' + Section;
426 Info += " ";
427 Info += Type;
428 return Info;
429 }
430 /*}}}*/
431 bool debTranslationsIndex::HasPackages() const /*{{{*/
432 {
433 if(!TranslationsAvailable())
434 return false;
435
436 return FileExists(IndexFile(Language));
437 }
438 /*}}}*/
439 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
440 // ---------------------------------------------------------------------
441 /* */
442 bool debTranslationsIndex::Exists() const
443 {
444 return FileExists(IndexFile(Language));
445 }
446 /*}}}*/
447 // TranslationsIndex::Size - Return the size of the index /*{{{*/
448 // ---------------------------------------------------------------------
449 /* This is really only used for progress reporting. */
450 unsigned long debTranslationsIndex::Size() const
451 {
452 struct stat S;
453 if (stat(IndexFile(Language).c_str(),&S) != 0)
454 return 0;
455 return S.st_size;
456 }
457 /*}}}*/
458 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
459 // ---------------------------------------------------------------------
460 /* */
461 bool debTranslationsIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
462 {
463 // Check the translation file, if in use
464 string TranslationFile = IndexFile(Language);
465 if (TranslationsAvailable() && FileExists(TranslationFile))
466 {
467 FileFd Trans(TranslationFile,FileFd::ReadOnlyGzip);
468 debListParser TransParser(&Trans);
469 if (_error->PendingError() == true)
470 return false;
471
472 Prog.SubProgress(0, Info(TranslationFile.c_str()));
473 if (Gen.SelectFile(TranslationFile,string(),*this) == false)
474 return _error->Error("Problem with SelectFile %s",TranslationFile.c_str());
475
476 // Store the IMS information
477 pkgCache::PkgFileIterator TransFile = Gen.GetCurFile();
478 struct stat TransSt;
479 if (fstat(Trans.Fd(),&TransSt) != 0)
480 return _error->Errno("fstat","Failed to stat");
481 TransFile->Size = TransSt.st_size;
482 TransFile->mtime = TransSt.st_mtime;
483
484 if (Gen.MergeList(TransParser) == false)
485 return _error->Error("Problem with MergeList %s",TranslationFile.c_str());
486 }
487
488 return true;
489 }
490 /*}}}*/
491 // TranslationsIndex::FindInCache - Find this index /*{{{*/
492 // ---------------------------------------------------------------------
493 /* */
494 pkgCache::PkgFileIterator debTranslationsIndex::FindInCache(pkgCache &Cache) const
495 {
496 string FileName = IndexFile(Language);
497
498 pkgCache::PkgFileIterator File = Cache.FileBegin();
499 for (; File.end() == false; File++)
500 {
501 if (FileName != File.FileName())
502 continue;
503
504 struct stat St;
505 if (stat(File.FileName(),&St) != 0)
506 {
507 if (_config->FindB("Debug::pkgCacheGen", false))
508 std::clog << "TranslationIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
509 return pkgCache::PkgFileIterator(Cache);
510 }
511 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
512 {
513 if (_config->FindB("Debug::pkgCacheGen", false))
514 std::clog << "TranslationIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
515 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
516 << ") doesn't match for " << File.FileName() << std::endl;
517 return pkgCache::PkgFileIterator(Cache);
518 }
519 return File;
520 }
521 return File;
522 }
523 /*}}}*/
524 // StatusIndex::debStatusIndex - Constructor /*{{{*/
525 // ---------------------------------------------------------------------
526 /* */
527 debStatusIndex::debStatusIndex(string File) : pkgIndexFile(true), File(File)
528 {
529 }
530 /*}}}*/
531 // StatusIndex::Size - Return the size of the index /*{{{*/
532 // ---------------------------------------------------------------------
533 /* */
534 unsigned long debStatusIndex::Size() const
535 {
536 struct stat S;
537 if (stat(File.c_str(),&S) != 0)
538 return 0;
539 return S.st_size;
540 }
541 /*}}}*/
542 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
543 // ---------------------------------------------------------------------
544 /* */
545 bool debStatusIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
546 {
547 FileFd Pkg(File,FileFd::ReadOnlyGzip);
548 if (_error->PendingError() == true)
549 return false;
550 debListParser Parser(&Pkg);
551 if (_error->PendingError() == true)
552 return false;
553
554 Prog.SubProgress(0,File);
555 if (Gen.SelectFile(File,string(),*this,pkgCache::Flag::NotSource) == false)
556 return _error->Error("Problem with SelectFile %s",File.c_str());
557
558 // Store the IMS information
559 pkgCache::PkgFileIterator CFile = Gen.GetCurFile();
560 struct stat St;
561 if (fstat(Pkg.Fd(),&St) != 0)
562 return _error->Errno("fstat","Failed to stat");
563 CFile->Size = St.st_size;
564 CFile->mtime = St.st_mtime;
565 CFile->Archive = Gen.WriteUniqString("now");
566
567 if (Gen.MergeList(Parser) == false)
568 return _error->Error("Problem with MergeList %s",File.c_str());
569 return true;
570 }
571 /*}}}*/
572 // StatusIndex::FindInCache - Find this index /*{{{*/
573 // ---------------------------------------------------------------------
574 /* */
575 pkgCache::PkgFileIterator debStatusIndex::FindInCache(pkgCache &Cache) const
576 {
577 pkgCache::PkgFileIterator File = Cache.FileBegin();
578 for (; File.end() == false; File++)
579 {
580 if (this->File != File.FileName())
581 continue;
582
583 struct stat St;
584 if (stat(File.FileName(),&St) != 0)
585 {
586 if (_config->FindB("Debug::pkgCacheGen", false))
587 std::clog << "StatusIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
588 return pkgCache::PkgFileIterator(Cache);
589 }
590 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
591 {
592 if (_config->FindB("Debug::pkgCacheGen", false))
593 std::clog << "StatusIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
594 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
595 << ") doesn't match for " << File.FileName() << std::endl;
596 return pkgCache::PkgFileIterator(Cache);
597 }
598 return File;
599 }
600 return File;
601 }
602 /*}}}*/
603 // StatusIndex::Exists - Check if the index is available /*{{{*/
604 // ---------------------------------------------------------------------
605 /* */
606 bool debStatusIndex::Exists() const
607 {
608 // Abort if the file does not exist.
609 return true;
610 }
611 /*}}}*/
612
613 // Index File types for Debian /*{{{*/
614 class debIFTypeSrc : public pkgIndexFile::Type
615 {
616 public:
617
618 debIFTypeSrc() {Label = "Debian Source Index";};
619 };
620 class debIFTypePkg : public pkgIndexFile::Type
621 {
622 public:
623
624 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
625 {
626 return new debRecordParser(File.FileName(),*File.Cache());
627 };
628 debIFTypePkg() {Label = "Debian Package Index";};
629 };
630 class debIFTypeTrans : public debIFTypePkg
631 {
632 public:
633 debIFTypeTrans() {Label = "Debian Translation Index";};
634 };
635 class debIFTypeStatus : public pkgIndexFile::Type
636 {
637 public:
638
639 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
640 {
641 return new debRecordParser(File.FileName(),*File.Cache());
642 };
643 debIFTypeStatus() {Label = "Debian dpkg status file";};
644 };
645 static debIFTypeSrc _apt_Src;
646 static debIFTypePkg _apt_Pkg;
647 static debIFTypeTrans _apt_Trans;
648 static debIFTypeStatus _apt_Status;
649
650 const pkgIndexFile::Type *debSourcesIndex::GetType() const
651 {
652 return &_apt_Src;
653 }
654 const pkgIndexFile::Type *debPackagesIndex::GetType() const
655 {
656 return &_apt_Pkg;
657 }
658 const pkgIndexFile::Type *debTranslationsIndex::GetType() const
659 {
660 return &_apt_Trans;
661 }
662 const pkgIndexFile::Type *debStatusIndex::GetType() const
663 {
664 return &_apt_Status;
665 }
666
667 /*}}}*/