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.
 
 
 
 
 
 

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