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.
 
 
 
 
 
 

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