pkgcachegen.cc 24 KB

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