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