pkgcachegen.cc 50 KB

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