pkgcachegen.cc 66 KB

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