pkgcachegen.cc 30 KB

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