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