merged from the mvo branch
[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 = URItoFileName(IndexURI("Sources"));
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 /* */
74 string debSourcesIndex::Describe(bool Short) const
75 {
76 char S[300];
77 if (Short == true)
78 snprintf(S,sizeof(S),"%s",Info("Sources").c_str());
79 else
80 snprintf(S,sizeof(S),"%s (%s)",Info("Sources").c_str(),
81 IndexFile("Sources").c_str());
82
83 return S;
84 }
85 /*}}}*/
86 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
87 // ---------------------------------------------------------------------
88 /* */
89 string debSourcesIndex::Info(const char *Type) const
90 {
91 string Info = ::URI::NoUserPassword(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 /* */
107 inline string debSourcesIndex::IndexFile(const char *Type) const
108 {
109 return URItoFileName(IndexURI(Type));
110 }
111 string 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 /*}}}*/
129 // SourcesIndex::Exists - Check if the index is available /*{{{*/
130 // ---------------------------------------------------------------------
131 /* */
132 bool debSourcesIndex::Exists() const
133 {
134 return FileExists(IndexFile("Sources"));
135 }
136 /*}}}*/
137 // SourcesIndex::Size - Return the size of the index /*{{{*/
138 // ---------------------------------------------------------------------
139 /* */
140 unsigned 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 /* */
152 debPackagesIndex::debPackagesIndex(string URI,string Dist,string Section,bool Trusted) :
153 pkgIndexFile(Trusted), URI(URI), Dist(Dist), Section(Section)
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 */
160 string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver) const
161 {
162 string Res = ::URI::NoUserPassword(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 */
182 string debPackagesIndex::Describe(bool Short) const
183 {
184 char S[300];
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());
190 return S;
191 }
192 /*}}}*/
193 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
194 // ---------------------------------------------------------------------
195 /* */
196 string debPackagesIndex::Info(const char *Type) const
197 {
198 string Info = ::URI::NoUserPassword(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 /* */
214 inline string debPackagesIndex::IndexFile(const char *Type) const
215 {
216 return _config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
217 }
218 string 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 /*}}}*/
236 // PackagesIndex::Exists - Check if the index is available /*{{{*/
237 // ---------------------------------------------------------------------
238 /* */
239 bool 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. */
247 unsigned 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 /* */
258 bool 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
283 string ReleaseFile = debReleaseIndex(URI,Dist).MetaIndexFile("Release");
284 if (FileExists(ReleaseFile) == true)
285 {
286 FileFd Rel(ReleaseFile,FileFd::ReadOnly);
287 if (_error->PendingError() == true)
288 return false;
289 Parser.LoadReleaseInfo(File,Rel,Section);
290 }
291
292 return true;
293 }
294 /*}}}*/
295 // PackagesIndex::FindInCache - Find this index /*{{{*/
296 // ---------------------------------------------------------------------
297 /* */
298 pkgCache::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 {
304 if (File.FileName() == NULL || FileName != File.FileName())
305 continue;
306
307 struct stat St;
308 if (stat(File.FileName(),&St) != 0)
309 {
310 if (_config->FindB("Debug::pkgCacheGen", false))
311 std::clog << "PackagesIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
312 return pkgCache::PkgFileIterator(Cache);
313 }
314 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
315 {
316 if (_config->FindB("Debug::pkgCacheGen", false))
317 std::clog << "PackagesIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
318 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
319 << ") doesn't match for " << File.FileName() << std::endl;
320 return pkgCache::PkgFileIterator(Cache);
321 }
322 return File;
323 }
324
325 return File;
326 }
327 /*}}}*/
328
329 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
330 // ---------------------------------------------------------------------
331 /* */
332 debTranslationsIndex::debTranslationsIndex(string URI,string Dist,string Section) :
333 pkgIndexFile(true), URI(URI), Dist(Dist), Section(Section)
334 {
335 }
336 /*}}}*/
337 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
338 // ---------------------------------------------------------------------
339 /* */
340 inline string debTranslationsIndex::IndexFile(const char *Type) const
341 {
342 return _config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
343 }
344 string debTranslationsIndex::IndexURI(const char *Type) const
345 {
346 string Res;
347 if (Dist[Dist.size() - 1] == '/')
348 {
349 if (Dist != "/")
350 Res = URI + Dist;
351 else
352 Res = URI;
353 }
354 else
355 Res = URI + "dists/" + Dist + '/' + Section +
356 "/i18n/Translation-";
357
358 Res += Type;
359 return Res;
360 }
361 /*}}}*/
362 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
363 // ---------------------------------------------------------------------
364 /* */
365 bool debTranslationsIndex::GetIndexes(pkgAcquire *Owner) const
366 {
367 if (TranslationsAvailable()) {
368 string TranslationFile = "Translation-" + LanguageCode();
369 new pkgAcqIndexTrans(Owner, IndexURI(LanguageCode().c_str()),
370 Info(TranslationFile.c_str()),
371 TranslationFile);
372 }
373
374 return true;
375 }
376 /*}}}*/
377 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
378 // ---------------------------------------------------------------------
379 /* This should help the user find the index in the sources.list and
380 in the filesystem for problem solving */
381 string debTranslationsIndex::Describe(bool Short) const
382 {
383 char S[300];
384 if (Short == true)
385 snprintf(S,sizeof(S),"%s",Info(TranslationFile().c_str()).c_str());
386 else
387 snprintf(S,sizeof(S),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
388 IndexFile(LanguageCode().c_str()).c_str());
389 return S;
390 }
391 /*}}}*/
392 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
393 // ---------------------------------------------------------------------
394 /* */
395 string debTranslationsIndex::Info(const char *Type) const
396 {
397 string Info = ::URI::NoUserPassword(URI) + ' ';
398 if (Dist[Dist.size() - 1] == '/')
399 {
400 if (Dist != "/")
401 Info += Dist;
402 }
403 else
404 Info += Dist + '/' + Section;
405 Info += " ";
406 Info += Type;
407 return Info;
408 }
409 /*}}}*/
410 bool debTranslationsIndex::HasPackages() const
411 {
412 if(!TranslationsAvailable())
413 return false;
414
415 return FileExists(IndexFile(LanguageCode().c_str()));
416 }
417
418 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
419 // ---------------------------------------------------------------------
420 /* */
421 bool debTranslationsIndex::Exists() const
422 {
423 return FileExists(IndexFile(LanguageCode().c_str()));
424 }
425 /*}}}*/
426 // TranslationsIndex::Size - Return the size of the index /*{{{*/
427 // ---------------------------------------------------------------------
428 /* This is really only used for progress reporting. */
429 unsigned long debTranslationsIndex::Size() const
430 {
431 struct stat S;
432 if (stat(IndexFile(LanguageCode().c_str()).c_str(),&S) != 0)
433 return 0;
434 return S.st_size;
435 }
436 /*}}}*/
437 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
438 // ---------------------------------------------------------------------
439 /* */
440 bool debTranslationsIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
441 {
442 // Check the translation file, if in use
443 string TranslationFile = IndexFile(LanguageCode().c_str());
444 if (TranslationsAvailable() && FileExists(TranslationFile))
445 {
446 FileFd Trans(TranslationFile,FileFd::ReadOnly);
447 debListParser TransParser(&Trans);
448 if (_error->PendingError() == true)
449 return false;
450
451 Prog.SubProgress(0, Info(TranslationFile.c_str()));
452 if (Gen.SelectFile(TranslationFile,string(),*this) == false)
453 return _error->Error("Problem with SelectFile %s",TranslationFile.c_str());
454
455 // Store the IMS information
456 pkgCache::PkgFileIterator TransFile = Gen.GetCurFile();
457 struct stat TransSt;
458 if (fstat(Trans.Fd(),&TransSt) != 0)
459 return _error->Errno("fstat","Failed to stat");
460 TransFile->Size = TransSt.st_size;
461 TransFile->mtime = TransSt.st_mtime;
462
463 if (Gen.MergeList(TransParser) == false)
464 return _error->Error("Problem with MergeList %s",TranslationFile.c_str());
465 }
466
467 return true;
468 }
469 /*}}}*/
470 // TranslationsIndex::FindInCache - Find this index /*{{{*/
471 // ---------------------------------------------------------------------
472 /* */
473 pkgCache::PkgFileIterator debTranslationsIndex::FindInCache(pkgCache &Cache) const
474 {
475 string FileName = IndexFile(LanguageCode().c_str());
476
477 pkgCache::PkgFileIterator File = Cache.FileBegin();
478 for (; File.end() == false; File++)
479 {
480 if (FileName != File.FileName())
481 continue;
482
483 struct stat St;
484 if (stat(File.FileName(),&St) != 0)
485 {
486 if (_config->FindB("Debug::pkgCacheGen", false))
487 std::clog << "TranslationIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
488 return pkgCache::PkgFileIterator(Cache);
489 }
490 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
491 {
492 if (_config->FindB("Debug::pkgCacheGen", false))
493 std::clog << "TranslationIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
494 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
495 << ") doesn't match for " << File.FileName() << std::endl;
496 return pkgCache::PkgFileIterator(Cache);
497 }
498 return File;
499 }
500 return File;
501 }
502 /*}}}*/
503 // StatusIndex::debStatusIndex - Constructor /*{{{*/
504 // ---------------------------------------------------------------------
505 /* */
506 debStatusIndex::debStatusIndex(string File) : pkgIndexFile(true), File(File)
507 {
508 }
509 /*}}}*/
510 // StatusIndex::Size - Return the size of the index /*{{{*/
511 // ---------------------------------------------------------------------
512 /* */
513 unsigned long debStatusIndex::Size() const
514 {
515 struct stat S;
516 if (stat(File.c_str(),&S) != 0)
517 return 0;
518 return S.st_size;
519 }
520 /*}}}*/
521 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
522 // ---------------------------------------------------------------------
523 /* */
524 bool debStatusIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
525 {
526 FileFd Pkg(File,FileFd::ReadOnly);
527 if (_error->PendingError() == true)
528 return false;
529 debListParser Parser(&Pkg);
530 if (_error->PendingError() == true)
531 return false;
532
533 Prog.SubProgress(0,File);
534 if (Gen.SelectFile(File,string(),*this,pkgCache::Flag::NotSource) == false)
535 return _error->Error("Problem with SelectFile %s",File.c_str());
536
537 // Store the IMS information
538 pkgCache::PkgFileIterator CFile = Gen.GetCurFile();
539 struct stat St;
540 if (fstat(Pkg.Fd(),&St) != 0)
541 return _error->Errno("fstat","Failed to stat");
542 CFile->Size = St.st_size;
543 CFile->mtime = St.st_mtime;
544 CFile->Archive = Gen.WriteUniqString("now");
545
546 if (Gen.MergeList(Parser) == false)
547 return _error->Error("Problem with MergeList %s",File.c_str());
548 return true;
549 }
550 /*}}}*/
551 // StatusIndex::FindInCache - Find this index /*{{{*/
552 // ---------------------------------------------------------------------
553 /* */
554 pkgCache::PkgFileIterator debStatusIndex::FindInCache(pkgCache &Cache) const
555 {
556 pkgCache::PkgFileIterator File = Cache.FileBegin();
557 for (; File.end() == false; File++)
558 {
559 if (this->File != File.FileName())
560 continue;
561
562 struct stat St;
563 if (stat(File.FileName(),&St) != 0)
564 {
565 if (_config->FindB("Debug::pkgCacheGen", false))
566 std::clog << "StatusIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
567 return pkgCache::PkgFileIterator(Cache);
568 }
569 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
570 {
571 if (_config->FindB("Debug::pkgCacheGen", false))
572 std::clog << "StatusIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
573 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
574 << ") doesn't match for " << File.FileName() << std::endl;
575 return pkgCache::PkgFileIterator(Cache);
576 }
577 return File;
578 }
579 return File;
580 }
581 /*}}}*/
582 // StatusIndex::Exists - Check if the index is available /*{{{*/
583 // ---------------------------------------------------------------------
584 /* */
585 bool debStatusIndex::Exists() const
586 {
587 // Abort if the file does not exist.
588 return true;
589 }
590 /*}}}*/
591
592 // Index File types for Debian /*{{{*/
593 class debIFTypeSrc : public pkgIndexFile::Type
594 {
595 public:
596
597 debIFTypeSrc() {Label = "Debian Source Index";};
598 };
599 class debIFTypePkg : public pkgIndexFile::Type
600 {
601 public:
602
603 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
604 {
605 return new debRecordParser(File.FileName(),*File.Cache());
606 };
607 debIFTypePkg() {Label = "Debian Package Index";};
608 };
609 class debIFTypeTrans : public debIFTypePkg
610 {
611 public:
612 debIFTypeTrans() {Label = "Debian Translation Index";};
613 };
614 class debIFTypeStatus : public pkgIndexFile::Type
615 {
616 public:
617
618 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
619 {
620 return new debRecordParser(File.FileName(),*File.Cache());
621 };
622 debIFTypeStatus() {Label = "Debian dpkg status file";};
623 };
624 static debIFTypeSrc _apt_Src;
625 static debIFTypePkg _apt_Pkg;
626 static debIFTypeTrans _apt_Trans;
627 static debIFTypeStatus _apt_Status;
628
629 const pkgIndexFile::Type *debSourcesIndex::GetType() const
630 {
631 return &_apt_Src;
632 }
633 const pkgIndexFile::Type *debPackagesIndex::GetType() const
634 {
635 return &_apt_Pkg;
636 }
637 const pkgIndexFile::Type *debTranslationsIndex::GetType() const
638 {
639 return &_apt_Trans;
640 }
641 const pkgIndexFile::Type *debStatusIndex::GetType() const
642 {
643 return &_apt_Status;
644 }
645
646 /*}}}*/