pkgcachegen.cc 40 KB

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