]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcachegen.cc,v 1.14 1998/08/26 04:52:23 jgg Exp $
4 /* ######################################################################
6 Package Cache Generator - Generator for the cache structure.
8 This builds the cache structure from the abstract package list parser.
10 ##################################################################### */
12 // Include Files /*{{{*/
14 #pragma implementation "apt-pkg/pkgcachegen.h"
17 #include <apt-pkg/pkgcachegen.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/version.h>
20 #include <apt-pkg/progress.h>
21 #include <apt-pkg/sourcelist.h>
22 #include <apt-pkg/configuration.h>
23 #include <apt-pkg/deblistparser.h>
31 // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
33 /* We set the diry flag and make sure that is written to the disk */
34 pkgCacheGenerator::pkgCacheGenerator(DynamicMMap
&Map
,OpProgress
&Prog
) :
35 Map(Map
), Cache(Map
), Progress(Prog
)
37 if (_error
->PendingError() == true)
42 Map
.RawAllocate(sizeof(pkgCache::Header
));
43 *Cache
.HeaderP
= pkgCache::Header();
45 Cache
.HeaderP
->Dirty
= true;
46 Map
.Sync(0,sizeof(pkgCache::Header
));
47 Map
.UsePools(*Cache
.HeaderP
->Pools
,sizeof(Cache
.HeaderP
->Pools
)/sizeof(Cache
.HeaderP
->Pools
[0]));
50 // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
51 // ---------------------------------------------------------------------
52 /* We sync the data then unset the dirty flag in two steps so as to
53 advoid a problem during a crash */
54 pkgCacheGenerator::~pkgCacheGenerator()
56 if (_error
->PendingError() == true)
58 if (Map
.Sync() == false)
61 Cache
.HeaderP
->Dirty
= false;
62 Map
.Sync(0,sizeof(pkgCache::Header
));
65 // CacheGenerator::MergeList - Merge the package list /*{{{*/
66 // ---------------------------------------------------------------------
67 /* This provides the generation of the entries in the cache. Each loop
68 goes through a single package record from the underlying parse engine. */
69 bool pkgCacheGenerator::MergeList(ListParser
&List
)
73 while (List
.Step() == true)
75 // Get a pointer to the package structure
76 string PackageName
= List
.Package();
77 pkgCache::PkgIterator Pkg
;
78 if (NewPackage(Pkg
,PackageName
) == false)
79 return _error
->Error("Error occured while processing %s (NewPackage)",PackageName
.c_str());
80 Progress
.Progress(List
.Offset());
82 /* Get a pointer to the version structure. We know the list is sorted
83 so we use that fact in the search. Insertion of new versions is
84 done with correct sorting */
85 string Version
= List
.Version();
86 if (Version
.empty() == true)
88 if (List
.UsePackage(Pkg
,pkgCache::VerIterator(Cache
)) == false)
89 return _error
->Error("Error occured while processing %s (UsePackage1)",PackageName
.c_str());
93 pkgCache::VerIterator Ver
= Pkg
.VersionList();
94 unsigned long *Last
= &Pkg
->VersionList
;
96 for (; Ver
.end() == false; Last
= &Ver
->NextVer
, Ver
++)
98 Res
= pkgVersionCompare(Version
.begin(),Version
.end(),Ver
.VerStr(),
99 Ver
.VerStr() + strlen(Ver
.VerStr()));
104 /* We already have a version for this item, record that we
108 if (List
.UsePackage(Pkg
,Ver
) == false)
109 return _error
->Error("Error occured while processing %s (UsePackage2)",PackageName
.c_str());
111 if (NewFileVer(Ver
,List
) == false)
112 return _error
->Error("Error occured while processing %s (NewFileVer1)",PackageName
.c_str());
118 *Last
= NewVersion(Ver
,Version
,*Last
);
119 Ver
->ParentPkg
= Pkg
.Index();
120 if (List
.NewVersion(Ver
) == false)
121 return _error
->Error("Error occured while processing %s (NewVersion1)",PackageName
.c_str());
123 if (List
.UsePackage(Pkg
,Ver
) == false)
124 return _error
->Error("Error occured while processing %s (UsePackage3)",PackageName
.c_str());
126 if (NewFileVer(Ver
,List
) == false)
127 return _error
->Error("Error occured while processing %s (NewVersion2)",PackageName
.c_str());
133 // CacheGenerator::NewPackage - Add a new package /*{{{*/
134 // ---------------------------------------------------------------------
135 /* This creates a new package structure and adds it to the hash table */
136 bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator
&Pkg
,string Name
)
138 Pkg
= Cache
.FindPkg(Name
);
139 if (Pkg
.end() == false)
143 unsigned long Package
= Map
.Allocate(sizeof(pkgCache::Package
));
147 Pkg
= pkgCache::PkgIterator(Cache
,Cache
.PkgP
+ Package
);
149 // Insert it into the hash table
150 unsigned long Hash
= Cache
.Hash(Name
);
151 Pkg
->NextPackage
= Cache
.HeaderP
->HashTable
[Hash
];
152 Cache
.HeaderP
->HashTable
[Hash
] = Package
;
154 // Set the name and the ID
155 Pkg
->Name
= Map
.WriteString(Name
);
158 Pkg
->ID
= Cache
.HeaderP
->PackageCount
++;
163 // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
164 // ---------------------------------------------------------------------
166 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator
&Ver
,
170 unsigned long VerFile
= Map
.Allocate(sizeof(pkgCache::VerFile
));
174 pkgCache::VerFileIterator
VF(Cache
,Cache
.VerFileP
+ VerFile
);
175 VF
->File
= CurrentFile
- Cache
.PkgFileP
;
176 VF
->NextFile
= Ver
->FileList
;
177 Ver
->FileList
= VF
.Index();
178 VF
->Offset
= List
.Offset();
179 VF
->Size
= List
.Size();
180 if (Cache
.HeaderP
->MaxVerFileSize
< VF
->Size
)
181 Cache
.HeaderP
->MaxVerFileSize
= VF
->Size
;
185 // CacheGenerator::NewVersion - Create a new Version /*{{{*/
186 // ---------------------------------------------------------------------
187 /* This puts a version structure in the linked list */
188 unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator
&Ver
,
193 unsigned long Version
= Map
.Allocate(sizeof(pkgCache::Version
));
198 Ver
= pkgCache::VerIterator(Cache
,Cache
.VerP
+ Version
);
200 Ver
->ID
= Cache
.HeaderP
->VersionCount
++;
201 Ver
->VerStr
= Map
.WriteString(VerStr
);
202 if (Ver
->VerStr
== 0)
208 // ListParser::NewDepends - Create a dependency element /*{{{*/
209 // ---------------------------------------------------------------------
210 /* This creates a dependency element in the tree. It is linked to the
211 version and to the package that it is pointing to. */
212 bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver
,
218 pkgCache
&Cache
= Owner
->Cache
;
221 unsigned long Dependency
= Owner
->Map
.Allocate(sizeof(pkgCache::Dependency
));
226 pkgCache::DepIterator
Dep(Cache
,Cache
.DepP
+ Dependency
);
227 Dep
->ParentVer
= Ver
.Index();
230 Dep
->ID
= Cache
.HeaderP
->DependsCount
++;
232 // Locate the target package
233 pkgCache::PkgIterator Pkg
;
234 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
237 // Probe the reverse dependency list for a version string that matches
238 if (Version
.empty() == false)
240 for (pkgCache::DepIterator I
= Pkg
.RevDependsList(); I
.end() == false; I
++)
241 if (I
->Version
!= 0 && I
.TargetVer() == Version
)
242 Dep
->Version
= I
->Version
;
243 if (Dep
->Version
== 0)
244 if ((Dep
->Version
= WriteString(Version
)) == 0)
248 // Link it to the package
249 Dep
->Package
= Pkg
.Index();
250 Dep
->NextRevDepends
= Pkg
->RevDepends
;
251 Pkg
->RevDepends
= Dep
.Index();
253 // Link it to the version (at the end of the list)
254 unsigned long *Last
= &Ver
->DependsList
;
255 for (pkgCache::DepIterator D
= Ver
.DependsList(); D
.end() == false; D
++)
256 Last
= &D
->NextDepends
;
257 Dep
->NextDepends
= *Last
;
263 // ListParser::NewProvides - Create a Provides element /*{{{*/
264 // ---------------------------------------------------------------------
266 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver
,
270 pkgCache
&Cache
= Owner
->Cache
;
272 // We do not add self referencing provides
273 if (Ver
.ParentPkg().Name() == PackageName
)
277 unsigned long Provides
= Owner
->Map
.Allocate(sizeof(pkgCache::Provides
));
282 pkgCache::PrvIterator
Prv(Cache
,Cache
.ProvideP
+ Provides
,Cache
.PkgP
);
283 Prv
->Version
= Ver
.Index();
284 Prv
->NextPkgProv
= Ver
->ProvidesList
;
285 Ver
->ProvidesList
= Prv
.Index();
286 if (Version
.empty() == false && (Prv
->Version
= WriteString(Version
)) == 0)
289 // Locate the target package
290 pkgCache::PkgIterator Pkg
;
291 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
294 // Link it to the package
295 Prv
->ParentPkg
= Pkg
.Index();
296 Prv
->NextProvides
= Pkg
->ProvidesList
;
297 Pkg
->ProvidesList
= Prv
.Index();
302 // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
303 // ---------------------------------------------------------------------
304 /* This is used to select which file is to be associated with all newly
306 bool pkgCacheGenerator::SelectFile(string File
,unsigned long Flags
)
309 if (stat(File
.c_str(),&Buf
) == -1)
310 return _error
->Errno("stat","Couldn't stat ",File
.c_str());
312 // Get some space for the structure
313 CurrentFile
= Cache
.PkgFileP
+ Map
.Allocate(sizeof(*CurrentFile
));
314 if (CurrentFile
== Cache
.PkgFileP
)
318 CurrentFile
->FileName
= Map
.WriteString(File
);
319 CurrentFile
->Size
= Buf
.st_size
;
320 CurrentFile
->mtime
= Buf
.st_mtime
;
321 CurrentFile
->NextFile
= Cache
.HeaderP
->FileList
;
322 CurrentFile
->Flags
= Flags
;
324 Cache
.HeaderP
->FileList
= CurrentFile
- Cache
.PkgFileP
;
325 Cache
.HeaderP
->PackageFileCount
++;
327 if (CurrentFile
->FileName
== 0)
330 Progress
.SubProgress(Buf
.st_size
);
334 // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
335 // ---------------------------------------------------------------------
336 /* This is used to create handles to strings. Given the same text it
337 always returns the same number */
338 unsigned long pkgCacheGenerator::WriteUniqString(const char *S
,
341 // Search for an insertion point
342 pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.HeaderP
->StringList
;
344 unsigned long *Last
= &Cache
.HeaderP
->StringList
;
345 for (; I
!= Cache
.StringItemP
; Last
= &I
->NextItem
,
346 I
= Cache
.StringItemP
+ I
->NextItem
)
348 Res
= stringcmp(S
,S
+Size
,Cache
.StrP
+ I
->String
);
358 unsigned long Item
= Map
.Allocate(sizeof(pkgCache::StringItem
));
362 // Fill in the structure
363 pkgCache::StringItem
*ItemP
= Cache
.StringItemP
+ Item
;
364 ItemP
->NextItem
= I
- Cache
.StringItemP
;
366 ItemP
->String
= Map
.WriteString(S
,Size
);
367 if (ItemP
->String
== 0)
370 return ItemP
->String
;
374 // SrcCacheCheck - Check if the source package cache is uptodate /*{{{*/
375 // ---------------------------------------------------------------------
376 /* The source cache is checked against the source list and the files
377 on disk, any difference results in a false. */
378 bool pkgSrcCacheCheck(pkgSourceList
&List
)
380 if (_error
->PendingError() == true)
383 // Open the source package cache
384 string CacheFile
= _config
->FindDir("Dir::Cache::srcpkgcache");
385 string ListDir
= _config
->FindDir("Dir::State::lists");
386 if (FileExists(CacheFile
) == false)
389 FileFd
CacheF(CacheFile
,FileFd::ReadOnly
);
390 if (_error
->PendingError() == true)
396 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
397 if (_error
->PendingError() == true)
404 if (_error
->PendingError() == true)
410 // They are certianly out of sync
411 if (Cache
.Head().PackageFileCount
!= List
.size())
414 for (pkgCache::PkgFileIterator
F(Cache
); F
.end() == false; F
++)
416 // Search for a match in the source list
418 for (pkgSourceList::const_iterator I
= List
.begin();
419 I
!= List
.end(); I
++)
421 string File
= ListDir
+ URItoFileName(I
->PackagesURI());
422 if (F
.FileName() == File
)
429 // Check if the file matches what was cached
438 // PkgCacheCheck - Check if the package cache is uptodate /*{{{*/
439 // ---------------------------------------------------------------------
440 /* This does a simple check of all files used to compose the cache */
441 bool pkgPkgCacheCheck(string CacheFile
)
443 if (_error
->PendingError() == true)
446 // Open the source package cache
447 if (FileExists(CacheFile
) == false)
450 FileFd
CacheF(CacheFile
,FileFd::ReadOnly
);
451 if (_error
->PendingError() == true)
457 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
458 if (_error
->PendingError() == true)
465 if (_error
->PendingError() == true)
472 for (pkgCache::PkgFileIterator
F(Cache
); F
.end() == false; F
++)
473 if (F
.IsOk() == false)
478 // AddSourcesSize - Add the size of the status files /*{{{*/
479 // ---------------------------------------------------------------------
480 /* This adds the size of all the status files to the size counter */
481 static bool pkgAddSourcesSize(unsigned long &TotalSize
)
483 // Grab the file names
484 string xstatus
= _config
->FindDir("Dir::State::xstatus");
485 string userstatus
= _config
->FindDir("Dir::State::userstatus");
486 string status
= _config
->FindDir("Dir::State::status");
490 if (stat(xstatus
.c_str(),&Buf
) == 0)
491 TotalSize
+= Buf
.st_size
;
492 if (stat(userstatus
.c_str(),&Buf
) == 0)
493 TotalSize
+= Buf
.st_size
;
494 if (stat(status
.c_str(),&Buf
) != 0)
495 return _error
->Errno("stat","Couldn't stat the status file %s",status
.c_str());
496 TotalSize
+= Buf
.st_size
;
501 // MergeStatus - Add the status files to the cache /*{{{*/
502 // ---------------------------------------------------------------------
503 /* This adds the status files to the map */
504 static bool pkgMergeStatus(OpProgress
&Progress
,pkgCacheGenerator
&Gen
,
505 unsigned long &CurrentSize
,unsigned long TotalSize
)
507 // Grab the file names
509 Status
[0] = _config
->FindDir("Dir::State::xstatus");
510 Status
[1]= _config
->FindDir("Dir::State::userstatus");
511 Status
[2] = _config
->FindDir("Dir::State::status");
513 for (int I
= 0; I
!= 3; I
++)
515 // Check if the file exists and it is not the primary status file.
516 string File
= Status
[I
];
517 if (I
!= 2 && FileExists(File
) == false)
520 FileFd
Pkg(File
,FileFd::ReadOnly
);
521 debListParser
Parser(Pkg
);
522 Progress
.OverallProgress(CurrentSize
,TotalSize
,Pkg
.Size(),"Reading Package Lists");
523 if (_error
->PendingError() == true)
524 return _error
->Error("Problem opening %s",File
.c_str());
525 CurrentSize
+= Pkg
.Size();
527 Progress
.SubProgress(0,"Local Package State - " + flNotDir(File
));
528 if (Gen
.SelectFile(File
,pkgCache::Flag::NotSource
) == false)
529 return _error
->Error("Problem with SelectFile %s",File
.c_str());
531 if (Gen
.MergeList(Parser
) == false)
532 return _error
->Error("Problem with MergeList %s",File
.c_str());
533 Progress
.Progress(Pkg
.Size());
539 // MakeStatusCache - Generates a cache that includes the status files /*{{{*/
540 // ---------------------------------------------------------------------
541 /* This copies the package source cache and then merges the status and
542 xstatus files into it. */
543 bool pkgMakeStatusCache(pkgSourceList
&List
,OpProgress
&Progress
)
545 Progress
.OverallProgress(0,1,1,"Reading Package Lists");
547 string CacheFile
= _config
->FindDir("Dir::Cache::pkgcache");
548 bool SrcOk
= pkgSrcCacheCheck(List
);
549 bool PkgOk
= pkgPkgCacheCheck(CacheFile
);
551 // Rebuild the source and package caches
554 string SCacheFile
= _config
->FindDir("Dir::Cache::srcpkgcache");
555 string ListDir
= _config
->FindDir("Dir::State::lists");
557 FileFd
SCacheF(SCacheFile
,FileFd::WriteEmpty
);
558 FileFd
CacheF(CacheFile
,FileFd::WriteEmpty
);
559 DynamicMMap
Map(CacheF
,MMap::Public
);
560 if (_error
->PendingError() == true)
563 pkgCacheGenerator
Gen(Map
,Progress
);
565 // Prepare the progress indicator
566 unsigned long TotalSize
= 0;
568 for (pkgSourceList::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
570 string File
= ListDir
+ URItoFileName(I
->PackagesURI());
571 if (stat(File
.c_str(),&Buf
) != 0)
572 return _error
->Errno("stat","Couldn't stat source package list %s",File
.c_str());
573 TotalSize
+= Buf
.st_size
;
576 if (pkgAddSourcesSize(TotalSize
) == false)
579 // Generate the pkg source cache
580 unsigned long CurrentSize
= 0;
581 for (pkgSourceList::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
583 string File
= ListDir
+ URItoFileName(I
->PackagesURI());
584 FileFd
Pkg(File
,FileFd::ReadOnly
);
585 debListParser
Parser(Pkg
);
586 Progress
.OverallProgress(CurrentSize
,TotalSize
,Pkg
.Size(),"Reading Package Lists");
587 if (_error
->PendingError() == true)
588 return _error
->Error("Problem opening %s",File
.c_str());
589 CurrentSize
+= Pkg
.Size();
591 Progress
.SubProgress(0,I
->PackagesInfo());
592 if (Gen
.SelectFile(File
) == false)
593 return _error
->Error("Problem with SelectFile %s",File
.c_str());
595 if (Gen
.MergeList(Parser
) == false)
596 return _error
->Error("Problem with MergeList %s",File
.c_str());
599 // Write the src cache
600 Gen
.GetCache().HeaderP
->Dirty
= false;
601 if (SCacheF
.Write(Map
.Data(),Map
.Size()) == false)
602 return _error
->Error("IO Error saving source cache");
603 Gen
.GetCache().HeaderP
->Dirty
= true;
605 // Merge in the source caches
606 return pkgMergeStatus(Progress
,Gen
,CurrentSize
,TotalSize
);
611 Progress
.OverallProgress(1,1,1,"Reading Package Lists");
615 // We use the source cache to generate the package cache
616 string SCacheFile
= _config
->FindDir("Dir::Cache::srcpkgcache");
618 FileFd
SCacheF(SCacheFile
,FileFd::ReadOnly
);
619 FileFd
CacheF(CacheFile
,FileFd::WriteEmpty
);
620 DynamicMMap
Map(CacheF
,MMap::Public
);
621 if (_error
->PendingError() == true)
624 // Preload the map with the source cache
625 if (SCacheF
.Read((unsigned char *)Map
.Data() + Map
.RawAllocate(SCacheF
.Size()),
626 SCacheF
.Size()) == false)
629 pkgCacheGenerator
Gen(Map
,Progress
);
631 // Compute the progress
632 unsigned long TotalSize
= 0;
633 if (pkgAddSourcesSize(TotalSize
) == false)
636 unsigned long CurrentSize
= 0;
637 return pkgMergeStatus(Progress
,Gen
,CurrentSize
,TotalSize
);