pkgcachegen.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.20 1998/10/20 02:39: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. unsigned long *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. VF->NextFile = Ver->FileList;
  155. Ver->FileList = VF.Index();
  156. VF->Offset = List.Offset();
  157. VF->Size = List.Size();
  158. if (Cache.HeaderP->MaxVerFileSize < VF->Size)
  159. Cache.HeaderP->MaxVerFileSize = VF->Size;
  160. return true;
  161. }
  162. /*}}}*/
  163. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  164. // ---------------------------------------------------------------------
  165. /* This puts a version structure in the linked list */
  166. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  167. string VerStr,
  168. unsigned long Next)
  169. {
  170. // Get a structure
  171. unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
  172. if (Version == 0)
  173. return 0;
  174. // Fill it in
  175. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  176. Ver->NextVer = Next;
  177. Ver->ID = Cache.HeaderP->VersionCount++;
  178. Ver->VerStr = Map.WriteString(VerStr);
  179. if (Ver->VerStr == 0)
  180. return 0;
  181. return Version;
  182. }
  183. /*}}}*/
  184. // ListParser::NewDepends - Create a dependency element /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* This creates a dependency element in the tree. It is linked to the
  187. version and to the package that it is pointing to. */
  188. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
  189. string PackageName,
  190. string Version,
  191. unsigned int Op,
  192. unsigned int Type)
  193. {
  194. pkgCache &Cache = Owner->Cache;
  195. // Get a structure
  196. unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
  197. if (Dependency == 0)
  198. return false;
  199. // Fill it in
  200. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  201. Dep->ParentVer = Ver.Index();
  202. Dep->Type = Type;
  203. Dep->CompareOp = Op;
  204. Dep->ID = Cache.HeaderP->DependsCount++;
  205. // Locate the target package
  206. pkgCache::PkgIterator Pkg;
  207. if (Owner->NewPackage(Pkg,PackageName) == false)
  208. return false;
  209. // Probe the reverse dependency list for a version string that matches
  210. if (Version.empty() == false)
  211. {
  212. for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  213. if (I->Version != 0 && I.TargetVer() == Version)
  214. Dep->Version = I->Version;
  215. if (Dep->Version == 0)
  216. if ((Dep->Version = WriteString(Version)) == 0)
  217. return false;
  218. }
  219. // Link it to the package
  220. Dep->Package = Pkg.Index();
  221. Dep->NextRevDepends = Pkg->RevDepends;
  222. Pkg->RevDepends = Dep.Index();
  223. // Link it to the version (at the end of the list)
  224. unsigned long *Last = &Ver->DependsList;
  225. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  226. Last = &D->NextDepends;
  227. Dep->NextDepends = *Last;
  228. *Last = Dep.Index();
  229. return true;
  230. }
  231. /*}}}*/
  232. // ListParser::NewProvides - Create a Provides element /*{{{*/
  233. // ---------------------------------------------------------------------
  234. /* */
  235. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
  236. string PackageName,
  237. string Version)
  238. {
  239. pkgCache &Cache = Owner->Cache;
  240. // We do not add self referencing provides
  241. if (Ver.ParentPkg().Name() == PackageName)
  242. return true;
  243. // Get a structure
  244. unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
  245. if (Provides == 0)
  246. return false;
  247. // Fill it in
  248. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  249. Prv->Version = Ver.Index();
  250. Prv->NextPkgProv = Ver->ProvidesList;
  251. Ver->ProvidesList = Prv.Index();
  252. if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
  253. return false;
  254. // Locate the target package
  255. pkgCache::PkgIterator Pkg;
  256. if (Owner->NewPackage(Pkg,PackageName) == false)
  257. return false;
  258. // Link it to the package
  259. Prv->ParentPkg = Pkg.Index();
  260. Prv->NextProvides = Pkg->ProvidesList;
  261. Pkg->ProvidesList = Prv.Index();
  262. return true;
  263. }
  264. /*}}}*/
  265. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  266. // ---------------------------------------------------------------------
  267. /* This is used to select which file is to be associated with all newly
  268. added versions. */
  269. bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
  270. {
  271. struct stat Buf;
  272. if (stat(File.c_str(),&Buf) == -1)
  273. return _error->Errno("stat","Couldn't stat ",File.c_str());
  274. // Get some space for the structure
  275. CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
  276. if (CurrentFile == Cache.PkgFileP)
  277. return false;
  278. // Fill it in
  279. CurrentFile->FileName = Map.WriteString(File);
  280. CurrentFile->Size = Buf.st_size;
  281. CurrentFile->mtime = Buf.st_mtime;
  282. CurrentFile->NextFile = Cache.HeaderP->FileList;
  283. CurrentFile->Flags = Flags;
  284. CurrentFile->ID = Cache.HeaderP->PackageFileCount;
  285. PkgFileName = File;
  286. Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
  287. Cache.HeaderP->PackageFileCount++;
  288. if (CurrentFile->FileName == 0)
  289. return false;
  290. Progress.SubProgress(Buf.st_size);
  291. return true;
  292. }
  293. /*}}}*/
  294. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  295. // ---------------------------------------------------------------------
  296. /* This is used to create handles to strings. Given the same text it
  297. always returns the same number */
  298. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  299. unsigned int Size)
  300. {
  301. // Search for an insertion point
  302. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  303. int Res = 1;
  304. unsigned long *Last = &Cache.HeaderP->StringList;
  305. for (; I != Cache.StringItemP; Last = &I->NextItem,
  306. I = Cache.StringItemP + I->NextItem)
  307. {
  308. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  309. if (Res >= 0)
  310. break;
  311. }
  312. // Match
  313. if (Res == 0)
  314. return I->String;
  315. // Get a structure
  316. unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
  317. if (Item == 0)
  318. return 0;
  319. // Fill in the structure
  320. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  321. ItemP->NextItem = I - Cache.StringItemP;
  322. *Last = Item;
  323. ItemP->String = Map.WriteString(S,Size);
  324. if (ItemP->String == 0)
  325. return 0;
  326. return ItemP->String;
  327. }
  328. /*}}}*/
  329. // SrcCacheCheck - Check if the source package cache is uptodate /*{{{*/
  330. // ---------------------------------------------------------------------
  331. /* The source cache is checked against the source list and the files
  332. on disk, any difference results in a false. */
  333. bool pkgSrcCacheCheck(pkgSourceList &List)
  334. {
  335. if (_error->PendingError() == true)
  336. return false;
  337. string CacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  338. string ListDir = _config->FindFile("Dir::State::lists");
  339. // Count the number of missing files
  340. int Missing = 0;
  341. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  342. {
  343. string File = ListDir + URItoFileName(I->PackagesURI());
  344. struct stat Buf;
  345. if (stat(File.c_str(),&Buf) != 0)
  346. {
  347. _error->WarningE("stat","Couldn't stat source package list '%s' (%s)",
  348. I->PackagesInfo().c_str(),File.c_str());
  349. Missing++;
  350. }
  351. }
  352. // Open the source package cache
  353. if (FileExists(CacheFile) == false)
  354. return false;
  355. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  356. if (_error->PendingError() == true)
  357. {
  358. _error->Discard();
  359. return false;
  360. }
  361. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  362. if (_error->PendingError() == true || Map.Size() == 0)
  363. {
  364. _error->Discard();
  365. return false;
  366. }
  367. pkgCache Cache(Map);
  368. if (_error->PendingError() == true)
  369. {
  370. _error->Discard();
  371. return false;
  372. }
  373. // They are certianly out of sync
  374. if (Cache.Head().PackageFileCount != List.size() - Missing)
  375. return false;
  376. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  377. {
  378. // Search for a match in the source list
  379. bool Bad = true;
  380. for (pkgSourceList::const_iterator I = List.begin();
  381. I != List.end(); I++)
  382. {
  383. string File = ListDir + URItoFileName(I->PackagesURI());
  384. if (F.FileName() == File)
  385. {
  386. Bad = false;
  387. break;
  388. }
  389. }
  390. // Check if the file matches what was cached
  391. Bad |= !F.IsOk();
  392. if (Bad == true)
  393. return false;
  394. }
  395. return true;
  396. }
  397. /*}}}*/
  398. // PkgCacheCheck - Check if the package cache is uptodate /*{{{*/
  399. // ---------------------------------------------------------------------
  400. /* This does a simple check of all files used to compose the cache */
  401. bool pkgPkgCacheCheck(string CacheFile)
  402. {
  403. if (_error->PendingError() == true)
  404. return false;
  405. // Open the source package cache
  406. if (FileExists(CacheFile) == false)
  407. return false;
  408. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  409. if (_error->PendingError() == true)
  410. {
  411. _error->Discard();
  412. return false;
  413. }
  414. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  415. if (_error->PendingError() == true || Map.Size() == 0)
  416. {
  417. _error->Discard();
  418. return false;
  419. }
  420. pkgCache Cache(Map);
  421. if (_error->PendingError() == true)
  422. {
  423. _error->Discard();
  424. return false;
  425. }
  426. // Status files that must be in the cache
  427. string Status[3];
  428. Status[0] = _config->FindFile("Dir::State::xstatus");
  429. Status[1]= _config->FindFile("Dir::State::userstatus");
  430. Status[2] = _config->FindFile("Dir::State::status");
  431. // Cheack each file
  432. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  433. {
  434. if (F.IsOk() == false)
  435. return false;
  436. // See if this is one of the status files
  437. for (int I = 0; I != 3; I++)
  438. if (F.FileName() == Status[I])
  439. Status[I] = string();
  440. }
  441. // Make sure all the status files are loaded.
  442. for (int I = 0; I != 3; I++)
  443. {
  444. if (Status[I].empty() == false && FileExists(Status[I]) == true)
  445. return false;
  446. }
  447. return true;
  448. }
  449. /*}}}*/
  450. // AddSourcesSize - Add the size of the status files /*{{{*/
  451. // ---------------------------------------------------------------------
  452. /* This adds the size of all the status files to the size counter */
  453. static bool pkgAddSourcesSize(unsigned long &TotalSize)
  454. {
  455. // Grab the file names
  456. string xstatus = _config->FindFile("Dir::State::xstatus");
  457. string userstatus = _config->FindFile("Dir::State::userstatus");
  458. string status = _config->FindFile("Dir::State::status");
  459. // Grab the sizes
  460. struct stat Buf;
  461. if (stat(xstatus.c_str(),&Buf) == 0)
  462. TotalSize += Buf.st_size;
  463. if (stat(userstatus.c_str(),&Buf) == 0)
  464. TotalSize += Buf.st_size;
  465. if (stat(status.c_str(),&Buf) != 0)
  466. return _error->Errno("stat","Couldn't stat the status file %s",status.c_str());
  467. TotalSize += Buf.st_size;
  468. return true;
  469. }
  470. /*}}}*/
  471. // MergeStatus - Add the status files to the cache /*{{{*/
  472. // ---------------------------------------------------------------------
  473. /* This adds the status files to the map */
  474. static bool pkgMergeStatus(OpProgress &Progress,pkgCacheGenerator &Gen,
  475. unsigned long &CurrentSize,unsigned long TotalSize)
  476. {
  477. // Grab the file names
  478. string Status[3];
  479. Status[0] = _config->FindFile("Dir::State::xstatus");
  480. Status[1]= _config->FindFile("Dir::State::userstatus");
  481. Status[2] = _config->FindFile("Dir::State::status");
  482. for (int I = 0; I != 3; I++)
  483. {
  484. // Check if the file exists and it is not the primary status file.
  485. string File = Status[I];
  486. if (I != 2 && FileExists(File) == false)
  487. continue;
  488. FileFd Pkg(File,FileFd::ReadOnly);
  489. debListParser Parser(Pkg);
  490. Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
  491. if (_error->PendingError() == true)
  492. return _error->Error("Problem opening %s",File.c_str());
  493. CurrentSize += Pkg.Size();
  494. Progress.SubProgress(0,"Local Package State - " + flNotDir(File));
  495. if (Gen.SelectFile(File,pkgCache::Flag::NotSource) == false)
  496. return _error->Error("Problem with SelectFile %s",File.c_str());
  497. if (Gen.MergeList(Parser) == false)
  498. return _error->Error("Problem with MergeList %s",File.c_str());
  499. Progress.Progress(Pkg.Size());
  500. }
  501. return true;
  502. }
  503. /*}}}*/
  504. // MakeStatusCache - Generates a cache that includes the status files /*{{{*/
  505. // ---------------------------------------------------------------------
  506. /* This copies the package source cache and then merges the status and
  507. xstatus files into it. */
  508. bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress)
  509. {
  510. Progress.OverallProgress(0,1,1,"Reading Package Lists");
  511. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  512. bool SrcOk = pkgSrcCacheCheck(List);
  513. bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile);
  514. // Rebuild the source and package caches
  515. if (SrcOk == false)
  516. {
  517. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  518. string ListDir = _config->FindFile("Dir::State::lists");
  519. FileFd SCacheF(SCacheFile,FileFd::WriteEmpty);
  520. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  521. DynamicMMap Map(CacheF,MMap::Public);
  522. if (_error->PendingError() == true)
  523. return false;
  524. pkgCacheGenerator Gen(Map,Progress);
  525. // Prepare the progress indicator
  526. unsigned long TotalSize = 0;
  527. struct stat Buf;
  528. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  529. {
  530. string File = ListDir + URItoFileName(I->PackagesURI());
  531. if (stat(File.c_str(),&Buf) != 0)
  532. continue;
  533. TotalSize += Buf.st_size;
  534. }
  535. if (pkgAddSourcesSize(TotalSize) == false)
  536. return false;
  537. // Generate the pkg source cache
  538. unsigned long CurrentSize = 0;
  539. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  540. {
  541. string File = ListDir + URItoFileName(I->PackagesURI());
  542. if (stat(File.c_str(),&Buf) != 0)
  543. continue;
  544. FileFd Pkg(File,FileFd::ReadOnly);
  545. debListParser Parser(Pkg);
  546. Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
  547. if (_error->PendingError() == true)
  548. return _error->Error("Problem opening %s",File.c_str());
  549. CurrentSize += Pkg.Size();
  550. Progress.SubProgress(0,I->PackagesInfo());
  551. if (Gen.SelectFile(File) == false)
  552. return _error->Error("Problem with SelectFile %s",File.c_str());
  553. if (Gen.MergeList(Parser) == false)
  554. return _error->Error("Problem with MergeList %s",File.c_str());
  555. }
  556. // Write the src cache
  557. Gen.GetCache().HeaderP->Dirty = false;
  558. if (SCacheF.Write(Map.Data(),Map.Size()) == false)
  559. return _error->Error("IO Error saving source cache");
  560. Gen.GetCache().HeaderP->Dirty = true;
  561. // Merge in the source caches
  562. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  563. }
  564. if (PkgOk == true)
  565. {
  566. Progress.OverallProgress(1,1,1,"Reading Package Lists");
  567. return true;
  568. }
  569. // We use the source cache to generate the package cache
  570. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  571. FileFd SCacheF(SCacheFile,FileFd::ReadOnly);
  572. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  573. DynamicMMap Map(CacheF,MMap::Public);
  574. if (_error->PendingError() == true)
  575. return false;
  576. // Preload the map with the source cache
  577. if (SCacheF.Read((unsigned char *)Map.Data() + Map.RawAllocate(SCacheF.Size()),
  578. SCacheF.Size()) == false)
  579. return false;
  580. pkgCacheGenerator Gen(Map,Progress);
  581. // Compute the progress
  582. unsigned long TotalSize = 0;
  583. if (pkgAddSourcesSize(TotalSize) == false)
  584. return false;
  585. unsigned long CurrentSize = 0;
  586. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  587. }
  588. /*}}}*/