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