]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcachegen.cc,v 1.27 1999/01/27 02:48:52 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>
24 #include <apt-pkg/strutl.h>
30 // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
31 // ---------------------------------------------------------------------
32 /* We set the diry flag and make sure that is written to the disk */
33 pkgCacheGenerator::pkgCacheGenerator(DynamicMMap
&Map
,OpProgress
&Prog
) :
34 Map(Map
), Cache(Map
), Progress(Prog
)
36 if (_error
->PendingError() == true)
41 Map
.RawAllocate(sizeof(pkgCache::Header
));
42 *Cache
.HeaderP
= pkgCache::Header();
44 Cache
.HeaderP
->Dirty
= true;
45 Map
.Sync(0,sizeof(pkgCache::Header
));
46 Map
.UsePools(*Cache
.HeaderP
->Pools
,sizeof(Cache
.HeaderP
->Pools
)/sizeof(Cache
.HeaderP
->Pools
[0]));
49 // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
50 // ---------------------------------------------------------------------
51 /* We sync the data then unset the dirty flag in two steps so as to
52 advoid a problem during a crash */
53 pkgCacheGenerator::~pkgCacheGenerator()
55 if (_error
->PendingError() == true)
57 if (Map
.Sync() == false)
60 Cache
.HeaderP
->Dirty
= false;
61 Map
.Sync(0,sizeof(pkgCache::Header
));
64 // CacheGenerator::MergeList - Merge the package list /*{{{*/
65 // ---------------------------------------------------------------------
66 /* This provides the generation of the entries in the cache. Each loop
67 goes through a single package record from the underlying parse engine. */
68 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());
81 if (Counter
% 100 == 0)
82 Progress
.Progress(List
.Offset());
84 /* Get a pointer to the version structure. We know the list is sorted
85 so we use that fact in the search. Insertion of new versions is
86 done with correct sorting */
87 string Version
= List
.Version();
88 if (Version
.empty() == true)
90 if (List
.UsePackage(Pkg
,pkgCache::VerIterator(Cache
)) == false)
91 return _error
->Error("Error occured while processing %s (UsePackage1)",PackageName
.c_str());
95 pkgCache::VerIterator Ver
= Pkg
.VersionList();
96 __apt_ptrloc
*Last
= &Pkg
->VersionList
;
98 for (; Ver
.end() == false; Last
= &Ver
->NextVer
, Ver
++)
100 Res
= pkgVersionCompare(Version
.begin(),Version
.end(),Ver
.VerStr(),
101 Ver
.VerStr() + strlen(Ver
.VerStr()));
106 /* We already have a version for this item, record that we
110 if (List
.UsePackage(Pkg
,Ver
) == false)
111 return _error
->Error("Error occured while processing %s (UsePackage2)",PackageName
.c_str());
113 if (NewFileVer(Ver
,List
) == false)
114 return _error
->Error("Error occured while processing %s (NewFileVer1)",PackageName
.c_str());
120 *Last
= NewVersion(Ver
,Version
,*Last
);
121 Ver
->ParentPkg
= Pkg
.Index();
122 if (List
.NewVersion(Ver
) == false)
123 return _error
->Error("Error occured while processing %s (NewVersion1)",PackageName
.c_str());
125 if (List
.UsePackage(Pkg
,Ver
) == false)
126 return _error
->Error("Error occured while processing %s (UsePackage3)",PackageName
.c_str());
128 if (NewFileVer(Ver
,List
) == false)
129 return _error
->Error("Error occured while processing %s (NewVersion2)",PackageName
.c_str());
135 // CacheGenerator::NewPackage - Add a new package /*{{{*/
136 // ---------------------------------------------------------------------
137 /* This creates a new package structure and adds it to the hash table */
138 bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator
&Pkg
,string Name
)
140 Pkg
= Cache
.FindPkg(Name
);
141 if (Pkg
.end() == false)
145 unsigned long Package
= Map
.Allocate(sizeof(pkgCache::Package
));
149 Pkg
= pkgCache::PkgIterator(Cache
,Cache
.PkgP
+ Package
);
151 // Insert it into the hash table
152 unsigned long Hash
= Cache
.Hash(Name
);
153 Pkg
->NextPackage
= Cache
.HeaderP
->HashTable
[Hash
];
154 Cache
.HeaderP
->HashTable
[Hash
] = Package
;
156 // Set the name and the ID
157 Pkg
->Name
= Map
.WriteString(Name
);
160 Pkg
->ID
= Cache
.HeaderP
->PackageCount
++;
165 // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
166 // ---------------------------------------------------------------------
168 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator
&Ver
,
172 unsigned long VerFile
= Map
.Allocate(sizeof(pkgCache::VerFile
));
176 pkgCache::VerFileIterator
VF(Cache
,Cache
.VerFileP
+ VerFile
);
177 VF
->File
= CurrentFile
- Cache
.PkgFileP
;
179 // Link it to the end of the list
180 __apt_ptrloc
*Last
= &Ver
->FileList
;
181 for (pkgCache::VerFileIterator V
= Ver
.FileList(); V
.end() == false; V
++)
183 VF
->NextFile
= *Last
;
186 VF
->Offset
= List
.Offset();
187 VF
->Size
= List
.Size();
188 if (Cache
.HeaderP
->MaxVerFileSize
< VF
->Size
)
189 Cache
.HeaderP
->MaxVerFileSize
= VF
->Size
;
190 Cache
.HeaderP
->VerFileCount
++;
195 // CacheGenerator::NewVersion - Create a new Version /*{{{*/
196 // ---------------------------------------------------------------------
197 /* This puts a version structure in the linked list */
198 unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator
&Ver
,
203 unsigned long Version
= Map
.Allocate(sizeof(pkgCache::Version
));
208 Ver
= pkgCache::VerIterator(Cache
,Cache
.VerP
+ Version
);
210 Ver
->ID
= Cache
.HeaderP
->VersionCount
++;
211 Ver
->VerStr
= Map
.WriteString(VerStr
);
212 if (Ver
->VerStr
== 0)
218 // ListParser::NewDepends - Create a dependency element /*{{{*/
219 // ---------------------------------------------------------------------
220 /* This creates a dependency element in the tree. It is linked to the
221 version and to the package that it is pointing to. */
222 bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver
,
228 pkgCache
&Cache
= Owner
->Cache
;
231 unsigned long Dependency
= Owner
->Map
.Allocate(sizeof(pkgCache::Dependency
));
236 pkgCache::DepIterator
Dep(Cache
,Cache
.DepP
+ Dependency
);
237 Dep
->ParentVer
= Ver
.Index();
240 Dep
->ID
= Cache
.HeaderP
->DependsCount
++;
242 // Locate the target package
243 pkgCache::PkgIterator Pkg
;
244 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
247 // Probe the reverse dependency list for a version string that matches
248 if (Version
.empty() == false)
250 for (pkgCache::DepIterator I
= Pkg
.RevDependsList(); I
.end() == false; I
++)
251 if (I
->Version
!= 0 && I
.TargetVer() == Version
)
252 Dep
->Version
= I
->Version
;
253 if (Dep
->Version
== 0)
254 if ((Dep
->Version
= WriteString(Version
)) == 0)
258 // Link it to the package
259 Dep
->Package
= Pkg
.Index();
260 Dep
->NextRevDepends
= Pkg
->RevDepends
;
261 Pkg
->RevDepends
= Dep
.Index();
263 /* Link it to the version (at the end of the list)
264 Caching the old end point speeds up generation substantially */
265 static pkgCache::VerIterator
OldVer(Cache
);
266 static __apt_ptrloc
*OldLast
;
269 OldLast
= &Ver
->DependsList
;
270 for (pkgCache::DepIterator D
= Ver
.DependsList(); D
.end() == false; D
++)
271 OldLast
= &D
->NextDepends
;
275 Dep
->NextDepends
= *OldLast
;
276 *OldLast
= Dep
.Index();
277 OldLast
= &Dep
->NextDepends
;
282 // ListParser::NewProvides - Create a Provides element /*{{{*/
283 // ---------------------------------------------------------------------
285 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver
,
289 pkgCache
&Cache
= Owner
->Cache
;
291 // We do not add self referencing provides
292 if (Ver
.ParentPkg().Name() == PackageName
)
296 unsigned long Provides
= Owner
->Map
.Allocate(sizeof(pkgCache::Provides
));
299 Cache
.HeaderP
->ProvidesCount
++;
302 pkgCache::PrvIterator
Prv(Cache
,Cache
.ProvideP
+ Provides
,Cache
.PkgP
);
303 Prv
->Version
= Ver
.Index();
304 Prv
->NextPkgProv
= Ver
->ProvidesList
;
305 Ver
->ProvidesList
= Prv
.Index();
306 if (Version
.empty() == false && (Prv
->Version
= WriteString(Version
)) == 0)
309 // Locate the target package
310 pkgCache::PkgIterator Pkg
;
311 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
314 // Link it to the package
315 Prv
->ParentPkg
= Pkg
.Index();
316 Prv
->NextProvides
= Pkg
->ProvidesList
;
317 Pkg
->ProvidesList
= Prv
.Index();
322 // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
323 // ---------------------------------------------------------------------
324 /* This is used to select which file is to be associated with all newly
326 bool pkgCacheGenerator::SelectFile(string File
,unsigned long Flags
)
329 if (stat(File
.c_str(),&Buf
) == -1)
330 return _error
->Errno("stat","Couldn't stat ",File
.c_str());
332 // Get some space for the structure
333 CurrentFile
= Cache
.PkgFileP
+ Map
.Allocate(sizeof(*CurrentFile
));
334 if (CurrentFile
== Cache
.PkgFileP
)
338 CurrentFile
->FileName
= Map
.WriteString(File
);
339 CurrentFile
->Size
= Buf
.st_size
;
340 CurrentFile
->mtime
= Buf
.st_mtime
;
341 CurrentFile
->NextFile
= Cache
.HeaderP
->FileList
;
342 CurrentFile
->Flags
= Flags
;
343 CurrentFile
->ID
= Cache
.HeaderP
->PackageFileCount
;
345 Cache
.HeaderP
->FileList
= CurrentFile
- Cache
.PkgFileP
;
346 Cache
.HeaderP
->PackageFileCount
++;
348 if (CurrentFile
->FileName
== 0)
351 Progress
.SubProgress(Buf
.st_size
);
355 // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
356 // ---------------------------------------------------------------------
357 /* This is used to create handles to strings. Given the same text it
358 always returns the same number */
359 unsigned long pkgCacheGenerator::WriteUniqString(const char *S
,
362 // Search for an insertion point
363 pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.HeaderP
->StringList
;
365 __apt_ptrloc
*Last
= &Cache
.HeaderP
->StringList
;
366 for (; I
!= Cache
.StringItemP
; Last
= &I
->NextItem
,
367 I
= Cache
.StringItemP
+ I
->NextItem
)
369 Res
= stringcmp(S
,S
+Size
,Cache
.StrP
+ I
->String
);
379 unsigned long Item
= Map
.Allocate(sizeof(pkgCache::StringItem
));
383 // Fill in the structure
384 pkgCache::StringItem
*ItemP
= Cache
.StringItemP
+ Item
;
385 ItemP
->NextItem
= I
- Cache
.StringItemP
;
387 ItemP
->String
= Map
.WriteString(S
,Size
);
388 if (ItemP
->String
== 0)
391 return ItemP
->String
;
395 // SrcCacheCheck - Check if the source package cache is uptodate /*{{{*/
396 // ---------------------------------------------------------------------
397 /* The source cache is checked against the source list and the files
398 on disk, any difference results in a false. */
399 bool pkgSrcCacheCheck(pkgSourceList
&List
)
401 if (_error
->PendingError() == true)
404 string CacheFile
= _config
->FindFile("Dir::Cache::srcpkgcache");
405 string ListDir
= _config
->FindDir("Dir::State::lists");
407 // Count the number of missing files
409 for (pkgSourceList::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
411 string File
= ListDir
+ URItoFileName(I
->PackagesURI());
413 if (stat(File
.c_str(),&Buf
) != 0)
415 _error
->WarningE("stat","Couldn't stat source package list '%s' (%s)",
416 I
->PackagesInfo().c_str(),File
.c_str());
421 // Open the source package cache
422 if (FileExists(CacheFile
) == false)
425 FileFd
CacheF(CacheFile
,FileFd::ReadOnly
);
426 if (_error
->PendingError() == true)
432 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
433 if (_error
->PendingError() == true || Map
.Size() == 0)
440 if (_error
->PendingError() == true)
446 // They are certianly out of sync
447 if (Cache
.Head().PackageFileCount
!= List
.size() - Missing
)
450 for (pkgCache::PkgFileIterator
F(Cache
); F
.end() == false; F
++)
452 // Search for a match in the source list
454 for (pkgSourceList::const_iterator I
= List
.begin();
455 I
!= List
.end(); I
++)
457 string File
= ListDir
+ URItoFileName(I
->PackagesURI());
458 if (F
.FileName() == File
)
465 // Check if the file matches what was cached
474 // PkgCacheCheck - Check if the package cache is uptodate /*{{{*/
475 // ---------------------------------------------------------------------
476 /* This does a simple check of all files used to compose the cache */
477 bool pkgPkgCacheCheck(string CacheFile
)
479 if (_error
->PendingError() == true)
482 // Open the source package cache
483 if (FileExists(CacheFile
) == false)
486 FileFd
CacheF(CacheFile
,FileFd::ReadOnly
);
487 if (_error
->PendingError() == true)
493 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
494 if (_error
->PendingError() == true || Map
.Size() == 0)
501 if (_error
->PendingError() == true)
507 // Status files that must be in the cache
509 Status
[0] = _config
->FindFile("Dir::State::xstatus");
510 Status
[1]= _config
->FindFile("Dir::State::userstatus");
511 Status
[2] = _config
->FindFile("Dir::State::status");
514 for (pkgCache::PkgFileIterator
F(Cache
); F
.end() == false; F
++)
516 if (F
.IsOk() == false)
519 // See if this is one of the status files
520 for (int I
= 0; I
!= 3; I
++)
521 if (F
.FileName() == Status
[I
])
522 Status
[I
] = string();
525 // Make sure all the status files are loaded.
526 for (int I
= 0; I
!= 3; I
++)
528 if (Status
[I
].empty() == false && FileExists(Status
[I
]) == true)
535 // AddSourcesSize - Add the size of the status files /*{{{*/
536 // ---------------------------------------------------------------------
537 /* This adds the size of all the status files to the size counter */
538 static bool pkgAddSourcesSize(unsigned long &TotalSize
)
540 // Grab the file names
541 string xstatus
= _config
->FindFile("Dir::State::xstatus");
542 string userstatus
= _config
->FindFile("Dir::State::userstatus");
543 string status
= _config
->FindFile("Dir::State::status");
547 if (stat(xstatus
.c_str(),&Buf
) == 0)
548 TotalSize
+= Buf
.st_size
;
549 if (stat(userstatus
.c_str(),&Buf
) == 0)
550 TotalSize
+= Buf
.st_size
;
551 if (stat(status
.c_str(),&Buf
) != 0)
552 return _error
->Errno("stat","Couldn't stat the status file %s",status
.c_str());
553 TotalSize
+= Buf
.st_size
;
558 // MergeStatus - Add the status files to the cache /*{{{*/
559 // ---------------------------------------------------------------------
560 /* This adds the status files to the map */
561 static bool pkgMergeStatus(OpProgress
&Progress
,pkgCacheGenerator
&Gen
,
562 unsigned long &CurrentSize
,unsigned long TotalSize
)
564 // Grab the file names
566 Status
[0] = _config
->FindFile("Dir::State::xstatus");
567 Status
[1]= _config
->FindFile("Dir::State::userstatus");
568 Status
[2] = _config
->FindFile("Dir::State::status");
570 for (int I
= 0; I
!= 3; I
++)
572 // Check if the file exists and it is not the primary status file.
573 string File
= Status
[I
];
574 if (I
!= 2 && FileExists(File
) == false)
577 FileFd
Pkg(File
,FileFd::ReadOnly
);
578 debListParser
Parser(Pkg
);
579 Progress
.OverallProgress(CurrentSize
,TotalSize
,Pkg
.Size(),"Reading Package Lists");
580 if (_error
->PendingError() == true)
581 return _error
->Error("Problem opening %s",File
.c_str());
582 CurrentSize
+= Pkg
.Size();
584 Progress
.SubProgress(0,"Local Package State - " + flNotDir(File
));
585 if (Gen
.SelectFile(File
,pkgCache::Flag::NotSource
) == false)
586 return _error
->Error("Problem with SelectFile %s",File
.c_str());
588 if (Gen
.MergeList(Parser
) == false)
589 return _error
->Error("Problem with MergeList %s",File
.c_str());
590 Progress
.Progress(Pkg
.Size());
596 // MakeStatusCache - Generates a cache that includes the status files /*{{{*/
597 // ---------------------------------------------------------------------
598 /* This copies the package source cache and then merges the status and
599 xstatus files into it. */
600 bool pkgMakeStatusCache(pkgSourceList
&List
,OpProgress
&Progress
)
602 Progress
.OverallProgress(0,1,1,"Reading Package Lists");
604 string CacheFile
= _config
->FindFile("Dir::Cache::pkgcache");
605 bool SrcOk
= pkgSrcCacheCheck(List
);
606 bool PkgOk
= SrcOk
&& pkgPkgCacheCheck(CacheFile
);
608 // Rebuild the source and package caches
611 string SCacheFile
= _config
->FindFile("Dir::Cache::srcpkgcache");
612 string ListDir
= _config
->FindDir("Dir::State::lists");
613 FileFd
SCacheF(SCacheFile
,FileFd::WriteEmpty
);
614 FileFd
CacheF(CacheFile
,FileFd::WriteEmpty
);
615 DynamicMMap
Map(CacheF
,MMap::Public
);
616 if (_error
->PendingError() == true)
619 pkgCacheGenerator
Gen(Map
,Progress
);
621 // Prepare the progress indicator
622 unsigned long TotalSize
= 0;
624 for (pkgSourceList::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
626 string File
= ListDir
+ URItoFileName(I
->PackagesURI());
627 if (stat(File
.c_str(),&Buf
) != 0)
629 TotalSize
+= Buf
.st_size
;
632 if (pkgAddSourcesSize(TotalSize
) == false)
635 // Generate the pkg source cache
636 unsigned long CurrentSize
= 0;
637 for (pkgSourceList::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
639 string File
= ListDir
+ URItoFileName(I
->PackagesURI());
641 if (FileExists(File
) == false)
644 FileFd
Pkg(File
,FileFd::ReadOnly
);
645 debListParser
Parser(Pkg
);
646 Progress
.OverallProgress(CurrentSize
,TotalSize
,Pkg
.Size(),"Reading Package Lists");
647 if (_error
->PendingError() == true)
648 return _error
->Error("Problem opening %s",File
.c_str());
649 CurrentSize
+= Pkg
.Size();
651 Progress
.SubProgress(0,I
->PackagesInfo());
652 if (Gen
.SelectFile(File
) == false)
653 return _error
->Error("Problem with SelectFile %s",File
.c_str());
655 if (Gen
.MergeList(Parser
) == false)
656 return _error
->Error("Problem with MergeList %s",File
.c_str());
658 // Check the release file
659 string RFile
= ListDir
+ URItoFileName(I
->ReleaseURI());
660 if (FileExists(RFile
) == true)
662 FileFd
Rel(RFile
,FileFd::ReadOnly
);
663 if (_error
->PendingError() == true)
665 Parser
.LoadReleaseInfo(Gen
.GetCurFile(),Rel
);
669 // Write the src cache
670 Gen
.GetCache().HeaderP
->Dirty
= false;
671 if (SCacheF
.Write(Map
.Data(),Map
.Size()) == false)
672 return _error
->Error("IO Error saving source cache");
673 Gen
.GetCache().HeaderP
->Dirty
= true;
675 // Merge in the source caches
676 return pkgMergeStatus(Progress
,Gen
,CurrentSize
,TotalSize
);
681 Progress
.OverallProgress(1,1,1,"Reading Package Lists");
685 // We use the source cache to generate the package cache
686 string SCacheFile
= _config
->FindFile("Dir::Cache::srcpkgcache");
688 FileFd
SCacheF(SCacheFile
,FileFd::ReadOnly
);
689 FileFd
CacheF(CacheFile
,FileFd::WriteEmpty
);
690 DynamicMMap
Map(CacheF
,MMap::Public
);
691 if (_error
->PendingError() == true)
694 // Preload the map with the source cache
695 if (SCacheF
.Read((unsigned char *)Map
.Data() + Map
.RawAllocate(SCacheF
.Size()),
696 SCacheF
.Size()) == false)
699 pkgCacheGenerator
Gen(Map
,Progress
);
701 // Compute the progress
702 unsigned long TotalSize
= 0;
703 if (pkgAddSourcesSize(TotalSize
) == false)
706 unsigned long CurrentSize
= 0;
707 return pkgMergeStatus(Progress
,Gen
,CurrentSize
,TotalSize
);