pkgcachegen.cc 32 KB

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