pkgcachegen.cc 31 KB

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