pkgcachegen.cc 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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/aptconfiguration.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/sptr.h>
  20. #include <apt-pkg/pkgsystem.h>
  21. #include <apt-pkg/macros.h>
  22. #include <apt-pkg/tagfile.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. /*}}}*/
  30. typedef vector<pkgIndexFile *>::iterator FileIterator;
  31. template <typename Iter> std::vector<Iter*> pkgCacheGenerator::Dynamic<Iter>::toReMap;
  32. // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* We set the dirty 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. if (Map.RawAllocate(sizeof(pkgCache::Header)) == 0 && _error->PendingError() == true)
  48. return;
  49. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  50. // Starting header
  51. *Cache.HeaderP = pkgCache::Header();
  52. map_ptrloc const idxVerSysName = WriteStringInMap(_system->VS->Label);
  53. Cache.HeaderP->VerSysName = idxVerSysName;
  54. map_ptrloc const idxArchitecture = WriteStringInMap(_config->Find("APT::Architecture"));
  55. Cache.HeaderP->Architecture = idxArchitecture;
  56. if (unlikely(idxVerSysName == 0 || idxArchitecture == 0))
  57. return;
  58. Cache.ReMap();
  59. }
  60. else
  61. {
  62. // Map directly from the existing file
  63. Cache.ReMap();
  64. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  65. if (Cache.VS != _system->VS)
  66. {
  67. _error->Error(_("Cache has an incompatible versioning system"));
  68. return;
  69. }
  70. }
  71. Cache.HeaderP->Dirty = true;
  72. Map.Sync(0,sizeof(pkgCache::Header));
  73. }
  74. /*}}}*/
  75. // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
  76. // ---------------------------------------------------------------------
  77. /* We sync the data then unset the dirty flag in two steps so as to
  78. advoid a problem during a crash */
  79. pkgCacheGenerator::~pkgCacheGenerator()
  80. {
  81. if (_error->PendingError() == true)
  82. return;
  83. if (Map.Sync() == false)
  84. return;
  85. Cache.HeaderP->Dirty = false;
  86. Map.Sync(0,sizeof(pkgCache::Header));
  87. }
  88. /*}}}*/
  89. void pkgCacheGenerator::ReMap(void const * const oldMap, void const * const newMap) {/*{{{*/
  90. if (oldMap == newMap)
  91. return;
  92. Cache.ReMap(false);
  93. CurrentFile += (pkgCache::PackageFile*) newMap - (pkgCache::PackageFile*) oldMap;
  94. for (size_t i = 0; i < _count(UniqHash); ++i)
  95. if (UniqHash[i] != 0)
  96. UniqHash[i] += (pkgCache::StringItem*) newMap - (pkgCache::StringItem*) oldMap;
  97. for (std::vector<pkgCache::GrpIterator*>::const_iterator i = Dynamic<pkgCache::GrpIterator>::toReMap.begin();
  98. i != Dynamic<pkgCache::GrpIterator>::toReMap.end(); ++i)
  99. (*i)->ReMap(oldMap, newMap);
  100. for (std::vector<pkgCache::PkgIterator*>::const_iterator i = Dynamic<pkgCache::PkgIterator>::toReMap.begin();
  101. i != Dynamic<pkgCache::PkgIterator>::toReMap.end(); ++i)
  102. (*i)->ReMap(oldMap, newMap);
  103. for (std::vector<pkgCache::VerIterator*>::const_iterator i = Dynamic<pkgCache::VerIterator>::toReMap.begin();
  104. i != Dynamic<pkgCache::VerIterator>::toReMap.end(); ++i)
  105. (*i)->ReMap(oldMap, newMap);
  106. for (std::vector<pkgCache::DepIterator*>::const_iterator i = Dynamic<pkgCache::DepIterator>::toReMap.begin();
  107. i != Dynamic<pkgCache::DepIterator>::toReMap.end(); ++i)
  108. (*i)->ReMap(oldMap, newMap);
  109. for (std::vector<pkgCache::DescIterator*>::const_iterator i = Dynamic<pkgCache::DescIterator>::toReMap.begin();
  110. i != Dynamic<pkgCache::DescIterator>::toReMap.end(); ++i)
  111. (*i)->ReMap(oldMap, newMap);
  112. for (std::vector<pkgCache::PrvIterator*>::const_iterator i = Dynamic<pkgCache::PrvIterator>::toReMap.begin();
  113. i != Dynamic<pkgCache::PrvIterator>::toReMap.end(); ++i)
  114. (*i)->ReMap(oldMap, newMap);
  115. for (std::vector<pkgCache::PkgFileIterator*>::const_iterator i = Dynamic<pkgCache::PkgFileIterator>::toReMap.begin();
  116. i != Dynamic<pkgCache::PkgFileIterator>::toReMap.end(); ++i)
  117. (*i)->ReMap(oldMap, newMap);
  118. } /*}}}*/
  119. // CacheGenerator::WriteStringInMap /*{{{*/
  120. map_ptrloc pkgCacheGenerator::WriteStringInMap(const char *String,
  121. const unsigned long &Len) {
  122. void const * const oldMap = Map.Data();
  123. map_ptrloc const index = Map.WriteString(String, Len);
  124. if (index != 0)
  125. ReMap(oldMap, Map.Data());
  126. return index;
  127. }
  128. /*}}}*/
  129. // CacheGenerator::WriteStringInMap /*{{{*/
  130. map_ptrloc pkgCacheGenerator::WriteStringInMap(const char *String) {
  131. void const * const oldMap = Map.Data();
  132. map_ptrloc const index = Map.WriteString(String);
  133. if (index != 0)
  134. ReMap(oldMap, Map.Data());
  135. return index;
  136. }
  137. /*}}}*/
  138. map_ptrloc pkgCacheGenerator::AllocateInMap(const unsigned long &size) {/*{{{*/
  139. void const * const oldMap = Map.Data();
  140. map_ptrloc const index = Map.Allocate(size);
  141. if (index != 0)
  142. ReMap(oldMap, Map.Data());
  143. return index;
  144. }
  145. /*}}}*/
  146. // CacheGenerator::MergeList - Merge the package list /*{{{*/
  147. // ---------------------------------------------------------------------
  148. /* This provides the generation of the entries in the cache. Each loop
  149. goes through a single package record from the underlying parse engine. */
  150. bool pkgCacheGenerator::MergeList(ListParser &List,
  151. pkgCache::VerIterator *OutVer)
  152. {
  153. List.Owner = this;
  154. unsigned int Counter = 0;
  155. while (List.Step() == true)
  156. {
  157. string const PackageName = List.Package();
  158. if (PackageName.empty() == true)
  159. return false;
  160. string const Arch = List.Architecture();
  161. // Get a pointer to the package structure
  162. pkgCache::PkgIterator Pkg;
  163. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  164. if (NewPackage(Pkg, PackageName, Arch) == false)
  165. return _error->Error(_("Error occurred while processing %s (NewPackage)"),PackageName.c_str());
  166. Counter++;
  167. if (Counter % 100 == 0 && Progress != 0)
  168. Progress->Progress(List.Offset());
  169. /* Get a pointer to the version structure. We know the list is sorted
  170. so we use that fact in the search. Insertion of new versions is
  171. done with correct sorting */
  172. string Version = List.Version();
  173. if (Version.empty() == true)
  174. {
  175. // we first process the package, then the descriptions
  176. // (this has the bonus that we get MMap error when we run out
  177. // of MMap space)
  178. pkgCache::VerIterator Ver(Cache);
  179. Dynamic<pkgCache::VerIterator> DynVer(Ver);
  180. if (List.UsePackage(Pkg, Ver) == false)
  181. return _error->Error(_("Error occurred while processing %s (UsePackage1)"),
  182. PackageName.c_str());
  183. // Find the right version to write the description
  184. MD5SumValue CurMd5 = List.Description_md5();
  185. Ver = Pkg.VersionList();
  186. for (; Ver.end() == false; ++Ver)
  187. {
  188. pkgCache::DescIterator Desc = Ver.DescriptionList();
  189. Dynamic<pkgCache::DescIterator> DynDesc(Desc);
  190. map_ptrloc *LastDesc = &Ver->DescriptionList;
  191. bool duplicate=false;
  192. // don't add a new description if we have one for the given
  193. // md5 && language
  194. for ( ; Desc.end() == false; Desc++)
  195. if (MD5SumValue(Desc.md5()) == CurMd5 &&
  196. Desc.LanguageCode() == List.DescriptionLanguage())
  197. duplicate=true;
  198. if(duplicate)
  199. continue;
  200. for (Desc = Ver.DescriptionList();
  201. Desc.end() == false;
  202. LastDesc = &Desc->NextDesc, Desc++)
  203. {
  204. if (MD5SumValue(Desc.md5()) == CurMd5)
  205. {
  206. // Add new description
  207. void const * const oldMap = Map.Data();
  208. map_ptrloc const descindex = NewDescription(Desc, List.DescriptionLanguage(), CurMd5, *LastDesc);
  209. if (oldMap != Map.Data())
  210. LastDesc += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  211. *LastDesc = descindex;
  212. Desc->ParentPkg = Pkg.Index();
  213. if ((*LastDesc == 0 && _error->PendingError()) || NewFileDesc(Desc,List) == false)
  214. return _error->Error(_("Error occurred while processing %s (NewFileDesc1)"),PackageName.c_str());
  215. break;
  216. }
  217. }
  218. }
  219. continue;
  220. }
  221. pkgCache::VerIterator Ver = Pkg.VersionList();
  222. Dynamic<pkgCache::VerIterator> DynVer(Ver);
  223. map_ptrloc *LastVer = &Pkg->VersionList;
  224. void const * oldMap = Map.Data();
  225. int Res = 1;
  226. unsigned long const Hash = List.VersionHash();
  227. for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
  228. {
  229. Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
  230. // Version is higher as current version - insert here
  231. if (Res > 0)
  232. break;
  233. // Versionstrings are equal - is hash also equal?
  234. if (Res == 0 && Ver->Hash == Hash)
  235. break;
  236. // proceed with the next till we have either the right
  237. // or we found another version (which will be lower)
  238. }
  239. /* We already have a version for this item, record that we saw it */
  240. if (Res == 0 && Ver.end() == false && Ver->Hash == Hash)
  241. {
  242. if (List.UsePackage(Pkg,Ver) == false)
  243. return _error->Error(_("Error occurred while processing %s (UsePackage2)"),
  244. PackageName.c_str());
  245. if (NewFileVer(Ver,List) == false)
  246. return _error->Error(_("Error occurred while processing %s (NewFileVer1)"),
  247. PackageName.c_str());
  248. // Read only a single record and return
  249. if (OutVer != 0)
  250. {
  251. *OutVer = Ver;
  252. FoundFileDeps |= List.HasFileDeps();
  253. return true;
  254. }
  255. continue;
  256. }
  257. // Add a new version
  258. map_ptrloc const verindex = NewVersion(Ver,Version,*LastVer);
  259. if (verindex == 0 && _error->PendingError())
  260. return _error->Error(_("Error occurred while processing %s (NewVersion%d)"),
  261. PackageName.c_str(), 1);
  262. if (oldMap != Map.Data())
  263. LastVer += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  264. *LastVer = verindex;
  265. Ver->ParentPkg = Pkg.Index();
  266. Ver->Hash = Hash;
  267. if (List.NewVersion(Ver) == false)
  268. return _error->Error(_("Error occurred while processing %s (NewVersion%d)"),
  269. PackageName.c_str(), 2);
  270. if (List.UsePackage(Pkg,Ver) == false)
  271. return _error->Error(_("Error occurred while processing %s (UsePackage3)"),
  272. PackageName.c_str());
  273. if (NewFileVer(Ver,List) == false)
  274. return _error->Error(_("Error occurred while processing %s (NewVersion%d)"),
  275. PackageName.c_str(), 3);
  276. // Read only a single record and return
  277. if (OutVer != 0)
  278. {
  279. *OutVer = Ver;
  280. FoundFileDeps |= List.HasFileDeps();
  281. return true;
  282. }
  283. /* Record the Description data. Description data always exist in
  284. Packages and Translation-* files. */
  285. pkgCache::DescIterator Desc = Ver.DescriptionList();
  286. Dynamic<pkgCache::DescIterator> DynDesc(Desc);
  287. map_ptrloc *LastDesc = &Ver->DescriptionList;
  288. // Skip to the end of description set
  289. for (; Desc.end() == false; LastDesc = &Desc->NextDesc, Desc++);
  290. // Add new description
  291. oldMap = Map.Data();
  292. map_ptrloc const descindex = NewDescription(Desc, List.DescriptionLanguage(), List.Description_md5(), *LastDesc);
  293. if (oldMap != Map.Data())
  294. LastDesc += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  295. *LastDesc = descindex;
  296. Desc->ParentPkg = Pkg.Index();
  297. if ((*LastDesc == 0 && _error->PendingError()) || NewFileDesc(Desc,List) == false)
  298. return _error->Error(_("Error occurred while processing %s (NewFileDesc2)"),PackageName.c_str());
  299. }
  300. FoundFileDeps |= List.HasFileDeps();
  301. if (Cache.HeaderP->PackageCount >= (1ULL<<sizeof(Cache.PkgP->ID)*8)-1)
  302. return _error->Error(_("Wow, you exceeded the number of package "
  303. "names this APT is capable of."));
  304. if (Cache.HeaderP->VersionCount >= (1ULL<<(sizeof(Cache.VerP->ID)*8))-1)
  305. return _error->Error(_("Wow, you exceeded the number of versions "
  306. "this APT is capable of."));
  307. if (Cache.HeaderP->DescriptionCount >= (1ULL<<(sizeof(Cache.DescP->ID)*8))-1)
  308. return _error->Error(_("Wow, you exceeded the number of descriptions "
  309. "this APT is capable of."));
  310. if (Cache.HeaderP->DependsCount >= (1ULL<<(sizeof(Cache.DepP->ID)*8))-1ULL)
  311. return _error->Error(_("Wow, you exceeded the number of dependencies "
  312. "this APT is capable of."));
  313. return true;
  314. }
  315. /*}}}*/
  316. // CacheGenerator::MergeFileProvides - Merge file provides /*{{{*/
  317. // ---------------------------------------------------------------------
  318. /* If we found any file depends while parsing the main list we need to
  319. resolve them. Since it is undesired to load the entire list of files
  320. into the cache as virtual packages we do a two stage effort. MergeList
  321. identifies the file depends and this creates Provdies for them by
  322. re-parsing all the indexs. */
  323. bool pkgCacheGenerator::MergeFileProvides(ListParser &List)
  324. {
  325. List.Owner = this;
  326. unsigned int Counter = 0;
  327. while (List.Step() == true)
  328. {
  329. string PackageName = List.Package();
  330. if (PackageName.empty() == true)
  331. return false;
  332. string Version = List.Version();
  333. if (Version.empty() == true)
  334. continue;
  335. pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
  336. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  337. if (Pkg.end() == true)
  338. return _error->Error(_("Error occurred while processing %s (FindPkg)"),
  339. PackageName.c_str());
  340. Counter++;
  341. if (Counter % 100 == 0 && Progress != 0)
  342. Progress->Progress(List.Offset());
  343. unsigned long Hash = List.VersionHash();
  344. pkgCache::VerIterator Ver = Pkg.VersionList();
  345. Dynamic<pkgCache::VerIterator> DynVer(Ver);
  346. for (; Ver.end() == false; Ver++)
  347. {
  348. if (Ver->Hash == Hash && Version.c_str() == Ver.VerStr())
  349. {
  350. if (List.CollectFileProvides(Cache,Ver) == false)
  351. return _error->Error(_("Error occurred while processing %s (CollectFileProvides)"),PackageName.c_str());
  352. break;
  353. }
  354. }
  355. if (Ver.end() == true)
  356. _error->Warning(_("Package %s %s was not found while processing file dependencies"),PackageName.c_str(),Version.c_str());
  357. }
  358. return true;
  359. }
  360. /*}}}*/
  361. // CacheGenerator::NewGroup - Add a new group /*{{{*/
  362. // ---------------------------------------------------------------------
  363. /* This creates a new group structure and adds it to the hash table */
  364. bool pkgCacheGenerator::NewGroup(pkgCache::GrpIterator &Grp, const string &Name)
  365. {
  366. Grp = Cache.FindGrp(Name);
  367. if (Grp.end() == false)
  368. return true;
  369. // Get a structure
  370. map_ptrloc const Group = AllocateInMap(sizeof(pkgCache::Group));
  371. if (unlikely(Group == 0))
  372. return false;
  373. Grp = pkgCache::GrpIterator(Cache, Cache.GrpP + Group);
  374. map_ptrloc const idxName = WriteStringInMap(Name);
  375. if (unlikely(idxName == 0))
  376. return false;
  377. Grp->Name = idxName;
  378. // Insert it into the hash table
  379. unsigned long const Hash = Cache.Hash(Name);
  380. Grp->Next = Cache.HeaderP->GrpHashTable[Hash];
  381. Cache.HeaderP->GrpHashTable[Hash] = Group;
  382. Grp->ID = Cache.HeaderP->GroupCount++;
  383. return true;
  384. }
  385. /*}}}*/
  386. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  387. // ---------------------------------------------------------------------
  388. /* This creates a new package structure and adds it to the hash table */
  389. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name,
  390. const string &Arch) {
  391. pkgCache::GrpIterator Grp;
  392. Dynamic<pkgCache::GrpIterator> DynGrp(Grp);
  393. if (unlikely(NewGroup(Grp, Name) == false))
  394. return false;
  395. Pkg = Grp.FindPkg(Arch);
  396. if (Pkg.end() == false)
  397. return true;
  398. // Get a structure
  399. map_ptrloc const Package = AllocateInMap(sizeof(pkgCache::Package));
  400. if (unlikely(Package == 0))
  401. return false;
  402. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  403. // Insert the package into our package list
  404. if (Grp->FirstPackage == 0) // the group is new
  405. {
  406. // Insert it into the hash table
  407. unsigned long const Hash = Cache.Hash(Name);
  408. Pkg->NextPackage = Cache.HeaderP->PkgHashTable[Hash];
  409. Cache.HeaderP->PkgHashTable[Hash] = Package;
  410. Grp->FirstPackage = Package;
  411. }
  412. else // Group the Packages together
  413. {
  414. // this package is the new last package
  415. pkgCache::PkgIterator LastPkg(Cache, Cache.PkgP + Grp->LastPackage);
  416. Pkg->NextPackage = LastPkg->NextPackage;
  417. LastPkg->NextPackage = Package;
  418. }
  419. Grp->LastPackage = Package;
  420. // Set the name, arch and the ID
  421. Pkg->Name = Grp->Name;
  422. Pkg->Group = Grp.Index();
  423. map_ptrloc const idxArch = WriteUniqString((Arch == "all") ? _config->Find("APT::Architecture") : Arch.c_str());
  424. if (unlikely(idxArch == 0))
  425. return false;
  426. Pkg->Arch = idxArch;
  427. Pkg->ID = Cache.HeaderP->PackageCount++;
  428. return true;
  429. }
  430. /*}}}*/
  431. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  432. // ---------------------------------------------------------------------
  433. /* */
  434. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  435. ListParser &List)
  436. {
  437. if (CurrentFile == 0)
  438. return true;
  439. // Get a structure
  440. map_ptrloc const VerFile = AllocateInMap(sizeof(pkgCache::VerFile));
  441. if (VerFile == 0)
  442. return 0;
  443. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  444. VF->File = CurrentFile - Cache.PkgFileP;
  445. // Link it to the end of the list
  446. map_ptrloc *Last = &Ver->FileList;
  447. for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; V++)
  448. Last = &V->NextFile;
  449. VF->NextFile = *Last;
  450. *Last = VF.Index();
  451. VF->Offset = List.Offset();
  452. VF->Size = List.Size();
  453. if (Cache.HeaderP->MaxVerFileSize < VF->Size)
  454. Cache.HeaderP->MaxVerFileSize = VF->Size;
  455. Cache.HeaderP->VerFileCount++;
  456. return true;
  457. }
  458. /*}}}*/
  459. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  460. // ---------------------------------------------------------------------
  461. /* This puts a version structure in the linked list */
  462. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  463. const string &VerStr,
  464. unsigned long Next)
  465. {
  466. // Get a structure
  467. map_ptrloc const Version = AllocateInMap(sizeof(pkgCache::Version));
  468. if (Version == 0)
  469. return 0;
  470. // Fill it in
  471. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  472. Ver->NextVer = Next;
  473. Ver->ID = Cache.HeaderP->VersionCount++;
  474. map_ptrloc const idxVerStr = WriteStringInMap(VerStr);
  475. if (unlikely(idxVerStr == 0))
  476. return 0;
  477. Ver->VerStr = idxVerStr;
  478. return Version;
  479. }
  480. /*}}}*/
  481. // CacheGenerator::NewFileDesc - Create a new File<->Desc association /*{{{*/
  482. // ---------------------------------------------------------------------
  483. /* */
  484. bool pkgCacheGenerator::NewFileDesc(pkgCache::DescIterator &Desc,
  485. ListParser &List)
  486. {
  487. if (CurrentFile == 0)
  488. return true;
  489. // Get a structure
  490. map_ptrloc const DescFile = AllocateInMap(sizeof(pkgCache::DescFile));
  491. if (DescFile == 0)
  492. return false;
  493. pkgCache::DescFileIterator DF(Cache,Cache.DescFileP + DescFile);
  494. DF->File = CurrentFile - Cache.PkgFileP;
  495. // Link it to the end of the list
  496. map_ptrloc *Last = &Desc->FileList;
  497. for (pkgCache::DescFileIterator D = Desc.FileList(); D.end() == false; D++)
  498. Last = &D->NextFile;
  499. DF->NextFile = *Last;
  500. *Last = DF.Index();
  501. DF->Offset = List.Offset();
  502. DF->Size = List.Size();
  503. if (Cache.HeaderP->MaxDescFileSize < DF->Size)
  504. Cache.HeaderP->MaxDescFileSize = DF->Size;
  505. Cache.HeaderP->DescFileCount++;
  506. return true;
  507. }
  508. /*}}}*/
  509. // CacheGenerator::NewDescription - Create a new Description /*{{{*/
  510. // ---------------------------------------------------------------------
  511. /* This puts a description structure in the linked list */
  512. map_ptrloc pkgCacheGenerator::NewDescription(pkgCache::DescIterator &Desc,
  513. const string &Lang,
  514. const MD5SumValue &md5sum,
  515. map_ptrloc Next)
  516. {
  517. // Get a structure
  518. map_ptrloc const Description = AllocateInMap(sizeof(pkgCache::Description));
  519. if (Description == 0)
  520. return 0;
  521. // Fill it in
  522. Desc = pkgCache::DescIterator(Cache,Cache.DescP + Description);
  523. Desc->NextDesc = Next;
  524. Desc->ID = Cache.HeaderP->DescriptionCount++;
  525. map_ptrloc const idxlanguage_code = WriteStringInMap(Lang);
  526. map_ptrloc const idxmd5sum = WriteStringInMap(md5sum.Value());
  527. if (unlikely(idxlanguage_code == 0 || idxmd5sum == 0))
  528. return 0;
  529. Desc->language_code = idxlanguage_code;
  530. Desc->md5sum = idxmd5sum;
  531. return Description;
  532. }
  533. /*}}}*/
  534. // CacheGenerator::FinishCache - do various finish operations /*{{{*/
  535. // ---------------------------------------------------------------------
  536. /* This prepares the Cache for delivery */
  537. bool pkgCacheGenerator::FinishCache(OpProgress *Progress)
  538. {
  539. // FIXME: add progress reporting for this operation
  540. // Do we have different architectures in your groups ?
  541. vector<string> archs = APT::Configuration::getArchitectures();
  542. if (archs.size() > 1)
  543. {
  544. // Create Conflicts in between the group
  545. pkgCache::GrpIterator G = GetCache().GrpBegin();
  546. Dynamic<pkgCache::GrpIterator> DynG(G);
  547. for (; G.end() != true; G++)
  548. {
  549. string const PkgName = G.Name();
  550. pkgCache::PkgIterator P = G.PackageList();
  551. Dynamic<pkgCache::PkgIterator> DynP(P);
  552. for (; P.end() != true; P = G.NextPkg(P))
  553. {
  554. pkgCache::PkgIterator allPkg;
  555. Dynamic<pkgCache::PkgIterator> DynallPkg(allPkg);
  556. pkgCache::VerIterator V = P.VersionList();
  557. Dynamic<pkgCache::VerIterator> DynV(V);
  558. for (; V.end() != true; V++)
  559. {
  560. char const * const Arch = P.Arch();
  561. map_ptrloc *OldDepLast = NULL;
  562. /* MultiArch handling introduces a lot of implicit Dependencies:
  563. - MultiArch: same → Co-Installable if they have the same version
  564. - Architecture: all → Need to be Co-Installable for internal reasons
  565. - All others conflict with all other group members */
  566. bool const coInstall = ((V->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same);
  567. for (vector<string>::const_iterator A = archs.begin(); A != archs.end(); ++A)
  568. {
  569. if (*A == Arch)
  570. continue;
  571. /* We allow only one installed arch at the time
  572. per group, therefore each group member conflicts
  573. with all other group members */
  574. pkgCache::PkgIterator D = G.FindPkg(*A);
  575. Dynamic<pkgCache::PkgIterator> DynD(D);
  576. if (D.end() == true)
  577. continue;
  578. if (coInstall == true)
  579. {
  580. // Replaces: ${self}:other ( << ${binary:Version})
  581. NewDepends(D, V, V.VerStr(),
  582. pkgCache::Dep::Less, pkgCache::Dep::Replaces,
  583. OldDepLast);
  584. // Breaks: ${self}:other (!= ${binary:Version})
  585. NewDepends(D, V, V.VerStr(),
  586. pkgCache::Dep::NotEquals, pkgCache::Dep::DpkgBreaks,
  587. OldDepLast);
  588. } else {
  589. // Conflicts: ${self}:other
  590. NewDepends(D, V, "",
  591. pkgCache::Dep::NoOp, pkgCache::Dep::Conflicts,
  592. OldDepLast);
  593. }
  594. }
  595. }
  596. }
  597. }
  598. }
  599. return true;
  600. }
  601. /*}}}*/
  602. // CacheGenerator::NewDepends - Create a dependency element /*{{{*/
  603. // ---------------------------------------------------------------------
  604. /* This creates a dependency element in the tree. It is linked to the
  605. version and to the package that it is pointing to. */
  606. bool pkgCacheGenerator::NewDepends(pkgCache::PkgIterator &Pkg,
  607. pkgCache::VerIterator &Ver,
  608. string const &Version,
  609. unsigned int const &Op,
  610. unsigned int const &Type,
  611. map_ptrloc *OldDepLast)
  612. {
  613. void const * const oldMap = Map.Data();
  614. // Get a structure
  615. map_ptrloc const Dependency = AllocateInMap(sizeof(pkgCache::Dependency));
  616. if (unlikely(Dependency == 0))
  617. return false;
  618. // Fill it in
  619. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  620. Dynamic<pkgCache::DepIterator> DynDep(Dep);
  621. Dep->ParentVer = Ver.Index();
  622. Dep->Type = Type;
  623. Dep->CompareOp = Op;
  624. Dep->ID = Cache.HeaderP->DependsCount++;
  625. // Probe the reverse dependency list for a version string that matches
  626. if (Version.empty() == false)
  627. {
  628. /* for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  629. if (I->Version != 0 && I.TargetVer() == Version)
  630. Dep->Version = I->Version;*/
  631. if (Dep->Version == 0) {
  632. map_ptrloc const index = WriteStringInMap(Version);
  633. if (unlikely(index == 0))
  634. return false;
  635. Dep->Version = index;
  636. }
  637. }
  638. // Link it to the package
  639. Dep->Package = Pkg.Index();
  640. Dep->NextRevDepends = Pkg->RevDepends;
  641. Pkg->RevDepends = Dep.Index();
  642. // Do we know where to link the Dependency to?
  643. if (OldDepLast == NULL)
  644. {
  645. OldDepLast = &Ver->DependsList;
  646. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  647. OldDepLast = &D->NextDepends;
  648. } else if (oldMap != Map.Data())
  649. OldDepLast += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  650. Dep->NextDepends = *OldDepLast;
  651. *OldDepLast = Dep.Index();
  652. OldDepLast = &Dep->NextDepends;
  653. return true;
  654. }
  655. /*}}}*/
  656. // ListParser::NewDepends - Create the environment for a new dependency /*{{{*/
  657. // ---------------------------------------------------------------------
  658. /* This creates a Group and the Package to link this dependency to if
  659. needed and handles also the caching of the old endpoint */
  660. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator &Ver,
  661. const string &PackageName,
  662. const string &Arch,
  663. const string &Version,
  664. unsigned int Op,
  665. unsigned int Type)
  666. {
  667. pkgCache::GrpIterator Grp;
  668. Dynamic<pkgCache::GrpIterator> DynGrp(Grp);
  669. if (unlikely(Owner->NewGroup(Grp, PackageName) == false))
  670. return false;
  671. // Locate the target package
  672. pkgCache::PkgIterator Pkg = Grp.FindPkg(Arch);
  673. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  674. if (Pkg.end() == true) {
  675. if (unlikely(Owner->NewPackage(Pkg, PackageName, Arch) == false))
  676. return false;
  677. }
  678. // Is it a file dependency?
  679. if (unlikely(PackageName[0] == '/'))
  680. FoundFileDeps = true;
  681. /* Caching the old end point speeds up generation substantially */
  682. if (OldDepVer != Ver) {
  683. OldDepLast = NULL;
  684. OldDepVer = Ver;
  685. }
  686. return Owner->NewDepends(Pkg, Ver, Version, Op, Type, OldDepLast);
  687. }
  688. /*}}}*/
  689. // ListParser::NewProvides - Create a Provides element /*{{{*/
  690. // ---------------------------------------------------------------------
  691. /* */
  692. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator &Ver,
  693. const string &PkgName,
  694. const string &PkgArch,
  695. const string &Version)
  696. {
  697. pkgCache &Cache = Owner->Cache;
  698. // We do not add self referencing provides
  699. if (Ver.ParentPkg().Name() == PkgName && (PkgArch == Ver.ParentPkg().Arch() ||
  700. (PkgArch == "all" && _config->Find("APT::Architecture") == Ver.ParentPkg().Arch())))
  701. return true;
  702. // Get a structure
  703. map_ptrloc const Provides = Owner->AllocateInMap(sizeof(pkgCache::Provides));
  704. if (unlikely(Provides == 0))
  705. return false;
  706. Cache.HeaderP->ProvidesCount++;
  707. // Fill it in
  708. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  709. Dynamic<pkgCache::PrvIterator> DynPrv(Prv);
  710. Prv->Version = Ver.Index();
  711. Prv->NextPkgProv = Ver->ProvidesList;
  712. Ver->ProvidesList = Prv.Index();
  713. if (Version.empty() == false && unlikely((Prv->ProvideVersion = WriteString(Version)) == 0))
  714. return false;
  715. // Locate the target package
  716. pkgCache::PkgIterator Pkg;
  717. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  718. if (unlikely(Owner->NewPackage(Pkg,PkgName, PkgArch) == false))
  719. return false;
  720. // Link it to the package
  721. Prv->ParentPkg = Pkg.Index();
  722. Prv->NextProvides = Pkg->ProvidesList;
  723. Pkg->ProvidesList = Prv.Index();
  724. return true;
  725. }
  726. /*}}}*/
  727. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  728. // ---------------------------------------------------------------------
  729. /* This is used to select which file is to be associated with all newly
  730. added versions. The caller is responsible for setting the IMS fields. */
  731. bool pkgCacheGenerator::SelectFile(const string &File,const string &Site,
  732. const pkgIndexFile &Index,
  733. unsigned long Flags)
  734. {
  735. // Get some space for the structure
  736. map_ptrloc const idxFile = AllocateInMap(sizeof(*CurrentFile));
  737. if (unlikely(idxFile == 0))
  738. return false;
  739. CurrentFile = Cache.PkgFileP + idxFile;
  740. // Fill it in
  741. map_ptrloc const idxFileName = WriteStringInMap(File);
  742. map_ptrloc const idxSite = WriteUniqString(Site);
  743. if (unlikely(idxFileName == 0 || idxSite == 0))
  744. return false;
  745. CurrentFile->FileName = idxFileName;
  746. CurrentFile->Site = idxSite;
  747. CurrentFile->NextFile = Cache.HeaderP->FileList;
  748. CurrentFile->Flags = Flags;
  749. CurrentFile->ID = Cache.HeaderP->PackageFileCount;
  750. map_ptrloc const idxIndexType = WriteUniqString(Index.GetType()->Label);
  751. if (unlikely(idxIndexType == 0))
  752. return false;
  753. CurrentFile->IndexType = idxIndexType;
  754. PkgFileName = File;
  755. Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
  756. Cache.HeaderP->PackageFileCount++;
  757. if (Progress != 0)
  758. Progress->SubProgress(Index.Size());
  759. return true;
  760. }
  761. /*}}}*/
  762. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  763. // ---------------------------------------------------------------------
  764. /* This is used to create handles to strings. Given the same text it
  765. always returns the same number */
  766. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  767. unsigned int Size)
  768. {
  769. /* We use a very small transient hash table here, this speeds up generation
  770. by a fair amount on slower machines */
  771. pkgCache::StringItem *&Bucket = UniqHash[(S[0]*5 + S[1]) % _count(UniqHash)];
  772. if (Bucket != 0 &&
  773. stringcmp(S,S+Size,Cache.StrP + Bucket->String) == 0)
  774. return Bucket->String;
  775. // Search for an insertion point
  776. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  777. int Res = 1;
  778. map_ptrloc *Last = &Cache.HeaderP->StringList;
  779. for (; I != Cache.StringItemP; Last = &I->NextItem,
  780. I = Cache.StringItemP + I->NextItem)
  781. {
  782. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  783. if (Res >= 0)
  784. break;
  785. }
  786. // Match
  787. if (Res == 0)
  788. {
  789. Bucket = I;
  790. return I->String;
  791. }
  792. // Get a structure
  793. void const * const oldMap = Map.Data();
  794. map_ptrloc const Item = AllocateInMap(sizeof(pkgCache::StringItem));
  795. if (Item == 0)
  796. return 0;
  797. map_ptrloc const idxString = WriteStringInMap(S,Size);
  798. if (unlikely(idxString == 0))
  799. return 0;
  800. if (oldMap != Map.Data()) {
  801. Last += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  802. I += (pkgCache::StringItem*) Map.Data() - (pkgCache::StringItem*) oldMap;
  803. }
  804. *Last = Item;
  805. // Fill in the structure
  806. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  807. ItemP->NextItem = I - Cache.StringItemP;
  808. ItemP->String = idxString;
  809. Bucket = ItemP;
  810. return ItemP->String;
  811. }
  812. /*}}}*/
  813. // CheckValidity - Check that a cache is up-to-date /*{{{*/
  814. // ---------------------------------------------------------------------
  815. /* This just verifies that each file in the list of index files exists,
  816. has matching attributes with the cache and the cache does not have
  817. any extra files. */
  818. static bool CheckValidity(const string &CacheFile, FileIterator Start,
  819. FileIterator End,MMap **OutMap = 0)
  820. {
  821. bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
  822. // No file, certainly invalid
  823. if (CacheFile.empty() == true || FileExists(CacheFile) == false)
  824. {
  825. if (Debug == true)
  826. std::clog << "CacheFile doesn't exist" << std::endl;
  827. return false;
  828. }
  829. // Map it
  830. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  831. SPtr<MMap> Map = new MMap(CacheF,0);
  832. pkgCache Cache(Map);
  833. if (_error->PendingError() == true || Map->Size() == 0)
  834. {
  835. if (Debug == true)
  836. std::clog << "Errors are pending or Map is empty()" << std::endl;
  837. _error->Discard();
  838. return false;
  839. }
  840. /* Now we check every index file, see if it is in the cache,
  841. verify the IMS data and check that it is on the disk too.. */
  842. SPtrArray<bool> Visited = new bool[Cache.HeaderP->PackageFileCount];
  843. memset(Visited,0,sizeof(*Visited)*Cache.HeaderP->PackageFileCount);
  844. for (; Start != End; Start++)
  845. {
  846. if (Debug == true)
  847. std::clog << "Checking PkgFile " << (*Start)->Describe() << ": ";
  848. if ((*Start)->HasPackages() == false)
  849. {
  850. if (Debug == true)
  851. std::clog << "Has NO packages" << std::endl;
  852. continue;
  853. }
  854. if ((*Start)->Exists() == false)
  855. {
  856. #if 0 // mvo: we no longer give a message here (Default Sources spec)
  857. _error->WarningE("stat",_("Couldn't stat source package list %s"),
  858. (*Start)->Describe().c_str());
  859. #endif
  860. if (Debug == true)
  861. std::clog << "file doesn't exist" << std::endl;
  862. continue;
  863. }
  864. // FindInCache is also expected to do an IMS check.
  865. pkgCache::PkgFileIterator File = (*Start)->FindInCache(Cache);
  866. if (File.end() == true)
  867. {
  868. if (Debug == true)
  869. std::clog << "FindInCache returned end-Pointer" << std::endl;
  870. return false;
  871. }
  872. Visited[File->ID] = true;
  873. if (Debug == true)
  874. std::clog << "with ID " << File->ID << " is valid" << std::endl;
  875. }
  876. for (unsigned I = 0; I != Cache.HeaderP->PackageFileCount; I++)
  877. if (Visited[I] == false)
  878. {
  879. if (Debug == true)
  880. std::clog << "File with ID" << I << " wasn't visited" << std::endl;
  881. return false;
  882. }
  883. if (_error->PendingError() == true)
  884. {
  885. if (Debug == true)
  886. {
  887. std::clog << "Validity failed because of pending errors:" << std::endl;
  888. _error->DumpErrors();
  889. }
  890. _error->Discard();
  891. return false;
  892. }
  893. if (OutMap != 0)
  894. *OutMap = Map.UnGuard();
  895. return true;
  896. }
  897. /*}}}*/
  898. // ComputeSize - Compute the total size of a bunch of files /*{{{*/
  899. // ---------------------------------------------------------------------
  900. /* Size is kind of an abstract notion that is only used for the progress
  901. meter */
  902. static unsigned long ComputeSize(FileIterator Start,FileIterator End)
  903. {
  904. unsigned long TotalSize = 0;
  905. for (; Start != End; Start++)
  906. {
  907. if ((*Start)->HasPackages() == false)
  908. continue;
  909. TotalSize += (*Start)->Size();
  910. }
  911. return TotalSize;
  912. }
  913. /*}}}*/
  914. // BuildCache - Merge the list of index files into the cache /*{{{*/
  915. // ---------------------------------------------------------------------
  916. /* */
  917. static bool BuildCache(pkgCacheGenerator &Gen,
  918. OpProgress *Progress,
  919. unsigned long &CurrentSize,unsigned long TotalSize,
  920. FileIterator Start, FileIterator End)
  921. {
  922. FileIterator I;
  923. for (I = Start; I != End; I++)
  924. {
  925. if ((*I)->HasPackages() == false)
  926. continue;
  927. if ((*I)->Exists() == false)
  928. continue;
  929. if ((*I)->FindInCache(Gen.GetCache()).end() == false)
  930. {
  931. _error->Warning("Duplicate sources.list entry %s",
  932. (*I)->Describe().c_str());
  933. continue;
  934. }
  935. unsigned long Size = (*I)->Size();
  936. if (Progress != NULL)
  937. Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
  938. CurrentSize += Size;
  939. if ((*I)->Merge(Gen,Progress) == false)
  940. return false;
  941. }
  942. if (Gen.HasFileDeps() == true)
  943. {
  944. if (Progress != NULL)
  945. Progress->Done();
  946. TotalSize = ComputeSize(Start, End);
  947. CurrentSize = 0;
  948. for (I = Start; I != End; I++)
  949. {
  950. unsigned long Size = (*I)->Size();
  951. if (Progress != NULL)
  952. Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
  953. CurrentSize += Size;
  954. if ((*I)->MergeFileProvides(Gen,Progress) == false)
  955. return false;
  956. }
  957. }
  958. return true;
  959. }
  960. /*}}}*/
  961. // CacheGenerator::CreateDynamicMMap - load an mmap with configuration options /*{{{*/
  962. DynamicMMap* pkgCacheGenerator::CreateDynamicMMap(FileFd *CacheF, unsigned long Flags) {
  963. unsigned long const MapStart = _config->FindI("APT::Cache-Start", 24*1024*1024);
  964. unsigned long const MapGrow = _config->FindI("APT::Cache-Grow", 1*1024*1024);
  965. unsigned long const MapLimit = _config->FindI("APT::Cache-Limit", 0);
  966. Flags |= MMap::Moveable;
  967. if (_config->FindB("APT::Cache-Fallback", false) == true)
  968. Flags |= MMap::Fallback;
  969. if (CacheF != NULL)
  970. return new DynamicMMap(*CacheF, Flags, MapStart, MapGrow, MapLimit);
  971. else
  972. return new DynamicMMap(Flags, MapStart, MapGrow, MapLimit);
  973. }
  974. /*}}}*/
  975. // CacheGenerator::MakeStatusCache - Construct the status cache /*{{{*/
  976. // ---------------------------------------------------------------------
  977. /* This makes sure that the status cache (the cache that has all
  978. index files from the sources list and all local ones) is ready
  979. to be mmaped. If OutMap is not zero then a MMap object representing
  980. the cache will be stored there. This is pretty much mandetory if you
  981. are using AllowMem. AllowMem lets the function be run as non-root
  982. where it builds the cache 'fast' into a memory buffer. */
  983. __deprecated bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
  984. MMap **OutMap, bool AllowMem)
  985. { return pkgCacheGenerator::MakeStatusCache(List, &Progress, OutMap, AllowMem); }
  986. bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
  987. MMap **OutMap,bool AllowMem)
  988. {
  989. bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
  990. vector<pkgIndexFile *> Files;
  991. for (vector<metaIndex *>::const_iterator i = List.begin();
  992. i != List.end();
  993. i++)
  994. {
  995. vector <pkgIndexFile *> *Indexes = (*i)->GetIndexFiles();
  996. for (vector<pkgIndexFile *>::const_iterator j = Indexes->begin();
  997. j != Indexes->end();
  998. j++)
  999. Files.push_back (*j);
  1000. }
  1001. unsigned long const EndOfSource = Files.size();
  1002. if (_system->AddStatusFiles(Files) == false)
  1003. return false;
  1004. // Decide if we can write to the files..
  1005. string const CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  1006. string const SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  1007. // ensure the cache directory exists
  1008. if (CacheFile.empty() == false || SrcCacheFile.empty() == false)
  1009. {
  1010. string dir = _config->FindDir("Dir::Cache");
  1011. size_t const len = dir.size();
  1012. if (len > 5 && dir.find("/apt/", len - 6, 5) == len - 5)
  1013. dir = dir.substr(0, len - 5);
  1014. if (CacheFile.empty() == false)
  1015. CreateDirectory(dir, flNotFile(CacheFile));
  1016. if (SrcCacheFile.empty() == false)
  1017. CreateDirectory(dir, flNotFile(SrcCacheFile));
  1018. }
  1019. // Decide if we can write to the cache
  1020. bool Writeable = false;
  1021. if (CacheFile.empty() == false)
  1022. Writeable = access(flNotFile(CacheFile).c_str(),W_OK) == 0;
  1023. else
  1024. if (SrcCacheFile.empty() == false)
  1025. Writeable = access(flNotFile(SrcCacheFile).c_str(),W_OK) == 0;
  1026. if (Debug == true)
  1027. std::clog << "Do we have write-access to the cache files? " << (Writeable ? "YES" : "NO") << std::endl;
  1028. if (Writeable == false && AllowMem == false && CacheFile.empty() == false)
  1029. return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
  1030. if (Progress != NULL)
  1031. Progress->OverallProgress(0,1,1,_("Reading package lists"));
  1032. // Cache is OK, Fin.
  1033. if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
  1034. {
  1035. if (Progress != NULL)
  1036. Progress->OverallProgress(1,1,1,_("Reading package lists"));
  1037. if (Debug == true)
  1038. std::clog << "pkgcache.bin is valid - no need to build anything" << std::endl;
  1039. return true;
  1040. }
  1041. else if (Debug == true)
  1042. std::clog << "pkgcache.bin is NOT valid" << std::endl;
  1043. /* At this point we know we need to reconstruct the package cache,
  1044. begin. */
  1045. SPtr<FileFd> CacheF;
  1046. SPtr<DynamicMMap> Map;
  1047. if (Writeable == true && CacheFile.empty() == false)
  1048. {
  1049. unlink(CacheFile.c_str());
  1050. CacheF = new FileFd(CacheFile,FileFd::WriteAtomic);
  1051. fchmod(CacheF->Fd(),0644);
  1052. Map = CreateDynamicMMap(CacheF, MMap::Public);
  1053. if (_error->PendingError() == true)
  1054. return false;
  1055. if (Debug == true)
  1056. std::clog << "Open filebased MMap" << std::endl;
  1057. }
  1058. else
  1059. {
  1060. // Just build it in memory..
  1061. Map = CreateDynamicMMap(NULL);
  1062. if (Debug == true)
  1063. std::clog << "Open memory Map (not filebased)" << std::endl;
  1064. }
  1065. // Lets try the source cache.
  1066. unsigned long CurrentSize = 0;
  1067. unsigned long TotalSize = 0;
  1068. if (CheckValidity(SrcCacheFile,Files.begin(),
  1069. Files.begin()+EndOfSource) == true)
  1070. {
  1071. if (Debug == true)
  1072. std::clog << "srcpkgcache.bin is valid - populate MMap with it." << std::endl;
  1073. // Preload the map with the source cache
  1074. FileFd SCacheF(SrcCacheFile,FileFd::ReadOnly);
  1075. unsigned long const alloc = Map->RawAllocate(SCacheF.Size());
  1076. if ((alloc == 0 && _error->PendingError())
  1077. || SCacheF.Read((unsigned char *)Map->Data() + alloc,
  1078. SCacheF.Size()) == false)
  1079. return false;
  1080. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  1081. // Build the status cache
  1082. pkgCacheGenerator Gen(Map.Get(),Progress);
  1083. if (_error->PendingError() == true)
  1084. return false;
  1085. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1086. Files.begin()+EndOfSource,Files.end()) == false)
  1087. return false;
  1088. // FIXME: move me to a better place
  1089. Gen.FinishCache(Progress);
  1090. }
  1091. else
  1092. {
  1093. if (Debug == true)
  1094. std::clog << "srcpkgcache.bin is NOT valid - rebuild" << std::endl;
  1095. TotalSize = ComputeSize(Files.begin(),Files.end());
  1096. // Build the source cache
  1097. pkgCacheGenerator Gen(Map.Get(),Progress);
  1098. if (_error->PendingError() == true)
  1099. return false;
  1100. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1101. Files.begin(),Files.begin()+EndOfSource) == false)
  1102. return false;
  1103. // Write it back
  1104. if (Writeable == true && SrcCacheFile.empty() == false)
  1105. {
  1106. FileFd SCacheF(SrcCacheFile,FileFd::WriteAtomic);
  1107. if (_error->PendingError() == true)
  1108. return false;
  1109. fchmod(SCacheF.Fd(),0644);
  1110. // Write out the main data
  1111. if (SCacheF.Write(Map->Data(),Map->Size()) == false)
  1112. return _error->Error(_("IO Error saving source cache"));
  1113. SCacheF.Sync();
  1114. // Write out the proper header
  1115. Gen.GetCache().HeaderP->Dirty = false;
  1116. if (SCacheF.Seek(0) == false ||
  1117. SCacheF.Write(Map->Data(),sizeof(*Gen.GetCache().HeaderP)) == false)
  1118. return _error->Error(_("IO Error saving source cache"));
  1119. Gen.GetCache().HeaderP->Dirty = true;
  1120. SCacheF.Sync();
  1121. }
  1122. // Build the status cache
  1123. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1124. Files.begin()+EndOfSource,Files.end()) == false)
  1125. return false;
  1126. // FIXME: move me to a better place
  1127. Gen.FinishCache(Progress);
  1128. }
  1129. if (Debug == true)
  1130. std::clog << "Caches are ready for shipping" << std::endl;
  1131. if (_error->PendingError() == true)
  1132. return false;
  1133. if (OutMap != 0)
  1134. {
  1135. if (CacheF != 0)
  1136. {
  1137. delete Map.UnGuard();
  1138. *OutMap = new MMap(*CacheF,0);
  1139. }
  1140. else
  1141. {
  1142. *OutMap = Map.UnGuard();
  1143. }
  1144. }
  1145. return true;
  1146. }
  1147. /*}}}*/
  1148. // CacheGenerator::MakeOnlyStatusCache - Build only a status files cache/*{{{*/
  1149. // ---------------------------------------------------------------------
  1150. /* */
  1151. __deprecated bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
  1152. { return pkgCacheGenerator::MakeOnlyStatusCache(&Progress, OutMap); }
  1153. bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap)
  1154. {
  1155. vector<pkgIndexFile *> Files;
  1156. unsigned long EndOfSource = Files.size();
  1157. if (_system->AddStatusFiles(Files) == false)
  1158. return false;
  1159. SPtr<DynamicMMap> Map = CreateDynamicMMap(NULL);
  1160. unsigned long CurrentSize = 0;
  1161. unsigned long TotalSize = 0;
  1162. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  1163. // Build the status cache
  1164. if (Progress != NULL)
  1165. Progress->OverallProgress(0,1,1,_("Reading package lists"));
  1166. pkgCacheGenerator Gen(Map.Get(),Progress);
  1167. if (_error->PendingError() == true)
  1168. return false;
  1169. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1170. Files.begin()+EndOfSource,Files.end()) == false)
  1171. return false;
  1172. // FIXME: move me to a better place
  1173. Gen.FinishCache(Progress);
  1174. if (_error->PendingError() == true)
  1175. return false;
  1176. *OutMap = Map.UnGuard();
  1177. return true;
  1178. }
  1179. /*}}}*/