pkgcachegen.cc 52 KB

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