]> git.saurik.com Git - apt.git/blob - apt-pkg/pkgcache.cc
* fix a off-by-one error when showing the description
[apt.git] / apt-pkg / pkgcache.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: pkgcache.cc,v 1.37 2003/02/10 01:40:58 doogie Exp $
4 /* ######################################################################
5
6 Package Cache - Accessor code for the cache
7
8 Please see doc/apt-pkg/cache.sgml for a more detailed description of
9 this format. Also be sure to keep that file up-to-date!!
10
11 This is the general utility functions for cache managment. They provide
12 a complete set of accessor functions for the cache. The cacheiterators
13 header contains the STL-like iterators that can be used to easially
14 navigate the cache as well as seemlessly dereference the mmap'd
15 indexes. Use these always.
16
17 The main class provides for ways to get package indexes and some
18 general lookup functions to start the iterators.
19
20 ##################################################################### */
21 /*}}}*/
22 // Include Files /*{{{*/
23 #ifdef __GNUG__
24 #pragma implementation "apt-pkg/pkgcache.h"
25 #pragma implementation "apt-pkg/cacheiterators.h"
26 #endif
27
28 #include <apt-pkg/pkgcache.h>
29 #include <apt-pkg/version.h>
30 #include <apt-pkg/error.h>
31 #include <apt-pkg/strutl.h>
32 #include <apt-pkg/configuration.h>
33
34 #include <apti18n.h>
35
36 #include <string>
37 #include <sys/stat.h>
38 #include <unistd.h>
39
40 #include <ctype.h>
41 #include <system.h>
42 /*}}}*/
43
44 using std::string;
45
46 // Cache::Header::Header - Constructor /*{{{*/
47 // ---------------------------------------------------------------------
48 /* Simply initialize the header */
49 pkgCache::Header::Header()
50 {
51 Signature = 0x98FE76DC;
52
53 /* Whenever the structures change the major version should be bumped,
54 whenever the generator changes the minor version should be bumped. */
55 MajorVersion = 5;
56 MinorVersion = 0;
57 Dirty = false;
58
59 HeaderSz = sizeof(pkgCache::Header);
60 PackageSz = sizeof(pkgCache::Package);
61 PackageFileSz = sizeof(pkgCache::PackageFile);
62 VersionSz = sizeof(pkgCache::Version);
63 DescriptionSz = sizeof(pkgCache::Description);
64 DependencySz = sizeof(pkgCache::Dependency);
65 ProvidesSz = sizeof(pkgCache::Provides);
66 VerFileSz = sizeof(pkgCache::VerFile);
67 DescFileSz = sizeof(pkgCache::DescFile);
68
69 PackageCount = 0;
70 VersionCount = 0;
71 DescriptionCount = 0;
72 DependsCount = 0;
73 PackageFileCount = 0;
74 VerFileCount = 0;
75 DescFileCount = 0;
76 ProvidesCount = 0;
77 MaxVerFileSize = 0;
78 MaxDescFileSize = 0;
79
80 FileList = 0;
81 StringList = 0;
82 VerSysName = 0;
83 Architecture = 0;
84 memset(HashTable,0,sizeof(HashTable));
85 memset(Pools,0,sizeof(Pools));
86 }
87 /*}}}*/
88 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
89 // ---------------------------------------------------------------------
90 /* */
91 bool pkgCache::Header::CheckSizes(Header &Against) const
92 {
93 if (HeaderSz == Against.HeaderSz &&
94 PackageSz == Against.PackageSz &&
95 PackageFileSz == Against.PackageFileSz &&
96 VersionSz == Against.VersionSz &&
97 DescriptionSz == Against.DescriptionSz &&
98 DependencySz == Against.DependencySz &&
99 VerFileSz == Against.VerFileSz &&
100 DescFileSz == Against.DescFileSz &&
101 ProvidesSz == Against.ProvidesSz)
102 return true;
103 return false;
104 }
105 /*}}}*/
106
107 // Cache::pkgCache - Constructor /*{{{*/
108 // ---------------------------------------------------------------------
109 /* */
110 pkgCache::pkgCache(MMap *Map, bool DoMap) : Map(*Map)
111 {
112 if (DoMap == true)
113 ReMap();
114 }
115 /*}}}*/
116 // Cache::ReMap - Reopen the cache file /*{{{*/
117 // ---------------------------------------------------------------------
118 /* If the file is already closed then this will open it open it. */
119 bool pkgCache::ReMap()
120 {
121 // Apply the typecasts.
122 HeaderP = (Header *)Map.Data();
123 PkgP = (Package *)Map.Data();
124 VerFileP = (VerFile *)Map.Data();
125 DescFileP = (DescFile *)Map.Data();
126 PkgFileP = (PackageFile *)Map.Data();
127 VerP = (Version *)Map.Data();
128 DescP = (Description *)Map.Data();
129 ProvideP = (Provides *)Map.Data();
130 DepP = (Dependency *)Map.Data();
131 StringItemP = (StringItem *)Map.Data();
132 StrP = (char *)Map.Data();
133
134 if (Map.Size() == 0 || HeaderP == 0)
135 return _error->Error(_("Empty package cache"));
136
137 // Check the header
138 Header DefHeader;
139 if (HeaderP->Signature != DefHeader.Signature ||
140 HeaderP->Dirty == true)
141 return _error->Error(_("The package cache file is corrupted"));
142
143 if (HeaderP->MajorVersion != DefHeader.MajorVersion ||
144 HeaderP->MinorVersion != DefHeader.MinorVersion ||
145 HeaderP->CheckSizes(DefHeader) == false)
146 return _error->Error(_("The package cache file is an incompatible version"));
147
148 // Locate our VS..
149 if (HeaderP->VerSysName == 0 ||
150 (VS = pkgVersioningSystem::GetVS(StrP + HeaderP->VerSysName)) == 0)
151 return _error->Error(_("This APT does not support the versioning system '%s'"),StrP + HeaderP->VerSysName);
152
153 // Chcek the arhcitecture
154 if (HeaderP->Architecture == 0 ||
155 _config->Find("APT::Architecture") != StrP + HeaderP->Architecture)
156 return _error->Error(_("The package cache was built for a different architecture"));
157 return true;
158 }
159 /*}}}*/
160 // Cache::Hash - Hash a string /*{{{*/
161 // ---------------------------------------------------------------------
162 /* This is used to generate the hash entries for the HashTable. With my
163 package list from bo this function gets 94% table usage on a 512 item
164 table (480 used items) */
165 unsigned long pkgCache::sHash(string Str) const
166 {
167 unsigned long Hash = 0;
168 for (string::const_iterator I = Str.begin(); I != Str.end(); I++)
169 Hash = 5*Hash + tolower(*I);
170 return Hash % _count(HeaderP->HashTable);
171 }
172
173 unsigned long pkgCache::sHash(const char *Str) const
174 {
175 unsigned long Hash = 0;
176 for (const char *I = Str; *I != 0; I++)
177 Hash = 5*Hash + tolower(*I);
178 return Hash % _count(HeaderP->HashTable);
179 }
180
181 /*}}}*/
182 // Cache::FindPkg - Locate a package by name /*{{{*/
183 // ---------------------------------------------------------------------
184 /* Returns 0 on error, pointer to the package otherwise */
185 pkgCache::PkgIterator pkgCache::FindPkg(string Name)
186 {
187 // Look at the hash bucket
188 Package *Pkg = PkgP + HeaderP->HashTable[Hash(Name)];
189 for (; Pkg != PkgP; Pkg = PkgP + Pkg->NextPackage)
190 {
191 if (Pkg->Name != 0 && StrP[Pkg->Name] == Name[0] &&
192 stringcasecmp(Name,StrP + Pkg->Name) == 0)
193 return PkgIterator(*this,Pkg);
194 }
195 return PkgIterator(*this,0);
196 }
197 /*}}}*/
198 // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This returns a string representation of the dependency compare
201 type in the weird debian style.. */
202 const char *pkgCache::CompTypeDeb(unsigned char Comp)
203 {
204 const char *Ops[] = {"","<=",">=","<<",">>","=","!="};
205 if ((unsigned)(Comp & 0xF) < 7)
206 return Ops[Comp & 0xF];
207 return "";
208 }
209 /*}}}*/
210 // Cache::CompType - Return a string describing the compare type /*{{{*/
211 // ---------------------------------------------------------------------
212 /* This returns a string representation of the dependency compare
213 type */
214 const char *pkgCache::CompType(unsigned char Comp)
215 {
216 const char *Ops[] = {"","<=",">=","<",">","=","!="};
217 if ((unsigned)(Comp & 0xF) < 7)
218 return Ops[Comp & 0xF];
219 return "";
220 }
221 /*}}}*/
222 // Cache::DepType - Return a string describing the dep type /*{{{*/
223 // ---------------------------------------------------------------------
224 /* */
225 const char *pkgCache::DepType(unsigned char Type)
226 {
227 const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
228 _("Recommends"),_("Conflicts"),_("Replaces"),
229 _("Obsoletes")};
230 if (Type < 8)
231 return Types[Type];
232 return "";
233 }
234 /*}}}*/
235 // Cache::Priority - Convert a priority value to a string /*{{{*/
236 // ---------------------------------------------------------------------
237 /* */
238 const char *pkgCache::Priority(unsigned char Prio)
239 {
240 const char *Mapping[] = {0,_("important"),_("required"),_("standard"),
241 _("optional"),_("extra")};
242 if (Prio < _count(Mapping))
243 return Mapping[Prio];
244 return 0;
245 }
246 /*}}}*/
247 // Bases for iterator classes /*{{{*/
248 void pkgCache::VerIterator::_dummy() {}
249 void pkgCache::DepIterator::_dummy() {}
250 void pkgCache::PrvIterator::_dummy() {}
251 void pkgCache::DescIterator::_dummy() {}
252 /*}}}*/
253 // PkgIterator::operator ++ - Postfix incr /*{{{*/
254 // ---------------------------------------------------------------------
255 /* This will advance to the next logical package in the hash table. */
256 void pkgCache::PkgIterator::operator ++(int)
257 {
258 // Follow the current links
259 if (Pkg != Owner->PkgP)
260 Pkg = Owner->PkgP + Pkg->NextPackage;
261
262 // Follow the hash table
263 while (Pkg == Owner->PkgP && (HashIndex+1) < (signed)_count(Owner->HeaderP->HashTable))
264 {
265 HashIndex++;
266 Pkg = Owner->PkgP + Owner->HeaderP->HashTable[HashIndex];
267 }
268 };
269 /*}}}*/
270 // PkgIterator::State - Check the State of the package /*{{{*/
271 // ---------------------------------------------------------------------
272 /* By this we mean if it is either cleanly installed or cleanly removed. */
273 pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const
274 {
275 if (Pkg->InstState == pkgCache::State::ReInstReq ||
276 Pkg->InstState == pkgCache::State::HoldReInstReq)
277 return NeedsUnpack;
278
279 if (Pkg->CurrentState == pkgCache::State::UnPacked ||
280 Pkg->CurrentState == pkgCache::State::HalfConfigured)
281 return NeedsConfigure;
282
283 if (Pkg->CurrentState == pkgCache::State::HalfInstalled ||
284 Pkg->InstState != pkgCache::State::Ok)
285 return NeedsUnpack;
286
287 return NeedsNothing;
288 }
289 /*}}}*/
290 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
291 // ---------------------------------------------------------------------
292 /* Currently critical deps are defined as depends, predepends and
293 conflicts. */
294 bool pkgCache::DepIterator::IsCritical()
295 {
296 if (Dep->Type == pkgCache::Dep::Conflicts ||
297 Dep->Type == pkgCache::Dep::Obsoletes ||
298 Dep->Type == pkgCache::Dep::Depends ||
299 Dep->Type == pkgCache::Dep::PreDepends)
300 return true;
301 return false;
302 }
303 /*}}}*/
304 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
305 // ---------------------------------------------------------------------
306 /* This intellegently looks at dep target packages and tries to figure
307 out which package should be used. This is needed to nicely handle
308 provide mapping. If the target package has no other providing packages
309 then it returned. Otherwise the providing list is looked at to
310 see if there is one one unique providing package if so it is returned.
311 Otherwise true is returned and the target package is set. The return
312 result indicates whether the node should be expandable
313
314 In Conjunction with the DepCache the value of Result may not be
315 super-good since the policy may have made it uninstallable. Using
316 AllTargets is better in this case. */
317 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result)
318 {
319 Result = TargetPkg();
320
321 // No provides at all
322 if (Result->ProvidesList == 0)
323 return false;
324
325 // There is the Base package and the providing ones which is at least 2
326 if (Result->VersionList != 0)
327 return true;
328
329 /* We have to skip over indirect provisions of the package that
330 owns the dependency. For instance, if libc5-dev depends on the
331 virtual package libc-dev which is provided by libc5-dev and libc6-dev
332 we must ignore libc5-dev when considering the provides list. */
333 PrvIterator PStart = Result.ProvidesList();
334 for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++);
335
336 // Nothing but indirect self provides
337 if (PStart.end() == true)
338 return false;
339
340 // Check for single packages in the provides list
341 PrvIterator P = PStart;
342 for (; P.end() != true; P++)
343 {
344 // Skip over self provides
345 if (P.OwnerPkg() == ParentPkg())
346 continue;
347 if (PStart.OwnerPkg() != P.OwnerPkg())
348 break;
349 }
350
351 Result = PStart.OwnerPkg();
352
353 // Check for non dups
354 if (P.end() != true)
355 return true;
356
357 return false;
358 }
359 /*}}}*/
360 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
361 // ---------------------------------------------------------------------
362 /* This is a more useful version of TargetPkg() that follows versioned
363 provides. It includes every possible package-version that could satisfy
364 the dependency. The last item in the list has a 0. The resulting pointer
365 must be delete [] 'd */
366 pkgCache::Version **pkgCache::DepIterator::AllTargets()
367 {
368 Version **Res = 0;
369 unsigned long Size =0;
370 while (1)
371 {
372 Version **End = Res;
373 PkgIterator DPkg = TargetPkg();
374
375 // Walk along the actual package providing versions
376 for (VerIterator I = DPkg.VersionList(); I.end() == false; I++)
377 {
378 if (Owner->VS->CheckDep(I.VerStr(),Dep->CompareOp,TargetVer()) == false)
379 continue;
380
381 if ((Dep->Type == pkgCache::Dep::Conflicts ||
382 Dep->Type == pkgCache::Dep::Obsoletes) &&
383 ParentPkg() == I.ParentPkg())
384 continue;
385
386 Size++;
387 if (Res != 0)
388 *End++ = I;
389 }
390
391 // Follow all provides
392 for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++)
393 {
394 if (Owner->VS->CheckDep(I.ProvideVersion(),Dep->CompareOp,TargetVer()) == false)
395 continue;
396
397 if ((Dep->Type == pkgCache::Dep::Conflicts ||
398 Dep->Type == pkgCache::Dep::Obsoletes) &&
399 ParentPkg() == I.OwnerPkg())
400 continue;
401
402 Size++;
403 if (Res != 0)
404 *End++ = I.OwnerVer();
405 }
406
407 // Do it again and write it into the array
408 if (Res == 0)
409 {
410 Res = new Version *[Size+1];
411 Size = 0;
412 }
413 else
414 {
415 *End = 0;
416 break;
417 }
418 }
419
420 return Res;
421 }
422 /*}}}*/
423 // DepIterator::GlobOr - Compute an OR group /*{{{*/
424 // ---------------------------------------------------------------------
425 /* This Takes an iterator, iterates past the current dependency grouping
426 and returns Start and End so that so End is the final element
427 in the group, if End == Start then D is End++ and End is the
428 dependency D was pointing to. Use in loops to iterate sensibly. */
429 void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End)
430 {
431 // Compute a single dependency element (glob or)
432 Start = *this;
433 End = *this;
434 for (bool LastOR = true; end() == false && LastOR == true;)
435 {
436 LastOR = (Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
437 (*this)++;
438 if (LastOR == true)
439 End = (*this);
440 }
441 }
442 /*}}}*/
443 // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
444 // ---------------------------------------------------------------------
445 /* This just looks over the version list to see if B is listed before A. In
446 most cases this will return in under 4 checks, ver lists are short. */
447 int pkgCache::VerIterator::CompareVer(const VerIterator &B) const
448 {
449 // Check if they are equal
450 if (*this == B)
451 return 0;
452 if (end() == true)
453 return -1;
454 if (B.end() == true)
455 return 1;
456
457 /* Start at A and look for B. If B is found then A > B otherwise
458 B was before A so A < B */
459 VerIterator I = *this;
460 for (;I.end() == false; I++)
461 if (I == B)
462 return 1;
463 return -1;
464 }
465 /*}}}*/
466 // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
467 // ---------------------------------------------------------------------
468 /* */
469 bool pkgCache::VerIterator::Downloadable() const
470 {
471 VerFileIterator Files = FileList();
472 for (; Files.end() == false; Files++)
473 if ((Files.File()->Flags & pkgCache::Flag::NotSource) != pkgCache::Flag::NotSource)
474 return true;
475 return false;
476 }
477 /*}}}*/
478 // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/
479 // ---------------------------------------------------------------------
480 /* This checks to see if any of the versions files are not NotAutomatic.
481 True if this version is selectable for automatic installation. */
482 bool pkgCache::VerIterator::Automatic() const
483 {
484 VerFileIterator Files = FileList();
485 for (; Files.end() == false; Files++)
486 if ((Files.File()->Flags & pkgCache::Flag::NotAutomatic) != pkgCache::Flag::NotAutomatic)
487 return true;
488 return false;
489 }
490 /*}}}*/
491 // VerIterator::NewestFile - Return the newest file version relation /*{{{*/
492 // ---------------------------------------------------------------------
493 /* This looks at the version numbers associated with all of the sources
494 this version is in and returns the highest.*/
495 pkgCache::VerFileIterator pkgCache::VerIterator::NewestFile() const
496 {
497 VerFileIterator Files = FileList();
498 VerFileIterator Highest = Files;
499 for (; Files.end() == false; Files++)
500 {
501 if (Owner->VS->CmpReleaseVer(Files.File().Version(),Highest.File().Version()) > 0)
502 Highest = Files;
503 }
504
505 return Highest;
506 }
507 /*}}}*/
508 // VerIterator::RelStr - Release description string /*{{{*/
509 // ---------------------------------------------------------------------
510 /* This describes the version from a release-centric manner. The output is a
511 list of Label:Version/Archive */
512 string pkgCache::VerIterator::RelStr()
513 {
514 bool First = true;
515 string Res;
516 for (pkgCache::VerFileIterator I = this->FileList(); I.end() == false; I++)
517 {
518 // Do not print 'not source' entries'
519 pkgCache::PkgFileIterator File = I.File();
520 if ((File->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
521 continue;
522
523 // See if we have already printed this out..
524 bool Seen = false;
525 for (pkgCache::VerFileIterator J = this->FileList(); I != J; J++)
526 {
527 pkgCache::PkgFileIterator File2 = J.File();
528 if (File2->Label == 0 || File->Label == 0)
529 continue;
530
531 if (strcmp(File.Label(),File2.Label()) != 0)
532 continue;
533
534 if (File2->Version == File->Version)
535 {
536 Seen = true;
537 break;
538 }
539 if (File2->Version == 0 || File->Version == 0)
540 break;
541 if (strcmp(File.Version(),File2.Version()) == 0)
542 Seen = true;
543 }
544
545 if (Seen == true)
546 continue;
547
548 if (First == false)
549 Res += ", ";
550 else
551 First = false;
552
553 if (File->Label != 0)
554 Res = Res + File.Label() + ':';
555
556 if (File->Archive != 0)
557 {
558 if (File->Version == 0)
559 Res += File.Archive();
560 else
561 Res = Res + File.Version() + '/' + File.Archive();
562 }
563 else
564 {
565 // No release file, print the host name that this came from
566 if (File->Site == 0 || File.Site()[0] == 0)
567 Res += "localhost";
568 else
569 Res += File.Site();
570 }
571 }
572 return Res;
573 }
574 /*}}}*/
575 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
576 // ---------------------------------------------------------------------
577 /* This stats the file and compares its stats with the ones that were
578 stored during generation. Date checks should probably also be
579 included here. */
580 bool pkgCache::PkgFileIterator::IsOk()
581 {
582 struct stat Buf;
583 if (stat(FileName(),&Buf) != 0)
584 return false;
585
586 if (Buf.st_size != (signed)File->Size || Buf.st_mtime != File->mtime)
587 return false;
588
589 return true;
590 }
591 /*}}}*/
592 // PkgFileIterator::RelStr - Return the release string /*{{{*/
593 // ---------------------------------------------------------------------
594 /* */
595 string pkgCache::PkgFileIterator::RelStr()
596 {
597 string Res;
598 if (Version() != 0)
599 Res = Res + (Res.empty() == true?"v=":",v=") + Version();
600 if (Origin() != 0)
601 Res = Res + (Res.empty() == true?"o=":",o=") + Origin();
602 if (Archive() != 0)
603 Res = Res + (Res.empty() == true?"a=":",a=") + Archive();
604 if (Label() != 0)
605 Res = Res + (Res.empty() == true?"l=":",l=") + Label();
606 if (Component() != 0)
607 Res = Res + (Res.empty() == true?"c=":",c=") + Component();
608 return Res;
609 }
610 /*}}}*/