* debug support (Debug::pkgAcquire::RRed) for rred method added
[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 #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 #include <apt-pkg/debmetaindex.h>
27
28 #include <sys/stat.h>
29 /*}}}*/
30
31 // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
33 /* */
34 debSourcesIndex::debSourcesIndex(string URI,string Dist,string Section,bool Trusted) :
35 pkgIndexFile(Trusted), URI(URI), Dist(Dist), Section(Section)
36 {
37 }
38 /*}}}*/
39 // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
40 // ---------------------------------------------------------------------
41 /* The result looks like:
42 http://foo/ stable/main src 1.1.1 (dsc) */
43 string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
44 pkgSrcRecords::File const &File) const
45 {
46 string Res;
47 Res = ::URI::SiteOnly(URI) + ' ';
48 if (Dist[Dist.size() - 1] == '/')
49 {
50 if (Dist != "/")
51 Res += Dist;
52 }
53 else
54 Res += Dist + '/' + Section;
55
56 Res += " ";
57 Res += Record.Package();
58 Res += " ";
59 Res += Record.Version();
60 if (File.Type.empty() == false)
61 Res += " (" + File.Type + ")";
62 return Res;
63 }
64 /*}}}*/
65 // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
66 // ---------------------------------------------------------------------
67 /* */
68 pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const
69 {
70 string SourcesURI = URItoFileName(IndexURI("Sources"));
71 return new debSrcRecordParser(_config->FindDir("Dir::State::lists") +
72 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::SiteOnly(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 return URItoFileName(IndexURI(Type));
114 }
115 string debSourcesIndex::IndexURI(const char *Type) const
116 {
117 string Res;
118 if (Dist[Dist.size() - 1] == '/')
119 {
120 if (Dist != "/")
121 Res = URI + Dist;
122 else
123 Res = URI;
124 }
125 else
126 Res = URI + "dists/" + Dist + '/' + Section +
127 "/source/";
128
129 Res += Type;
130 return Res;
131 }
132 /*}}}*/
133 // SourcesIndex::Exists - Check if the index is available /*{{{*/
134 // ---------------------------------------------------------------------
135 /* */
136 bool debSourcesIndex::Exists() const
137 {
138 return FileExists(IndexFile("Sources"));
139 }
140 /*}}}*/
141 // SourcesIndex::Size - Return the size of the index /*{{{*/
142 // ---------------------------------------------------------------------
143 /* */
144 unsigned long debSourcesIndex::Size() const
145 {
146 struct stat S;
147 if (stat(IndexFile("Sources").c_str(),&S) != 0)
148 return 0;
149 return S.st_size;
150 }
151 /*}}}*/
152
153 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
154 // ---------------------------------------------------------------------
155 /* */
156 debPackagesIndex::debPackagesIndex(string URI,string Dist,string Section,bool Trusted) :
157 pkgIndexFile(Trusted), URI(URI), Dist(Dist), Section(Section)
158 {
159 }
160 /*}}}*/
161
162 string debPackagesIndex::ArchiveURI(string File) const
163 {
164 // FIXME: Remove as soon as pdiff support is offical
165 string remap = _config->Find("APT::Diffs::Remap::"+URI,"");
166 if(!remap.empty())
167 {
168 std::cout << "doing a evil remapping to the URI as requested!\n";
169 std::cout << URI << " -> " << remap << std::endl;
170 return remap+File;
171 }
172
173 return URI + File;
174 }
175
176 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
177 // ---------------------------------------------------------------------
178 /* This is a shorter version that is designed to be < 60 chars or so */
179 string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver) const
180 {
181 string Res = ::URI::SiteOnly(URI) + ' ';
182 if (Dist[Dist.size() - 1] == '/')
183 {
184 if (Dist != "/")
185 Res += Dist;
186 }
187 else
188 Res += Dist + '/' + Section;
189
190 Res += " ";
191 Res += Ver.ParentPkg().Name();
192 Res += " ";
193 Res += Ver.VerStr();
194 return Res;
195 }
196 /*}}}*/
197 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
198 // ---------------------------------------------------------------------
199 /* This should help the user find the index in the sources.list and
200 in the filesystem for problem solving */
201 string debPackagesIndex::Describe(bool Short) const
202 {
203 char S[300];
204 if (Short == true)
205 snprintf(S,sizeof(S),"%s",Info("Packages").c_str());
206 else
207 snprintf(S,sizeof(S),"%s (%s)",Info("Packages").c_str(),
208 IndexFile("Packages").c_str());
209 return S;
210 }
211 /*}}}*/
212 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
213 // ---------------------------------------------------------------------
214 /* */
215 string debPackagesIndex::Info(const char *Type) const
216 {
217 string Info = ::URI::SiteOnly(URI) + ' ';
218 if (Dist[Dist.size() - 1] == '/')
219 {
220 if (Dist != "/")
221 Info += Dist;
222 }
223 else
224 Info += Dist + '/' + Section;
225 Info += " ";
226 Info += Type;
227 return Info;
228 }
229 /*}}}*/
230 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
231 // ---------------------------------------------------------------------
232 /* */
233 inline string debPackagesIndex::IndexFile(const char *Type) const
234 {
235 return _config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
236 }
237 string debPackagesIndex::IndexURI(const char *Type) const
238 {
239 string Res;
240 if (Dist[Dist.size() - 1] == '/')
241 {
242 if (Dist != "/")
243 Res = URI + Dist;
244 else
245 Res = URI;
246 }
247 else
248 Res = URI + "dists/" + Dist + '/' + Section +
249 "/binary-" + _config->Find("APT::Architecture") + '/';
250
251 Res += Type;
252 return Res;
253 }
254 /*}}}*/
255 // PackagesIndex::Exists - Check if the index is available /*{{{*/
256 // ---------------------------------------------------------------------
257 /* */
258 bool debPackagesIndex::Exists() const
259 {
260 return FileExists(IndexFile("Packages"));
261 }
262 /*}}}*/
263 // PackagesIndex::Size - Return the size of the index /*{{{*/
264 // ---------------------------------------------------------------------
265 /* This is really only used for progress reporting. */
266 unsigned long debPackagesIndex::Size() const
267 {
268 struct stat S;
269 if (stat(IndexFile("Packages").c_str(),&S) != 0)
270 return 0;
271 return S.st_size;
272 }
273 /*}}}*/
274 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
275 // ---------------------------------------------------------------------
276 /* */
277 bool debPackagesIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
278 {
279 string PackageFile = IndexFile("Packages");
280 FileFd Pkg(PackageFile,FileFd::ReadOnly);
281 debListParser Parser(&Pkg);
282 if (_error->PendingError() == true)
283 return _error->Error("Problem opening %s",PackageFile.c_str());
284
285 Prog.SubProgress(0,Info("Packages"));
286 ::URI Tmp(URI);
287 if (Gen.SelectFile(PackageFile,Tmp.Host,*this) == false)
288 return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
289
290 // Store the IMS information
291 pkgCache::PkgFileIterator File = Gen.GetCurFile();
292 struct stat St;
293 if (fstat(Pkg.Fd(),&St) != 0)
294 return _error->Errno("fstat","Failed to stat");
295 File->Size = St.st_size;
296 File->mtime = St.st_mtime;
297
298 if (Gen.MergeList(Parser) == false)
299 return _error->Error("Problem with MergeList %s",PackageFile.c_str());
300
301 // Check the release file
302 string ReleaseFile = debReleaseIndex(URI,Dist).MetaIndexFile("Release");
303 if (FileExists(ReleaseFile) == true)
304 {
305 FileFd Rel(ReleaseFile,FileFd::ReadOnly);
306 if (_error->PendingError() == true)
307 return false;
308 Parser.LoadReleaseInfo(File,Rel,Section);
309 }
310
311 return true;
312 }
313 /*}}}*/
314 // PackagesIndex::FindInCache - Find this index /*{{{*/
315 // ---------------------------------------------------------------------
316 /* */
317 pkgCache::PkgFileIterator debPackagesIndex::FindInCache(pkgCache &Cache) const
318 {
319 string FileName = IndexFile("Packages");
320 pkgCache::PkgFileIterator File = Cache.FileBegin();
321 for (; File.end() == false; File++)
322 {
323 if (FileName != File.FileName())
324 continue;
325
326 struct stat St;
327 if (stat(File.FileName(),&St) != 0)
328 return pkgCache::PkgFileIterator(Cache);
329 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
330 return pkgCache::PkgFileIterator(Cache);
331 return File;
332 }
333
334 return File;
335 }
336 /*}}}*/
337
338 // StatusIndex::debStatusIndex - Constructor /*{{{*/
339 // ---------------------------------------------------------------------
340 /* */
341 debStatusIndex::debStatusIndex(string File) : pkgIndexFile(true), File(File)
342 {
343 }
344 /*}}}*/
345 // StatusIndex::Size - Return the size of the index /*{{{*/
346 // ---------------------------------------------------------------------
347 /* */
348 unsigned long debStatusIndex::Size() const
349 {
350 struct stat S;
351 if (stat(File.c_str(),&S) != 0)
352 return 0;
353 return S.st_size;
354 }
355 /*}}}*/
356 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
357 // ---------------------------------------------------------------------
358 /* */
359 bool debStatusIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
360 {
361 FileFd Pkg(File,FileFd::ReadOnly);
362 if (_error->PendingError() == true)
363 return false;
364 debListParser Parser(&Pkg);
365 if (_error->PendingError() == true)
366 return false;
367
368 Prog.SubProgress(0,File);
369 if (Gen.SelectFile(File,string(),*this,pkgCache::Flag::NotSource) == false)
370 return _error->Error("Problem with SelectFile %s",File.c_str());
371
372 // Store the IMS information
373 pkgCache::PkgFileIterator CFile = Gen.GetCurFile();
374 struct stat St;
375 if (fstat(Pkg.Fd(),&St) != 0)
376 return _error->Errno("fstat","Failed to stat");
377 CFile->Size = St.st_size;
378 CFile->mtime = St.st_mtime;
379 CFile->Archive = Gen.WriteUniqString("now");
380
381 if (Gen.MergeList(Parser) == false)
382 return _error->Error("Problem with MergeList %s",File.c_str());
383 return true;
384 }
385 /*}}}*/
386 // StatusIndex::FindInCache - Find this index /*{{{*/
387 // ---------------------------------------------------------------------
388 /* */
389 pkgCache::PkgFileIterator debStatusIndex::FindInCache(pkgCache &Cache) const
390 {
391 pkgCache::PkgFileIterator File = Cache.FileBegin();
392 for (; File.end() == false; File++)
393 {
394 if (this->File != File.FileName())
395 continue;
396
397 struct stat St;
398 if (stat(File.FileName(),&St) != 0)
399 return pkgCache::PkgFileIterator(Cache);
400 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
401 return pkgCache::PkgFileIterator(Cache);
402 return File;
403 }
404 return File;
405 }
406 /*}}}*/
407 // StatusIndex::Exists - Check if the index is available /*{{{*/
408 // ---------------------------------------------------------------------
409 /* */
410 bool debStatusIndex::Exists() const
411 {
412 // Abort if the file does not exist.
413 return true;
414 }
415 /*}}}*/
416
417 // Index File types for Debian /*{{{*/
418 class debIFTypeSrc : public pkgIndexFile::Type
419 {
420 public:
421
422 debIFTypeSrc() {Label = "Debian Source Index";};
423 };
424 class debIFTypePkg : public pkgIndexFile::Type
425 {
426 public:
427
428 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
429 {
430 return new debRecordParser(File.FileName(),*File.Cache());
431 };
432 debIFTypePkg() {Label = "Debian Package Index";};
433 };
434 class debIFTypeStatus : public pkgIndexFile::Type
435 {
436 public:
437
438 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
439 {
440 return new debRecordParser(File.FileName(),*File.Cache());
441 };
442 debIFTypeStatus() {Label = "Debian dpkg status file";};
443 };
444 static debIFTypeSrc _apt_Src;
445 static debIFTypePkg _apt_Pkg;
446 static debIFTypeStatus _apt_Status;
447
448 const pkgIndexFile::Type *debSourcesIndex::GetType() const
449 {
450 return &_apt_Src;
451 }
452 const pkgIndexFile::Type *debPackagesIndex::GetType() const
453 {
454 return &_apt_Pkg;
455 }
456 const pkgIndexFile::Type *debStatusIndex::GetType() const
457 {
458 return &_apt_Status;
459 }
460
461 /*}}}*/