pkgcachegen.cc 22 KB

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