merged from lp:~donkult/apt/sid
[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 const &URI, string const &Dist, string const &Section,
163 bool const &Trusted, string const &Arch) :
164 pkgIndexFile(Trusted), URI(URI), Dist(Dist), Section(Section), Architecture(Arch)
165 {
166 if (Architecture == "native")
167 Architecture = _config->Find("APT::Architecture");
168 }
169 /*}}}*/
170 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
171 // ---------------------------------------------------------------------
172 /* This is a shorter version that is designed to be < 60 chars or so */
173 string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver) const
174 {
175 string Res = ::URI::NoUserPassword(URI) + ' ';
176 if (Dist[Dist.size() - 1] == '/')
177 {
178 if (Dist != "/")
179 Res += Dist;
180 }
181 else
182 Res += Dist + '/' + Section;
183
184 Res += " ";
185 Res += Ver.ParentPkg().Name();
186 Res += " ";
187 if (Dist[Dist.size() - 1] != '/')
188 Res.append(Ver.Arch()).append(" ");
189 Res += Ver.VerStr();
190 return Res;
191 }
192 /*}}}*/
193 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
194 // ---------------------------------------------------------------------
195 /* This should help the user find the index in the sources.list and
196 in the filesystem for problem solving */
197 string debPackagesIndex::Describe(bool Short) const
198 {
199 char S[300];
200 if (Short == true)
201 snprintf(S,sizeof(S),"%s",Info("Packages").c_str());
202 else
203 snprintf(S,sizeof(S),"%s (%s)",Info("Packages").c_str(),
204 IndexFile("Packages").c_str());
205 return S;
206 }
207 /*}}}*/
208 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
209 // ---------------------------------------------------------------------
210 /* */
211 string debPackagesIndex::Info(const char *Type) const
212 {
213 string Info = ::URI::NoUserPassword(URI) + ' ';
214 if (Dist[Dist.size() - 1] == '/')
215 {
216 if (Dist != "/")
217 Info += Dist;
218 }
219 else
220 Info += Dist + '/' + Section;
221 Info += " ";
222 if (Dist[Dist.size() - 1] != '/')
223 Info += Architecture + " ";
224 Info += Type;
225 return Info;
226 }
227 /*}}}*/
228 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
229 // ---------------------------------------------------------------------
230 /* */
231 inline string debPackagesIndex::IndexFile(const char *Type) const
232 {
233 string s =_config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
234 string sgzip = s + ".gz";
235 if (!FileExists(s) && FileExists(sgzip))
236 return sgzip;
237 else
238 return s;
239 }
240 string debPackagesIndex::IndexURI(const char *Type) const
241 {
242 string Res;
243 if (Dist[Dist.size() - 1] == '/')
244 {
245 if (Dist != "/")
246 Res = URI + Dist;
247 else
248 Res = URI;
249 }
250 else
251 Res = URI + "dists/" + Dist + '/' + Section +
252 "/binary-" + Architecture + '/';
253
254 Res += Type;
255 return Res;
256 }
257 /*}}}*/
258 // PackagesIndex::Exists - Check if the index is available /*{{{*/
259 // ---------------------------------------------------------------------
260 /* */
261 bool debPackagesIndex::Exists() const
262 {
263 return FileExists(IndexFile("Packages"));
264 }
265 /*}}}*/
266 // PackagesIndex::Size - Return the size of the index /*{{{*/
267 // ---------------------------------------------------------------------
268 /* This is really only used for progress reporting. */
269 unsigned long debPackagesIndex::Size() const
270 {
271 struct stat S;
272 if (stat(IndexFile("Packages").c_str(),&S) != 0)
273 return 0;
274 return S.st_size;
275 }
276 /*}}}*/
277 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
278 // ---------------------------------------------------------------------
279 /* */
280 bool debPackagesIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const
281 {
282 string PackageFile = IndexFile("Packages");
283 FileFd Pkg(PackageFile,FileFd::ReadOnlyGzip);
284 debListParser Parser(&Pkg, Architecture);
285
286 if (_error->PendingError() == true)
287 return _error->Error("Problem opening %s",PackageFile.c_str());
288 if (Prog != NULL)
289 Prog->SubProgress(0,Info("Packages"));
290 ::URI Tmp(URI);
291 if (Gen.SelectFile(PackageFile,Tmp.Host,*this) == false)
292 return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
293
294 // Store the IMS information
295 pkgCache::PkgFileIterator File = Gen.GetCurFile();
296 pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
297 struct stat St;
298 if (fstat(Pkg.Fd(),&St) != 0)
299 return _error->Errno("fstat","Failed to stat");
300 File->Size = St.st_size;
301 File->mtime = St.st_mtime;
302
303 if (Gen.MergeList(Parser) == false)
304 return _error->Error("Problem with MergeList %s",PackageFile.c_str());
305
306 // Check the release file
307 string ReleaseFile = debReleaseIndex(URI,Dist).MetaIndexFile("Release");
308 if (FileExists(ReleaseFile) == true)
309 {
310 FileFd Rel(ReleaseFile,FileFd::ReadOnly);
311 if (_error->PendingError() == true)
312 return false;
313 Parser.LoadReleaseInfo(File,Rel,Section);
314 }
315
316 return true;
317 }
318 /*}}}*/
319 // PackagesIndex::FindInCache - Find this index /*{{{*/
320 // ---------------------------------------------------------------------
321 /* */
322 pkgCache::PkgFileIterator debPackagesIndex::FindInCache(pkgCache &Cache) const
323 {
324 string FileName = IndexFile("Packages");
325 pkgCache::PkgFileIterator File = Cache.FileBegin();
326 for (; File.end() == false; File++)
327 {
328 if (File.FileName() == NULL || FileName != File.FileName())
329 continue;
330
331 struct stat St;
332 if (stat(File.FileName(),&St) != 0)
333 {
334 if (_config->FindB("Debug::pkgCacheGen", false))
335 std::clog << "PackagesIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
336 return pkgCache::PkgFileIterator(Cache);
337 }
338 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
339 {
340 if (_config->FindB("Debug::pkgCacheGen", false))
341 std::clog << "PackagesIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
342 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
343 << ") doesn't match for " << File.FileName() << std::endl;
344 return pkgCache::PkgFileIterator(Cache);
345 }
346 return File;
347 }
348
349 return File;
350 }
351 /*}}}*/
352
353 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
354 // ---------------------------------------------------------------------
355 /* */
356 debTranslationsIndex::debTranslationsIndex(string URI,string Dist,string Section,
357 char const * const Translation) :
358 pkgIndexFile(true), URI(URI), Dist(Dist), Section(Section),
359 Language(Translation)
360 {}
361 /*}}}*/
362 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
363 // ---------------------------------------------------------------------
364 /* */
365 inline string debTranslationsIndex::IndexFile(const char *Type) const
366 {
367 string s =_config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
368 string sgzip = s + ".gz";
369 if (!FileExists(s) && FileExists(sgzip))
370 return sgzip;
371 else
372 return s;
373 }
374 string debTranslationsIndex::IndexURI(const char *Type) const
375 {
376 string Res;
377 if (Dist[Dist.size() - 1] == '/')
378 {
379 if (Dist != "/")
380 Res = URI + Dist;
381 else
382 Res = URI;
383 }
384 else
385 Res = URI + "dists/" + Dist + '/' + Section +
386 "/i18n/Translation-";
387
388 Res += Type;
389 return Res;
390 }
391 /*}}}*/
392 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
393 // ---------------------------------------------------------------------
394 /* */
395 bool debTranslationsIndex::GetIndexes(pkgAcquire *Owner) const
396 {
397 if (TranslationsAvailable()) {
398 string const TranslationFile = string("Translation-").append(Language);
399 new pkgAcqIndexTrans(Owner, IndexURI(Language),
400 Info(TranslationFile.c_str()),
401 TranslationFile);
402 }
403
404 return true;
405 }
406 /*}}}*/
407 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
408 // ---------------------------------------------------------------------
409 /* This should help the user find the index in the sources.list and
410 in the filesystem for problem solving */
411 string debTranslationsIndex::Describe(bool Short) const
412 {
413 char S[300];
414 if (Short == true)
415 snprintf(S,sizeof(S),"%s",Info(TranslationFile().c_str()).c_str());
416 else
417 snprintf(S,sizeof(S),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
418 IndexFile(Language).c_str());
419 return S;
420 }
421 /*}}}*/
422 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
423 // ---------------------------------------------------------------------
424 /* */
425 string debTranslationsIndex::Info(const char *Type) const
426 {
427 string Info = ::URI::NoUserPassword(URI) + ' ';
428 if (Dist[Dist.size() - 1] == '/')
429 {
430 if (Dist != "/")
431 Info += Dist;
432 }
433 else
434 Info += Dist + '/' + Section;
435 Info += " ";
436 Info += Type;
437 return Info;
438 }
439 /*}}}*/
440 bool debTranslationsIndex::HasPackages() const /*{{{*/
441 {
442 if(!TranslationsAvailable())
443 return false;
444
445 return FileExists(IndexFile(Language));
446 }
447 /*}}}*/
448 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
449 // ---------------------------------------------------------------------
450 /* */
451 bool debTranslationsIndex::Exists() const
452 {
453 return FileExists(IndexFile(Language));
454 }
455 /*}}}*/
456 // TranslationsIndex::Size - Return the size of the index /*{{{*/
457 // ---------------------------------------------------------------------
458 /* This is really only used for progress reporting. */
459 unsigned long debTranslationsIndex::Size() const
460 {
461 struct stat S;
462 if (stat(IndexFile(Language).c_str(),&S) != 0)
463 return 0;
464 return S.st_size;
465 }
466 /*}}}*/
467 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
468 // ---------------------------------------------------------------------
469 /* */
470 bool debTranslationsIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const
471 {
472 // Check the translation file, if in use
473 string TranslationFile = IndexFile(Language);
474 if (TranslationsAvailable() && FileExists(TranslationFile))
475 {
476 FileFd Trans(TranslationFile,FileFd::ReadOnlyGzip);
477 debListParser TransParser(&Trans);
478 if (_error->PendingError() == true)
479 return false;
480
481 if (Prog != NULL)
482 Prog->SubProgress(0, Info(TranslationFile.c_str()));
483 if (Gen.SelectFile(TranslationFile,string(),*this) == false)
484 return _error->Error("Problem with SelectFile %s",TranslationFile.c_str());
485
486 // Store the IMS information
487 pkgCache::PkgFileIterator TransFile = Gen.GetCurFile();
488 struct stat TransSt;
489 if (fstat(Trans.Fd(),&TransSt) != 0)
490 return _error->Errno("fstat","Failed to stat");
491 TransFile->Size = TransSt.st_size;
492 TransFile->mtime = TransSt.st_mtime;
493
494 if (Gen.MergeList(TransParser) == false)
495 return _error->Error("Problem with MergeList %s",TranslationFile.c_str());
496 }
497
498 return true;
499 }
500 /*}}}*/
501 // TranslationsIndex::FindInCache - Find this index /*{{{*/
502 // ---------------------------------------------------------------------
503 /* */
504 pkgCache::PkgFileIterator debTranslationsIndex::FindInCache(pkgCache &Cache) const
505 {
506 string FileName = IndexFile(Language);
507
508 pkgCache::PkgFileIterator File = Cache.FileBegin();
509 for (; File.end() == false; File++)
510 {
511 if (FileName != File.FileName())
512 continue;
513
514 struct stat St;
515 if (stat(File.FileName(),&St) != 0)
516 {
517 if (_config->FindB("Debug::pkgCacheGen", false))
518 std::clog << "TranslationIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
519 return pkgCache::PkgFileIterator(Cache);
520 }
521 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
522 {
523 if (_config->FindB("Debug::pkgCacheGen", false))
524 std::clog << "TranslationIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
525 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
526 << ") doesn't match for " << File.FileName() << std::endl;
527 return pkgCache::PkgFileIterator(Cache);
528 }
529 return File;
530 }
531 return File;
532 }
533 /*}}}*/
534 // StatusIndex::debStatusIndex - Constructor /*{{{*/
535 // ---------------------------------------------------------------------
536 /* */
537 debStatusIndex::debStatusIndex(string File) : pkgIndexFile(true), File(File)
538 {
539 }
540 /*}}}*/
541 // StatusIndex::Size - Return the size of the index /*{{{*/
542 // ---------------------------------------------------------------------
543 /* */
544 unsigned long debStatusIndex::Size() const
545 {
546 struct stat S;
547 if (stat(File.c_str(),&S) != 0)
548 return 0;
549 return S.st_size;
550 }
551 /*}}}*/
552 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
553 // ---------------------------------------------------------------------
554 /* */
555 bool debStatusIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const
556 {
557 FileFd Pkg(File,FileFd::ReadOnlyGzip);
558 if (_error->PendingError() == true)
559 return false;
560 debListParser Parser(&Pkg);
561 if (_error->PendingError() == true)
562 return false;
563
564 if (Prog != NULL)
565 Prog->SubProgress(0,File);
566 if (Gen.SelectFile(File,string(),*this,pkgCache::Flag::NotSource) == false)
567 return _error->Error("Problem with SelectFile %s",File.c_str());
568
569 // Store the IMS information
570 pkgCache::PkgFileIterator CFile = Gen.GetCurFile();
571 struct stat St;
572 if (fstat(Pkg.Fd(),&St) != 0)
573 return _error->Errno("fstat","Failed to stat");
574 CFile->Size = St.st_size;
575 CFile->mtime = St.st_mtime;
576 CFile->Archive = Gen.WriteUniqString("now");
577
578 if (Gen.MergeList(Parser) == false)
579 return _error->Error("Problem with MergeList %s",File.c_str());
580 return true;
581 }
582 /*}}}*/
583 // StatusIndex::FindInCache - Find this index /*{{{*/
584 // ---------------------------------------------------------------------
585 /* */
586 pkgCache::PkgFileIterator debStatusIndex::FindInCache(pkgCache &Cache) const
587 {
588 pkgCache::PkgFileIterator File = Cache.FileBegin();
589 for (; File.end() == false; File++)
590 {
591 if (this->File != File.FileName())
592 continue;
593
594 struct stat St;
595 if (stat(File.FileName(),&St) != 0)
596 {
597 if (_config->FindB("Debug::pkgCacheGen", false))
598 std::clog << "StatusIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
599 return pkgCache::PkgFileIterator(Cache);
600 }
601 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
602 {
603 if (_config->FindB("Debug::pkgCacheGen", false))
604 std::clog << "StatusIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
605 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
606 << ") doesn't match for " << File.FileName() << std::endl;
607 return pkgCache::PkgFileIterator(Cache);
608 }
609 return File;
610 }
611 return File;
612 }
613 /*}}}*/
614 // StatusIndex::Exists - Check if the index is available /*{{{*/
615 // ---------------------------------------------------------------------
616 /* */
617 bool debStatusIndex::Exists() const
618 {
619 // Abort if the file does not exist.
620 return true;
621 }
622 /*}}}*/
623
624 // Index File types for Debian /*{{{*/
625 class debIFTypeSrc : public pkgIndexFile::Type
626 {
627 public:
628
629 debIFTypeSrc() {Label = "Debian Source Index";};
630 };
631 class debIFTypePkg : public pkgIndexFile::Type
632 {
633 public:
634
635 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
636 {
637 return new debRecordParser(File.FileName(),*File.Cache());
638 };
639 debIFTypePkg() {Label = "Debian Package Index";};
640 };
641 class debIFTypeTrans : public debIFTypePkg
642 {
643 public:
644 debIFTypeTrans() {Label = "Debian Translation Index";};
645 };
646 class debIFTypeStatus : public pkgIndexFile::Type
647 {
648 public:
649
650 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
651 {
652 return new debRecordParser(File.FileName(),*File.Cache());
653 };
654 debIFTypeStatus() {Label = "Debian dpkg status file";};
655 };
656 static debIFTypeSrc _apt_Src;
657 static debIFTypePkg _apt_Pkg;
658 static debIFTypeTrans _apt_Trans;
659 static debIFTypeStatus _apt_Status;
660
661 const pkgIndexFile::Type *debSourcesIndex::GetType() const
662 {
663 return &_apt_Src;
664 }
665 const pkgIndexFile::Type *debPackagesIndex::GetType() const
666 {
667 return &_apt_Pkg;
668 }
669 const pkgIndexFile::Type *debTranslationsIndex::GetType() const
670 {
671 return &_apt_Trans;
672 }
673 const pkgIndexFile::Type *debStatusIndex::GetType() const
674 {
675 return &_apt_Status;
676 }
677
678 /*}}}*/