pkgcachegen.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. #define APT_COMPATIBILITY 986
  11. #include <apt-pkg/pkgcachegen.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/version.h>
  14. #include <apt-pkg/progress.h>
  15. #include <apt-pkg/sourcelist.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <apt-pkg/aptconfiguration.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/sptr.h>
  20. #include <apt-pkg/pkgsystem.h>
  21. #include <apt-pkg/tagfile.h>
  22. #include <apti18n.h>
  23. #include <vector>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <system.h>
  29. /*}}}*/
  30. typedef vector<pkgIndexFile *>::iterator FileIterator;
  31. // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* We set the dirty flag and make sure that is written to the disk */
  34. pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
  35. Map(*pMap), Cache(pMap,false), Progress(Prog),
  36. FoundFileDeps(0)
  37. {
  38. CurrentFile = 0;
  39. memset(UniqHash,0,sizeof(UniqHash));
  40. if (_error->PendingError() == true)
  41. return;
  42. if (Map.Size() == 0)
  43. {
  44. // Setup the map interface..
  45. Cache.HeaderP = (pkgCache::Header *)Map.Data();
  46. if (Map.RawAllocate(sizeof(pkgCache::Header)) == 0 && _error->PendingError() == true)
  47. return;
  48. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  49. // Starting header
  50. *Cache.HeaderP = pkgCache::Header();
  51. Cache.HeaderP->VerSysName = Map.WriteString(_system->VS->Label);
  52. Cache.HeaderP->Architecture = Map.WriteString(_config->Find("APT::Architecture"));
  53. Cache.ReMap();
  54. }
  55. else
  56. {
  57. // Map directly from the existing file
  58. Cache.ReMap();
  59. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  60. if (Cache.VS != _system->VS)
  61. {
  62. _error->Error(_("Cache has an incompatible versioning system"));
  63. return;
  64. }
  65. }
  66. Cache.HeaderP->Dirty = true;
  67. Map.Sync(0,sizeof(pkgCache::Header));
  68. }
  69. /*}}}*/
  70. // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* We sync the data then unset the dirty flag in two steps so as to
  73. advoid a problem during a crash */
  74. pkgCacheGenerator::~pkgCacheGenerator()
  75. {
  76. if (_error->PendingError() == true)
  77. return;
  78. if (Map.Sync() == false)
  79. return;
  80. Cache.HeaderP->Dirty = false;
  81. Map.Sync(0,sizeof(pkgCache::Header));
  82. }
  83. /*}}}*/
  84. // CacheGenerator::MergeList - Merge the package list /*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* This provides the generation of the entries in the cache. Each loop
  87. goes through a single package record from the underlying parse engine. */
  88. bool pkgCacheGenerator::MergeList(ListParser &List,
  89. pkgCache::VerIterator *OutVer)
  90. {
  91. List.Owner = this;
  92. unsigned int Counter = 0;
  93. while (List.Step() == true)
  94. {
  95. string const PackageName = List.Package();
  96. if (PackageName.empty() == true)
  97. return false;
  98. /* As we handle Arch all packages as architecture bounded
  99. we add all information to every (simulated) arch package */
  100. std::vector<string> genArch;
  101. if (List.ArchitectureAll() == true)
  102. genArch = APT::Configuration::getArchitectures();
  103. else
  104. genArch.push_back(List.Architecture());
  105. for (std::vector<string>::const_iterator arch = genArch.begin();
  106. arch != genArch.end(); ++arch)
  107. {
  108. // Get a pointer to the package structure
  109. pkgCache::PkgIterator Pkg;
  110. if (NewPackage(Pkg, PackageName, *arch) == false)
  111. return _error->Error(_("Error occurred while processing %s (NewPackage)"),PackageName.c_str());
  112. Counter++;
  113. if (Counter % 100 == 0 && Progress != 0)
  114. Progress->Progress(List.Offset());
  115. /* Get a pointer to the version structure. We know the list is sorted
  116. so we use that fact in the search. Insertion of new versions is
  117. done with correct sorting */
  118. string Version = List.Version();
  119. if (Version.empty() == true)
  120. {
  121. // we first process the package, then the descriptions
  122. // (this has the bonus that we get MMap error when we run out
  123. // of MMap space)
  124. if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
  125. return _error->Error(_("Error occurred while processing %s (UsePackage1)"),
  126. PackageName.c_str());
  127. // Find the right version to write the description
  128. MD5SumValue CurMd5 = List.Description_md5();
  129. pkgCache::VerIterator Ver = Pkg.VersionList();
  130. map_ptrloc *LastVer = &Pkg->VersionList;
  131. for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
  132. {
  133. pkgCache::DescIterator Desc = Ver.DescriptionList();
  134. map_ptrloc *LastDesc = &Ver->DescriptionList;
  135. bool duplicate=false;
  136. // don't add a new description if we have one for the given
  137. // md5 && language
  138. for ( ; Desc.end() == false; Desc++)
  139. if (MD5SumValue(Desc.md5()) == CurMd5 &&
  140. Desc.LanguageCode() == List.DescriptionLanguage())
  141. duplicate=true;
  142. if(duplicate)
  143. continue;
  144. for (Desc = Ver.DescriptionList();
  145. Desc.end() == false;
  146. LastDesc = &Desc->NextDesc, Desc++)
  147. {
  148. if (MD5SumValue(Desc.md5()) == CurMd5)
  149. {
  150. // Add new description
  151. *LastDesc = NewDescription(Desc, List.DescriptionLanguage(), CurMd5, *LastDesc);
  152. Desc->ParentPkg = Pkg.Index();
  153. if ((*LastDesc == 0 && _error->PendingError()) || NewFileDesc(Desc,List) == false)
  154. return _error->Error(_("Error occurred while processing %s (NewFileDesc1)"),PackageName.c_str());
  155. break;
  156. }
  157. }
  158. }
  159. continue;
  160. }
  161. pkgCache::VerIterator Ver = Pkg.VersionList();
  162. map_ptrloc *LastVer = &Pkg->VersionList;
  163. int Res = 1;
  164. for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
  165. {
  166. Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
  167. if (Res >= 0)
  168. break;
  169. }
  170. /* We already have a version for this item, record that we
  171. saw it */
  172. unsigned long Hash = List.VersionHash();
  173. if (Res == 0 && Ver->Hash == Hash)
  174. {
  175. if (List.UsePackage(Pkg,Ver) == false)
  176. return _error->Error(_("Error occurred while processing %s (UsePackage2)"),
  177. PackageName.c_str());
  178. if (NewFileVer(Ver,List) == false)
  179. return _error->Error(_("Error occurred while processing %s (NewFileVer1)"),
  180. PackageName.c_str());
  181. // Read only a single record and return
  182. if (OutVer != 0)
  183. {
  184. *OutVer = Ver;
  185. FoundFileDeps |= List.HasFileDeps();
  186. return true;
  187. }
  188. continue;
  189. }
  190. // Skip to the end of the same version set.
  191. if (Res == 0)
  192. {
  193. for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
  194. {
  195. Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
  196. if (Res != 0)
  197. break;
  198. }
  199. }
  200. // Add a new version
  201. *LastVer = NewVersion(Ver,Version,*LastVer);
  202. Ver->ParentPkg = Pkg.Index();
  203. Ver->Hash = Hash;
  204. if ((*LastVer == 0 && _error->PendingError()) || List.NewVersion(Ver) == false)
  205. return _error->Error(_("Error occurred while processing %s (NewVersion1)"),
  206. PackageName.c_str());
  207. if (List.UsePackage(Pkg,Ver) == false)
  208. return _error->Error(_("Error occurred while processing %s (UsePackage3)"),
  209. PackageName.c_str());
  210. if (NewFileVer(Ver,List) == false)
  211. return _error->Error(_("Error occurred while processing %s (NewVersion2)"),
  212. PackageName.c_str());
  213. // Read only a single record and return
  214. if (OutVer != 0)
  215. {
  216. *OutVer = Ver;
  217. FoundFileDeps |= List.HasFileDeps();
  218. return true;
  219. }
  220. /* Record the Description data. Description data always exist in
  221. Packages and Translation-* files. */
  222. pkgCache::DescIterator Desc = Ver.DescriptionList();
  223. map_ptrloc *LastDesc = &Ver->DescriptionList;
  224. // Skip to the end of description set
  225. for (; Desc.end() == false; LastDesc = &Desc->NextDesc, Desc++);
  226. // Add new description
  227. *LastDesc = NewDescription(Desc, List.DescriptionLanguage(), List.Description_md5(), *LastDesc);
  228. Desc->ParentPkg = Pkg.Index();
  229. if ((*LastDesc == 0 && _error->PendingError()) || NewFileDesc(Desc,List) == false)
  230. return _error->Error(_("Error occurred while processing %s (NewFileDesc2)"),PackageName.c_str());
  231. }
  232. }
  233. FoundFileDeps |= List.HasFileDeps();
  234. if (Cache.HeaderP->PackageCount >= (1ULL<<sizeof(Cache.PkgP->ID)*8)-1)
  235. return _error->Error(_("Wow, you exceeded the number of package "
  236. "names this APT is capable of."));
  237. if (Cache.HeaderP->VersionCount >= (1ULL<<(sizeof(Cache.VerP->ID)*8))-1)
  238. return _error->Error(_("Wow, you exceeded the number of versions "
  239. "this APT is capable of."));
  240. if (Cache.HeaderP->DescriptionCount >= (1ULL<<(sizeof(Cache.DescP->ID)*8))-1)
  241. return _error->Error(_("Wow, you exceeded the number of descriptions "
  242. "this APT is capable of."));
  243. if (Cache.HeaderP->DependsCount >= (1ULL<<(sizeof(Cache.DepP->ID)*8))-1ULL)
  244. return _error->Error(_("Wow, you exceeded the number of dependencies "
  245. "this APT is capable of."));
  246. return true;
  247. }
  248. /*}}}*/
  249. // CacheGenerator::MergeFileProvides - Merge file provides /*{{{*/
  250. // ---------------------------------------------------------------------
  251. /* If we found any file depends while parsing the main list we need to
  252. resolve them. Since it is undesired to load the entire list of files
  253. into the cache as virtual packages we do a two stage effort. MergeList
  254. identifies the file depends and this creates Provdies for them by
  255. re-parsing all the indexs. */
  256. bool pkgCacheGenerator::MergeFileProvides(ListParser &List)
  257. {
  258. List.Owner = this;
  259. unsigned int Counter = 0;
  260. while (List.Step() == true)
  261. {
  262. string PackageName = List.Package();
  263. if (PackageName.empty() == true)
  264. return false;
  265. string Version = List.Version();
  266. if (Version.empty() == true)
  267. continue;
  268. pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
  269. if (Pkg.end() == true)
  270. return _error->Error(_("Error occurred while processing %s (FindPkg)"),
  271. PackageName.c_str());
  272. Counter++;
  273. if (Counter % 100 == 0 && Progress != 0)
  274. Progress->Progress(List.Offset());
  275. unsigned long Hash = List.VersionHash();
  276. pkgCache::VerIterator Ver = Pkg.VersionList();
  277. for (; Ver.end() == false; Ver++)
  278. {
  279. if (Ver->Hash == Hash && Version.c_str() == Ver.VerStr())
  280. {
  281. if (List.CollectFileProvides(Cache,Ver) == false)
  282. return _error->Error(_("Error occurred while processing %s (CollectFileProvides)"),PackageName.c_str());
  283. break;
  284. }
  285. }
  286. if (Ver.end() == true)
  287. _error->Warning(_("Package %s %s was not found while processing file dependencies"),PackageName.c_str(),Version.c_str());
  288. }
  289. return true;
  290. }
  291. /*}}}*/
  292. // CacheGenerator::NewGroup - Add a new group /*{{{*/
  293. // ---------------------------------------------------------------------
  294. /* This creates a new group structure and adds it to the hash table */
  295. bool pkgCacheGenerator::NewGroup(pkgCache::GrpIterator &Grp, const string &Name) {
  296. Grp = Cache.FindGrp(Name);
  297. if (Grp.end() == false)
  298. return true;
  299. // Get a structure
  300. unsigned long const Group = Map.Allocate(sizeof(pkgCache::Group));
  301. if (unlikely(Group == 0))
  302. return false;
  303. Grp = pkgCache::GrpIterator(Cache, Cache.GrpP + Group);
  304. Grp->Name = Map.WriteString(Name);
  305. if (unlikely(Grp->Name == 0))
  306. return false;
  307. // Insert it into the hash table
  308. unsigned long const Hash = Cache.Hash(Name);
  309. Grp->Next = Cache.HeaderP->GrpHashTable[Hash];
  310. Cache.HeaderP->GrpHashTable[Hash] = Group;
  311. Cache.HeaderP->GroupCount++;
  312. return true;
  313. }
  314. /*}}}*/
  315. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  316. // ---------------------------------------------------------------------
  317. /* This creates a new package structure and adds it to the hash table */
  318. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name,
  319. const string &Arch) {
  320. pkgCache::GrpIterator Grp;
  321. if (unlikely(NewGroup(Grp, Name) == false))
  322. return false;
  323. Pkg = Grp.FindPkg(Arch);
  324. if (Pkg.end() == false)
  325. return true;
  326. // Get a structure
  327. unsigned long const Package = Map.Allocate(sizeof(pkgCache::Package));
  328. if (unlikely(Package == 0))
  329. return false;
  330. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  331. // Insert it into the hash table
  332. unsigned long const Hash = Cache.Hash(Name);
  333. Pkg->NextPackage = Cache.HeaderP->PkgHashTable[Hash];
  334. Cache.HeaderP->PkgHashTable[Hash] = Package;
  335. // remember the packages in the group
  336. Grp->FirstPackage = Package;
  337. if (Grp->LastPackage == 0)
  338. Grp->LastPackage = Package;
  339. // Set the name, arch and the ID
  340. Pkg->Name = Grp->Name;
  341. Pkg->Group = Grp.Index();
  342. Pkg->Arch = WriteUniqString(Arch.c_str());
  343. if (unlikely(Pkg->Arch == 0))
  344. return false;
  345. Pkg->ID = Cache.HeaderP->PackageCount++;
  346. return true;
  347. }
  348. /*}}}*/
  349. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  350. // ---------------------------------------------------------------------
  351. /* */
  352. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  353. ListParser &List)
  354. {
  355. if (CurrentFile == 0)
  356. return true;
  357. // Get a structure
  358. unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
  359. if (VerFile == 0)
  360. return 0;
  361. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  362. VF->File = CurrentFile - Cache.PkgFileP;
  363. // Link it to the end of the list
  364. map_ptrloc *Last = &Ver->FileList;
  365. for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; V++)
  366. Last = &V->NextFile;
  367. VF->NextFile = *Last;
  368. *Last = VF.Index();
  369. VF->Offset = List.Offset();
  370. VF->Size = List.Size();
  371. if (Cache.HeaderP->MaxVerFileSize < VF->Size)
  372. Cache.HeaderP->MaxVerFileSize = VF->Size;
  373. Cache.HeaderP->VerFileCount++;
  374. return true;
  375. }
  376. /*}}}*/
  377. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  378. // ---------------------------------------------------------------------
  379. /* This puts a version structure in the linked list */
  380. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  381. const string &VerStr,
  382. unsigned long Next)
  383. {
  384. // Get a structure
  385. unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
  386. if (Version == 0)
  387. return 0;
  388. // Fill it in
  389. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  390. Ver->NextVer = Next;
  391. Ver->ID = Cache.HeaderP->VersionCount++;
  392. Ver->VerStr = Map.WriteString(VerStr);
  393. if (Ver->VerStr == 0)
  394. return 0;
  395. return Version;
  396. }
  397. /*}}}*/
  398. // CacheGenerator::NewFileDesc - Create a new File<->Desc association /*{{{*/
  399. // ---------------------------------------------------------------------
  400. /* */
  401. bool pkgCacheGenerator::NewFileDesc(pkgCache::DescIterator &Desc,
  402. ListParser &List)
  403. {
  404. if (CurrentFile == 0)
  405. return true;
  406. // Get a structure
  407. unsigned long DescFile = Map.Allocate(sizeof(pkgCache::DescFile));
  408. if (DescFile == 0)
  409. return false;
  410. pkgCache::DescFileIterator DF(Cache,Cache.DescFileP + DescFile);
  411. DF->File = CurrentFile - Cache.PkgFileP;
  412. // Link it to the end of the list
  413. map_ptrloc *Last = &Desc->FileList;
  414. for (pkgCache::DescFileIterator D = Desc.FileList(); D.end() == false; D++)
  415. Last = &D->NextFile;
  416. DF->NextFile = *Last;
  417. *Last = DF.Index();
  418. DF->Offset = List.Offset();
  419. DF->Size = List.Size();
  420. if (Cache.HeaderP->MaxDescFileSize < DF->Size)
  421. Cache.HeaderP->MaxDescFileSize = DF->Size;
  422. Cache.HeaderP->DescFileCount++;
  423. return true;
  424. }
  425. /*}}}*/
  426. // CacheGenerator::NewDescription - Create a new Description /*{{{*/
  427. // ---------------------------------------------------------------------
  428. /* This puts a description structure in the linked list */
  429. map_ptrloc pkgCacheGenerator::NewDescription(pkgCache::DescIterator &Desc,
  430. const string &Lang,
  431. const MD5SumValue &md5sum,
  432. map_ptrloc Next)
  433. {
  434. // Get a structure
  435. map_ptrloc Description = Map.Allocate(sizeof(pkgCache::Description));
  436. if (Description == 0)
  437. return 0;
  438. // Fill it in
  439. Desc = pkgCache::DescIterator(Cache,Cache.DescP + Description);
  440. Desc->NextDesc = Next;
  441. Desc->ID = Cache.HeaderP->DescriptionCount++;
  442. Desc->language_code = Map.WriteString(Lang);
  443. Desc->md5sum = Map.WriteString(md5sum.Value());
  444. if (Desc->language_code == 0 || Desc->md5sum == 0)
  445. return 0;
  446. return Description;
  447. }
  448. /*}}}*/
  449. // CacheGenerator::FinishCache - do various finish operations /*{{{*/
  450. // ---------------------------------------------------------------------
  451. /* This prepares the Cache for delivery */
  452. bool pkgCacheGenerator::FinishCache(OpProgress &Progress) {
  453. // FIXME: add progress reporting for this operation
  454. // Do we have different architectures in your groups ?
  455. vector<string> archs = APT::Configuration::getArchitectures();
  456. if (archs.size() > 1) {
  457. // Create Conflicts in between the group
  458. for (pkgCache::GrpIterator G = GetCache().GrpBegin(); G.end() != true; G++) {
  459. string const PkgName = G.Name();
  460. for (pkgCache::PkgIterator P = G.PackageList(); P.end() != true; P = G.NextPkg(P)) {
  461. for (pkgCache::VerIterator V = P.VersionList(); V.end() != true; V++) {
  462. string const Arch = V.Arch();
  463. map_ptrloc *OldDepLast = NULL;
  464. /* MultiArch handling introduces a lot of implicit Dependencies:
  465. - MultiArch: same → Co-Installable if they have the same version
  466. - Architecture: all → Need to be Co-Installable for internal reasons
  467. - All others conflict with all other group members */
  468. bool const coInstall = (V->MultiArch == pkgCache::Version::All ||
  469. V->MultiArch == pkgCache::Version::Same);
  470. for (vector<string>::const_iterator A = archs.begin(); A != archs.end(); ++A) {
  471. if (*A == Arch)
  472. continue;
  473. /* We allow only one installed arch at the time
  474. per group, therefore each group member conflicts
  475. with all other group members */
  476. pkgCache::PkgIterator D = G.FindPkg(*A);
  477. if (D.end() == true)
  478. continue;
  479. if (coInstall == true) {
  480. // Replaces: ${self}:other ( << ${binary:Version})
  481. NewDepends(D, V, V.VerStr(),
  482. pkgCache::Dep::Less, pkgCache::Dep::Replaces,
  483. OldDepLast);
  484. // Breaks: ${self}:other (!= ${binary:Version})
  485. NewDepends(D, V, V.VerStr(),
  486. pkgCache::Dep::Less, pkgCache::Dep::DpkgBreaks,
  487. OldDepLast);
  488. NewDepends(D, V, V.VerStr(),
  489. pkgCache::Dep::Greater, pkgCache::Dep::DpkgBreaks,
  490. OldDepLast);
  491. } else {
  492. // Conflicts: ${self}:other
  493. NewDepends(D, V, "",
  494. pkgCache::Dep::NoOp, pkgCache::Dep::Conflicts,
  495. OldDepLast);
  496. }
  497. }
  498. }
  499. }
  500. }
  501. }
  502. return true;
  503. }
  504. /*}}}*/
  505. // CacheGenerator::NewDepends - Create a dependency element /*{{{*/
  506. // ---------------------------------------------------------------------
  507. /* This creates a dependency element in the tree. It is linked to the
  508. version and to the package that it is pointing to. */
  509. bool pkgCacheGenerator::NewDepends(pkgCache::PkgIterator &Pkg,
  510. pkgCache::VerIterator &Ver,
  511. string const &Version,
  512. unsigned int const &Op,
  513. unsigned int const &Type,
  514. map_ptrloc *OldDepLast)
  515. {
  516. // Get a structure
  517. unsigned long const Dependency = Map.Allocate(sizeof(pkgCache::Dependency));
  518. if (unlikely(Dependency == 0))
  519. return false;
  520. // Fill it in
  521. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  522. Dep->ParentVer = Ver.Index();
  523. Dep->Type = Type;
  524. Dep->CompareOp = Op;
  525. Dep->ID = Cache.HeaderP->DependsCount++;
  526. // Probe the reverse dependency list for a version string that matches
  527. if (Version.empty() == false)
  528. {
  529. /* for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  530. if (I->Version != 0 && I.TargetVer() == Version)
  531. Dep->Version = I->Version;*/
  532. if (Dep->Version == 0)
  533. if (unlikely((Dep->Version = Map.WriteString(Version)) == 0))
  534. return false;
  535. }
  536. // Link it to the package
  537. Dep->Package = Pkg.Index();
  538. Dep->NextRevDepends = Pkg->RevDepends;
  539. Pkg->RevDepends = Dep.Index();
  540. // Do we know where to link the Dependency to?
  541. if (OldDepLast == NULL)
  542. {
  543. OldDepLast = &Ver->DependsList;
  544. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  545. OldDepLast = &D->NextDepends;
  546. }
  547. Dep->NextDepends = *OldDepLast;
  548. *OldDepLast = Dep.Index();
  549. OldDepLast = &Dep->NextDepends;
  550. return true;
  551. }
  552. /*}}}*/
  553. // ListParser::NewDepends - Create the environment for a new dependency /*{{{*/
  554. // ---------------------------------------------------------------------
  555. /* This creates a Group and the Package to link this dependency to if
  556. needed and handles also the caching of the old endpoint */
  557. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
  558. const string &PackageName,
  559. const string &Arch,
  560. const string &Version,
  561. unsigned int Op,
  562. unsigned int Type)
  563. {
  564. pkgCache::GrpIterator Grp;
  565. if (unlikely(Owner->NewGroup(Grp, PackageName) == false))
  566. return false;
  567. // Locate the target package
  568. pkgCache::PkgIterator Pkg = Grp.FindPkg(Arch);
  569. if (Pkg.end() == true) {
  570. if (unlikely(Owner->NewPackage(Pkg, PackageName, Arch) == false))
  571. return false;
  572. }
  573. // Is it a file dependency?
  574. if (unlikely(PackageName[0] == '/'))
  575. FoundFileDeps = true;
  576. /* Caching the old end point speeds up generation substantially */
  577. if (OldDepVer != Ver) {
  578. OldDepLast = NULL;
  579. OldDepVer = Ver;
  580. }
  581. return Owner->NewDepends(Pkg, Ver, Version, Op, Type, OldDepLast);
  582. }
  583. /*}}}*/
  584. // ListParser::NewProvides - Create a Provides element /*{{{*/
  585. // ---------------------------------------------------------------------
  586. /* */
  587. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
  588. const string &PkgName,
  589. const string &PkgArch,
  590. const string &Version)
  591. {
  592. pkgCache &Cache = Owner->Cache;
  593. // We do not add self referencing provides
  594. if (Ver.ParentPkg().Name() == PkgName && PkgArch == Ver.Arch())
  595. return true;
  596. // Get a structure
  597. unsigned long const Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
  598. if (unlikely(Provides == 0))
  599. return false;
  600. Cache.HeaderP->ProvidesCount++;
  601. // Fill it in
  602. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  603. Prv->Version = Ver.Index();
  604. Prv->NextPkgProv = Ver->ProvidesList;
  605. Ver->ProvidesList = Prv.Index();
  606. if (Version.empty() == false && unlikely((Prv->ProvideVersion = WriteString(Version)) == 0))
  607. return false;
  608. // Locate the target package
  609. pkgCache::PkgIterator Pkg;
  610. if (unlikely(Owner->NewPackage(Pkg,PkgName, PkgArch) == false))
  611. return false;
  612. // Link it to the package
  613. Prv->ParentPkg = Pkg.Index();
  614. Prv->NextProvides = Pkg->ProvidesList;
  615. Pkg->ProvidesList = Prv.Index();
  616. return true;
  617. }
  618. /*}}}*/
  619. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  620. // ---------------------------------------------------------------------
  621. /* This is used to select which file is to be associated with all newly
  622. added versions. The caller is responsible for setting the IMS fields. */
  623. bool pkgCacheGenerator::SelectFile(const string &File,const string &Site,
  624. const pkgIndexFile &Index,
  625. unsigned long Flags)
  626. {
  627. // Get some space for the structure
  628. CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
  629. if (CurrentFile == Cache.PkgFileP)
  630. return false;
  631. // Fill it in
  632. CurrentFile->FileName = Map.WriteString(File);
  633. CurrentFile->Site = WriteUniqString(Site);
  634. CurrentFile->NextFile = Cache.HeaderP->FileList;
  635. CurrentFile->Flags = Flags;
  636. CurrentFile->ID = Cache.HeaderP->PackageFileCount;
  637. CurrentFile->IndexType = WriteUniqString(Index.GetType()->Label);
  638. PkgFileName = File;
  639. Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
  640. Cache.HeaderP->PackageFileCount++;
  641. if (CurrentFile->FileName == 0)
  642. return false;
  643. if (Progress != 0)
  644. Progress->SubProgress(Index.Size());
  645. return true;
  646. }
  647. /*}}}*/
  648. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  649. // ---------------------------------------------------------------------
  650. /* This is used to create handles to strings. Given the same text it
  651. always returns the same number */
  652. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  653. unsigned int Size)
  654. {
  655. /* We use a very small transient hash table here, this speeds up generation
  656. by a fair amount on slower machines */
  657. pkgCache::StringItem *&Bucket = UniqHash[(S[0]*5 + S[1]) % _count(UniqHash)];
  658. if (Bucket != 0 &&
  659. stringcmp(S,S+Size,Cache.StrP + Bucket->String) == 0)
  660. return Bucket->String;
  661. // Search for an insertion point
  662. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  663. int Res = 1;
  664. map_ptrloc *Last = &Cache.HeaderP->StringList;
  665. for (; I != Cache.StringItemP; Last = &I->NextItem,
  666. I = Cache.StringItemP + I->NextItem)
  667. {
  668. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  669. if (Res >= 0)
  670. break;
  671. }
  672. // Match
  673. if (Res == 0)
  674. {
  675. Bucket = I;
  676. return I->String;
  677. }
  678. // Get a structure
  679. unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
  680. if (Item == 0)
  681. return 0;
  682. // Fill in the structure
  683. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  684. ItemP->NextItem = I - Cache.StringItemP;
  685. *Last = Item;
  686. ItemP->String = Map.WriteString(S,Size);
  687. if (ItemP->String == 0)
  688. return 0;
  689. Bucket = ItemP;
  690. return ItemP->String;
  691. }
  692. /*}}}*/
  693. // CheckValidity - Check that a cache is up-to-date /*{{{*/
  694. // ---------------------------------------------------------------------
  695. /* This just verifies that each file in the list of index files exists,
  696. has matching attributes with the cache and the cache does not have
  697. any extra files. */
  698. static bool CheckValidity(const string &CacheFile, FileIterator Start,
  699. FileIterator End,MMap **OutMap = 0)
  700. {
  701. // No file, certainly invalid
  702. if (CacheFile.empty() == true || FileExists(CacheFile) == false)
  703. return false;
  704. // Map it
  705. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  706. SPtr<MMap> Map = new MMap(CacheF,0);
  707. pkgCache Cache(Map);
  708. if (_error->PendingError() == true || Map->Size() == 0)
  709. {
  710. _error->Discard();
  711. return false;
  712. }
  713. /* Now we check every index file, see if it is in the cache,
  714. verify the IMS data and check that it is on the disk too.. */
  715. SPtrArray<bool> Visited = new bool[Cache.HeaderP->PackageFileCount];
  716. memset(Visited,0,sizeof(*Visited)*Cache.HeaderP->PackageFileCount);
  717. for (; Start != End; Start++)
  718. {
  719. if ((*Start)->HasPackages() == false)
  720. continue;
  721. if ((*Start)->Exists() == false)
  722. {
  723. #if 0 // mvo: we no longer give a message here (Default Sources spec)
  724. _error->WarningE("stat",_("Couldn't stat source package list %s"),
  725. (*Start)->Describe().c_str());
  726. #endif
  727. continue;
  728. }
  729. // FindInCache is also expected to do an IMS check.
  730. pkgCache::PkgFileIterator File = (*Start)->FindInCache(Cache);
  731. if (File.end() == true)
  732. return false;
  733. Visited[File->ID] = true;
  734. }
  735. for (unsigned I = 0; I != Cache.HeaderP->PackageFileCount; I++)
  736. if (Visited[I] == false)
  737. return false;
  738. if (_error->PendingError() == true)
  739. {
  740. _error->Discard();
  741. return false;
  742. }
  743. if (OutMap != 0)
  744. *OutMap = Map.UnGuard();
  745. return true;
  746. }
  747. /*}}}*/
  748. // ComputeSize - Compute the total size of a bunch of files /*{{{*/
  749. // ---------------------------------------------------------------------
  750. /* Size is kind of an abstract notion that is only used for the progress
  751. meter */
  752. static unsigned long ComputeSize(FileIterator Start,FileIterator End)
  753. {
  754. unsigned long TotalSize = 0;
  755. for (; Start != End; Start++)
  756. {
  757. if ((*Start)->HasPackages() == false)
  758. continue;
  759. TotalSize += (*Start)->Size();
  760. }
  761. return TotalSize;
  762. }
  763. /*}}}*/
  764. // BuildCache - Merge the list of index files into the cache /*{{{*/
  765. // ---------------------------------------------------------------------
  766. /* */
  767. static bool BuildCache(pkgCacheGenerator &Gen,
  768. OpProgress &Progress,
  769. unsigned long &CurrentSize,unsigned long TotalSize,
  770. FileIterator Start, FileIterator End)
  771. {
  772. FileIterator I;
  773. for (I = Start; I != End; I++)
  774. {
  775. if ((*I)->HasPackages() == false)
  776. continue;
  777. if ((*I)->Exists() == false)
  778. continue;
  779. if ((*I)->FindInCache(Gen.GetCache()).end() == false)
  780. {
  781. _error->Warning("Duplicate sources.list entry %s",
  782. (*I)->Describe().c_str());
  783. continue;
  784. }
  785. unsigned long Size = (*I)->Size();
  786. Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
  787. CurrentSize += Size;
  788. if ((*I)->Merge(Gen,Progress) == false)
  789. return false;
  790. }
  791. if (Gen.HasFileDeps() == true)
  792. {
  793. Progress.Done();
  794. TotalSize = ComputeSize(Start, End);
  795. CurrentSize = 0;
  796. for (I = Start; I != End; I++)
  797. {
  798. unsigned long Size = (*I)->Size();
  799. Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
  800. CurrentSize += Size;
  801. if ((*I)->MergeFileProvides(Gen,Progress) == false)
  802. return false;
  803. }
  804. }
  805. return true;
  806. }
  807. /*}}}*/
  808. // MakeStatusCache - Construct the status cache /*{{{*/
  809. // ---------------------------------------------------------------------
  810. /* This makes sure that the status cache (the cache that has all
  811. index files from the sources list and all local ones) is ready
  812. to be mmaped. If OutMap is not zero then a MMap object representing
  813. the cache will be stored there. This is pretty much mandetory if you
  814. are using AllowMem. AllowMem lets the function be run as non-root
  815. where it builds the cache 'fast' into a memory buffer. */
  816. bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
  817. MMap **OutMap,bool AllowMem)
  818. {
  819. unsigned long MapSize = _config->FindI("APT::Cache-Limit",24*1024*1024);
  820. vector<pkgIndexFile *> Files;
  821. for (vector<metaIndex *>::const_iterator i = List.begin();
  822. i != List.end();
  823. i++)
  824. {
  825. vector <pkgIndexFile *> *Indexes = (*i)->GetIndexFiles();
  826. for (vector<pkgIndexFile *>::const_iterator j = Indexes->begin();
  827. j != Indexes->end();
  828. j++)
  829. Files.push_back (*j);
  830. }
  831. unsigned long EndOfSource = Files.size();
  832. if (_system->AddStatusFiles(Files) == false)
  833. return false;
  834. // Decide if we can write to the files..
  835. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  836. string SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  837. // Decide if we can write to the cache
  838. bool Writeable = false;
  839. if (CacheFile.empty() == false)
  840. Writeable = access(flNotFile(CacheFile).c_str(),W_OK) == 0;
  841. else
  842. if (SrcCacheFile.empty() == false)
  843. Writeable = access(flNotFile(SrcCacheFile).c_str(),W_OK) == 0;
  844. if (Writeable == false && AllowMem == false && CacheFile.empty() == false)
  845. return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
  846. Progress.OverallProgress(0,1,1,_("Reading package lists"));
  847. // Cache is OK, Fin.
  848. if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
  849. {
  850. Progress.OverallProgress(1,1,1,_("Reading package lists"));
  851. return true;
  852. }
  853. /* At this point we know we need to reconstruct the package cache,
  854. begin. */
  855. SPtr<FileFd> CacheF;
  856. SPtr<DynamicMMap> Map;
  857. if (Writeable == true && CacheFile.empty() == false)
  858. {
  859. unlink(CacheFile.c_str());
  860. CacheF = new FileFd(CacheFile,FileFd::WriteEmpty);
  861. fchmod(CacheF->Fd(),0644);
  862. Map = new DynamicMMap(*CacheF,MMap::Public,MapSize);
  863. if (_error->PendingError() == true)
  864. return false;
  865. }
  866. else
  867. {
  868. // Just build it in memory..
  869. Map = new DynamicMMap(0,MapSize);
  870. }
  871. // Lets try the source cache.
  872. unsigned long CurrentSize = 0;
  873. unsigned long TotalSize = 0;
  874. if (CheckValidity(SrcCacheFile,Files.begin(),
  875. Files.begin()+EndOfSource) == true)
  876. {
  877. // Preload the map with the source cache
  878. FileFd SCacheF(SrcCacheFile,FileFd::ReadOnly);
  879. unsigned long alloc = Map->RawAllocate(SCacheF.Size());
  880. if ((alloc == 0 && _error->PendingError())
  881. || SCacheF.Read((unsigned char *)Map->Data() + alloc,
  882. SCacheF.Size()) == false)
  883. return false;
  884. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  885. // Build the status cache
  886. pkgCacheGenerator Gen(Map.Get(),&Progress);
  887. if (_error->PendingError() == true)
  888. return false;
  889. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  890. Files.begin()+EndOfSource,Files.end()) == false)
  891. return false;
  892. // FIXME: move me to a better place
  893. Gen.FinishCache(Progress);
  894. }
  895. else
  896. {
  897. TotalSize = ComputeSize(Files.begin(),Files.end());
  898. // Build the source cache
  899. pkgCacheGenerator Gen(Map.Get(),&Progress);
  900. if (_error->PendingError() == true)
  901. return false;
  902. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  903. Files.begin(),Files.begin()+EndOfSource) == false)
  904. return false;
  905. // Write it back
  906. if (Writeable == true && SrcCacheFile.empty() == false)
  907. {
  908. FileFd SCacheF(SrcCacheFile,FileFd::WriteEmpty);
  909. if (_error->PendingError() == true)
  910. return false;
  911. fchmod(SCacheF.Fd(),0644);
  912. // Write out the main data
  913. if (SCacheF.Write(Map->Data(),Map->Size()) == false)
  914. return _error->Error(_("IO Error saving source cache"));
  915. SCacheF.Sync();
  916. // Write out the proper header
  917. Gen.GetCache().HeaderP->Dirty = false;
  918. if (SCacheF.Seek(0) == false ||
  919. SCacheF.Write(Map->Data(),sizeof(*Gen.GetCache().HeaderP)) == false)
  920. return _error->Error(_("IO Error saving source cache"));
  921. Gen.GetCache().HeaderP->Dirty = true;
  922. SCacheF.Sync();
  923. }
  924. // Build the status cache
  925. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  926. Files.begin()+EndOfSource,Files.end()) == false)
  927. return false;
  928. // FIXME: move me to a better place
  929. Gen.FinishCache(Progress);
  930. }
  931. if (_error->PendingError() == true)
  932. return false;
  933. if (OutMap != 0)
  934. {
  935. if (CacheF != 0)
  936. {
  937. delete Map.UnGuard();
  938. *OutMap = new MMap(*CacheF,0);
  939. }
  940. else
  941. {
  942. *OutMap = Map.UnGuard();
  943. }
  944. }
  945. return true;
  946. }
  947. /*}}}*/
  948. // MakeOnlyStatusCache - Build a cache with just the status files /*{{{*/
  949. // ---------------------------------------------------------------------
  950. /* */
  951. bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
  952. {
  953. unsigned long MapSize = _config->FindI("APT::Cache-Limit",20*1024*1024);
  954. vector<pkgIndexFile *> Files;
  955. unsigned long EndOfSource = Files.size();
  956. if (_system->AddStatusFiles(Files) == false)
  957. return false;
  958. SPtr<DynamicMMap> Map = new DynamicMMap(0,MapSize);
  959. unsigned long CurrentSize = 0;
  960. unsigned long TotalSize = 0;
  961. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  962. // Build the status cache
  963. Progress.OverallProgress(0,1,1,_("Reading package lists"));
  964. pkgCacheGenerator Gen(Map.Get(),&Progress);
  965. if (_error->PendingError() == true)
  966. return false;
  967. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  968. Files.begin()+EndOfSource,Files.end()) == false)
  969. return false;
  970. // FIXME: move me to a better place
  971. Gen.FinishCache(Progress);
  972. if (_error->PendingError() == true)
  973. return false;
  974. *OutMap = Map.UnGuard();
  975. return true;
  976. }
  977. /*}}}*/