pkgcachegen.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.53.2.1 2003/12/24 23:09:17 mdz Exp $
  4. /* ######################################################################
  5. Package Cache Generator - Generator for the cache structure.
  6. This builds the cache structure from the abstract package list parser.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #ifdef __GNUG__
  11. #pragma implementation "apt-pkg/pkgcachegen.h"
  12. #endif
  13. #define APT_COMPATIBILITY 986
  14. #include <apt-pkg/pkgcachegen.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/version.h>
  17. #include <apt-pkg/progress.h>
  18. #include <apt-pkg/sourcelist.h>
  19. #include <apt-pkg/configuration.h>
  20. #include <apt-pkg/strutl.h>
  21. #include <apt-pkg/sptr.h>
  22. #include <apt-pkg/pkgsystem.h>
  23. #include <apti18n.h>
  24. #include <vector>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <stdio.h>
  29. #include <system.h>
  30. /*}}}*/
  31. typedef vector<pkgIndexFile *>::iterator FileIterator;
  32. // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* We set the diry flag and make sure that is written to the disk */
  35. pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
  36. Map(*pMap), Cache(pMap,false), Progress(Prog),
  37. FoundFileDeps(0)
  38. {
  39. CurrentFile = 0;
  40. memset(UniqHash,0,sizeof(UniqHash));
  41. if (_error->PendingError() == true)
  42. return;
  43. if (Map.Size() == 0)
  44. {
  45. // Setup the map interface..
  46. Cache.HeaderP = (pkgCache::Header *)Map.Data();
  47. Map.RawAllocate(sizeof(pkgCache::Header));
  48. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  49. // Starting header
  50. *Cache.HeaderP = pkgCache::Header();
  51. Cache.HeaderP->VerSysName = Map.WriteString(_system->VS->Label);
  52. Cache.HeaderP->Architecture = Map.WriteString(_config->Find("APT::Architecture"));
  53. Cache.ReMap();
  54. }
  55. else
  56. {
  57. // Map directly from the existing file
  58. Cache.ReMap();
  59. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  60. if (Cache.VS != _system->VS)
  61. {
  62. _error->Error(_("Cache has an incompatible versioning system"));
  63. return;
  64. }
  65. }
  66. Cache.HeaderP->Dirty = true;
  67. Map.Sync(0,sizeof(pkgCache::Header));
  68. }
  69. /*}}}*/
  70. // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* We sync the data then unset the dirty flag in two steps so as to
  73. advoid a problem during a crash */
  74. pkgCacheGenerator::~pkgCacheGenerator()
  75. {
  76. if (_error->PendingError() == true)
  77. return;
  78. if (Map.Sync() == false)
  79. return;
  80. Cache.HeaderP->Dirty = false;
  81. Map.Sync(0,sizeof(pkgCache::Header));
  82. }
  83. /*}}}*/
  84. // CacheGenerator::MergeList - Merge the package list /*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* This provides the generation of the entries in the cache. Each loop
  87. goes through a single package record from the underlying parse engine. */
  88. bool pkgCacheGenerator::MergeList(ListParser &List,
  89. pkgCache::VerIterator *OutVer)
  90. {
  91. List.Owner = this;
  92. unsigned int Counter = 0;
  93. while (List.Step() == true)
  94. {
  95. // Get a pointer to the package structure
  96. string PackageName = List.Package();
  97. if (PackageName.empty() == true)
  98. return false;
  99. pkgCache::PkgIterator Pkg;
  100. if (NewPackage(Pkg,PackageName) == false)
  101. return _error->Error(_("Error occurred while processing %s (NewPackage)"),PackageName.c_str());
  102. Counter++;
  103. if (Counter % 100 == 0 && Progress != 0)
  104. Progress->Progress(List.Offset());
  105. /* Get a pointer to the version structure. We know the list is sorted
  106. so we use that fact in the search. Insertion of new versions is
  107. done with correct sorting */
  108. string Version = List.Version();
  109. if (Version.empty() == true)
  110. {
  111. if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
  112. return _error->Error(_("Error occurred while processing %s (UsePackage1)"),
  113. PackageName.c_str());
  114. continue;
  115. }
  116. pkgCache::VerIterator Ver = Pkg.VersionList();
  117. map_ptrloc *Last = &Pkg->VersionList;
  118. int Res = 1;
  119. for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
  120. {
  121. Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
  122. if (Res >= 0)
  123. break;
  124. }
  125. /* We already have a version for this item, record that we
  126. saw it */
  127. unsigned long Hash = List.VersionHash();
  128. if (Res == 0 && Ver->Hash == Hash)
  129. {
  130. if (List.UsePackage(Pkg,Ver) == false)
  131. return _error->Error(_("Error occurred while processing %s (UsePackage2)"),
  132. PackageName.c_str());
  133. if (NewFileVer(Ver,List) == false)
  134. return _error->Error(_("Error occurred while processing %s (NewFileVer1)"),
  135. PackageName.c_str());
  136. // Read only a single record and return
  137. if (OutVer != 0)
  138. {
  139. *OutVer = Ver;
  140. FoundFileDeps |= List.HasFileDeps();
  141. return true;
  142. }
  143. continue;
  144. }
  145. // Skip to the end of the same version set.
  146. if (Res == 0)
  147. {
  148. for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
  149. {
  150. Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
  151. if (Res != 0)
  152. break;
  153. }
  154. }
  155. // Add a new version
  156. *Last = NewVersion(Ver,Version,*Last);
  157. Ver->ParentPkg = Pkg.Index();
  158. Ver->Hash = Hash;
  159. if (List.NewVersion(Ver) == false)
  160. return _error->Error(_("Error occurred while processing %s (NewVersion1)"),
  161. PackageName.c_str());
  162. if (List.UsePackage(Pkg,Ver) == false)
  163. return _error->Error(_("Error occurred while processing %s (UsePackage3)"),
  164. PackageName.c_str());
  165. if (NewFileVer(Ver,List) == false)
  166. return _error->Error(_("Error occurred while processing %s (NewVersion2)"),
  167. PackageName.c_str());
  168. // Read only a single record and return
  169. if (OutVer != 0)
  170. {
  171. *OutVer = Ver;
  172. FoundFileDeps |= List.HasFileDeps();
  173. return true;
  174. }
  175. }
  176. FoundFileDeps |= List.HasFileDeps();
  177. if (Cache.HeaderP->PackageCount >= (1ULL<<sizeof(Cache.PkgP->ID)*8)-1)
  178. return _error->Error(_("Wow, you exceeded the number of package "
  179. "names this APT is capable of."));
  180. if (Cache.HeaderP->VersionCount >= (1ULL<<(sizeof(Cache.VerP->ID)*8))-1)
  181. return _error->Error(_("Wow, you exceeded the number of versions "
  182. "this APT is capable of."));
  183. if (Cache.HeaderP->DependsCount >= (1ULL<<(sizeof(Cache.DepP->ID)*8))-1ULL)
  184. return _error->Error(_("Wow, you exceeded the number of dependencies "
  185. "this APT is capable of."));
  186. return true;
  187. }
  188. /*}}}*/
  189. // CacheGenerator::MergeFileProvides - Merge file provides /*{{{*/
  190. // ---------------------------------------------------------------------
  191. /* If we found any file depends while parsing the main list we need to
  192. resolve them. Since it is undesired to load the entire list of files
  193. into the cache as virtual packages we do a two stage effort. MergeList
  194. identifies the file depends and this creates Provdies for them by
  195. re-parsing all the indexs. */
  196. bool pkgCacheGenerator::MergeFileProvides(ListParser &List)
  197. {
  198. List.Owner = this;
  199. unsigned int Counter = 0;
  200. while (List.Step() == true)
  201. {
  202. string PackageName = List.Package();
  203. if (PackageName.empty() == true)
  204. return false;
  205. string Version = List.Version();
  206. if (Version.empty() == true)
  207. continue;
  208. pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
  209. if (Pkg.end() == true)
  210. return _error->Error(_("Error occurred while processing %s (FindPkg)"),
  211. PackageName.c_str());
  212. Counter++;
  213. if (Counter % 100 == 0 && Progress != 0)
  214. Progress->Progress(List.Offset());
  215. unsigned long Hash = List.VersionHash();
  216. pkgCache::VerIterator Ver = Pkg.VersionList();
  217. for (; Ver.end() == false; Ver++)
  218. {
  219. if (Ver->Hash == Hash && Version.c_str() == Ver.VerStr())
  220. {
  221. if (List.CollectFileProvides(Cache,Ver) == false)
  222. return _error->Error(_("Error occurred while processing %s (CollectFileProvides)"),PackageName.c_str());
  223. break;
  224. }
  225. }
  226. if (Ver.end() == true)
  227. _error->Warning(_("Package %s %s was not found while processing file dependencies"),PackageName.c_str(),Version.c_str());
  228. }
  229. return true;
  230. }
  231. /*}}}*/
  232. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  233. // ---------------------------------------------------------------------
  234. /* This creates a new package structure and adds it to the hash table */
  235. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
  236. {
  237. Pkg = Cache.FindPkg(Name);
  238. if (Pkg.end() == false)
  239. return true;
  240. // Get a structure
  241. unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
  242. if (Package == 0)
  243. return false;
  244. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  245. // Insert it into the hash table
  246. unsigned long Hash = Cache.Hash(Name);
  247. Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
  248. Cache.HeaderP->HashTable[Hash] = Package;
  249. // Set the name and the ID
  250. Pkg->Name = Map.WriteString(Name);
  251. if (Pkg->Name == 0)
  252. return false;
  253. Pkg->ID = Cache.HeaderP->PackageCount++;
  254. return true;
  255. }
  256. /*}}}*/
  257. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  258. // ---------------------------------------------------------------------
  259. /* */
  260. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  261. ListParser &List)
  262. {
  263. if (CurrentFile == 0)
  264. return true;
  265. // Get a structure
  266. unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
  267. if (VerFile == 0)
  268. return 0;
  269. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  270. VF->File = CurrentFile - Cache.PkgFileP;
  271. // Link it to the end of the list
  272. map_ptrloc *Last = &Ver->FileList;
  273. for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; V++)
  274. Last = &V->NextFile;
  275. VF->NextFile = *Last;
  276. *Last = VF.Index();
  277. VF->Offset = List.Offset();
  278. VF->Size = List.Size();
  279. if (Cache.HeaderP->MaxVerFileSize < VF->Size)
  280. Cache.HeaderP->MaxVerFileSize = VF->Size;
  281. Cache.HeaderP->VerFileCount++;
  282. return true;
  283. }
  284. /*}}}*/
  285. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  286. // ---------------------------------------------------------------------
  287. /* This puts a version structure in the linked list */
  288. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  289. string VerStr,
  290. unsigned long Next)
  291. {
  292. // Get a structure
  293. unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
  294. if (Version == 0)
  295. return 0;
  296. // Fill it in
  297. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  298. Ver->NextVer = Next;
  299. Ver->ID = Cache.HeaderP->VersionCount++;
  300. Ver->VerStr = Map.WriteString(VerStr);
  301. if (Ver->VerStr == 0)
  302. return 0;
  303. return Version;
  304. }
  305. /*}}}*/
  306. // ListParser::NewDepends - Create a dependency element /*{{{*/
  307. // ---------------------------------------------------------------------
  308. /* This creates a dependency element in the tree. It is linked to the
  309. version and to the package that it is pointing to. */
  310. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
  311. string PackageName,
  312. string Version,
  313. unsigned int Op,
  314. unsigned int Type)
  315. {
  316. pkgCache &Cache = Owner->Cache;
  317. // Get a structure
  318. unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
  319. if (Dependency == 0)
  320. return false;
  321. // Fill it in
  322. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  323. Dep->ParentVer = Ver.Index();
  324. Dep->Type = Type;
  325. Dep->CompareOp = Op;
  326. Dep->ID = Cache.HeaderP->DependsCount++;
  327. // Locate the target package
  328. pkgCache::PkgIterator Pkg;
  329. if (Owner->NewPackage(Pkg,PackageName) == false)
  330. return false;
  331. // Probe the reverse dependency list for a version string that matches
  332. if (Version.empty() == false)
  333. {
  334. /* for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  335. if (I->Version != 0 && I.TargetVer() == Version)
  336. Dep->Version = I->Version;*/
  337. if (Dep->Version == 0)
  338. if ((Dep->Version = WriteString(Version)) == 0)
  339. return false;
  340. }
  341. // Link it to the package
  342. Dep->Package = Pkg.Index();
  343. Dep->NextRevDepends = Pkg->RevDepends;
  344. Pkg->RevDepends = Dep.Index();
  345. /* Link it to the version (at the end of the list)
  346. Caching the old end point speeds up generation substantially */
  347. if (OldDepVer != Ver)
  348. {
  349. OldDepLast = &Ver->DependsList;
  350. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  351. OldDepLast = &D->NextDepends;
  352. OldDepVer = Ver;
  353. }
  354. // Is it a file dependency?
  355. if (PackageName[0] == '/')
  356. FoundFileDeps = true;
  357. Dep->NextDepends = *OldDepLast;
  358. *OldDepLast = Dep.Index();
  359. OldDepLast = &Dep->NextDepends;
  360. return true;
  361. }
  362. /*}}}*/
  363. // ListParser::NewProvides - Create a Provides element /*{{{*/
  364. // ---------------------------------------------------------------------
  365. /* */
  366. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
  367. string PackageName,
  368. string Version)
  369. {
  370. pkgCache &Cache = Owner->Cache;
  371. // We do not add self referencing provides
  372. if (Ver.ParentPkg().Name() == PackageName)
  373. return true;
  374. // Get a structure
  375. unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
  376. if (Provides == 0)
  377. return false;
  378. Cache.HeaderP->ProvidesCount++;
  379. // Fill it in
  380. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  381. Prv->Version = Ver.Index();
  382. Prv->NextPkgProv = Ver->ProvidesList;
  383. Ver->ProvidesList = Prv.Index();
  384. if (Version.empty() == false && (Prv->ProvideVersion = WriteString(Version)) == 0)
  385. return false;
  386. // Locate the target package
  387. pkgCache::PkgIterator Pkg;
  388. if (Owner->NewPackage(Pkg,PackageName) == false)
  389. return false;
  390. // Link it to the package
  391. Prv->ParentPkg = Pkg.Index();
  392. Prv->NextProvides = Pkg->ProvidesList;
  393. Pkg->ProvidesList = Prv.Index();
  394. return true;
  395. }
  396. /*}}}*/
  397. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  398. // ---------------------------------------------------------------------
  399. /* This is used to select which file is to be associated with all newly
  400. added versions. The caller is responsible for setting the IMS fields. */
  401. bool pkgCacheGenerator::SelectFile(string File,string Site,
  402. const pkgIndexFile &Index,
  403. unsigned long Flags)
  404. {
  405. // Get some space for the structure
  406. CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
  407. if (CurrentFile == Cache.PkgFileP)
  408. return false;
  409. // Fill it in
  410. CurrentFile->FileName = Map.WriteString(File);
  411. CurrentFile->Site = WriteUniqString(Site);
  412. CurrentFile->NextFile = Cache.HeaderP->FileList;
  413. CurrentFile->Flags = Flags;
  414. CurrentFile->ID = Cache.HeaderP->PackageFileCount;
  415. CurrentFile->IndexType = WriteUniqString(Index.GetType()->Label);
  416. PkgFileName = File;
  417. Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
  418. Cache.HeaderP->PackageFileCount++;
  419. if (CurrentFile->FileName == 0)
  420. return false;
  421. if (Progress != 0)
  422. Progress->SubProgress(Index.Size());
  423. return true;
  424. }
  425. /*}}}*/
  426. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  427. // ---------------------------------------------------------------------
  428. /* This is used to create handles to strings. Given the same text it
  429. always returns the same number */
  430. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  431. unsigned int Size)
  432. {
  433. /* We use a very small transient hash table here, this speeds up generation
  434. by a fair amount on slower machines */
  435. pkgCache::StringItem *&Bucket = UniqHash[(S[0]*5 + S[1]) % _count(UniqHash)];
  436. if (Bucket != 0 &&
  437. stringcmp(S,S+Size,Cache.StrP + Bucket->String) == 0)
  438. return Bucket->String;
  439. // Search for an insertion point
  440. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  441. int Res = 1;
  442. map_ptrloc *Last = &Cache.HeaderP->StringList;
  443. for (; I != Cache.StringItemP; Last = &I->NextItem,
  444. I = Cache.StringItemP + I->NextItem)
  445. {
  446. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  447. if (Res >= 0)
  448. break;
  449. }
  450. // Match
  451. if (Res == 0)
  452. {
  453. Bucket = I;
  454. return I->String;
  455. }
  456. // Get a structure
  457. unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
  458. if (Item == 0)
  459. return 0;
  460. // Fill in the structure
  461. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  462. ItemP->NextItem = I - Cache.StringItemP;
  463. *Last = Item;
  464. ItemP->String = Map.WriteString(S,Size);
  465. if (ItemP->String == 0)
  466. return 0;
  467. Bucket = ItemP;
  468. return ItemP->String;
  469. }
  470. /*}}}*/
  471. // CheckValidity - Check that a cache is up-to-date /*{{{*/
  472. // ---------------------------------------------------------------------
  473. /* This just verifies that each file in the list of index files exists,
  474. has matching attributes with the cache and the cache does not have
  475. any extra files. */
  476. static bool CheckValidity(string CacheFile, FileIterator Start,
  477. FileIterator End,MMap **OutMap = 0)
  478. {
  479. // No file, certainly invalid
  480. if (CacheFile.empty() == true || FileExists(CacheFile) == false)
  481. return false;
  482. // Map it
  483. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  484. SPtr<MMap> Map = new MMap(CacheF,MMap::Public | MMap::ReadOnly);
  485. pkgCache Cache(Map);
  486. if (_error->PendingError() == true || Map->Size() == 0)
  487. {
  488. _error->Discard();
  489. return false;
  490. }
  491. /* Now we check every index file, see if it is in the cache,
  492. verify the IMS data and check that it is on the disk too.. */
  493. SPtrArray<bool> Visited = new bool[Cache.HeaderP->PackageFileCount];
  494. memset(Visited,0,sizeof(*Visited)*Cache.HeaderP->PackageFileCount);
  495. for (; Start != End; Start++)
  496. {
  497. if ((*Start)->HasPackages() == false)
  498. continue;
  499. if ((*Start)->Exists() == false)
  500. {
  501. _error->WarningE("stat",_("Couldn't stat source package list %s"),
  502. (*Start)->Describe().c_str());
  503. continue;
  504. }
  505. // FindInCache is also expected to do an IMS check.
  506. pkgCache::PkgFileIterator File = (*Start)->FindInCache(Cache);
  507. if (File.end() == true)
  508. return false;
  509. Visited[File->ID] = true;
  510. }
  511. for (unsigned I = 0; I != Cache.HeaderP->PackageFileCount; I++)
  512. if (Visited[I] == false)
  513. return false;
  514. if (_error->PendingError() == true)
  515. {
  516. _error->Discard();
  517. return false;
  518. }
  519. if (OutMap != 0)
  520. *OutMap = Map.UnGuard();
  521. return true;
  522. }
  523. /*}}}*/
  524. // ComputeSize - Compute the total size of a bunch of files /*{{{*/
  525. // ---------------------------------------------------------------------
  526. /* Size is kind of an abstract notion that is only used for the progress
  527. meter */
  528. static unsigned long ComputeSize(FileIterator Start,FileIterator End)
  529. {
  530. unsigned long TotalSize = 0;
  531. for (; Start != End; Start++)
  532. {
  533. if ((*Start)->HasPackages() == false)
  534. continue;
  535. TotalSize += (*Start)->Size();
  536. }
  537. return TotalSize;
  538. }
  539. /*}}}*/
  540. // BuildCache - Merge the list of index files into the cache /*{{{*/
  541. // ---------------------------------------------------------------------
  542. /* */
  543. static bool BuildCache(pkgCacheGenerator &Gen,
  544. OpProgress &Progress,
  545. unsigned long &CurrentSize,unsigned long TotalSize,
  546. FileIterator Start, FileIterator End)
  547. {
  548. FileIterator I;
  549. for (I = Start; I != End; I++)
  550. {
  551. if ((*I)->HasPackages() == false)
  552. continue;
  553. if ((*I)->Exists() == false)
  554. continue;
  555. if ((*I)->FindInCache(Gen.GetCache()).end() == false)
  556. {
  557. _error->Warning("Duplicate sources.list entry %s",
  558. (*I)->Describe().c_str());
  559. continue;
  560. }
  561. unsigned long Size = (*I)->Size();
  562. Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
  563. CurrentSize += Size;
  564. if ((*I)->Merge(Gen,Progress) == false)
  565. return false;
  566. }
  567. if (Gen.HasFileDeps() == true)
  568. {
  569. Progress.Done();
  570. TotalSize = ComputeSize(Start, End);
  571. CurrentSize = 0;
  572. for (I = Start; I != End; I++)
  573. {
  574. unsigned long Size = (*I)->Size();
  575. Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
  576. CurrentSize += Size;
  577. if ((*I)->MergeFileProvides(Gen,Progress) == false)
  578. return false;
  579. }
  580. }
  581. return true;
  582. }
  583. /*}}}*/
  584. // MakeStatusCache - Construct the status cache /*{{{*/
  585. // ---------------------------------------------------------------------
  586. /* This makes sure that the status cache (the cache that has all
  587. index files from the sources list and all local ones) is ready
  588. to be mmaped. If OutMap is not zero then a MMap object representing
  589. the cache will be stored there. This is pretty much mandetory if you
  590. are using AllowMem. AllowMem lets the function be run as non-root
  591. where it builds the cache 'fast' into a memory buffer. */
  592. bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
  593. MMap **OutMap,bool AllowMem)
  594. {
  595. unsigned long MapSize = _config->FindI("APT::Cache-Limit",12*1024*1024);
  596. vector<pkgIndexFile *> Files;
  597. for (vector<metaIndex *>::const_iterator i = List.begin();
  598. i != List.end();
  599. i++)
  600. {
  601. vector <pkgIndexFile *> *Indexes = (*i)->GetIndexFiles();
  602. for (vector<pkgIndexFile *>::const_iterator j = Indexes->begin();
  603. j != Indexes->end();
  604. j++)
  605. Files.push_back (*j);
  606. }
  607. unsigned long EndOfSource = Files.size();
  608. if (_system->AddStatusFiles(Files) == false)
  609. return false;
  610. // Decide if we can write to the files..
  611. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  612. string SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  613. // Decide if we can write to the cache
  614. bool Writeable = false;
  615. if (CacheFile.empty() == false)
  616. Writeable = access(flNotFile(CacheFile).c_str(),W_OK) == 0;
  617. else
  618. if (SrcCacheFile.empty() == false)
  619. Writeable = access(flNotFile(SrcCacheFile).c_str(),W_OK) == 0;
  620. if (Writeable == false && AllowMem == false && CacheFile.empty() == false)
  621. return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
  622. Progress.OverallProgress(0,1,1,_("Reading package lists"));
  623. // Cache is OK, Fin.
  624. if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
  625. {
  626. Progress.OverallProgress(1,1,1,_("Reading package lists"));
  627. return true;
  628. }
  629. /* At this point we know we need to reconstruct the package cache,
  630. begin. */
  631. SPtr<FileFd> CacheF;
  632. SPtr<DynamicMMap> Map;
  633. if (Writeable == true && CacheFile.empty() == false)
  634. {
  635. unlink(CacheFile.c_str());
  636. CacheF = new FileFd(CacheFile,FileFd::WriteEmpty);
  637. fchmod(CacheF->Fd(),0644);
  638. Map = new DynamicMMap(*CacheF,MMap::Public,MapSize);
  639. if (_error->PendingError() == true)
  640. return false;
  641. }
  642. else
  643. {
  644. // Just build it in memory..
  645. Map = new DynamicMMap(MMap::Public,MapSize);
  646. }
  647. // Lets try the source cache.
  648. unsigned long CurrentSize = 0;
  649. unsigned long TotalSize = 0;
  650. if (CheckValidity(SrcCacheFile,Files.begin(),
  651. Files.begin()+EndOfSource) == true)
  652. {
  653. // Preload the map with the source cache
  654. FileFd SCacheF(SrcCacheFile,FileFd::ReadOnly);
  655. if (SCacheF.Read((unsigned char *)Map->Data() + Map->RawAllocate(SCacheF.Size()),
  656. SCacheF.Size()) == false)
  657. return false;
  658. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  659. // Build the status cache
  660. pkgCacheGenerator Gen(Map.Get(),&Progress);
  661. if (_error->PendingError() == true)
  662. return false;
  663. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  664. Files.begin()+EndOfSource,Files.end()) == false)
  665. return false;
  666. }
  667. else
  668. {
  669. TotalSize = ComputeSize(Files.begin(),Files.end());
  670. // Build the source cache
  671. pkgCacheGenerator Gen(Map.Get(),&Progress);
  672. if (_error->PendingError() == true)
  673. return false;
  674. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  675. Files.begin(),Files.begin()+EndOfSource) == false)
  676. return false;
  677. // Write it back
  678. if (Writeable == true && SrcCacheFile.empty() == false)
  679. {
  680. FileFd SCacheF(SrcCacheFile,FileFd::WriteEmpty);
  681. if (_error->PendingError() == true)
  682. return false;
  683. fchmod(SCacheF.Fd(),0644);
  684. // Write out the main data
  685. if (SCacheF.Write(Map->Data(),Map->Size()) == false)
  686. return _error->Error(_("IO Error saving source cache"));
  687. SCacheF.Sync();
  688. // Write out the proper header
  689. Gen.GetCache().HeaderP->Dirty = false;
  690. if (SCacheF.Seek(0) == false ||
  691. SCacheF.Write(Map->Data(),sizeof(*Gen.GetCache().HeaderP)) == false)
  692. return _error->Error(_("IO Error saving source cache"));
  693. Gen.GetCache().HeaderP->Dirty = true;
  694. SCacheF.Sync();
  695. }
  696. // Build the status cache
  697. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  698. Files.begin()+EndOfSource,Files.end()) == false)
  699. return false;
  700. }
  701. if (_error->PendingError() == true)
  702. return false;
  703. if (OutMap != 0)
  704. {
  705. if (CacheF != 0)
  706. {
  707. delete Map.UnGuard();
  708. *OutMap = new MMap(*CacheF,MMap::Public | MMap::ReadOnly);
  709. }
  710. else
  711. {
  712. *OutMap = Map.UnGuard();
  713. }
  714. }
  715. return true;
  716. }
  717. /*}}}*/
  718. // MakeOnlyStatusCache - Build a cache with just the status files /*{{{*/
  719. // ---------------------------------------------------------------------
  720. /* */
  721. bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
  722. {
  723. unsigned long MapSize = _config->FindI("APT::Cache-Limit",8*1024*1024);
  724. vector<pkgIndexFile *> Files;
  725. unsigned long EndOfSource = Files.size();
  726. if (_system->AddStatusFiles(Files) == false)
  727. return false;
  728. SPtr<DynamicMMap> Map;
  729. Map = new DynamicMMap(MMap::Public,MapSize);
  730. unsigned long CurrentSize = 0;
  731. unsigned long TotalSize = 0;
  732. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  733. // Build the status cache
  734. Progress.OverallProgress(0,1,1,_("Reading package lists"));
  735. pkgCacheGenerator Gen(Map.Get(),&Progress);
  736. if (_error->PendingError() == true)
  737. return false;
  738. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  739. Files.begin()+EndOfSource,Files.end()) == false)
  740. return false;
  741. if (_error->PendingError() == true)
  742. return false;
  743. *OutMap = Map.UnGuard();
  744. return true;
  745. }
  746. /*}}}*/