pkgcachegen.cc 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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/aptconfiguration.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/sptr.h>
  20. #include <apt-pkg/pkgsystem.h>
  21. #include <apt-pkg/macros.h>
  22. #include <apt-pkg/tagfile.h>
  23. #include <apt-pkg/metaindex.h>
  24. #include <apt-pkg/fileutl.h>
  25. #include <vector>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #include <apti18n.h>
  31. /*}}}*/
  32. typedef vector<pkgIndexFile *>::iterator FileIterator;
  33. template <typename Iter> std::vector<Iter*> pkgCacheGenerator::Dynamic<Iter>::toReMap;
  34. bool IsDuplicateDescription(pkgCache::DescIterator Desc,
  35. MD5SumValue const &CurMd5, std::string const &CurLang);
  36. // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
  37. // ---------------------------------------------------------------------
  38. /* We set the dirty flag and make sure that is written to the disk */
  39. pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
  40. Map(*pMap), Cache(pMap,false), Progress(Prog),
  41. FoundFileDeps(0)
  42. {
  43. CurrentFile = 0;
  44. memset(UniqHash,0,sizeof(UniqHash));
  45. if (_error->PendingError() == true)
  46. return;
  47. if (Map.Size() == 0)
  48. {
  49. // Setup the map interface..
  50. Cache.HeaderP = (pkgCache::Header *)Map.Data();
  51. if (Map.RawAllocate(sizeof(pkgCache::Header)) == 0 && _error->PendingError() == true)
  52. return;
  53. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  54. // Starting header
  55. *Cache.HeaderP = pkgCache::Header();
  56. map_ptrloc const idxVerSysName = WriteStringInMap(_system->VS->Label);
  57. Cache.HeaderP->VerSysName = idxVerSysName;
  58. map_ptrloc const idxArchitecture = WriteStringInMap(_config->Find("APT::Architecture"));
  59. Cache.HeaderP->Architecture = idxArchitecture;
  60. if (unlikely(idxVerSysName == 0 || idxArchitecture == 0))
  61. return;
  62. Cache.ReMap();
  63. }
  64. else
  65. {
  66. // Map directly from the existing file
  67. Cache.ReMap();
  68. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  69. if (Cache.VS != _system->VS)
  70. {
  71. _error->Error(_("Cache has an incompatible versioning system"));
  72. return;
  73. }
  74. }
  75. Cache.HeaderP->Dirty = true;
  76. Map.Sync(0,sizeof(pkgCache::Header));
  77. }
  78. /*}}}*/
  79. // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* We sync the data then unset the dirty flag in two steps so as to
  82. advoid a problem during a crash */
  83. pkgCacheGenerator::~pkgCacheGenerator()
  84. {
  85. if (_error->PendingError() == true)
  86. return;
  87. if (Map.Sync() == false)
  88. return;
  89. Cache.HeaderP->Dirty = false;
  90. Cache.HeaderP->CacheFileSize = Map.Size();
  91. Map.Sync(0,sizeof(pkgCache::Header));
  92. }
  93. /*}}}*/
  94. void pkgCacheGenerator::ReMap(void const * const oldMap, void const * const newMap) {/*{{{*/
  95. if (oldMap == newMap)
  96. return;
  97. if (_config->FindB("Debug::pkgCacheGen", false))
  98. std::clog << "Remaping from " << oldMap << " to " << newMap << std::endl;
  99. Cache.ReMap(false);
  100. CurrentFile += (pkgCache::PackageFile*) newMap - (pkgCache::PackageFile*) oldMap;
  101. for (size_t i = 0; i < _count(UniqHash); ++i)
  102. if (UniqHash[i] != 0)
  103. UniqHash[i] += (pkgCache::StringItem*) newMap - (pkgCache::StringItem*) oldMap;
  104. for (std::vector<pkgCache::GrpIterator*>::const_iterator i = Dynamic<pkgCache::GrpIterator>::toReMap.begin();
  105. i != Dynamic<pkgCache::GrpIterator>::toReMap.end(); ++i)
  106. (*i)->ReMap(oldMap, newMap);
  107. for (std::vector<pkgCache::PkgIterator*>::const_iterator i = Dynamic<pkgCache::PkgIterator>::toReMap.begin();
  108. i != Dynamic<pkgCache::PkgIterator>::toReMap.end(); ++i)
  109. (*i)->ReMap(oldMap, newMap);
  110. for (std::vector<pkgCache::VerIterator*>::const_iterator i = Dynamic<pkgCache::VerIterator>::toReMap.begin();
  111. i != Dynamic<pkgCache::VerIterator>::toReMap.end(); ++i)
  112. (*i)->ReMap(oldMap, newMap);
  113. for (std::vector<pkgCache::DepIterator*>::const_iterator i = Dynamic<pkgCache::DepIterator>::toReMap.begin();
  114. i != Dynamic<pkgCache::DepIterator>::toReMap.end(); ++i)
  115. (*i)->ReMap(oldMap, newMap);
  116. for (std::vector<pkgCache::DescIterator*>::const_iterator i = Dynamic<pkgCache::DescIterator>::toReMap.begin();
  117. i != Dynamic<pkgCache::DescIterator>::toReMap.end(); ++i)
  118. (*i)->ReMap(oldMap, newMap);
  119. for (std::vector<pkgCache::PrvIterator*>::const_iterator i = Dynamic<pkgCache::PrvIterator>::toReMap.begin();
  120. i != Dynamic<pkgCache::PrvIterator>::toReMap.end(); ++i)
  121. (*i)->ReMap(oldMap, newMap);
  122. for (std::vector<pkgCache::PkgFileIterator*>::const_iterator i = Dynamic<pkgCache::PkgFileIterator>::toReMap.begin();
  123. i != Dynamic<pkgCache::PkgFileIterator>::toReMap.end(); ++i)
  124. (*i)->ReMap(oldMap, newMap);
  125. } /*}}}*/
  126. // CacheGenerator::WriteStringInMap /*{{{*/
  127. map_ptrloc pkgCacheGenerator::WriteStringInMap(const char *String,
  128. const unsigned long &Len) {
  129. void const * const oldMap = Map.Data();
  130. map_ptrloc const index = Map.WriteString(String, Len);
  131. if (index != 0)
  132. ReMap(oldMap, Map.Data());
  133. return index;
  134. }
  135. /*}}}*/
  136. // CacheGenerator::WriteStringInMap /*{{{*/
  137. map_ptrloc pkgCacheGenerator::WriteStringInMap(const char *String) {
  138. void const * const oldMap = Map.Data();
  139. map_ptrloc const index = Map.WriteString(String);
  140. if (index != 0)
  141. ReMap(oldMap, Map.Data());
  142. return index;
  143. }
  144. /*}}}*/
  145. map_ptrloc pkgCacheGenerator::AllocateInMap(const unsigned long &size) {/*{{{*/
  146. void const * const oldMap = Map.Data();
  147. map_ptrloc const index = Map.Allocate(size);
  148. if (index != 0)
  149. ReMap(oldMap, Map.Data());
  150. return index;
  151. }
  152. /*}}}*/
  153. // CacheGenerator::MergeList - Merge the package list /*{{{*/
  154. // ---------------------------------------------------------------------
  155. /* This provides the generation of the entries in the cache. Each loop
  156. goes through a single package record from the underlying parse engine. */
  157. bool pkgCacheGenerator::MergeList(ListParser &List,
  158. pkgCache::VerIterator *OutVer)
  159. {
  160. List.Owner = this;
  161. unsigned int Counter = 0;
  162. while (List.Step() == true)
  163. {
  164. string const PackageName = List.Package();
  165. if (PackageName.empty() == true)
  166. return false;
  167. Counter++;
  168. if (Counter % 100 == 0 && Progress != 0)
  169. Progress->Progress(List.Offset());
  170. string Arch = List.Architecture();
  171. string const Version = List.Version();
  172. if (Version.empty() == true && Arch.empty() == true)
  173. {
  174. if (MergeListGroup(List, PackageName) == false)
  175. return false;
  176. }
  177. if (Arch.empty() == true)
  178. Arch = _config->Find("APT::Architecture");
  179. // Get a pointer to the package structure
  180. pkgCache::PkgIterator Pkg;
  181. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  182. if (NewPackage(Pkg, PackageName, Arch) == false)
  183. // TRANSLATOR: The first placeholder is a package name,
  184. // the other two should be copied verbatim as they include debug info
  185. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  186. PackageName.c_str(), "NewPackage", 1);
  187. if (Version.empty() == true)
  188. {
  189. if (MergeListPackage(List, Pkg) == false)
  190. return false;
  191. }
  192. else
  193. {
  194. if (MergeListVersion(List, Pkg, Version, OutVer) == false)
  195. return false;
  196. }
  197. if (OutVer != 0)
  198. {
  199. FoundFileDeps |= List.HasFileDeps();
  200. return true;
  201. }
  202. }
  203. if (Cache.HeaderP->PackageCount >= (1ULL<<sizeof(Cache.PkgP->ID)*8)-1)
  204. return _error->Error(_("Wow, you exceeded the number of package "
  205. "names this APT is capable of."));
  206. if (Cache.HeaderP->VersionCount >= (1ULL<<(sizeof(Cache.VerP->ID)*8))-1)
  207. return _error->Error(_("Wow, you exceeded the number of versions "
  208. "this APT is capable of."));
  209. if (Cache.HeaderP->DescriptionCount >= (1ULL<<(sizeof(Cache.DescP->ID)*8))-1)
  210. return _error->Error(_("Wow, you exceeded the number of descriptions "
  211. "this APT is capable of."));
  212. if (Cache.HeaderP->DependsCount >= (1ULL<<(sizeof(Cache.DepP->ID)*8))-1ULL)
  213. return _error->Error(_("Wow, you exceeded the number of dependencies "
  214. "this APT is capable of."));
  215. FoundFileDeps |= List.HasFileDeps();
  216. return true;
  217. }
  218. // CacheGenerator::MergeListGroup /*{{{*/
  219. bool pkgCacheGenerator::MergeListGroup(ListParser &List, std::string const &GrpName)
  220. {
  221. pkgCache::GrpIterator Grp = Cache.FindGrp(GrpName);
  222. // a group has no data on it's own, only packages have it but these
  223. // stanzas like this come from Translation- files to add descriptions,
  224. // but without a version we don't need a description for it…
  225. if (Grp.end() == true)
  226. return true;
  227. Dynamic<pkgCache::GrpIterator> DynGrp(Grp);
  228. pkgCache::PkgIterator Pkg;
  229. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  230. for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  231. if (MergeListPackage(List, Pkg) == false)
  232. return false;
  233. return true;
  234. }
  235. /*}}}*/
  236. // CacheGenerator::MergeListPackage /*{{{*/
  237. bool pkgCacheGenerator::MergeListPackage(ListParser &List, pkgCache::PkgIterator &Pkg)
  238. {
  239. // we first process the package, then the descriptions
  240. // (for deb this package processing is in fact a no-op)
  241. pkgCache::VerIterator Ver(Cache);
  242. Dynamic<pkgCache::VerIterator> DynVer(Ver);
  243. if (List.UsePackage(Pkg, Ver) == false)
  244. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  245. Pkg.Name(), "UsePackage", 1);
  246. // Find the right version to write the description
  247. MD5SumValue CurMd5 = List.Description_md5();
  248. std::string CurLang = List.DescriptionLanguage();
  249. for (Ver = Pkg.VersionList(); Ver.end() == false; ++Ver)
  250. {
  251. pkgCache::DescIterator Desc = Ver.DescriptionList();
  252. // a version can only have one md5 describing it
  253. if (MD5SumValue(Desc.md5()) != CurMd5)
  254. continue;
  255. // don't add a new description if we have one for the given
  256. // md5 && language
  257. if (IsDuplicateDescription(Desc, CurMd5, CurLang) == true)
  258. continue;
  259. Dynamic<pkgCache::DescIterator> DynDesc(Desc);
  260. // we add at the end, so that the start is constant as we need
  261. // that to be able to efficiently share these lists
  262. map_ptrloc *LastDesc = &Ver->DescriptionList;
  263. for (;Desc.end() == false && Desc->NextDesc != 0; ++Desc);
  264. if (Desc.end() == false)
  265. LastDesc = &Desc->NextDesc;
  266. void const * const oldMap = Map.Data();
  267. map_ptrloc const descindex = NewDescription(Desc, CurLang, CurMd5, *LastDesc);
  268. if (oldMap != Map.Data())
  269. LastDesc += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  270. *LastDesc = descindex;
  271. Desc->ParentPkg = Pkg.Index();
  272. if ((*LastDesc == 0 && _error->PendingError()) || NewFileDesc(Desc,List) == false)
  273. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  274. Pkg.Name(), "NewFileDesc", 1);
  275. // we can stop here as all "same" versions will share the description
  276. break;
  277. }
  278. return true;
  279. }
  280. /*}}}*/
  281. // CacheGenerator::MergeListVersion /*{{{*/
  282. bool pkgCacheGenerator::MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
  283. std::string const &Version, pkgCache::VerIterator* &OutVer)
  284. {
  285. pkgCache::VerIterator Ver = Pkg.VersionList();
  286. Dynamic<pkgCache::VerIterator> DynVer(Ver);
  287. map_ptrloc *LastVer = &Pkg->VersionList;
  288. void const * oldMap = Map.Data();
  289. unsigned long const Hash = List.VersionHash();
  290. if (Ver.end() == false)
  291. {
  292. /* We know the list is sorted so we use that fact in the search.
  293. Insertion of new versions is done with correct sorting */
  294. int Res = 1;
  295. for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
  296. {
  297. Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
  298. // Version is higher as current version - insert here
  299. if (Res > 0)
  300. break;
  301. // Versionstrings are equal - is hash also equal?
  302. if (Res == 0 && Ver->Hash == Hash)
  303. break;
  304. // proceed with the next till we have either the right
  305. // or we found another version (which will be lower)
  306. }
  307. /* We already have a version for this item, record that we saw it */
  308. if (Res == 0 && Ver.end() == false && Ver->Hash == Hash)
  309. {
  310. if (List.UsePackage(Pkg,Ver) == false)
  311. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  312. Pkg.Name(), "UsePackage", 2);
  313. if (NewFileVer(Ver,List) == false)
  314. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  315. Pkg.Name(), "NewFileVer", 1);
  316. // Read only a single record and return
  317. if (OutVer != 0)
  318. {
  319. *OutVer = Ver;
  320. return true;
  321. }
  322. return true;
  323. }
  324. }
  325. // Add a new version
  326. map_ptrloc const verindex = NewVersion(Ver,Version,*LastVer);
  327. if (verindex == 0 && _error->PendingError())
  328. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  329. Pkg.Name(), "NewVersion", 1);
  330. if (oldMap != Map.Data())
  331. LastVer += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  332. *LastVer = verindex;
  333. Ver->ParentPkg = Pkg.Index();
  334. Ver->Hash = Hash;
  335. if (unlikely(List.NewVersion(Ver) == false))
  336. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  337. Pkg.Name(), "NewVersion", 2);
  338. if (unlikely(List.UsePackage(Pkg,Ver) == false))
  339. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  340. Pkg.Name(), "UsePackage", 3);
  341. if (unlikely(NewFileVer(Ver,List) == false))
  342. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  343. Pkg.Name(), "NewFileVer", 2);
  344. pkgCache::GrpIterator Grp = Pkg.Group();
  345. Dynamic<pkgCache::GrpIterator> DynGrp(Grp);
  346. /* If it is the first version of this package we need to add implicit
  347. Multi-Arch dependencies to all other package versions in the group now -
  348. otherwise we just add them for this new version */
  349. if (Pkg.VersionList()->NextVer == 0)
  350. {
  351. pkgCache::PkgIterator P = Grp.PackageList();
  352. Dynamic<pkgCache::PkgIterator> DynP(P);
  353. for (; P.end() != true; P = Grp.NextPkg(P))
  354. {
  355. if (P->ID == Pkg->ID)
  356. continue;
  357. pkgCache::VerIterator V = P.VersionList();
  358. Dynamic<pkgCache::VerIterator> DynV(V);
  359. for (; V.end() != true; ++V)
  360. if (unlikely(AddImplicitDepends(V, Pkg) == false))
  361. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  362. Pkg.Name(), "AddImplicitDepends", 1);
  363. }
  364. }
  365. if (unlikely(AddImplicitDepends(Grp, Pkg, Ver) == false))
  366. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  367. Pkg.Name(), "AddImplicitDepends", 2);
  368. // Read only a single record and return
  369. if (OutVer != 0)
  370. {
  371. *OutVer = Ver;
  372. return true;
  373. }
  374. /* Record the Description (it is not translated) */
  375. MD5SumValue CurMd5 = List.Description_md5();
  376. if (CurMd5.Value().empty() == true)
  377. return true;
  378. std::string CurLang = List.DescriptionLanguage();
  379. /* Before we add a new description we first search in the group for
  380. a version with a description of the same MD5 - if so we reuse this
  381. description group instead of creating our own for this version */
  382. for (pkgCache::PkgIterator P = Grp.PackageList();
  383. P.end() == false; P = Grp.NextPkg(P))
  384. {
  385. for (pkgCache::VerIterator V = P.VersionList();
  386. V.end() == false; ++V)
  387. {
  388. if (IsDuplicateDescription(V.DescriptionList(), CurMd5, "") == false)
  389. continue;
  390. Ver->DescriptionList = V->DescriptionList;
  391. return true;
  392. }
  393. }
  394. // We haven't found reusable descriptions, so add the first description
  395. pkgCache::DescIterator Desc = Ver.DescriptionList();
  396. Dynamic<pkgCache::DescIterator> DynDesc(Desc);
  397. map_ptrloc *LastDesc = &Ver->DescriptionList;
  398. oldMap = Map.Data();
  399. map_ptrloc const descindex = NewDescription(Desc, CurLang, CurMd5, *LastDesc);
  400. if (oldMap != Map.Data())
  401. LastDesc += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  402. *LastDesc = descindex;
  403. Desc->ParentPkg = Pkg.Index();
  404. if ((*LastDesc == 0 && _error->PendingError()) || NewFileDesc(Desc,List) == false)
  405. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  406. Pkg.Name(), "NewFileDesc", 2);
  407. return true;
  408. }
  409. /*}}}*/
  410. /*}}}*/
  411. // CacheGenerator::MergeFileProvides - Merge file provides /*{{{*/
  412. // ---------------------------------------------------------------------
  413. /* If we found any file depends while parsing the main list we need to
  414. resolve them. Since it is undesired to load the entire list of files
  415. into the cache as virtual packages we do a two stage effort. MergeList
  416. identifies the file depends and this creates Provdies for them by
  417. re-parsing all the indexs. */
  418. bool pkgCacheGenerator::MergeFileProvides(ListParser &List)
  419. {
  420. List.Owner = this;
  421. unsigned int Counter = 0;
  422. while (List.Step() == true)
  423. {
  424. string PackageName = List.Package();
  425. if (PackageName.empty() == true)
  426. return false;
  427. string Version = List.Version();
  428. if (Version.empty() == true)
  429. continue;
  430. pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
  431. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  432. if (Pkg.end() == true)
  433. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  434. PackageName.c_str(), "FindPkg", 1);
  435. Counter++;
  436. if (Counter % 100 == 0 && Progress != 0)
  437. Progress->Progress(List.Offset());
  438. unsigned long Hash = List.VersionHash();
  439. pkgCache::VerIterator Ver = Pkg.VersionList();
  440. Dynamic<pkgCache::VerIterator> DynVer(Ver);
  441. for (; Ver.end() == false; ++Ver)
  442. {
  443. if (Ver->Hash == Hash && Version.c_str() == Ver.VerStr())
  444. {
  445. if (List.CollectFileProvides(Cache,Ver) == false)
  446. return _error->Error(_("Error occurred while processing %s (%s%d)"),
  447. PackageName.c_str(), "CollectFileProvides", 1);
  448. break;
  449. }
  450. }
  451. if (Ver.end() == true)
  452. _error->Warning(_("Package %s %s was not found while processing file dependencies"),PackageName.c_str(),Version.c_str());
  453. }
  454. return true;
  455. }
  456. /*}}}*/
  457. // CacheGenerator::NewGroup - Add a new group /*{{{*/
  458. // ---------------------------------------------------------------------
  459. /* This creates a new group structure and adds it to the hash table */
  460. bool pkgCacheGenerator::NewGroup(pkgCache::GrpIterator &Grp, const string &Name)
  461. {
  462. Grp = Cache.FindGrp(Name);
  463. if (Grp.end() == false)
  464. return true;
  465. // Get a structure
  466. map_ptrloc const Group = AllocateInMap(sizeof(pkgCache::Group));
  467. if (unlikely(Group == 0))
  468. return false;
  469. Grp = pkgCache::GrpIterator(Cache, Cache.GrpP + Group);
  470. map_ptrloc const idxName = WriteStringInMap(Name);
  471. if (unlikely(idxName == 0))
  472. return false;
  473. Grp->Name = idxName;
  474. // Insert it into the hash table
  475. unsigned long const Hash = Cache.Hash(Name);
  476. Grp->Next = Cache.HeaderP->GrpHashTable[Hash];
  477. Cache.HeaderP->GrpHashTable[Hash] = Group;
  478. Grp->ID = Cache.HeaderP->GroupCount++;
  479. return true;
  480. }
  481. /*}}}*/
  482. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  483. // ---------------------------------------------------------------------
  484. /* This creates a new package structure and adds it to the hash table */
  485. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name,
  486. const string &Arch) {
  487. pkgCache::GrpIterator Grp;
  488. Dynamic<pkgCache::GrpIterator> DynGrp(Grp);
  489. if (unlikely(NewGroup(Grp, Name) == false))
  490. return false;
  491. Pkg = Grp.FindPkg(Arch);
  492. if (Pkg.end() == false)
  493. return true;
  494. // Get a structure
  495. map_ptrloc const Package = AllocateInMap(sizeof(pkgCache::Package));
  496. if (unlikely(Package == 0))
  497. return false;
  498. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  499. // Insert the package into our package list
  500. if (Grp->FirstPackage == 0) // the group is new
  501. {
  502. // Insert it into the hash table
  503. unsigned long const Hash = Cache.Hash(Name);
  504. Pkg->NextPackage = Cache.HeaderP->PkgHashTable[Hash];
  505. Cache.HeaderP->PkgHashTable[Hash] = Package;
  506. Grp->FirstPackage = Package;
  507. }
  508. else // Group the Packages together
  509. {
  510. // this package is the new last package
  511. pkgCache::PkgIterator LastPkg(Cache, Cache.PkgP + Grp->LastPackage);
  512. Pkg->NextPackage = LastPkg->NextPackage;
  513. LastPkg->NextPackage = Package;
  514. }
  515. Grp->LastPackage = Package;
  516. // Set the name, arch and the ID
  517. Pkg->Name = Grp->Name;
  518. Pkg->Group = Grp.Index();
  519. // all is mapped to the native architecture
  520. map_ptrloc const idxArch = (Arch == "all") ? Cache.HeaderP->Architecture : WriteUniqString(Arch.c_str());
  521. if (unlikely(idxArch == 0))
  522. return false;
  523. Pkg->Arch = idxArch;
  524. Pkg->ID = Cache.HeaderP->PackageCount++;
  525. return true;
  526. }
  527. /*}}}*/
  528. // CacheGenerator::AddImplicitDepends /*{{{*/
  529. bool pkgCacheGenerator::AddImplicitDepends(pkgCache::GrpIterator &G,
  530. pkgCache::PkgIterator &P,
  531. pkgCache::VerIterator &V)
  532. {
  533. // copy P.Arch() into a string here as a cache remap
  534. // in NewDepends() later may alter the pointer location
  535. string Arch = P.Arch() == NULL ? "" : P.Arch();
  536. map_ptrloc *OldDepLast = NULL;
  537. /* MultiArch handling introduces a lot of implicit Dependencies:
  538. - MultiArch: same → Co-Installable if they have the same version
  539. - All others conflict with all other group members */
  540. bool const coInstall = ((V->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same);
  541. pkgCache::PkgIterator D = G.PackageList();
  542. Dynamic<pkgCache::PkgIterator> DynD(D);
  543. for (; D.end() != true; D = G.NextPkg(D))
  544. {
  545. if (Arch == D.Arch() || D->VersionList == 0)
  546. continue;
  547. /* We allow only one installed arch at the time
  548. per group, therefore each group member conflicts
  549. with all other group members */
  550. if (coInstall == true)
  551. {
  552. // Replaces: ${self}:other ( << ${binary:Version})
  553. NewDepends(D, V, V.VerStr(),
  554. pkgCache::Dep::Less, pkgCache::Dep::Replaces,
  555. OldDepLast);
  556. // Breaks: ${self}:other (!= ${binary:Version})
  557. NewDepends(D, V, V.VerStr(),
  558. pkgCache::Dep::NotEquals, pkgCache::Dep::DpkgBreaks,
  559. OldDepLast);
  560. } else {
  561. // Conflicts: ${self}:other
  562. NewDepends(D, V, "",
  563. pkgCache::Dep::NoOp, pkgCache::Dep::Conflicts,
  564. OldDepLast);
  565. }
  566. }
  567. return true;
  568. }
  569. bool pkgCacheGenerator::AddImplicitDepends(pkgCache::VerIterator &V,
  570. pkgCache::PkgIterator &D)
  571. {
  572. /* MultiArch handling introduces a lot of implicit Dependencies:
  573. - MultiArch: same → Co-Installable if they have the same version
  574. - All others conflict with all other group members */
  575. map_ptrloc *OldDepLast = NULL;
  576. bool const coInstall = ((V->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same);
  577. if (coInstall == true)
  578. {
  579. // Replaces: ${self}:other ( << ${binary:Version})
  580. NewDepends(D, V, V.VerStr(),
  581. pkgCache::Dep::Less, pkgCache::Dep::Replaces,
  582. OldDepLast);
  583. // Breaks: ${self}:other (!= ${binary:Version})
  584. NewDepends(D, V, V.VerStr(),
  585. pkgCache::Dep::NotEquals, pkgCache::Dep::DpkgBreaks,
  586. OldDepLast);
  587. } else {
  588. // Conflicts: ${self}:other
  589. NewDepends(D, V, "",
  590. pkgCache::Dep::NoOp, pkgCache::Dep::Conflicts,
  591. OldDepLast);
  592. }
  593. return true;
  594. }
  595. /*}}}*/
  596. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  597. // ---------------------------------------------------------------------
  598. /* */
  599. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  600. ListParser &List)
  601. {
  602. if (CurrentFile == 0)
  603. return true;
  604. // Get a structure
  605. map_ptrloc const VerFile = AllocateInMap(sizeof(pkgCache::VerFile));
  606. if (VerFile == 0)
  607. return 0;
  608. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  609. VF->File = CurrentFile - Cache.PkgFileP;
  610. // Link it to the end of the list
  611. map_ptrloc *Last = &Ver->FileList;
  612. for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; ++V)
  613. Last = &V->NextFile;
  614. VF->NextFile = *Last;
  615. *Last = VF.Index();
  616. VF->Offset = List.Offset();
  617. VF->Size = List.Size();
  618. if (Cache.HeaderP->MaxVerFileSize < VF->Size)
  619. Cache.HeaderP->MaxVerFileSize = VF->Size;
  620. Cache.HeaderP->VerFileCount++;
  621. return true;
  622. }
  623. /*}}}*/
  624. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  625. // ---------------------------------------------------------------------
  626. /* This puts a version structure in the linked list */
  627. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  628. const string &VerStr,
  629. unsigned long Next)
  630. {
  631. // Get a structure
  632. map_ptrloc const Version = AllocateInMap(sizeof(pkgCache::Version));
  633. if (Version == 0)
  634. return 0;
  635. // Fill it in
  636. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  637. Ver->NextVer = Next;
  638. Ver->ID = Cache.HeaderP->VersionCount++;
  639. map_ptrloc const idxVerStr = WriteStringInMap(VerStr);
  640. if (unlikely(idxVerStr == 0))
  641. return 0;
  642. Ver->VerStr = idxVerStr;
  643. return Version;
  644. }
  645. /*}}}*/
  646. // CacheGenerator::NewFileDesc - Create a new File<->Desc association /*{{{*/
  647. // ---------------------------------------------------------------------
  648. /* */
  649. bool pkgCacheGenerator::NewFileDesc(pkgCache::DescIterator &Desc,
  650. ListParser &List)
  651. {
  652. if (CurrentFile == 0)
  653. return true;
  654. // Get a structure
  655. map_ptrloc const DescFile = AllocateInMap(sizeof(pkgCache::DescFile));
  656. if (DescFile == 0)
  657. return false;
  658. pkgCache::DescFileIterator DF(Cache,Cache.DescFileP + DescFile);
  659. DF->File = CurrentFile - Cache.PkgFileP;
  660. // Link it to the end of the list
  661. map_ptrloc *Last = &Desc->FileList;
  662. for (pkgCache::DescFileIterator D = Desc.FileList(); D.end() == false; ++D)
  663. Last = &D->NextFile;
  664. DF->NextFile = *Last;
  665. *Last = DF.Index();
  666. DF->Offset = List.Offset();
  667. DF->Size = List.Size();
  668. if (Cache.HeaderP->MaxDescFileSize < DF->Size)
  669. Cache.HeaderP->MaxDescFileSize = DF->Size;
  670. Cache.HeaderP->DescFileCount++;
  671. return true;
  672. }
  673. /*}}}*/
  674. // CacheGenerator::NewDescription - Create a new Description /*{{{*/
  675. // ---------------------------------------------------------------------
  676. /* This puts a description structure in the linked list */
  677. map_ptrloc pkgCacheGenerator::NewDescription(pkgCache::DescIterator &Desc,
  678. const string &Lang,
  679. const MD5SumValue &md5sum,
  680. map_ptrloc Next)
  681. {
  682. // Get a structure
  683. map_ptrloc const Description = AllocateInMap(sizeof(pkgCache::Description));
  684. if (Description == 0)
  685. return 0;
  686. // Fill it in
  687. Desc = pkgCache::DescIterator(Cache,Cache.DescP + Description);
  688. Desc->NextDesc = Next;
  689. Desc->ID = Cache.HeaderP->DescriptionCount++;
  690. map_ptrloc const idxlanguage_code = WriteStringInMap(Lang);
  691. map_ptrloc const idxmd5sum = WriteStringInMap(md5sum.Value());
  692. if (unlikely(idxlanguage_code == 0 || idxmd5sum == 0))
  693. return 0;
  694. Desc->language_code = idxlanguage_code;
  695. Desc->md5sum = idxmd5sum;
  696. return Description;
  697. }
  698. /*}}}*/
  699. // CacheGenerator::NewDepends - Create a dependency element /*{{{*/
  700. // ---------------------------------------------------------------------
  701. /* This creates a dependency element in the tree. It is linked to the
  702. version and to the package that it is pointing to. */
  703. bool pkgCacheGenerator::NewDepends(pkgCache::PkgIterator &Pkg,
  704. pkgCache::VerIterator &Ver,
  705. string const &Version,
  706. unsigned int const &Op,
  707. unsigned int const &Type,
  708. map_ptrloc* &OldDepLast)
  709. {
  710. void const * const oldMap = Map.Data();
  711. // Get a structure
  712. map_ptrloc const Dependency = AllocateInMap(sizeof(pkgCache::Dependency));
  713. if (unlikely(Dependency == 0))
  714. return false;
  715. // Fill it in
  716. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  717. Dynamic<pkgCache::DepIterator> DynDep(Dep);
  718. Dep->ParentVer = Ver.Index();
  719. Dep->Type = Type;
  720. Dep->CompareOp = Op;
  721. Dep->ID = Cache.HeaderP->DependsCount++;
  722. // Probe the reverse dependency list for a version string that matches
  723. if (Version.empty() == false)
  724. {
  725. /* for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  726. if (I->Version != 0 && I.TargetVer() == Version)
  727. Dep->Version = I->Version;*/
  728. if (Dep->Version == 0) {
  729. map_ptrloc const index = WriteStringInMap(Version);
  730. if (unlikely(index == 0))
  731. return false;
  732. Dep->Version = index;
  733. }
  734. }
  735. // Link it to the package
  736. Dep->Package = Pkg.Index();
  737. Dep->NextRevDepends = Pkg->RevDepends;
  738. Pkg->RevDepends = Dep.Index();
  739. // Do we know where to link the Dependency to?
  740. if (OldDepLast == NULL)
  741. {
  742. OldDepLast = &Ver->DependsList;
  743. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; ++D)
  744. OldDepLast = &D->NextDepends;
  745. } else if (oldMap != Map.Data())
  746. OldDepLast += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  747. Dep->NextDepends = *OldDepLast;
  748. *OldDepLast = Dep.Index();
  749. OldDepLast = &Dep->NextDepends;
  750. return true;
  751. }
  752. /*}}}*/
  753. // ListParser::NewDepends - Create the environment for a new dependency /*{{{*/
  754. // ---------------------------------------------------------------------
  755. /* This creates a Group and the Package to link this dependency to if
  756. needed and handles also the caching of the old endpoint */
  757. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator &Ver,
  758. const string &PackageName,
  759. const string &Arch,
  760. const string &Version,
  761. unsigned int Op,
  762. unsigned int Type)
  763. {
  764. pkgCache::GrpIterator Grp;
  765. Dynamic<pkgCache::GrpIterator> DynGrp(Grp);
  766. if (unlikely(Owner->NewGroup(Grp, PackageName) == false))
  767. return false;
  768. // Locate the target package
  769. pkgCache::PkgIterator Pkg = Grp.FindPkg(Arch);
  770. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  771. if (Pkg.end() == true) {
  772. if (unlikely(Owner->NewPackage(Pkg, PackageName, Arch) == false))
  773. return false;
  774. }
  775. // Is it a file dependency?
  776. if (unlikely(PackageName[0] == '/'))
  777. FoundFileDeps = true;
  778. /* Caching the old end point speeds up generation substantially */
  779. if (OldDepVer != Ver) {
  780. OldDepLast = NULL;
  781. OldDepVer = Ver;
  782. }
  783. return Owner->NewDepends(Pkg, Ver, Version, Op, Type, OldDepLast);
  784. }
  785. /*}}}*/
  786. // ListParser::NewProvides - Create a Provides element /*{{{*/
  787. // ---------------------------------------------------------------------
  788. /* */
  789. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator &Ver,
  790. const string &PkgName,
  791. const string &PkgArch,
  792. const string &Version)
  793. {
  794. pkgCache &Cache = Owner->Cache;
  795. // We do not add self referencing provides
  796. if (Ver.ParentPkg().Name() == PkgName && (PkgArch == Ver.ParentPkg().Arch() ||
  797. (PkgArch == "all" && strcmp((Cache.StrP + Cache.HeaderP->Architecture), Ver.ParentPkg().Arch()) == 0)))
  798. return true;
  799. // Get a structure
  800. map_ptrloc const Provides = Owner->AllocateInMap(sizeof(pkgCache::Provides));
  801. if (unlikely(Provides == 0))
  802. return false;
  803. Cache.HeaderP->ProvidesCount++;
  804. // Fill it in
  805. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  806. Dynamic<pkgCache::PrvIterator> DynPrv(Prv);
  807. Prv->Version = Ver.Index();
  808. Prv->NextPkgProv = Ver->ProvidesList;
  809. Ver->ProvidesList = Prv.Index();
  810. if (Version.empty() == false && unlikely((Prv->ProvideVersion = WriteString(Version)) == 0))
  811. return false;
  812. // Locate the target package
  813. pkgCache::PkgIterator Pkg;
  814. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  815. if (unlikely(Owner->NewPackage(Pkg,PkgName, PkgArch) == false))
  816. return false;
  817. // Link it to the package
  818. Prv->ParentPkg = Pkg.Index();
  819. Prv->NextProvides = Pkg->ProvidesList;
  820. Pkg->ProvidesList = Prv.Index();
  821. return true;
  822. }
  823. /*}}}*/
  824. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  825. // ---------------------------------------------------------------------
  826. /* This is used to select which file is to be associated with all newly
  827. added versions. The caller is responsible for setting the IMS fields. */
  828. bool pkgCacheGenerator::SelectFile(const string &File,const string &Site,
  829. const pkgIndexFile &Index,
  830. unsigned long Flags)
  831. {
  832. // Get some space for the structure
  833. map_ptrloc const idxFile = AllocateInMap(sizeof(*CurrentFile));
  834. if (unlikely(idxFile == 0))
  835. return false;
  836. CurrentFile = Cache.PkgFileP + idxFile;
  837. // Fill it in
  838. map_ptrloc const idxFileName = WriteStringInMap(File);
  839. map_ptrloc const idxSite = WriteUniqString(Site);
  840. if (unlikely(idxFileName == 0 || idxSite == 0))
  841. return false;
  842. CurrentFile->FileName = idxFileName;
  843. CurrentFile->Site = idxSite;
  844. CurrentFile->NextFile = Cache.HeaderP->FileList;
  845. CurrentFile->Flags = Flags;
  846. CurrentFile->ID = Cache.HeaderP->PackageFileCount;
  847. map_ptrloc const idxIndexType = WriteUniqString(Index.GetType()->Label);
  848. if (unlikely(idxIndexType == 0))
  849. return false;
  850. CurrentFile->IndexType = idxIndexType;
  851. PkgFileName = File;
  852. Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
  853. Cache.HeaderP->PackageFileCount++;
  854. if (Progress != 0)
  855. Progress->SubProgress(Index.Size());
  856. return true;
  857. }
  858. /*}}}*/
  859. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  860. // ---------------------------------------------------------------------
  861. /* This is used to create handles to strings. Given the same text it
  862. always returns the same number */
  863. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  864. unsigned int Size)
  865. {
  866. /* We use a very small transient hash table here, this speeds up generation
  867. by a fair amount on slower machines */
  868. pkgCache::StringItem *&Bucket = UniqHash[(S[0]*5 + S[1]) % _count(UniqHash)];
  869. if (Bucket != 0 &&
  870. stringcmp(S,S+Size,Cache.StrP + Bucket->String) == 0)
  871. return Bucket->String;
  872. // Search for an insertion point
  873. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  874. int Res = 1;
  875. map_ptrloc *Last = &Cache.HeaderP->StringList;
  876. for (; I != Cache.StringItemP; Last = &I->NextItem,
  877. I = Cache.StringItemP + I->NextItem)
  878. {
  879. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  880. if (Res >= 0)
  881. break;
  882. }
  883. // Match
  884. if (Res == 0)
  885. {
  886. Bucket = I;
  887. return I->String;
  888. }
  889. // Get a structure
  890. void const * const oldMap = Map.Data();
  891. map_ptrloc const Item = AllocateInMap(sizeof(pkgCache::StringItem));
  892. if (Item == 0)
  893. return 0;
  894. map_ptrloc const idxString = WriteStringInMap(S,Size);
  895. if (unlikely(idxString == 0))
  896. return 0;
  897. if (oldMap != Map.Data()) {
  898. Last += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  899. I += (pkgCache::StringItem*) Map.Data() - (pkgCache::StringItem*) oldMap;
  900. }
  901. *Last = Item;
  902. // Fill in the structure
  903. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  904. ItemP->NextItem = I - Cache.StringItemP;
  905. ItemP->String = idxString;
  906. Bucket = ItemP;
  907. return ItemP->String;
  908. }
  909. /*}}}*/
  910. // CheckValidity - Check that a cache is up-to-date /*{{{*/
  911. // ---------------------------------------------------------------------
  912. /* This just verifies that each file in the list of index files exists,
  913. has matching attributes with the cache and the cache does not have
  914. any extra files. */
  915. static bool CheckValidity(const string &CacheFile,
  916. pkgSourceList &List,
  917. FileIterator Start,
  918. FileIterator End,
  919. MMap **OutMap = 0)
  920. {
  921. bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
  922. // No file, certainly invalid
  923. if (CacheFile.empty() == true || FileExists(CacheFile) == false)
  924. {
  925. if (Debug == true)
  926. std::clog << "CacheFile doesn't exist" << std::endl;
  927. return false;
  928. }
  929. if (List.GetLastModifiedTime() > GetModificationTime(CacheFile))
  930. {
  931. if (Debug == true)
  932. std::clog << "sources.list is newer than the cache" << std::endl;
  933. return false;
  934. }
  935. // Map it
  936. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  937. SPtr<MMap> Map = new MMap(CacheF,0);
  938. pkgCache Cache(Map);
  939. if (_error->PendingError() == true || Map->Size() == 0)
  940. {
  941. if (Debug == true)
  942. std::clog << "Errors are pending or Map is empty()" << std::endl;
  943. _error->Discard();
  944. return false;
  945. }
  946. /* Now we check every index file, see if it is in the cache,
  947. verify the IMS data and check that it is on the disk too.. */
  948. SPtrArray<bool> Visited = new bool[Cache.HeaderP->PackageFileCount];
  949. memset(Visited,0,sizeof(*Visited)*Cache.HeaderP->PackageFileCount);
  950. for (; Start != End; ++Start)
  951. {
  952. if (Debug == true)
  953. std::clog << "Checking PkgFile " << (*Start)->Describe() << ": ";
  954. if ((*Start)->HasPackages() == false)
  955. {
  956. if (Debug == true)
  957. std::clog << "Has NO packages" << std::endl;
  958. continue;
  959. }
  960. if ((*Start)->Exists() == false)
  961. {
  962. #if 0 // mvo: we no longer give a message here (Default Sources spec)
  963. _error->WarningE("stat",_("Couldn't stat source package list %s"),
  964. (*Start)->Describe().c_str());
  965. #endif
  966. if (Debug == true)
  967. std::clog << "file doesn't exist" << std::endl;
  968. continue;
  969. }
  970. // FindInCache is also expected to do an IMS check.
  971. pkgCache::PkgFileIterator File = (*Start)->FindInCache(Cache);
  972. if (File.end() == true)
  973. {
  974. if (Debug == true)
  975. std::clog << "FindInCache returned end-Pointer" << std::endl;
  976. return false;
  977. }
  978. Visited[File->ID] = true;
  979. if (Debug == true)
  980. std::clog << "with ID " << File->ID << " is valid" << std::endl;
  981. }
  982. for (unsigned I = 0; I != Cache.HeaderP->PackageFileCount; I++)
  983. if (Visited[I] == false)
  984. {
  985. if (Debug == true)
  986. std::clog << "File with ID" << I << " wasn't visited" << std::endl;
  987. return false;
  988. }
  989. if (_error->PendingError() == true)
  990. {
  991. if (Debug == true)
  992. {
  993. std::clog << "Validity failed because of pending errors:" << std::endl;
  994. _error->DumpErrors();
  995. }
  996. _error->Discard();
  997. return false;
  998. }
  999. if (OutMap != 0)
  1000. *OutMap = Map.UnGuard();
  1001. return true;
  1002. }
  1003. /*}}}*/
  1004. // ComputeSize - Compute the total size of a bunch of files /*{{{*/
  1005. // ---------------------------------------------------------------------
  1006. /* Size is kind of an abstract notion that is only used for the progress
  1007. meter */
  1008. static unsigned long ComputeSize(FileIterator Start,FileIterator End)
  1009. {
  1010. unsigned long TotalSize = 0;
  1011. for (; Start != End; ++Start)
  1012. {
  1013. if ((*Start)->HasPackages() == false)
  1014. continue;
  1015. TotalSize += (*Start)->Size();
  1016. }
  1017. return TotalSize;
  1018. }
  1019. /*}}}*/
  1020. // BuildCache - Merge the list of index files into the cache /*{{{*/
  1021. // ---------------------------------------------------------------------
  1022. /* */
  1023. static bool BuildCache(pkgCacheGenerator &Gen,
  1024. OpProgress *Progress,
  1025. unsigned long &CurrentSize,unsigned long TotalSize,
  1026. FileIterator Start, FileIterator End)
  1027. {
  1028. FileIterator I;
  1029. for (I = Start; I != End; ++I)
  1030. {
  1031. if ((*I)->HasPackages() == false)
  1032. continue;
  1033. if ((*I)->Exists() == false)
  1034. continue;
  1035. if ((*I)->FindInCache(Gen.GetCache()).end() == false)
  1036. {
  1037. _error->Warning("Duplicate sources.list entry %s",
  1038. (*I)->Describe().c_str());
  1039. continue;
  1040. }
  1041. unsigned long Size = (*I)->Size();
  1042. if (Progress != NULL)
  1043. Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
  1044. CurrentSize += Size;
  1045. if ((*I)->Merge(Gen,Progress) == false)
  1046. return false;
  1047. }
  1048. if (Gen.HasFileDeps() == true)
  1049. {
  1050. if (Progress != NULL)
  1051. Progress->Done();
  1052. TotalSize = ComputeSize(Start, End);
  1053. CurrentSize = 0;
  1054. for (I = Start; I != End; ++I)
  1055. {
  1056. unsigned long Size = (*I)->Size();
  1057. if (Progress != NULL)
  1058. Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
  1059. CurrentSize += Size;
  1060. if ((*I)->MergeFileProvides(Gen,Progress) == false)
  1061. return false;
  1062. }
  1063. }
  1064. return true;
  1065. }
  1066. /*}}}*/
  1067. // CacheGenerator::CreateDynamicMMap - load an mmap with configuration options /*{{{*/
  1068. DynamicMMap* pkgCacheGenerator::CreateDynamicMMap(FileFd *CacheF, unsigned long Flags) {
  1069. unsigned long const MapStart = _config->FindI("APT::Cache-Start", 24*1024*1024);
  1070. unsigned long const MapGrow = _config->FindI("APT::Cache-Grow", 1*1024*1024);
  1071. unsigned long const MapLimit = _config->FindI("APT::Cache-Limit", 0);
  1072. Flags |= MMap::Moveable;
  1073. if (_config->FindB("APT::Cache-Fallback", false) == true)
  1074. Flags |= MMap::Fallback;
  1075. if (CacheF != NULL)
  1076. return new DynamicMMap(*CacheF, Flags, MapStart, MapGrow, MapLimit);
  1077. else
  1078. return new DynamicMMap(Flags, MapStart, MapGrow, MapLimit);
  1079. }
  1080. /*}}}*/
  1081. // CacheGenerator::MakeStatusCache - Construct the status cache /*{{{*/
  1082. // ---------------------------------------------------------------------
  1083. /* This makes sure that the status cache (the cache that has all
  1084. index files from the sources list and all local ones) is ready
  1085. to be mmaped. If OutMap is not zero then a MMap object representing
  1086. the cache will be stored there. This is pretty much mandetory if you
  1087. are using AllowMem. AllowMem lets the function be run as non-root
  1088. where it builds the cache 'fast' into a memory buffer. */
  1089. __deprecated bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
  1090. MMap **OutMap, bool AllowMem)
  1091. { return pkgCacheGenerator::MakeStatusCache(List, &Progress, OutMap, AllowMem); }
  1092. bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
  1093. MMap **OutMap,bool AllowMem)
  1094. {
  1095. bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
  1096. vector<pkgIndexFile *> Files;
  1097. for (vector<metaIndex *>::const_iterator i = List.begin();
  1098. i != List.end();
  1099. ++i)
  1100. {
  1101. vector <pkgIndexFile *> *Indexes = (*i)->GetIndexFiles();
  1102. for (vector<pkgIndexFile *>::const_iterator j = Indexes->begin();
  1103. j != Indexes->end();
  1104. ++j)
  1105. Files.push_back (*j);
  1106. }
  1107. unsigned long const EndOfSource = Files.size();
  1108. if (_system->AddStatusFiles(Files) == false)
  1109. return false;
  1110. // Decide if we can write to the files..
  1111. string const CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  1112. string const SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  1113. // ensure the cache directory exists
  1114. if (CacheFile.empty() == false || SrcCacheFile.empty() == false)
  1115. {
  1116. string dir = _config->FindDir("Dir::Cache");
  1117. size_t const len = dir.size();
  1118. if (len > 5 && dir.find("/apt/", len - 6, 5) == len - 5)
  1119. dir = dir.substr(0, len - 5);
  1120. if (CacheFile.empty() == false)
  1121. CreateDirectory(dir, flNotFile(CacheFile));
  1122. if (SrcCacheFile.empty() == false)
  1123. CreateDirectory(dir, flNotFile(SrcCacheFile));
  1124. }
  1125. // Decide if we can write to the cache
  1126. bool Writeable = false;
  1127. if (CacheFile.empty() == false)
  1128. Writeable = access(flNotFile(CacheFile).c_str(),W_OK) == 0;
  1129. else
  1130. if (SrcCacheFile.empty() == false)
  1131. Writeable = access(flNotFile(SrcCacheFile).c_str(),W_OK) == 0;
  1132. if (Debug == true)
  1133. std::clog << "Do we have write-access to the cache files? " << (Writeable ? "YES" : "NO") << std::endl;
  1134. if (Writeable == false && AllowMem == false && CacheFile.empty() == false)
  1135. return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
  1136. if (Progress != NULL)
  1137. Progress->OverallProgress(0,1,1,_("Reading package lists"));
  1138. // Cache is OK, Fin.
  1139. if (CheckValidity(CacheFile, List, Files.begin(),Files.end(),OutMap) == true)
  1140. {
  1141. if (Progress != NULL)
  1142. Progress->OverallProgress(1,1,1,_("Reading package lists"));
  1143. if (Debug == true)
  1144. std::clog << "pkgcache.bin is valid - no need to build anything" << std::endl;
  1145. return true;
  1146. }
  1147. else if (Debug == true)
  1148. std::clog << "pkgcache.bin is NOT valid" << std::endl;
  1149. /* At this point we know we need to reconstruct the package cache,
  1150. begin. */
  1151. SPtr<FileFd> CacheF;
  1152. SPtr<DynamicMMap> Map;
  1153. if (Writeable == true && CacheFile.empty() == false)
  1154. {
  1155. _error->PushToStack();
  1156. unlink(CacheFile.c_str());
  1157. CacheF = new FileFd(CacheFile,FileFd::WriteAtomic);
  1158. fchmod(CacheF->Fd(),0644);
  1159. Map = CreateDynamicMMap(CacheF, MMap::Public);
  1160. if (_error->PendingError() == true)
  1161. {
  1162. delete CacheF.UnGuard();
  1163. delete Map.UnGuard();
  1164. if (Debug == true)
  1165. std::clog << "Open filebased MMap FAILED" << std::endl;
  1166. Writeable = false;
  1167. if (AllowMem == false)
  1168. {
  1169. _error->MergeWithStack();
  1170. return false;
  1171. }
  1172. _error->RevertToStack();
  1173. }
  1174. else if (Debug == true)
  1175. {
  1176. _error->MergeWithStack();
  1177. std::clog << "Open filebased MMap" << std::endl;
  1178. }
  1179. }
  1180. if (Writeable == false || CacheFile.empty() == true)
  1181. {
  1182. // Just build it in memory..
  1183. Map = CreateDynamicMMap(NULL);
  1184. if (Debug == true)
  1185. std::clog << "Open memory Map (not filebased)" << std::endl;
  1186. }
  1187. // Lets try the source cache.
  1188. unsigned long CurrentSize = 0;
  1189. unsigned long TotalSize = 0;
  1190. if (CheckValidity(SrcCacheFile, List, Files.begin(),
  1191. Files.begin()+EndOfSource) == true)
  1192. {
  1193. if (Debug == true)
  1194. std::clog << "srcpkgcache.bin is valid - populate MMap with it." << std::endl;
  1195. // Preload the map with the source cache
  1196. FileFd SCacheF(SrcCacheFile,FileFd::ReadOnly);
  1197. unsigned long const alloc = Map->RawAllocate(SCacheF.Size());
  1198. if ((alloc == 0 && _error->PendingError())
  1199. || SCacheF.Read((unsigned char *)Map->Data() + alloc,
  1200. SCacheF.Size()) == false)
  1201. return false;
  1202. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  1203. // Build the status cache
  1204. pkgCacheGenerator Gen(Map.Get(),Progress);
  1205. if (_error->PendingError() == true)
  1206. return false;
  1207. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1208. Files.begin()+EndOfSource,Files.end()) == false)
  1209. return false;
  1210. }
  1211. else
  1212. {
  1213. if (Debug == true)
  1214. std::clog << "srcpkgcache.bin is NOT valid - rebuild" << std::endl;
  1215. TotalSize = ComputeSize(Files.begin(),Files.end());
  1216. // Build the source cache
  1217. pkgCacheGenerator Gen(Map.Get(),Progress);
  1218. if (_error->PendingError() == true)
  1219. return false;
  1220. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1221. Files.begin(),Files.begin()+EndOfSource) == false)
  1222. return false;
  1223. // Write it back
  1224. if (Writeable == true && SrcCacheFile.empty() == false)
  1225. {
  1226. FileFd SCacheF(SrcCacheFile,FileFd::WriteAtomic);
  1227. if (_error->PendingError() == true)
  1228. return false;
  1229. fchmod(SCacheF.Fd(),0644);
  1230. // Write out the main data
  1231. if (SCacheF.Write(Map->Data(),Map->Size()) == false)
  1232. return _error->Error(_("IO Error saving source cache"));
  1233. SCacheF.Sync();
  1234. // Write out the proper header
  1235. Gen.GetCache().HeaderP->Dirty = false;
  1236. if (SCacheF.Seek(0) == false ||
  1237. SCacheF.Write(Map->Data(),sizeof(*Gen.GetCache().HeaderP)) == false)
  1238. return _error->Error(_("IO Error saving source cache"));
  1239. Gen.GetCache().HeaderP->Dirty = true;
  1240. SCacheF.Sync();
  1241. }
  1242. // Build the status cache
  1243. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1244. Files.begin()+EndOfSource,Files.end()) == false)
  1245. return false;
  1246. }
  1247. if (Debug == true)
  1248. std::clog << "Caches are ready for shipping" << std::endl;
  1249. if (_error->PendingError() == true)
  1250. return false;
  1251. if (OutMap != 0)
  1252. {
  1253. if (CacheF != 0)
  1254. {
  1255. delete Map.UnGuard();
  1256. *OutMap = new MMap(*CacheF,0);
  1257. }
  1258. else
  1259. {
  1260. *OutMap = Map.UnGuard();
  1261. }
  1262. }
  1263. return true;
  1264. }
  1265. /*}}}*/
  1266. // CacheGenerator::MakeOnlyStatusCache - Build only a status files cache/*{{{*/
  1267. // ---------------------------------------------------------------------
  1268. /* */
  1269. __deprecated bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
  1270. { return pkgCacheGenerator::MakeOnlyStatusCache(&Progress, OutMap); }
  1271. bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap)
  1272. {
  1273. vector<pkgIndexFile *> Files;
  1274. unsigned long EndOfSource = Files.size();
  1275. if (_system->AddStatusFiles(Files) == false)
  1276. return false;
  1277. SPtr<DynamicMMap> Map = CreateDynamicMMap(NULL);
  1278. unsigned long CurrentSize = 0;
  1279. unsigned long TotalSize = 0;
  1280. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  1281. // Build the status cache
  1282. if (Progress != NULL)
  1283. Progress->OverallProgress(0,1,1,_("Reading package lists"));
  1284. pkgCacheGenerator Gen(Map.Get(),Progress);
  1285. if (_error->PendingError() == true)
  1286. return false;
  1287. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1288. Files.begin()+EndOfSource,Files.end()) == false)
  1289. return false;
  1290. if (_error->PendingError() == true)
  1291. return false;
  1292. *OutMap = Map.UnGuard();
  1293. return true;
  1294. }
  1295. /*}}}*/
  1296. // IsDuplicateDescription /*{{{*/
  1297. bool IsDuplicateDescription(pkgCache::DescIterator Desc,
  1298. MD5SumValue const &CurMd5, std::string const &CurLang)
  1299. {
  1300. // Descriptions in the same link-list have all the same md5
  1301. if (MD5SumValue(Desc.md5()) != CurMd5)
  1302. return false;
  1303. for (; Desc.end() == false; ++Desc)
  1304. if (Desc.LanguageCode() == CurLang)
  1305. return true;
  1306. return false;
  1307. }
  1308. /*}}}*/
  1309. // CacheGenerator::FinishCache /*{{{*/
  1310. bool pkgCacheGenerator::FinishCache(OpProgress *Progress)
  1311. {
  1312. return true;
  1313. }
  1314. /*}}}*/