pkgcachegen.cc 22 KB

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