pkgcachegen.cc 48 KB

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