pkgcachegen.cc 36 KB

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