pkgcachegen.cc 55 KB

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