Fixed warning
[ntk/apt.git] / apt-pkg / deb / debindexfile.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: debindexfile.cc,v 1.2 2001/02/20 07:03:17 jgg 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#ifdef __GNUG__
13#pragma implementation "apt-pkg/debindexfile.h"
14#endif
15
16#include <apt-pkg/debindexfile.h>
17#include <apt-pkg/debsrcrecords.h>
18#include <apt-pkg/deblistparser.h>
19#include <apt-pkg/debrecords.h>
20#include <apt-pkg/sourcelist.h>
21#include <apt-pkg/configuration.h>
22#include <apt-pkg/progress.h>
23#include <apt-pkg/error.h>
24#include <apt-pkg/strutl.h>
25#include <apt-pkg/acquire-item.h>
26
27#include <sys/stat.h>
28 /*}}}*/
29
30// SourcesIndex::debSourcesIndex - Constructor /*{{{*/
31// ---------------------------------------------------------------------
32/* */
33debSourcesIndex::debSourcesIndex(string URI,string Dist,string Section) :
34 URI(URI), Dist(Dist), Section(Section)
35{
36}
37 /*}}}*/
38// SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
39// ---------------------------------------------------------------------
40/* The result looks like:
41 http://foo/ stable/main src 1.1.1 (dsc) */
42string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
43 pkgSrcRecords::File const &File) const
44{
45 string Res;
46 Res = ::URI::SiteOnly(URI) + ' ';
47 if (Dist[Dist.size() - 1] == '/')
48 {
49 if (Dist != "/")
50 Res += Dist;
51 }
52 else
53 Res += Dist + '/' + Section;
54
55 Res += " ";
56 Res += Record.Package();
57 Res += " ";
58 Res += Record.Version();
59 if (File.Type.empty() == false)
60 Res += " (" + File.Type + ")";
61 return Res;
62}
63 /*}}}*/
64// SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
65// ---------------------------------------------------------------------
66/* */
67pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const
68{
69 string SourcesURI;
70 if (Dist[Dist.size() - 1] == '/')
71 SourcesURI = URI + Dist;
72 else
73 SourcesURI = URI + "dists/" + Dist + '/' + Section +
74 "/source/";
75
76 SourcesURI += "Sources";
77 SourcesURI = URItoFileName(SourcesURI);
78 return new debSrcRecordParser(_config->FindDir("Dir::State::lists") +
79 SourcesURI,this);
80}
81 /*}}}*/
82// SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
83// ---------------------------------------------------------------------
84/* */
85string debSourcesIndex::Describe() const
86{
87 char S[300];
88 snprintf(S,sizeof(S),"%s (%s)",Info("Packages").c_str(),
89 IndexFile("Sources").c_str());
90 return S;
91}
92 /*}}}*/
93// SourcesIndex::Info - One liner describing the index URI /*{{{*/
94// ---------------------------------------------------------------------
95/* */
96string debSourcesIndex::Info(const char *Type) const
97{
98 string Info = ::URI::SiteOnly(URI) + ' ';
99 if (Dist[Dist.size() - 1] == '/')
100 {
101 if (Dist != "/")
102 Info += Dist;
103 }
104 else
105 Info += Dist + '/' + Section;
106 Info += " ";
107 Info += Type;
108 return Info;
109}
110 /*}}}*/
111// SourcesIndex::Index* - Return the URI to the index files /*{{{*/
112// ---------------------------------------------------------------------
113/* */
114inline string debSourcesIndex::IndexFile(const char *Type) const
115{
116 return URItoFileName(IndexURI(Type));
117}
118string debSourcesIndex::IndexURI(const char *Type) const
119{
120 string Res;
121 if (Dist[Dist.size() - 1] == '/')
122 {
123 if (Dist != "/")
124 Res = URI + Dist;
125 else
126 Res = URI;
127 }
128 else
129 Res = URI + "dists/" + Dist + '/' + Section +
130 "/source/";
131
132 Res += Type;
133 return Res;
134}
135 /*}}}*/
136// SourcesIndex::GetIndexes - Fetch the index files /*{{{*/
137// ---------------------------------------------------------------------
138/* */
139bool debSourcesIndex::GetIndexes(pkgAcquire *Owner) const
140{
141 new pkgAcqIndex(Owner,IndexURI("Sources"),Info("Sources"),"Sources");
142 new pkgAcqIndexRel(Owner,IndexURI("Release"),Info("Release"),"Release");
143 return true;
144}
145 /*}}}*/
146// SourcesIndex::Exists - Check if the index is available /*{{{*/
147// ---------------------------------------------------------------------
148/* */
149bool debSourcesIndex::Exists() const
150{
151 return FileExists(IndexFile("Sources"));
152}
153 /*}}}*/
154// SourcesIndex::Size - Return the size of the index /*{{{*/
155// ---------------------------------------------------------------------
156/* */
157unsigned long debSourcesIndex::Size() const
158{
159 struct stat S;
160 if (stat(IndexFile("Sources").c_str(),&S) != 0)
161 return 0;
162 return S.st_size;
163}
164 /*}}}*/
165
166// PackagesIndex::debPackagesIndex - Contructor /*{{{*/
167// ---------------------------------------------------------------------
168/* */
169debPackagesIndex::debPackagesIndex(string URI,string Dist,string Section) :
170 URI(URI), Dist(Dist), Section(Section)
171{
172}
173 /*}}}*/
174// PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
175// ---------------------------------------------------------------------
176/* This is a shorter version that is designed to be < 60 chars or so */
177string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver) const
178{
179 string Res = ::URI::SiteOnly(URI) + ' ';
180 if (Dist[Dist.size() - 1] == '/')
181 {
182 if (Dist != "/")
183 Res += Dist;
184 }
185 else
186 Res += Dist + '/' + Section;
187
188 Res += " ";
189 Res += Ver.ParentPkg().Name();
190 Res += " ";
191 Res += Ver.VerStr();
192 return Res;
193}
194 /*}}}*/
195// PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
196// ---------------------------------------------------------------------
197/* This should help the user find the index in the sources.list and
198 in the filesystem for problem solving */
199string debPackagesIndex::Describe() const
200{
201 char S[300];
202 snprintf(S,sizeof(S),"%s (%s)",Info("Packages").c_str(),
203 IndexFile("Packages").c_str());
204 return S;
205}
206 /*}}}*/
207// PackagesIndex::Info - One liner describing the index URI /*{{{*/
208// ---------------------------------------------------------------------
209/* */
210string debPackagesIndex::Info(const char *Type) const
211{
212 string Info = ::URI::SiteOnly(URI) + ' ';
213 if (Dist[Dist.size() - 1] == '/')
214 {
215 if (Dist != "/")
216 Info += Dist;
217 }
218 else
219 Info += Dist + '/' + Section;
220 Info += " ";
221 Info += Type;
222 return Info;
223}
224 /*}}}*/
225// PackagesIndex::Index* - Return the URI to the index files /*{{{*/
226// ---------------------------------------------------------------------
227/* */
228inline string debPackagesIndex::IndexFile(const char *Type) const
229{
230 return _config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
231}
232string debPackagesIndex::IndexURI(const char *Type) const
233{
234 string Res;
235 if (Dist[Dist.size() - 1] == '/')
236 {
237 if (Dist != "/")
238 Res = URI + Dist;
239 else
240 Res = URI;
241 }
242 else
243 Res = URI + "dists/" + Dist + '/' + Section +
244 "/binary-" + _config->Find("APT::Architecture") + '/';
245
246 Res += Type;
247 return Res;
248}
249 /*}}}*/
250// PackagesIndex::GetIndexes - Fetch the index files /*{{{*/
251// ---------------------------------------------------------------------
252/* */
253bool debPackagesIndex::GetIndexes(pkgAcquire *Owner) const
254{
255 new pkgAcqIndex(Owner,IndexURI("Packages"),Info("Packages"),"Packages");
256 new pkgAcqIndexRel(Owner,IndexURI("Release"),Info("Release"),"Release");
257 return true;
258}
259 /*}}}*/
260// PackagesIndex::Exists - Check if the index is available /*{{{*/
261// ---------------------------------------------------------------------
262/* */
263bool debPackagesIndex::Exists() const
264{
265 return FileExists(IndexFile("Packages"));
266}
267 /*}}}*/
268// PackagesIndex::Size - Return the size of the index /*{{{*/
269// ---------------------------------------------------------------------
270/* This is really only used for progress reporting. */
271unsigned long debPackagesIndex::Size() const
272{
273 struct stat S;
274 if (stat(IndexFile("Packages").c_str(),&S) != 0)
275 return 0;
276 return S.st_size;
277}
278 /*}}}*/
279// PackagesIndex::Merge - Load the index file into a cache /*{{{*/
280// ---------------------------------------------------------------------
281/* */
282bool debPackagesIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
283{
284 string PackageFile = IndexFile("Packages");
285 FileFd Pkg(PackageFile,FileFd::ReadOnly);
286 debListParser Parser(&Pkg);
287 if (_error->PendingError() == true)
288 return _error->Error("Problem opening %s",PackageFile.c_str());
289
290 Prog.SubProgress(0,Info("Packages"));
291 ::URI Tmp(URI);
292 if (Gen.SelectFile(PackageFile,Tmp.Host,*this) == false)
293 return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
294
295 // Store the IMS information
296 pkgCache::PkgFileIterator File = Gen.GetCurFile();
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 = IndexFile("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);
314 }
315
316 return true;
317}
318 /*}}}*/
319// PackagesIndex::FindInCache - Find this index /*{{{*/
320// ---------------------------------------------------------------------
321/* */
322pkgCache::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 (FileName != File.FileName())
329 continue;
330
331 struct stat St;
332 if (stat(File.FileName(),&St) != 0)
333 return pkgCache::PkgFileIterator(Cache);
334 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
335 return pkgCache::PkgFileIterator(Cache);
336 return File;
337 }
338
339 return File;
340}
341 /*}}}*/
342
343// StatusIndex::debStatusIndex - Constructor /*{{{*/
344// ---------------------------------------------------------------------
345/* */
346debStatusIndex::debStatusIndex(string File) : File(File)
347{
348}
349 /*}}}*/
350// StatusIndex::Size - Return the size of the index /*{{{*/
351// ---------------------------------------------------------------------
352/* */
353unsigned long debStatusIndex::Size() const
354{
355 struct stat S;
356 if (stat(File.c_str(),&S) != 0)
357 return 0;
358 return S.st_size;
359}
360 /*}}}*/
361// StatusIndex::Merge - Load the index file into a cache /*{{{*/
362// ---------------------------------------------------------------------
363/* */
364bool debStatusIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
365{
366 FileFd Pkg(File,FileFd::ReadOnly);
367 if (_error->PendingError() == true)
368 return false;
369 debListParser Parser(&Pkg);
370 if (_error->PendingError() == true)
371 return false;
372
373 Prog.SubProgress(0,File);
374 if (Gen.SelectFile(File,string(),*this,pkgCache::Flag::NotSource) == false)
375 return _error->Error("Problem with SelectFile %s",File.c_str());
376
377 // Store the IMS information
378 pkgCache::PkgFileIterator CFile = Gen.GetCurFile();
379 struct stat St;
380 if (fstat(Pkg.Fd(),&St) != 0)
381 return _error->Errno("fstat","Failed to stat");
382 CFile->Size = St.st_size;
383 CFile->mtime = St.st_mtime;
384 CFile->Archive = Gen.WriteUniqString("now");
385
386 if (Gen.MergeList(Parser) == false)
387 return _error->Error("Problem with MergeList %s",File.c_str());
388 return true;
389}
390 /*}}}*/
391// StatusIndex::FindInCache - Find this index /*{{{*/
392// ---------------------------------------------------------------------
393/* */
394pkgCache::PkgFileIterator debStatusIndex::FindInCache(pkgCache &Cache) const
395{
396 pkgCache::PkgFileIterator File = Cache.FileBegin();
397 for (; File.end() == false; File++)
398 {
399 if (this->File != File.FileName())
400 continue;
401
402 struct stat St;
403 if (stat(File.FileName(),&St) != 0)
404 return pkgCache::PkgFileIterator(Cache);
405 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
406 return pkgCache::PkgFileIterator(Cache);
407 return File;
408 }
409 return File;
410}
411 /*}}}*/
412// StatusIndex::Exists - Check if the index is available /*{{{*/
413// ---------------------------------------------------------------------
414/* */
415bool debStatusIndex::Exists() const
416{
417 // Abort if the file does not exist.
418 return true;
419}
420 /*}}}*/
421
422// Source List types for Debian /*{{{*/
423class debSLTypeDeb : public pkgSourceList::Type
424{
425 public:
426
427 bool CreateItem(vector<pkgIndexFile *> &List,string URI,
428 string Dist,string Section) const
429 {
430 List.push_back(new debPackagesIndex(URI,Dist,Section));
431 return true;
432 };
433
434 debSLTypeDeb()
435 {
436 Name = "deb";
437 Label = "Standard Debian binary tree";
438 }
439};
440
441class debSLTypeDebSrc : public pkgSourceList::Type
442{
443 public:
444
445 bool CreateItem(vector<pkgIndexFile *> &List,string URI,
446 string Dist,string Section) const
447 {
448 List.push_back(new debSourcesIndex(URI,Dist,Section));
449 return true;
450 };
451
452 debSLTypeDebSrc()
453 {
454 Name = "deb-src";
455 Label = "Standard Debian source tree";
456 }
457};
458
459debSLTypeDeb _apt_DebType;
460debSLTypeDebSrc _apt_DebSrcType;
461 /*}}}*/
462// Index File types for Debian /*{{{*/
463class debIFTypeSrc : public pkgIndexFile::Type
464{
465 public:
466
467 debIFTypeSrc() {Label = "Debian Source Index";};
468};
469class debIFTypePkg : public pkgIndexFile::Type
470{
471 public:
472
473 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
474 {
475 return new debRecordParser(File.FileName(),*File.Cache());
476 };
477 debIFTypePkg() {Label = "Debian Package Index";};
478};
479class debIFTypeStatus : public pkgIndexFile::Type
480{
481 public:
482
483 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
484 {
485 return new debRecordParser(File.FileName(),*File.Cache());
486 };
487 debIFTypeStatus() {Label = "Debian dpkg status file";};
488};
489static debIFTypeSrc _apt_Src;
490static debIFTypePkg _apt_Pkg;
491static debIFTypeStatus _apt_Status;
492
493const pkgIndexFile::Type *debSourcesIndex::GetType() const
494{
495 return &_apt_Src;
496}
497const pkgIndexFile::Type *debPackagesIndex::GetType() const
498{
499 return &_apt_Pkg;
500}
501const pkgIndexFile::Type *debStatusIndex::GetType() const
502{
503 return &_apt_Status;
504}
505
506 /*}}}*/