pkgcachegen.cc 62 KB

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