pkgcachegen.cc 61 KB

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