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.
 
 
 
 
 
 

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