pkgcachegen.cc 27 KB

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