pkgcachegen.cc 45 KB

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