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