pkgcachegen.cc 59 KB

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