pkgcachegen.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.24 1998/12/07 07:26:21 jgg 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. #include <apt-pkg/pkgcachegen.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/version.h>
  16. #include <apt-pkg/progress.h>
  17. #include <apt-pkg/sourcelist.h>
  18. #include <apt-pkg/configuration.h>
  19. #include <apt-pkg/deblistparser.h>
  20. #include <strutl.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. /*}}}*/
  24. // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* We set the diry flag and make sure that is written to the disk */
  27. pkgCacheGenerator::pkgCacheGenerator(DynamicMMap &Map,OpProgress &Prog) :
  28. Map(Map), Cache(Map), Progress(Prog)
  29. {
  30. if (_error->PendingError() == true)
  31. return;
  32. if (Map.Size() == 0)
  33. {
  34. Map.RawAllocate(sizeof(pkgCache::Header));
  35. *Cache.HeaderP = pkgCache::Header();
  36. }
  37. Cache.HeaderP->Dirty = true;
  38. Map.Sync(0,sizeof(pkgCache::Header));
  39. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  40. }
  41. /*}}}*/
  42. // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* We sync the data then unset the dirty flag in two steps so as to
  45. advoid a problem during a crash */
  46. pkgCacheGenerator::~pkgCacheGenerator()
  47. {
  48. if (_error->PendingError() == true)
  49. return;
  50. if (Map.Sync() == false)
  51. return;
  52. Cache.HeaderP->Dirty = false;
  53. Map.Sync(0,sizeof(pkgCache::Header));
  54. }
  55. /*}}}*/
  56. // CacheGenerator::MergeList - Merge the package list /*{{{*/
  57. // ---------------------------------------------------------------------
  58. /* This provides the generation of the entries in the cache. Each loop
  59. goes through a single package record from the underlying parse engine. */
  60. bool pkgCacheGenerator::MergeList(ListParser &List)
  61. {
  62. List.Owner = this;
  63. int Counter = 0;
  64. while (List.Step() == true)
  65. {
  66. // Get a pointer to the package structure
  67. string PackageName = List.Package();
  68. pkgCache::PkgIterator Pkg;
  69. if (NewPackage(Pkg,PackageName) == false)
  70. return _error->Error("Error occured while processing %s (NewPackage)",PackageName.c_str());
  71. Counter++;
  72. if (Counter % 100 == 0)
  73. Progress.Progress(List.Offset());
  74. /* Get a pointer to the version structure. We know the list is sorted
  75. so we use that fact in the search. Insertion of new versions is
  76. done with correct sorting */
  77. string Version = List.Version();
  78. if (Version.empty() == true)
  79. {
  80. if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
  81. return _error->Error("Error occured while processing %s (UsePackage1)",PackageName.c_str());
  82. continue;
  83. }
  84. pkgCache::VerIterator Ver = Pkg.VersionList();
  85. __apt_ptrloc *Last = &Pkg->VersionList;
  86. int Res = 1;
  87. for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
  88. {
  89. Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
  90. Ver.VerStr() + strlen(Ver.VerStr()));
  91. if (Res >= 0)
  92. break;
  93. }
  94. /* We already have a version for this item, record that we
  95. saw it */
  96. if (Res == 0)
  97. {
  98. if (List.UsePackage(Pkg,Ver) == false)
  99. return _error->Error("Error occured while processing %s (UsePackage2)",PackageName.c_str());
  100. if (NewFileVer(Ver,List) == false)
  101. return _error->Error("Error occured while processing %s (NewFileVer1)",PackageName.c_str());
  102. continue;
  103. }
  104. // Add a new version
  105. *Last = NewVersion(Ver,Version,*Last);
  106. Ver->ParentPkg = Pkg.Index();
  107. if (List.NewVersion(Ver) == false)
  108. return _error->Error("Error occured while processing %s (NewVersion1)",PackageName.c_str());
  109. if (List.UsePackage(Pkg,Ver) == false)
  110. return _error->Error("Error occured while processing %s (UsePackage3)",PackageName.c_str());
  111. if (NewFileVer(Ver,List) == false)
  112. return _error->Error("Error occured while processing %s (NewVersion2)",PackageName.c_str());
  113. }
  114. return true;
  115. }
  116. /*}}}*/
  117. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  118. // ---------------------------------------------------------------------
  119. /* This creates a new package structure and adds it to the hash table */
  120. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
  121. {
  122. Pkg = Cache.FindPkg(Name);
  123. if (Pkg.end() == false)
  124. return true;
  125. // Get a structure
  126. unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
  127. if (Package == 0)
  128. return false;
  129. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  130. // Insert it into the hash table
  131. unsigned long Hash = Cache.Hash(Name);
  132. Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
  133. Cache.HeaderP->HashTable[Hash] = Package;
  134. // Set the name and the ID
  135. Pkg->Name = Map.WriteString(Name);
  136. if (Pkg->Name == 0)
  137. return false;
  138. Pkg->ID = Cache.HeaderP->PackageCount++;
  139. return true;
  140. }
  141. /*}}}*/
  142. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  143. // ---------------------------------------------------------------------
  144. /* */
  145. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  146. ListParser &List)
  147. {
  148. // Get a structure
  149. unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
  150. if (VerFile == 0)
  151. return 0;
  152. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  153. VF->File = CurrentFile - Cache.PkgFileP;
  154. // Link it to the end of the list
  155. __apt_ptrloc *Last = &Ver->FileList;
  156. for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; V++)
  157. Last = &V->NextFile;
  158. VF->NextFile = *Last;
  159. *Last = VF.Index();
  160. VF->Offset = List.Offset();
  161. VF->Size = List.Size();
  162. if (Cache.HeaderP->MaxVerFileSize < VF->Size)
  163. Cache.HeaderP->MaxVerFileSize = VF->Size;
  164. return true;
  165. }
  166. /*}}}*/
  167. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  168. // ---------------------------------------------------------------------
  169. /* This puts a version structure in the linked list */
  170. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  171. string VerStr,
  172. unsigned long Next)
  173. {
  174. // Get a structure
  175. unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
  176. if (Version == 0)
  177. return 0;
  178. // Fill it in
  179. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  180. Ver->NextVer = Next;
  181. Ver->ID = Cache.HeaderP->VersionCount++;
  182. Ver->VerStr = Map.WriteString(VerStr);
  183. if (Ver->VerStr == 0)
  184. return 0;
  185. return Version;
  186. }
  187. /*}}}*/
  188. // ListParser::NewDepends - Create a dependency element /*{{{*/
  189. // ---------------------------------------------------------------------
  190. /* This creates a dependency element in the tree. It is linked to the
  191. version and to the package that it is pointing to. */
  192. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
  193. string PackageName,
  194. string Version,
  195. unsigned int Op,
  196. unsigned int Type)
  197. {
  198. pkgCache &Cache = Owner->Cache;
  199. // Get a structure
  200. unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
  201. if (Dependency == 0)
  202. return false;
  203. // Fill it in
  204. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  205. Dep->ParentVer = Ver.Index();
  206. Dep->Type = Type;
  207. Dep->CompareOp = Op;
  208. Dep->ID = Cache.HeaderP->DependsCount++;
  209. // Locate the target package
  210. pkgCache::PkgIterator Pkg;
  211. if (Owner->NewPackage(Pkg,PackageName) == false)
  212. return false;
  213. // Probe the reverse dependency list for a version string that matches
  214. if (Version.empty() == false)
  215. {
  216. for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  217. if (I->Version != 0 && I.TargetVer() == Version)
  218. Dep->Version = I->Version;
  219. if (Dep->Version == 0)
  220. if ((Dep->Version = WriteString(Version)) == 0)
  221. return false;
  222. }
  223. // Link it to the package
  224. Dep->Package = Pkg.Index();
  225. Dep->NextRevDepends = Pkg->RevDepends;
  226. Pkg->RevDepends = Dep.Index();
  227. /* Link it to the version (at the end of the list)
  228. Caching the old end point speeds up generation substantially */
  229. static pkgCache::VerIterator OldVer(Cache);
  230. static __apt_ptrloc *OldLast;
  231. if (OldVer != Ver)
  232. {
  233. OldLast = &Ver->DependsList;
  234. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  235. OldLast = &D->NextDepends;
  236. OldVer = Ver;
  237. }
  238. Dep->NextDepends = *OldLast;
  239. *OldLast = Dep.Index();
  240. OldLast = &Dep->NextDepends;
  241. return true;
  242. }
  243. /*}}}*/
  244. // ListParser::NewProvides - Create a Provides element /*{{{*/
  245. // ---------------------------------------------------------------------
  246. /* */
  247. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
  248. string PackageName,
  249. string Version)
  250. {
  251. pkgCache &Cache = Owner->Cache;
  252. // We do not add self referencing provides
  253. if (Ver.ParentPkg().Name() == PackageName)
  254. return true;
  255. // Get a structure
  256. unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
  257. if (Provides == 0)
  258. return false;
  259. // Fill it in
  260. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  261. Prv->Version = Ver.Index();
  262. Prv->NextPkgProv = Ver->ProvidesList;
  263. Ver->ProvidesList = Prv.Index();
  264. if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
  265. return false;
  266. // Locate the target package
  267. pkgCache::PkgIterator Pkg;
  268. if (Owner->NewPackage(Pkg,PackageName) == false)
  269. return false;
  270. // Link it to the package
  271. Prv->ParentPkg = Pkg.Index();
  272. Prv->NextProvides = Pkg->ProvidesList;
  273. Pkg->ProvidesList = Prv.Index();
  274. return true;
  275. }
  276. /*}}}*/
  277. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  278. // ---------------------------------------------------------------------
  279. /* This is used to select which file is to be associated with all newly
  280. added versions. */
  281. bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
  282. {
  283. struct stat Buf;
  284. if (stat(File.c_str(),&Buf) == -1)
  285. return _error->Errno("stat","Couldn't stat ",File.c_str());
  286. // Get some space for the structure
  287. CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
  288. if (CurrentFile == Cache.PkgFileP)
  289. return false;
  290. // Fill it in
  291. CurrentFile->FileName = Map.WriteString(File);
  292. CurrentFile->Size = Buf.st_size;
  293. CurrentFile->mtime = Buf.st_mtime;
  294. CurrentFile->NextFile = Cache.HeaderP->FileList;
  295. CurrentFile->Flags = Flags;
  296. CurrentFile->ID = Cache.HeaderP->PackageFileCount;
  297. PkgFileName = File;
  298. Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
  299. Cache.HeaderP->PackageFileCount++;
  300. if (CurrentFile->FileName == 0)
  301. return false;
  302. Progress.SubProgress(Buf.st_size);
  303. return true;
  304. }
  305. /*}}}*/
  306. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  307. // ---------------------------------------------------------------------
  308. /* This is used to create handles to strings. Given the same text it
  309. always returns the same number */
  310. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  311. unsigned int Size)
  312. {
  313. // Search for an insertion point
  314. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  315. int Res = 1;
  316. __apt_ptrloc *Last = &Cache.HeaderP->StringList;
  317. for (; I != Cache.StringItemP; Last = &I->NextItem,
  318. I = Cache.StringItemP + I->NextItem)
  319. {
  320. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  321. if (Res >= 0)
  322. break;
  323. }
  324. // Match
  325. if (Res == 0)
  326. return I->String;
  327. // Get a structure
  328. unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
  329. if (Item == 0)
  330. return 0;
  331. // Fill in the structure
  332. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  333. ItemP->NextItem = I - Cache.StringItemP;
  334. *Last = Item;
  335. ItemP->String = Map.WriteString(S,Size);
  336. if (ItemP->String == 0)
  337. return 0;
  338. return ItemP->String;
  339. }
  340. /*}}}*/
  341. // SrcCacheCheck - Check if the source package cache is uptodate /*{{{*/
  342. // ---------------------------------------------------------------------
  343. /* The source cache is checked against the source list and the files
  344. on disk, any difference results in a false. */
  345. bool pkgSrcCacheCheck(pkgSourceList &List)
  346. {
  347. if (_error->PendingError() == true)
  348. return false;
  349. string CacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  350. string ListDir = _config->FindDir("Dir::State::lists");
  351. // Count the number of missing files
  352. int Missing = 0;
  353. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  354. {
  355. string File = ListDir + URItoFileName(I->PackagesURI());
  356. struct stat Buf;
  357. if (stat(File.c_str(),&Buf) != 0)
  358. {
  359. _error->WarningE("stat","Couldn't stat source package list '%s' (%s)",
  360. I->PackagesInfo().c_str(),File.c_str());
  361. Missing++;
  362. }
  363. }
  364. // Open the source package cache
  365. if (FileExists(CacheFile) == false)
  366. return false;
  367. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  368. if (_error->PendingError() == true)
  369. {
  370. _error->Discard();
  371. return false;
  372. }
  373. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  374. if (_error->PendingError() == true || Map.Size() == 0)
  375. {
  376. _error->Discard();
  377. return false;
  378. }
  379. pkgCache Cache(Map);
  380. if (_error->PendingError() == true)
  381. {
  382. _error->Discard();
  383. return false;
  384. }
  385. // They are certianly out of sync
  386. if (Cache.Head().PackageFileCount != List.size() - Missing)
  387. return false;
  388. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  389. {
  390. // Search for a match in the source list
  391. bool Bad = true;
  392. for (pkgSourceList::const_iterator I = List.begin();
  393. I != List.end(); I++)
  394. {
  395. string File = ListDir + URItoFileName(I->PackagesURI());
  396. if (F.FileName() == File)
  397. {
  398. Bad = false;
  399. break;
  400. }
  401. }
  402. // Check if the file matches what was cached
  403. Bad |= !F.IsOk();
  404. if (Bad == true)
  405. return false;
  406. }
  407. return true;
  408. }
  409. /*}}}*/
  410. // PkgCacheCheck - Check if the package cache is uptodate /*{{{*/
  411. // ---------------------------------------------------------------------
  412. /* This does a simple check of all files used to compose the cache */
  413. bool pkgPkgCacheCheck(string CacheFile)
  414. {
  415. if (_error->PendingError() == true)
  416. return false;
  417. // Open the source package cache
  418. if (FileExists(CacheFile) == false)
  419. return false;
  420. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  421. if (_error->PendingError() == true)
  422. {
  423. _error->Discard();
  424. return false;
  425. }
  426. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  427. if (_error->PendingError() == true || Map.Size() == 0)
  428. {
  429. _error->Discard();
  430. return false;
  431. }
  432. pkgCache Cache(Map);
  433. if (_error->PendingError() == true)
  434. {
  435. _error->Discard();
  436. return false;
  437. }
  438. // Status files that must be in the cache
  439. string Status[3];
  440. Status[0] = _config->FindFile("Dir::State::xstatus");
  441. Status[1]= _config->FindFile("Dir::State::userstatus");
  442. Status[2] = _config->FindFile("Dir::State::status");
  443. // Cheack each file
  444. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  445. {
  446. if (F.IsOk() == false)
  447. return false;
  448. // See if this is one of the status files
  449. for (int I = 0; I != 3; I++)
  450. if (F.FileName() == Status[I])
  451. Status[I] = string();
  452. }
  453. // Make sure all the status files are loaded.
  454. for (int I = 0; I != 3; I++)
  455. {
  456. if (Status[I].empty() == false && FileExists(Status[I]) == true)
  457. return false;
  458. }
  459. return true;
  460. }
  461. /*}}}*/
  462. // AddSourcesSize - Add the size of the status files /*{{{*/
  463. // ---------------------------------------------------------------------
  464. /* This adds the size of all the status files to the size counter */
  465. static bool pkgAddSourcesSize(unsigned long &TotalSize)
  466. {
  467. // Grab the file names
  468. string xstatus = _config->FindFile("Dir::State::xstatus");
  469. string userstatus = _config->FindFile("Dir::State::userstatus");
  470. string status = _config->FindFile("Dir::State::status");
  471. // Grab the sizes
  472. struct stat Buf;
  473. if (stat(xstatus.c_str(),&Buf) == 0)
  474. TotalSize += Buf.st_size;
  475. if (stat(userstatus.c_str(),&Buf) == 0)
  476. TotalSize += Buf.st_size;
  477. if (stat(status.c_str(),&Buf) != 0)
  478. return _error->Errno("stat","Couldn't stat the status file %s",status.c_str());
  479. TotalSize += Buf.st_size;
  480. return true;
  481. }
  482. /*}}}*/
  483. // MergeStatus - Add the status files to the cache /*{{{*/
  484. // ---------------------------------------------------------------------
  485. /* This adds the status files to the map */
  486. static bool pkgMergeStatus(OpProgress &Progress,pkgCacheGenerator &Gen,
  487. unsigned long &CurrentSize,unsigned long TotalSize)
  488. {
  489. // Grab the file names
  490. string Status[3];
  491. Status[0] = _config->FindFile("Dir::State::xstatus");
  492. Status[1]= _config->FindFile("Dir::State::userstatus");
  493. Status[2] = _config->FindFile("Dir::State::status");
  494. for (int I = 0; I != 3; I++)
  495. {
  496. // Check if the file exists and it is not the primary status file.
  497. string File = Status[I];
  498. if (I != 2 && FileExists(File) == false)
  499. continue;
  500. FileFd Pkg(File,FileFd::ReadOnly);
  501. debListParser Parser(Pkg);
  502. Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
  503. if (_error->PendingError() == true)
  504. return _error->Error("Problem opening %s",File.c_str());
  505. CurrentSize += Pkg.Size();
  506. Progress.SubProgress(0,"Local Package State - " + flNotDir(File));
  507. if (Gen.SelectFile(File,pkgCache::Flag::NotSource) == false)
  508. return _error->Error("Problem with SelectFile %s",File.c_str());
  509. if (Gen.MergeList(Parser) == false)
  510. return _error->Error("Problem with MergeList %s",File.c_str());
  511. Progress.Progress(Pkg.Size());
  512. }
  513. return true;
  514. }
  515. /*}}}*/
  516. // MakeStatusCache - Generates a cache that includes the status files /*{{{*/
  517. // ---------------------------------------------------------------------
  518. /* This copies the package source cache and then merges the status and
  519. xstatus files into it. */
  520. bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress)
  521. {
  522. Progress.OverallProgress(0,1,1,"Reading Package Lists");
  523. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  524. bool SrcOk = pkgSrcCacheCheck(List);
  525. bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile);
  526. // Rebuild the source and package caches
  527. if (SrcOk == false)
  528. {
  529. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  530. string ListDir = _config->FindDir("Dir::State::lists");
  531. FileFd SCacheF(SCacheFile,FileFd::WriteEmpty);
  532. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  533. DynamicMMap Map(CacheF,MMap::Public);
  534. if (_error->PendingError() == true)
  535. return false;
  536. pkgCacheGenerator Gen(Map,Progress);
  537. // Prepare the progress indicator
  538. unsigned long TotalSize = 0;
  539. struct stat Buf;
  540. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  541. {
  542. string File = ListDir + URItoFileName(I->PackagesURI());
  543. if (stat(File.c_str(),&Buf) != 0)
  544. continue;
  545. TotalSize += Buf.st_size;
  546. }
  547. if (pkgAddSourcesSize(TotalSize) == false)
  548. return false;
  549. // Generate the pkg source cache
  550. unsigned long CurrentSize = 0;
  551. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  552. {
  553. string File = ListDir + URItoFileName(I->PackagesURI());
  554. if (stat(File.c_str(),&Buf) != 0)
  555. continue;
  556. FileFd Pkg(File,FileFd::ReadOnly);
  557. debListParser Parser(Pkg);
  558. Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
  559. if (_error->PendingError() == true)
  560. return _error->Error("Problem opening %s",File.c_str());
  561. CurrentSize += Pkg.Size();
  562. Progress.SubProgress(0,I->PackagesInfo());
  563. if (Gen.SelectFile(File) == false)
  564. return _error->Error("Problem with SelectFile %s",File.c_str());
  565. if (Gen.MergeList(Parser) == false)
  566. return _error->Error("Problem with MergeList %s",File.c_str());
  567. }
  568. // Write the src cache
  569. Gen.GetCache().HeaderP->Dirty = false;
  570. if (SCacheF.Write(Map.Data(),Map.Size()) == false)
  571. return _error->Error("IO Error saving source cache");
  572. Gen.GetCache().HeaderP->Dirty = true;
  573. // Merge in the source caches
  574. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  575. }
  576. if (PkgOk == true)
  577. {
  578. Progress.OverallProgress(1,1,1,"Reading Package Lists");
  579. return true;
  580. }
  581. // We use the source cache to generate the package cache
  582. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  583. FileFd SCacheF(SCacheFile,FileFd::ReadOnly);
  584. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  585. DynamicMMap Map(CacheF,MMap::Public);
  586. if (_error->PendingError() == true)
  587. return false;
  588. // Preload the map with the source cache
  589. if (SCacheF.Read((unsigned char *)Map.Data() + Map.RawAllocate(SCacheF.Size()),
  590. SCacheF.Size()) == false)
  591. return false;
  592. pkgCacheGenerator Gen(Map,Progress);
  593. // Compute the progress
  594. unsigned long TotalSize = 0;
  595. if (pkgAddSourcesSize(TotalSize) == false)
  596. return false;
  597. unsigned long CurrentSize = 0;
  598. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  599. }
  600. /*}}}*/