pkgcachegen.cc 54 KB

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