pkgcachegen.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. // we first process the package, then the descriptions
  112. // (this has the bonus that we get MMap error when we run out
  113. // of MMap space)
  114. if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
  115. return _error->Error(_("Error occurred while processing %s (UsePackage1)"),
  116. PackageName.c_str());
  117. // Find the right version to write the description
  118. MD5SumValue CurMd5 = List.Description_md5();
  119. pkgCache::VerIterator Ver = Pkg.VersionList();
  120. map_ptrloc *LastVer = &Pkg->VersionList;
  121. for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
  122. {
  123. pkgCache::DescIterator Desc = Ver.DescriptionList();
  124. map_ptrloc *LastDesc = &Ver->DescriptionList;
  125. for (; Desc.end() == false; LastDesc = &Desc->NextDesc, Desc++)
  126. {
  127. if (MD5SumValue(Desc.md5()) == CurMd5)
  128. {
  129. // Add new description
  130. *LastDesc = NewDescription(Desc, List.DescriptionLanguage(), CurMd5, *LastDesc);
  131. Desc->ParentPkg = Pkg.Index();
  132. if (NewFileDesc(Desc,List) == false)
  133. return _error->Error(_("Error occured while processing %s (NewFileDesc1)"),PackageName.c_str());
  134. break;
  135. }
  136. }
  137. }
  138. continue;
  139. }
  140. pkgCache::VerIterator Ver = Pkg.VersionList();
  141. map_ptrloc *LastVer = &Pkg->VersionList;
  142. int Res = 1;
  143. for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
  144. {
  145. Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
  146. if (Res >= 0)
  147. break;
  148. }
  149. /* We already have a version for this item, record that we
  150. saw it */
  151. unsigned long Hash = List.VersionHash();
  152. if (Res == 0 && Ver->Hash == Hash)
  153. {
  154. if (List.UsePackage(Pkg,Ver) == false)
  155. return _error->Error(_("Error occurred while processing %s (UsePackage2)"),
  156. PackageName.c_str());
  157. if (NewFileVer(Ver,List) == false)
  158. return _error->Error(_("Error occurred while processing %s (NewFileVer1)"),
  159. PackageName.c_str());
  160. // Read only a single record and return
  161. if (OutVer != 0)
  162. {
  163. *OutVer = Ver;
  164. FoundFileDeps |= List.HasFileDeps();
  165. return true;
  166. }
  167. continue;
  168. }
  169. // Skip to the end of the same version set.
  170. if (Res == 0)
  171. {
  172. for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
  173. {
  174. Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
  175. if (Res != 0)
  176. break;
  177. }
  178. }
  179. // Add a new version
  180. *LastVer = NewVersion(Ver,Version,*LastVer);
  181. Ver->ParentPkg = Pkg.Index();
  182. Ver->Hash = Hash;
  183. if (List.NewVersion(Ver) == false)
  184. return _error->Error(_("Error occurred while processing %s (NewVersion1)"),
  185. PackageName.c_str());
  186. if (List.UsePackage(Pkg,Ver) == false)
  187. return _error->Error(_("Error occurred while processing %s (UsePackage3)"),
  188. PackageName.c_str());
  189. if (NewFileVer(Ver,List) == false)
  190. return _error->Error(_("Error occurred while processing %s (NewVersion2)"),
  191. PackageName.c_str());
  192. // Read only a single record and return
  193. if (OutVer != 0)
  194. {
  195. *OutVer = Ver;
  196. FoundFileDeps |= List.HasFileDeps();
  197. return true;
  198. }
  199. /* Record the Description data. Description data always exist in
  200. Packages and Translation-* files. */
  201. pkgCache::DescIterator Desc = Ver.DescriptionList();
  202. map_ptrloc *LastDesc = &Ver->DescriptionList;
  203. // Skip to the end of description set
  204. for (; Desc.end() == false; LastDesc = &Desc->NextDesc, Desc++);
  205. // Add new description
  206. *LastDesc = NewDescription(Desc, List.DescriptionLanguage(), List.Description_md5(), *LastDesc);
  207. Desc->ParentPkg = Pkg.Index();
  208. if (NewFileDesc(Desc,List) == false)
  209. return _error->Error(_("Error occured while processing %s (NewFileDesc2)"),PackageName.c_str());
  210. }
  211. FoundFileDeps |= List.HasFileDeps();
  212. if (Cache.HeaderP->PackageCount >= (1ULL<<sizeof(Cache.PkgP->ID)*8)-1)
  213. return _error->Error(_("Wow, you exceeded the number of package "
  214. "names this APT is capable of."));
  215. if (Cache.HeaderP->VersionCount >= (1ULL<<(sizeof(Cache.VerP->ID)*8))-1)
  216. return _error->Error(_("Wow, you exceeded the number of versions "
  217. "this APT is capable of."));
  218. if (Cache.HeaderP->DescriptionCount >= (1ULL<<(sizeof(Cache.DescP->ID)*8))-1)
  219. return _error->Error(_("Wow, you exceeded the number of descriptions "
  220. "this APT is capable of."));
  221. if (Cache.HeaderP->DependsCount >= (1ULL<<(sizeof(Cache.DepP->ID)*8))-1ULL)
  222. return _error->Error(_("Wow, you exceeded the number of dependencies "
  223. "this APT is capable of."));
  224. return true;
  225. }
  226. /*}}}*/
  227. // CacheGenerator::MergeFileProvides - Merge file provides /*{{{*/
  228. // ---------------------------------------------------------------------
  229. /* If we found any file depends while parsing the main list we need to
  230. resolve them. Since it is undesired to load the entire list of files
  231. into the cache as virtual packages we do a two stage effort. MergeList
  232. identifies the file depends and this creates Provdies for them by
  233. re-parsing all the indexs. */
  234. bool pkgCacheGenerator::MergeFileProvides(ListParser &List)
  235. {
  236. List.Owner = this;
  237. unsigned int Counter = 0;
  238. while (List.Step() == true)
  239. {
  240. string PackageName = List.Package();
  241. if (PackageName.empty() == true)
  242. return false;
  243. string Version = List.Version();
  244. if (Version.empty() == true)
  245. continue;
  246. pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
  247. if (Pkg.end() == true)
  248. return _error->Error(_("Error occurred while processing %s (FindPkg)"),
  249. PackageName.c_str());
  250. Counter++;
  251. if (Counter % 100 == 0 && Progress != 0)
  252. Progress->Progress(List.Offset());
  253. unsigned long Hash = List.VersionHash();
  254. pkgCache::VerIterator Ver = Pkg.VersionList();
  255. for (; Ver.end() == false; Ver++)
  256. {
  257. if (Ver->Hash == Hash && Version.c_str() == Ver.VerStr())
  258. {
  259. if (List.CollectFileProvides(Cache,Ver) == false)
  260. return _error->Error(_("Error occurred while processing %s (CollectFileProvides)"),PackageName.c_str());
  261. break;
  262. }
  263. }
  264. if (Ver.end() == true)
  265. _error->Warning(_("Package %s %s was not found while processing file dependencies"),PackageName.c_str(),Version.c_str());
  266. }
  267. return true;
  268. }
  269. /*}}}*/
  270. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  271. // ---------------------------------------------------------------------
  272. /* This creates a new package structure and adds it to the hash table */
  273. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
  274. {
  275. Pkg = Cache.FindPkg(Name);
  276. if (Pkg.end() == false)
  277. return true;
  278. // Get a structure
  279. unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
  280. if (Package == 0)
  281. return false;
  282. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  283. // Insert it into the hash table
  284. unsigned long Hash = Cache.Hash(Name);
  285. Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
  286. Cache.HeaderP->HashTable[Hash] = Package;
  287. // Set the name and the ID
  288. Pkg->Name = Map.WriteString(Name);
  289. if (Pkg->Name == 0)
  290. return false;
  291. Pkg->ID = Cache.HeaderP->PackageCount++;
  292. return true;
  293. }
  294. /*}}}*/
  295. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  296. // ---------------------------------------------------------------------
  297. /* */
  298. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  299. ListParser &List)
  300. {
  301. if (CurrentFile == 0)
  302. return true;
  303. // Get a structure
  304. unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
  305. if (VerFile == 0)
  306. return 0;
  307. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  308. VF->File = CurrentFile - Cache.PkgFileP;
  309. // Link it to the end of the list
  310. map_ptrloc *Last = &Ver->FileList;
  311. for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; V++)
  312. Last = &V->NextFile;
  313. VF->NextFile = *Last;
  314. *Last = VF.Index();
  315. VF->Offset = List.Offset();
  316. VF->Size = List.Size();
  317. if (Cache.HeaderP->MaxVerFileSize < VF->Size)
  318. Cache.HeaderP->MaxVerFileSize = VF->Size;
  319. Cache.HeaderP->VerFileCount++;
  320. return true;
  321. }
  322. /*}}}*/
  323. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  324. // ---------------------------------------------------------------------
  325. /* This puts a version structure in the linked list */
  326. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  327. string VerStr,
  328. unsigned long Next)
  329. {
  330. // Get a structure
  331. unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
  332. if (Version == 0)
  333. return 0;
  334. // Fill it in
  335. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  336. Ver->NextVer = Next;
  337. Ver->ID = Cache.HeaderP->VersionCount++;
  338. Ver->VerStr = Map.WriteString(VerStr);
  339. if (Ver->VerStr == 0)
  340. return 0;
  341. return Version;
  342. }
  343. /*}}}*/
  344. // CacheGenerator::NewFileDesc - Create a new File<->Desc association /*{{{*/
  345. // ---------------------------------------------------------------------
  346. /* */
  347. bool pkgCacheGenerator::NewFileDesc(pkgCache::DescIterator &Desc,
  348. ListParser &List)
  349. {
  350. if (CurrentFile == 0)
  351. return true;
  352. // Get a structure
  353. unsigned long DescFile = Map.Allocate(sizeof(pkgCache::DescFile));
  354. if (DescFile == 0)
  355. return 0;
  356. pkgCache::DescFileIterator DF(Cache,Cache.DescFileP + DescFile);
  357. DF->File = CurrentFile - Cache.PkgFileP;
  358. // Link it to the end of the list
  359. map_ptrloc *Last = &Desc->FileList;
  360. for (pkgCache::DescFileIterator D = Desc.FileList(); D.end() == false; D++)
  361. Last = &D->NextFile;
  362. DF->NextFile = *Last;
  363. *Last = DF.Index();
  364. DF->Offset = List.Offset();
  365. DF->Size = List.Size();
  366. if (Cache.HeaderP->MaxDescFileSize < DF->Size)
  367. Cache.HeaderP->MaxDescFileSize = DF->Size;
  368. Cache.HeaderP->DescFileCount++;
  369. return true;
  370. }
  371. /*}}}*/
  372. // CacheGenerator::NewDescription - Create a new Description /*{{{*/
  373. // ---------------------------------------------------------------------
  374. /* This puts a description structure in the linked list */
  375. map_ptrloc pkgCacheGenerator::NewDescription(pkgCache::DescIterator &Desc,
  376. const string &Lang, const MD5SumValue &md5sum,
  377. map_ptrloc Next)
  378. {
  379. // Get a structure
  380. map_ptrloc Description = Map.Allocate(sizeof(pkgCache::Description));
  381. if (Description == 0)
  382. return 0;
  383. // Fill it in
  384. Desc = pkgCache::DescIterator(Cache,Cache.DescP + Description);
  385. Desc->NextDesc = Next;
  386. Desc->ID = Cache.HeaderP->DescriptionCount++;
  387. Desc->language_code = Map.WriteString(Lang);
  388. Desc->md5sum = Map.WriteString(md5sum.Value());
  389. return Description;
  390. }
  391. /*}}}*/
  392. // ListParser::NewDepends - Create a dependency element /*{{{*/
  393. // ---------------------------------------------------------------------
  394. /* This creates a dependency element in the tree. It is linked to the
  395. version and to the package that it is pointing to. */
  396. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
  397. string PackageName,
  398. string Version,
  399. unsigned int Op,
  400. unsigned int Type)
  401. {
  402. pkgCache &Cache = Owner->Cache;
  403. // Get a structure
  404. unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
  405. if (Dependency == 0)
  406. return false;
  407. // Fill it in
  408. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  409. Dep->ParentVer = Ver.Index();
  410. Dep->Type = Type;
  411. Dep->CompareOp = Op;
  412. Dep->ID = Cache.HeaderP->DependsCount++;
  413. // Locate the target package
  414. pkgCache::PkgIterator Pkg;
  415. if (Owner->NewPackage(Pkg,PackageName) == false)
  416. return false;
  417. // Probe the reverse dependency list for a version string that matches
  418. if (Version.empty() == false)
  419. {
  420. /* for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  421. if (I->Version != 0 && I.TargetVer() == Version)
  422. Dep->Version = I->Version;*/
  423. if (Dep->Version == 0)
  424. if ((Dep->Version = WriteString(Version)) == 0)
  425. return false;
  426. }
  427. // Link it to the package
  428. Dep->Package = Pkg.Index();
  429. Dep->NextRevDepends = Pkg->RevDepends;
  430. Pkg->RevDepends = Dep.Index();
  431. /* Link it to the version (at the end of the list)
  432. Caching the old end point speeds up generation substantially */
  433. if (OldDepVer != Ver)
  434. {
  435. OldDepLast = &Ver->DependsList;
  436. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  437. OldDepLast = &D->NextDepends;
  438. OldDepVer = Ver;
  439. }
  440. // Is it a file dependency?
  441. if (PackageName[0] == '/')
  442. FoundFileDeps = true;
  443. Dep->NextDepends = *OldDepLast;
  444. *OldDepLast = Dep.Index();
  445. OldDepLast = &Dep->NextDepends;
  446. return true;
  447. }
  448. /*}}}*/
  449. // ListParser::NewProvides - Create a Provides element /*{{{*/
  450. // ---------------------------------------------------------------------
  451. /* */
  452. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
  453. string PackageName,
  454. string Version)
  455. {
  456. pkgCache &Cache = Owner->Cache;
  457. // We do not add self referencing provides
  458. if (Ver.ParentPkg().Name() == PackageName)
  459. return true;
  460. // Get a structure
  461. unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
  462. if (Provides == 0)
  463. return false;
  464. Cache.HeaderP->ProvidesCount++;
  465. // Fill it in
  466. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  467. Prv->Version = Ver.Index();
  468. Prv->NextPkgProv = Ver->ProvidesList;
  469. Ver->ProvidesList = Prv.Index();
  470. if (Version.empty() == false && (Prv->ProvideVersion = WriteString(Version)) == 0)
  471. return false;
  472. // Locate the target package
  473. pkgCache::PkgIterator Pkg;
  474. if (Owner->NewPackage(Pkg,PackageName) == false)
  475. return false;
  476. // Link it to the package
  477. Prv->ParentPkg = Pkg.Index();
  478. Prv->NextProvides = Pkg->ProvidesList;
  479. Pkg->ProvidesList = Prv.Index();
  480. return true;
  481. }
  482. /*}}}*/
  483. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  484. // ---------------------------------------------------------------------
  485. /* This is used to select which file is to be associated with all newly
  486. added versions. The caller is responsible for setting the IMS fields. */
  487. bool pkgCacheGenerator::SelectFile(string File,string Site,
  488. const pkgIndexFile &Index,
  489. unsigned long Flags)
  490. {
  491. // Get some space for the structure
  492. CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
  493. if (CurrentFile == Cache.PkgFileP)
  494. return false;
  495. // Fill it in
  496. CurrentFile->FileName = Map.WriteString(File);
  497. CurrentFile->Site = WriteUniqString(Site);
  498. CurrentFile->NextFile = Cache.HeaderP->FileList;
  499. CurrentFile->Flags = Flags;
  500. CurrentFile->ID = Cache.HeaderP->PackageFileCount;
  501. CurrentFile->IndexType = WriteUniqString(Index.GetType()->Label);
  502. PkgFileName = File;
  503. Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
  504. Cache.HeaderP->PackageFileCount++;
  505. if (CurrentFile->FileName == 0)
  506. return false;
  507. if (Progress != 0)
  508. Progress->SubProgress(Index.Size());
  509. return true;
  510. }
  511. /*}}}*/
  512. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  513. // ---------------------------------------------------------------------
  514. /* This is used to create handles to strings. Given the same text it
  515. always returns the same number */
  516. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  517. unsigned int Size)
  518. {
  519. /* We use a very small transient hash table here, this speeds up generation
  520. by a fair amount on slower machines */
  521. pkgCache::StringItem *&Bucket = UniqHash[(S[0]*5 + S[1]) % _count(UniqHash)];
  522. if (Bucket != 0 &&
  523. stringcmp(S,S+Size,Cache.StrP + Bucket->String) == 0)
  524. return Bucket->String;
  525. // Search for an insertion point
  526. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  527. int Res = 1;
  528. map_ptrloc *Last = &Cache.HeaderP->StringList;
  529. for (; I != Cache.StringItemP; Last = &I->NextItem,
  530. I = Cache.StringItemP + I->NextItem)
  531. {
  532. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  533. if (Res >= 0)
  534. break;
  535. }
  536. // Match
  537. if (Res == 0)
  538. {
  539. Bucket = I;
  540. return I->String;
  541. }
  542. // Get a structure
  543. unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
  544. if (Item == 0)
  545. return 0;
  546. // Fill in the structure
  547. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  548. ItemP->NextItem = I - Cache.StringItemP;
  549. *Last = Item;
  550. ItemP->String = Map.WriteString(S,Size);
  551. if (ItemP->String == 0)
  552. return 0;
  553. Bucket = ItemP;
  554. return ItemP->String;
  555. }
  556. /*}}}*/
  557. // CheckValidity - Check that a cache is up-to-date /*{{{*/
  558. // ---------------------------------------------------------------------
  559. /* This just verifies that each file in the list of index files exists,
  560. has matching attributes with the cache and the cache does not have
  561. any extra files. */
  562. static bool CheckValidity(string CacheFile, FileIterator Start,
  563. FileIterator End,MMap **OutMap = 0)
  564. {
  565. // No file, certainly invalid
  566. if (CacheFile.empty() == true || FileExists(CacheFile) == false)
  567. return false;
  568. // Map it
  569. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  570. SPtr<MMap> Map = new MMap(CacheF,MMap::Public | MMap::ReadOnly);
  571. pkgCache Cache(Map);
  572. if (_error->PendingError() == true || Map->Size() == 0)
  573. {
  574. _error->Discard();
  575. return false;
  576. }
  577. /* Now we check every index file, see if it is in the cache,
  578. verify the IMS data and check that it is on the disk too.. */
  579. SPtrArray<bool> Visited = new bool[Cache.HeaderP->PackageFileCount];
  580. memset(Visited,0,sizeof(*Visited)*Cache.HeaderP->PackageFileCount);
  581. for (; Start != End; Start++)
  582. {
  583. if ((*Start)->HasPackages() == false)
  584. continue;
  585. if ((*Start)->Exists() == false)
  586. {
  587. _error->WarningE("stat",_("Couldn't stat source package list %s"),
  588. (*Start)->Describe().c_str());
  589. continue;
  590. }
  591. // FindInCache is also expected to do an IMS check.
  592. pkgCache::PkgFileIterator File = (*Start)->FindInCache(Cache);
  593. if (File.end() == true)
  594. return false;
  595. Visited[File->ID] = true;
  596. }
  597. for (unsigned I = 0; I != Cache.HeaderP->PackageFileCount; I++)
  598. if (Visited[I] == false)
  599. return false;
  600. if (_error->PendingError() == true)
  601. {
  602. _error->Discard();
  603. return false;
  604. }
  605. if (OutMap != 0)
  606. *OutMap = Map.UnGuard();
  607. return true;
  608. }
  609. /*}}}*/
  610. // ComputeSize - Compute the total size of a bunch of files /*{{{*/
  611. // ---------------------------------------------------------------------
  612. /* Size is kind of an abstract notion that is only used for the progress
  613. meter */
  614. static unsigned long ComputeSize(FileIterator Start,FileIterator End)
  615. {
  616. unsigned long TotalSize = 0;
  617. for (; Start != End; Start++)
  618. {
  619. if ((*Start)->HasPackages() == false)
  620. continue;
  621. TotalSize += (*Start)->Size();
  622. }
  623. return TotalSize;
  624. }
  625. /*}}}*/
  626. // BuildCache - Merge the list of index files into the cache /*{{{*/
  627. // ---------------------------------------------------------------------
  628. /* */
  629. static bool BuildCache(pkgCacheGenerator &Gen,
  630. OpProgress &Progress,
  631. unsigned long &CurrentSize,unsigned long TotalSize,
  632. FileIterator Start, FileIterator End)
  633. {
  634. FileIterator I;
  635. for (I = Start; I != End; I++)
  636. {
  637. if ((*I)->HasPackages() == false)
  638. continue;
  639. if ((*I)->Exists() == false)
  640. continue;
  641. if ((*I)->FindInCache(Gen.GetCache()).end() == false)
  642. {
  643. _error->Warning("Duplicate sources.list entry %s",
  644. (*I)->Describe().c_str());
  645. continue;
  646. }
  647. unsigned long Size = (*I)->Size();
  648. Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
  649. CurrentSize += Size;
  650. if ((*I)->Merge(Gen,Progress) == false)
  651. return false;
  652. }
  653. if (Gen.HasFileDeps() == true)
  654. {
  655. Progress.Done();
  656. TotalSize = ComputeSize(Start, End);
  657. CurrentSize = 0;
  658. for (I = Start; I != End; I++)
  659. {
  660. unsigned long Size = (*I)->Size();
  661. Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
  662. CurrentSize += Size;
  663. if ((*I)->MergeFileProvides(Gen,Progress) == false)
  664. return false;
  665. }
  666. }
  667. return true;
  668. }
  669. /*}}}*/
  670. // MakeStatusCache - Construct the status cache /*{{{*/
  671. // ---------------------------------------------------------------------
  672. /* This makes sure that the status cache (the cache that has all
  673. index files from the sources list and all local ones) is ready
  674. to be mmaped. If OutMap is not zero then a MMap object representing
  675. the cache will be stored there. This is pretty much mandetory if you
  676. are using AllowMem. AllowMem lets the function be run as non-root
  677. where it builds the cache 'fast' into a memory buffer. */
  678. bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
  679. MMap **OutMap,bool AllowMem)
  680. {
  681. unsigned long MapSize = _config->FindI("APT::Cache-Limit",12*1024*1024);
  682. vector<pkgIndexFile *> Files;
  683. for (vector<metaIndex *>::const_iterator i = List.begin();
  684. i != List.end();
  685. i++)
  686. {
  687. vector <pkgIndexFile *> *Indexes = (*i)->GetIndexFiles();
  688. for (vector<pkgIndexFile *>::const_iterator j = Indexes->begin();
  689. j != Indexes->end();
  690. j++)
  691. Files.push_back (*j);
  692. }
  693. unsigned long EndOfSource = Files.size();
  694. if (_system->AddStatusFiles(Files) == false)
  695. return false;
  696. // Decide if we can write to the files..
  697. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  698. string SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  699. // Decide if we can write to the cache
  700. bool Writeable = false;
  701. if (CacheFile.empty() == false)
  702. Writeable = access(flNotFile(CacheFile).c_str(),W_OK) == 0;
  703. else
  704. if (SrcCacheFile.empty() == false)
  705. Writeable = access(flNotFile(SrcCacheFile).c_str(),W_OK) == 0;
  706. if (Writeable == false && AllowMem == false && CacheFile.empty() == false)
  707. return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
  708. Progress.OverallProgress(0,1,1,_("Reading package lists"));
  709. // Cache is OK, Fin.
  710. if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
  711. {
  712. Progress.OverallProgress(1,1,1,_("Reading package lists"));
  713. return true;
  714. }
  715. /* At this point we know we need to reconstruct the package cache,
  716. begin. */
  717. SPtr<FileFd> CacheF;
  718. SPtr<DynamicMMap> Map;
  719. if (Writeable == true && CacheFile.empty() == false)
  720. {
  721. unlink(CacheFile.c_str());
  722. CacheF = new FileFd(CacheFile,FileFd::WriteEmpty);
  723. fchmod(CacheF->Fd(),0644);
  724. Map = new DynamicMMap(*CacheF,MMap::Public,MapSize);
  725. if (_error->PendingError() == true)
  726. return false;
  727. }
  728. else
  729. {
  730. // Just build it in memory..
  731. Map = new DynamicMMap(MMap::Public,MapSize);
  732. }
  733. // Lets try the source cache.
  734. unsigned long CurrentSize = 0;
  735. unsigned long TotalSize = 0;
  736. if (CheckValidity(SrcCacheFile,Files.begin(),
  737. Files.begin()+EndOfSource) == true)
  738. {
  739. // Preload the map with the source cache
  740. FileFd SCacheF(SrcCacheFile,FileFd::ReadOnly);
  741. if (SCacheF.Read((unsigned char *)Map->Data() + Map->RawAllocate(SCacheF.Size()),
  742. SCacheF.Size()) == false)
  743. return false;
  744. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  745. // Build the status cache
  746. pkgCacheGenerator Gen(Map.Get(),&Progress);
  747. if (_error->PendingError() == true)
  748. return false;
  749. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  750. Files.begin()+EndOfSource,Files.end()) == false)
  751. return false;
  752. }
  753. else
  754. {
  755. TotalSize = ComputeSize(Files.begin(),Files.end());
  756. // Build the source cache
  757. pkgCacheGenerator Gen(Map.Get(),&Progress);
  758. if (_error->PendingError() == true)
  759. return false;
  760. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  761. Files.begin(),Files.begin()+EndOfSource) == false)
  762. return false;
  763. // Write it back
  764. if (Writeable == true && SrcCacheFile.empty() == false)
  765. {
  766. FileFd SCacheF(SrcCacheFile,FileFd::WriteEmpty);
  767. if (_error->PendingError() == true)
  768. return false;
  769. fchmod(SCacheF.Fd(),0644);
  770. // Write out the main data
  771. if (SCacheF.Write(Map->Data(),Map->Size()) == false)
  772. return _error->Error(_("IO Error saving source cache"));
  773. SCacheF.Sync();
  774. // Write out the proper header
  775. Gen.GetCache().HeaderP->Dirty = false;
  776. if (SCacheF.Seek(0) == false ||
  777. SCacheF.Write(Map->Data(),sizeof(*Gen.GetCache().HeaderP)) == false)
  778. return _error->Error(_("IO Error saving source cache"));
  779. Gen.GetCache().HeaderP->Dirty = true;
  780. SCacheF.Sync();
  781. }
  782. // Build the status cache
  783. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  784. Files.begin()+EndOfSource,Files.end()) == false)
  785. return false;
  786. }
  787. if (_error->PendingError() == true)
  788. return false;
  789. if (OutMap != 0)
  790. {
  791. if (CacheF != 0)
  792. {
  793. delete Map.UnGuard();
  794. *OutMap = new MMap(*CacheF,MMap::Public | MMap::ReadOnly);
  795. }
  796. else
  797. {
  798. *OutMap = Map.UnGuard();
  799. }
  800. }
  801. return true;
  802. }
  803. /*}}}*/
  804. // MakeOnlyStatusCache - Build a cache with just the status files /*{{{*/
  805. // ---------------------------------------------------------------------
  806. /* */
  807. bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
  808. {
  809. unsigned long MapSize = _config->FindI("APT::Cache-Limit",8*1024*1024);
  810. vector<pkgIndexFile *> Files;
  811. unsigned long EndOfSource = Files.size();
  812. if (_system->AddStatusFiles(Files) == false)
  813. return false;
  814. SPtr<DynamicMMap> Map;
  815. Map = new DynamicMMap(MMap::Public,MapSize);
  816. unsigned long CurrentSize = 0;
  817. unsigned long TotalSize = 0;
  818. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  819. // Build the status cache
  820. Progress.OverallProgress(0,1,1,_("Reading package lists"));
  821. pkgCacheGenerator Gen(Map.Get(),&Progress);
  822. if (_error->PendingError() == true)
  823. return false;
  824. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  825. Files.begin()+EndOfSource,Files.end()) == false)
  826. return false;
  827. if (_error->PendingError() == true)
  828. return false;
  829. *OutMap = Map.UnGuard();
  830. return true;
  831. }
  832. /*}}}*/