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