pkgcachegen.cc 62 KB

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