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