pkgcachegen.cc 30 KB

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