pkgcachegen.cc 47 KB

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