pkgcachegen.cc 46 KB

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