Sync
[ntk/apt.git] / apt-pkg / pkgcachegen.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: pkgcachegen.cc,v 1.18 1998/10/02 04:39:47 jgg Exp $
4 /* ######################################################################
5
6 Package Cache Generator - Generator for the cache structure.
7
8 This builds the cache structure from the abstract package list parser.
9
10 ##################################################################### */
11 /*}}}*/
12 // Include Files /*{{{*/
13 #ifdef __GNUG__
14 #pragma implementation "apt-pkg/pkgcachegen.h"
15 #endif
16
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
25 #include <strutl.h>
26
27 #include <sys/stat.h>
28 #include <unistd.h>
29 /*}}}*/
30
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)
36 {
37 if (_error->PendingError() == true)
38 return;
39
40 if (Map.Size() == 0)
41 {
42 Map.RawAllocate(sizeof(pkgCache::Header));
43 *Cache.HeaderP = pkgCache::Header();
44 }
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]));
48 }
49 /*}}}*/
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()
55 {
56 if (_error->PendingError() == true)
57 return;
58 if (Map.Sync() == false)
59 return;
60
61 Cache.HeaderP->Dirty = false;
62 Map.Sync(0,sizeof(pkgCache::Header));
63 }
64 /*}}}*/
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)
70 {
71 List.Owner = this;
72
73 int Counter = 0;
74 while (List.Step() == true)
75 {
76 // Get a pointer to the package structure
77 string PackageName = List.Package();
78 pkgCache::PkgIterator Pkg;
79 if (NewPackage(Pkg,PackageName) == false)
80 return _error->Error("Error occured while processing %s (NewPackage)",PackageName.c_str());
81 Counter++;
82 if (Counter % 100 == 0)
83 Progress.Progress(List.Offset());
84
85 /* Get a pointer to the version structure. We know the list is sorted
86 so we use that fact in the search. Insertion of new versions is
87 done with correct sorting */
88 string Version = List.Version();
89 if (Version.empty() == true)
90 {
91 if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
92 return _error->Error("Error occured while processing %s (UsePackage1)",PackageName.c_str());
93 continue;
94 }
95
96 pkgCache::VerIterator Ver = Pkg.VersionList();
97 unsigned long *Last = &Pkg->VersionList;
98 int Res = 1;
99 for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
100 {
101 Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
102 Ver.VerStr() + strlen(Ver.VerStr()));
103 if (Res >= 0)
104 break;
105 }
106
107 /* We already have a version for this item, record that we
108 saw it */
109 if (Res == 0)
110 {
111 if (List.UsePackage(Pkg,Ver) == false)
112 return _error->Error("Error occured while processing %s (UsePackage2)",PackageName.c_str());
113
114 if (NewFileVer(Ver,List) == false)
115 return _error->Error("Error occured while processing %s (NewFileVer1)",PackageName.c_str());
116
117 continue;
118 }
119
120 // Add a new version
121 *Last = NewVersion(Ver,Version,*Last);
122 Ver->ParentPkg = Pkg.Index();
123 if (List.NewVersion(Ver) == false)
124 return _error->Error("Error occured while processing %s (NewVersion1)",PackageName.c_str());
125
126 if (List.UsePackage(Pkg,Ver) == false)
127 return _error->Error("Error occured while processing %s (UsePackage3)",PackageName.c_str());
128
129 if (NewFileVer(Ver,List) == false)
130 return _error->Error("Error occured while processing %s (NewVersion2)",PackageName.c_str());
131 }
132
133 return true;
134 }
135 /*}}}*/
136 // CacheGenerator::NewPackage - Add a new package /*{{{*/
137 // ---------------------------------------------------------------------
138 /* This creates a new package structure and adds it to the hash table */
139 bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
140 {
141 Pkg = Cache.FindPkg(Name);
142 if (Pkg.end() == false)
143 return true;
144
145 // Get a structure
146 unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
147 if (Package == 0)
148 return false;
149
150 Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
151
152 // Insert it into the hash table
153 unsigned long Hash = Cache.Hash(Name);
154 Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
155 Cache.HeaderP->HashTable[Hash] = Package;
156
157 // Set the name and the ID
158 Pkg->Name = Map.WriteString(Name);
159 if (Pkg->Name == 0)
160 return false;
161 Pkg->ID = Cache.HeaderP->PackageCount++;
162
163 return true;
164 }
165 /*}}}*/
166 // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
167 // ---------------------------------------------------------------------
168 /* */
169 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
170 ListParser &List)
171 {
172 // Get a structure
173 unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
174 if (VerFile == 0)
175 return 0;
176
177 pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
178 VF->File = CurrentFile - Cache.PkgFileP;
179 VF->NextFile = Ver->FileList;
180 Ver->FileList = VF.Index();
181 VF->Offset = List.Offset();
182 VF->Size = List.Size();
183 if (Cache.HeaderP->MaxVerFileSize < VF->Size)
184 Cache.HeaderP->MaxVerFileSize = VF->Size;
185 return true;
186 }
187 /*}}}*/
188 // CacheGenerator::NewVersion - Create a new Version /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This puts a version structure in the linked list */
191 unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
192 string VerStr,
193 unsigned long Next)
194 {
195 // Get a structure
196 unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
197 if (Version == 0)
198 return 0;
199
200 // Fill it in
201 Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
202 Ver->NextVer = Next;
203 Ver->ID = Cache.HeaderP->VersionCount++;
204 Ver->VerStr = Map.WriteString(VerStr);
205 if (Ver->VerStr == 0)
206 return 0;
207
208 return Version;
209 }
210 /*}}}*/
211 // ListParser::NewDepends - Create a dependency element /*{{{*/
212 // ---------------------------------------------------------------------
213 /* This creates a dependency element in the tree. It is linked to the
214 version and to the package that it is pointing to. */
215 bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
216 string PackageName,
217 string Version,
218 unsigned int Op,
219 unsigned int Type)
220 {
221 pkgCache &Cache = Owner->Cache;
222
223 // Get a structure
224 unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
225 if (Dependency == 0)
226 return false;
227
228 // Fill it in
229 pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
230 Dep->ParentVer = Ver.Index();
231 Dep->Type = Type;
232 Dep->CompareOp = Op;
233 Dep->ID = Cache.HeaderP->DependsCount++;
234
235 // Locate the target package
236 pkgCache::PkgIterator Pkg;
237 if (Owner->NewPackage(Pkg,PackageName) == false)
238 return false;
239
240 // Probe the reverse dependency list for a version string that matches
241 if (Version.empty() == false)
242 {
243 for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
244 if (I->Version != 0 && I.TargetVer() == Version)
245 Dep->Version = I->Version;
246 if (Dep->Version == 0)
247 if ((Dep->Version = WriteString(Version)) == 0)
248 return false;
249 }
250
251 // Link it to the package
252 Dep->Package = Pkg.Index();
253 Dep->NextRevDepends = Pkg->RevDepends;
254 Pkg->RevDepends = Dep.Index();
255
256 // Link it to the version (at the end of the list)
257 unsigned long *Last = &Ver->DependsList;
258 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
259 Last = &D->NextDepends;
260 Dep->NextDepends = *Last;
261 *Last = Dep.Index();
262
263 return true;
264 }
265 /*}}}*/
266 // ListParser::NewProvides - Create a Provides element /*{{{*/
267 // ---------------------------------------------------------------------
268 /* */
269 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
270 string PackageName,
271 string Version)
272 {
273 pkgCache &Cache = Owner->Cache;
274
275 // We do not add self referencing provides
276 if (Ver.ParentPkg().Name() == PackageName)
277 return true;
278
279 // Get a structure
280 unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
281 if (Provides == 0)
282 return false;
283
284 // Fill it in
285 pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
286 Prv->Version = Ver.Index();
287 Prv->NextPkgProv = Ver->ProvidesList;
288 Ver->ProvidesList = Prv.Index();
289 if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
290 return false;
291
292 // Locate the target package
293 pkgCache::PkgIterator Pkg;
294 if (Owner->NewPackage(Pkg,PackageName) == false)
295 return false;
296
297 // Link it to the package
298 Prv->ParentPkg = Pkg.Index();
299 Prv->NextProvides = Pkg->ProvidesList;
300 Pkg->ProvidesList = Prv.Index();
301
302 return true;
303 }
304 /*}}}*/
305 // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
306 // ---------------------------------------------------------------------
307 /* This is used to select which file is to be associated with all newly
308 added versions. */
309 bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
310 {
311 struct stat Buf;
312 if (stat(File.c_str(),&Buf) == -1)
313 return _error->Errno("stat","Couldn't stat ",File.c_str());
314
315 // Get some space for the structure
316 CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
317 if (CurrentFile == Cache.PkgFileP)
318 return false;
319
320 // Fill it in
321 CurrentFile->FileName = Map.WriteString(File);
322 CurrentFile->Size = Buf.st_size;
323 CurrentFile->mtime = Buf.st_mtime;
324 CurrentFile->NextFile = Cache.HeaderP->FileList;
325 CurrentFile->Flags = Flags;
326 CurrentFile->ID = Cache.HeaderP->PackageFileCount;
327 PkgFileName = File;
328 Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
329 Cache.HeaderP->PackageFileCount++;
330
331 if (CurrentFile->FileName == 0)
332 return false;
333
334 Progress.SubProgress(Buf.st_size);
335 return true;
336 }
337 /*}}}*/
338 // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
339 // ---------------------------------------------------------------------
340 /* This is used to create handles to strings. Given the same text it
341 always returns the same number */
342 unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
343 unsigned int Size)
344 {
345 // Search for an insertion point
346 pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
347 int Res = 1;
348 unsigned long *Last = &Cache.HeaderP->StringList;
349 for (; I != Cache.StringItemP; Last = &I->NextItem,
350 I = Cache.StringItemP + I->NextItem)
351 {
352 Res = stringcmp(S,S+Size,Cache.StrP + I->String);
353 if (Res >= 0)
354 break;
355 }
356
357 // Match
358 if (Res == 0)
359 return I->String;
360
361 // Get a structure
362 unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
363 if (Item == 0)
364 return 0;
365
366 // Fill in the structure
367 pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
368 ItemP->NextItem = I - Cache.StringItemP;
369 *Last = Item;
370 ItemP->String = Map.WriteString(S,Size);
371 if (ItemP->String == 0)
372 return 0;
373
374 return ItemP->String;
375 }
376 /*}}}*/
377
378 // SrcCacheCheck - Check if the source package cache is uptodate /*{{{*/
379 // ---------------------------------------------------------------------
380 /* The source cache is checked against the source list and the files
381 on disk, any difference results in a false. */
382 bool pkgSrcCacheCheck(pkgSourceList &List)
383 {
384 if (_error->PendingError() == true)
385 return false;
386
387 string CacheFile = _config->FindDir("Dir::Cache::srcpkgcache");
388 string ListDir = _config->FindDir("Dir::State::lists");
389
390 // Count the number of missing files
391 int Missing = 0;
392 for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
393 {
394 string File = ListDir + URItoFileName(I->PackagesURI());
395 struct stat Buf;
396 if (stat(File.c_str(),&Buf) != 0)
397 {
398 _error->WarningE("stat","Couldn't stat source package list '%s' (%s)",
399 I->PackagesInfo().c_str(),File.c_str());
400 Missing++;
401 }
402 }
403
404 // Open the source package cache
405 if (FileExists(CacheFile) == false)
406 return false;
407
408 FileFd CacheF(CacheFile,FileFd::ReadOnly);
409 if (_error->PendingError() == true)
410 {
411 _error->Discard();
412 return false;
413 }
414
415 MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
416 if (_error->PendingError() == true || Map.Size() == 0)
417 {
418 _error->Discard();
419 return false;
420 }
421
422 pkgCache Cache(Map);
423 if (_error->PendingError() == true)
424 {
425 _error->Discard();
426 return false;
427 }
428
429 // They are certianly out of sync
430 if (Cache.Head().PackageFileCount != List.size() - Missing)
431 return false;
432
433 for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
434 {
435 // Search for a match in the source list
436 bool Bad = true;
437 for (pkgSourceList::const_iterator I = List.begin();
438 I != List.end(); I++)
439 {
440 string File = ListDir + URItoFileName(I->PackagesURI());
441 if (F.FileName() == File)
442 {
443 Bad = false;
444 break;
445 }
446 }
447
448 // Check if the file matches what was cached
449 Bad |= !F.IsOk();
450 if (Bad == true)
451 return false;
452 }
453
454 return true;
455 }
456 /*}}}*/
457 // PkgCacheCheck - Check if the package cache is uptodate /*{{{*/
458 // ---------------------------------------------------------------------
459 /* This does a simple check of all files used to compose the cache */
460 bool pkgPkgCacheCheck(string CacheFile)
461 {
462 if (_error->PendingError() == true)
463 return false;
464
465 // Open the source package cache
466 if (FileExists(CacheFile) == false)
467 return false;
468
469 FileFd CacheF(CacheFile,FileFd::ReadOnly);
470 if (_error->PendingError() == true)
471 {
472 _error->Discard();
473 return false;
474 }
475
476 MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
477 if (_error->PendingError() == true || Map.Size() == 0)
478 {
479 _error->Discard();
480 return false;
481 }
482
483 pkgCache Cache(Map);
484 if (_error->PendingError() == true)
485 {
486 _error->Discard();
487 return false;
488 }
489
490 // Cheack each file
491 for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
492 if (F.IsOk() == false)
493 return false;
494 return true;
495 }
496 /*}}}*/
497 // AddSourcesSize - Add the size of the status files /*{{{*/
498 // ---------------------------------------------------------------------
499 /* This adds the size of all the status files to the size counter */
500 static bool pkgAddSourcesSize(unsigned long &TotalSize)
501 {
502 // Grab the file names
503 string xstatus = _config->FindDir("Dir::State::xstatus");
504 string userstatus = _config->FindDir("Dir::State::userstatus");
505 string status = _config->FindDir("Dir::State::status");
506
507 // Grab the sizes
508 struct stat Buf;
509 if (stat(xstatus.c_str(),&Buf) == 0)
510 TotalSize += Buf.st_size;
511 if (stat(userstatus.c_str(),&Buf) == 0)
512 TotalSize += Buf.st_size;
513 if (stat(status.c_str(),&Buf) != 0)
514 return _error->Errno("stat","Couldn't stat the status file %s",status.c_str());
515 TotalSize += Buf.st_size;
516
517 return true;
518 }
519 /*}}}*/
520 // MergeStatus - Add the status files to the cache /*{{{*/
521 // ---------------------------------------------------------------------
522 /* This adds the status files to the map */
523 static bool pkgMergeStatus(OpProgress &Progress,pkgCacheGenerator &Gen,
524 unsigned long &CurrentSize,unsigned long TotalSize)
525 {
526 // Grab the file names
527 string Status[3];
528 Status[0] = _config->FindDir("Dir::State::xstatus");
529 Status[1]= _config->FindDir("Dir::State::userstatus");
530 Status[2] = _config->FindDir("Dir::State::status");
531
532 for (int I = 0; I != 3; I++)
533 {
534 // Check if the file exists and it is not the primary status file.
535 string File = Status[I];
536 if (I != 2 && FileExists(File) == false)
537 continue;
538
539 FileFd Pkg(File,FileFd::ReadOnly);
540 debListParser Parser(Pkg);
541 Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
542 if (_error->PendingError() == true)
543 return _error->Error("Problem opening %s",File.c_str());
544 CurrentSize += Pkg.Size();
545
546 Progress.SubProgress(0,"Local Package State - " + flNotDir(File));
547 if (Gen.SelectFile(File,pkgCache::Flag::NotSource) == false)
548 return _error->Error("Problem with SelectFile %s",File.c_str());
549
550 if (Gen.MergeList(Parser) == false)
551 return _error->Error("Problem with MergeList %s",File.c_str());
552 Progress.Progress(Pkg.Size());
553 }
554
555 return true;
556 }
557 /*}}}*/
558 // MakeStatusCache - Generates a cache that includes the status files /*{{{*/
559 // ---------------------------------------------------------------------
560 /* This copies the package source cache and then merges the status and
561 xstatus files into it. */
562 bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress)
563 {
564 Progress.OverallProgress(0,1,1,"Reading Package Lists");
565
566 string CacheFile = _config->FindDir("Dir::Cache::pkgcache");
567 bool SrcOk = pkgSrcCacheCheck(List);
568 bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile);
569
570 // Rebuild the source and package caches
571 if (SrcOk == false)
572 {
573 string SCacheFile = _config->FindDir("Dir::Cache::srcpkgcache");
574 string ListDir = _config->FindDir("Dir::State::lists");
575
576 FileFd SCacheF(SCacheFile,FileFd::WriteEmpty);
577 FileFd CacheF(CacheFile,FileFd::WriteEmpty);
578 DynamicMMap Map(CacheF,MMap::Public);
579 if (_error->PendingError() == true)
580 return false;
581
582 pkgCacheGenerator Gen(Map,Progress);
583
584 // Prepare the progress indicator
585 unsigned long TotalSize = 0;
586 struct stat Buf;
587 for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
588 {
589 string File = ListDir + URItoFileName(I->PackagesURI());
590 if (stat(File.c_str(),&Buf) != 0)
591 continue;
592 TotalSize += Buf.st_size;
593 }
594
595 if (pkgAddSourcesSize(TotalSize) == false)
596 return false;
597
598 // Generate the pkg source cache
599 unsigned long CurrentSize = 0;
600 for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
601 {
602 string File = ListDir + URItoFileName(I->PackagesURI());
603
604 if (stat(File.c_str(),&Buf) != 0)
605 continue;
606
607 FileFd Pkg(File,FileFd::ReadOnly);
608 debListParser Parser(Pkg);
609 Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
610 if (_error->PendingError() == true)
611 return _error->Error("Problem opening %s",File.c_str());
612 CurrentSize += Pkg.Size();
613
614 Progress.SubProgress(0,I->PackagesInfo());
615 if (Gen.SelectFile(File) == false)
616 return _error->Error("Problem with SelectFile %s",File.c_str());
617
618 if (Gen.MergeList(Parser) == false)
619 return _error->Error("Problem with MergeList %s",File.c_str());
620 }
621
622 // Write the src cache
623 Gen.GetCache().HeaderP->Dirty = false;
624 if (SCacheF.Write(Map.Data(),Map.Size()) == false)
625 return _error->Error("IO Error saving source cache");
626 Gen.GetCache().HeaderP->Dirty = true;
627
628 // Merge in the source caches
629 return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
630 }
631
632 if (PkgOk == true)
633 {
634 Progress.OverallProgress(1,1,1,"Reading Package Lists");
635 return true;
636 }
637
638 // We use the source cache to generate the package cache
639 string SCacheFile = _config->FindDir("Dir::Cache::srcpkgcache");
640
641 FileFd SCacheF(SCacheFile,FileFd::ReadOnly);
642 FileFd CacheF(CacheFile,FileFd::WriteEmpty);
643 DynamicMMap Map(CacheF,MMap::Public);
644 if (_error->PendingError() == true)
645 return false;
646
647 // Preload the map with the source cache
648 if (SCacheF.Read((unsigned char *)Map.Data() + Map.RawAllocate(SCacheF.Size()),
649 SCacheF.Size()) == false)
650 return false;
651
652 pkgCacheGenerator Gen(Map,Progress);
653
654 // Compute the progress
655 unsigned long TotalSize = 0;
656 if (pkgAddSourcesSize(TotalSize) == false)
657 return false;
658
659 unsigned long CurrentSize = 0;
660 return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
661 }
662 /*}}}*/