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