pkgcachegen.cc 22 KB

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