pkgcachegen.cc 23 KB

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