You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1840 lines
64 KiB

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