pkgcachegen.cc 64 KB

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