pkgcachegen.cc 48 KB

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