pkgcachegen.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.34 1999/04/04 01:33:09 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. {
  372. Missing++;
  373. continue;
  374. }
  375. string File = ListDir + URItoFileName(I->PackagesURI());
  376. struct stat Buf;
  377. if (stat(File.c_str(),&Buf) != 0)
  378. {
  379. _error->WarningE("stat","Couldn't stat source package list '%s' (%s)",
  380. I->PackagesInfo().c_str(),File.c_str());
  381. Missing++;
  382. }
  383. }
  384. // Open the source package cache
  385. if (FileExists(CacheFile) == false)
  386. return false;
  387. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  388. if (_error->PendingError() == true)
  389. {
  390. _error->Discard();
  391. return false;
  392. }
  393. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  394. if (_error->PendingError() == true || Map.Size() == 0)
  395. {
  396. _error->Discard();
  397. return false;
  398. }
  399. pkgCache Cache(Map);
  400. if (_error->PendingError() == true)
  401. {
  402. _error->Discard();
  403. return false;
  404. }
  405. // They are certianly out of sync
  406. if (Cache.Head().PackageFileCount != List.size() - Missing)
  407. return false;
  408. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  409. {
  410. // Search for a match in the source list
  411. bool Bad = true;
  412. for (pkgSourceList::const_iterator I = List.begin();
  413. I != List.end(); I++)
  414. {
  415. // Only cache deb source types.
  416. if (I->Type != pkgSourceList::Item::Deb)
  417. continue;
  418. string File = ListDir + URItoFileName(I->PackagesURI());
  419. if (F.FileName() == File)
  420. {
  421. Bad = false;
  422. break;
  423. }
  424. }
  425. // Check if the file matches what was cached
  426. Bad |= !F.IsOk();
  427. if (Bad == true)
  428. return false;
  429. }
  430. return true;
  431. }
  432. /*}}}*/
  433. // PkgCacheCheck - Check if the package cache is uptodate /*{{{*/
  434. // ---------------------------------------------------------------------
  435. /* This does a simple check of all files used to compose the cache */
  436. bool pkgPkgCacheCheck(string CacheFile)
  437. {
  438. if (_error->PendingError() == true)
  439. return false;
  440. // Open the source package cache
  441. if (FileExists(CacheFile) == false)
  442. return false;
  443. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  444. if (_error->PendingError() == true)
  445. {
  446. _error->Discard();
  447. return false;
  448. }
  449. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  450. if (_error->PendingError() == true || Map.Size() == 0)
  451. {
  452. _error->Discard();
  453. return false;
  454. }
  455. pkgCache Cache(Map);
  456. if (_error->PendingError() == true)
  457. {
  458. _error->Discard();
  459. return false;
  460. }
  461. // Status files that must be in the cache
  462. string Status[3];
  463. Status[0] = _config->FindFile("Dir::State::xstatus");
  464. Status[1]= _config->FindFile("Dir::State::userstatus");
  465. Status[2] = _config->FindFile("Dir::State::status");
  466. // Cheack each file
  467. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  468. {
  469. if (F.IsOk() == false)
  470. return false;
  471. // See if this is one of the status files
  472. for (int I = 0; I != 3; I++)
  473. if (F.FileName() == Status[I])
  474. Status[I] = string();
  475. }
  476. // Make sure all the status files are loaded.
  477. for (int I = 0; I != 3; I++)
  478. {
  479. if (Status[I].empty() == false && FileExists(Status[I]) == true)
  480. return false;
  481. }
  482. return true;
  483. }
  484. /*}}}*/
  485. // AddSourcesSize - Add the size of the status files /*{{{*/
  486. // ---------------------------------------------------------------------
  487. /* This adds the size of all the status files to the size counter */
  488. static bool pkgAddSourcesSize(unsigned long &TotalSize)
  489. {
  490. // Grab the file names
  491. string xstatus = _config->FindFile("Dir::State::xstatus");
  492. string userstatus = _config->FindFile("Dir::State::userstatus");
  493. string status = _config->FindFile("Dir::State::status");
  494. // Grab the sizes
  495. struct stat Buf;
  496. if (stat(xstatus.c_str(),&Buf) == 0)
  497. TotalSize += Buf.st_size;
  498. if (stat(userstatus.c_str(),&Buf) == 0)
  499. TotalSize += Buf.st_size;
  500. if (stat(status.c_str(),&Buf) != 0)
  501. return _error->Errno("stat","Couldn't stat the status file %s",status.c_str());
  502. TotalSize += Buf.st_size;
  503. return true;
  504. }
  505. /*}}}*/
  506. // MergeStatus - Add the status files to the cache /*{{{*/
  507. // ---------------------------------------------------------------------
  508. /* This adds the status files to the map */
  509. static bool pkgMergeStatus(OpProgress &Progress,pkgCacheGenerator &Gen,
  510. unsigned long &CurrentSize,unsigned long TotalSize)
  511. {
  512. // Grab the file names
  513. string Status[3];
  514. Status[0] = _config->FindFile("Dir::State::xstatus");
  515. Status[1]= _config->FindFile("Dir::State::userstatus");
  516. Status[2] = _config->FindFile("Dir::State::status");
  517. for (int I = 0; I != 3; I++)
  518. {
  519. // Check if the file exists and it is not the primary status file.
  520. string File = Status[I];
  521. if (I != 2 && FileExists(File) == false)
  522. continue;
  523. FileFd Pkg(File,FileFd::ReadOnly);
  524. debListParser Parser(Pkg);
  525. Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
  526. if (_error->PendingError() == true)
  527. return _error->Error("Problem opening %s",File.c_str());
  528. CurrentSize += Pkg.Size();
  529. Progress.SubProgress(0,"Local Package State - " + flNotDir(File));
  530. if (Gen.SelectFile(File,pkgCache::Flag::NotSource) == false)
  531. return _error->Error("Problem with SelectFile %s",File.c_str());
  532. if (Gen.MergeList(Parser) == false)
  533. return _error->Error("Problem with MergeList %s",File.c_str());
  534. Progress.Progress(Pkg.Size());
  535. }
  536. return true;
  537. }
  538. /*}}}*/
  539. // MakeStatusCache - Generates a cache that includes the status files /*{{{*/
  540. // ---------------------------------------------------------------------
  541. /* This copies the package source cache and then merges the status and
  542. xstatus files into it. */
  543. bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress)
  544. {
  545. Progress.OverallProgress(0,1,1,"Reading Package Lists");
  546. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  547. bool SrcOk = pkgSrcCacheCheck(List);
  548. bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile);
  549. // Rebuild the source and package caches
  550. if (SrcOk == false)
  551. {
  552. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  553. string ListDir = _config->FindDir("Dir::State::lists");
  554. FileFd SCacheF(SCacheFile,FileFd::WriteEmpty);
  555. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  556. DynamicMMap Map(CacheF,MMap::Public);
  557. if (_error->PendingError() == true)
  558. return false;
  559. pkgCacheGenerator Gen(Map,Progress);
  560. // Prepare the progress indicator
  561. unsigned long TotalSize = 0;
  562. struct stat Buf;
  563. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  564. {
  565. string File = ListDir + URItoFileName(I->PackagesURI());
  566. if (stat(File.c_str(),&Buf) != 0)
  567. continue;
  568. TotalSize += Buf.st_size;
  569. }
  570. if (pkgAddSourcesSize(TotalSize) == false)
  571. return false;
  572. // Generate the pkg source cache
  573. unsigned long CurrentSize = 0;
  574. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  575. {
  576. // Only cache deb source types.
  577. if (I->Type != pkgSourceList::Item::Deb)
  578. continue;
  579. string File = ListDir + URItoFileName(I->PackagesURI());
  580. if (FileExists(File) == false)
  581. continue;
  582. FileFd Pkg(File,FileFd::ReadOnly);
  583. debListParser Parser(Pkg);
  584. Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
  585. if (_error->PendingError() == true)
  586. return _error->Error("Problem opening %s",File.c_str());
  587. CurrentSize += Pkg.Size();
  588. Progress.SubProgress(0,I->PackagesInfo());
  589. if (Gen.SelectFile(File) == false)
  590. return _error->Error("Problem with SelectFile %s",File.c_str());
  591. if (Gen.MergeList(Parser) == false)
  592. return _error->Error("Problem with MergeList %s",File.c_str());
  593. // Check the release file
  594. string RFile = ListDir + URItoFileName(I->ReleaseURI());
  595. if (FileExists(RFile) == true)
  596. {
  597. FileFd Rel(RFile,FileFd::ReadOnly);
  598. if (_error->PendingError() == true)
  599. return false;
  600. Parser.LoadReleaseInfo(Gen.GetCurFile(),Rel);
  601. }
  602. }
  603. // Write the src cache
  604. Gen.GetCache().HeaderP->Dirty = false;
  605. if (SCacheF.Write(Map.Data(),Map.Size()) == false)
  606. return _error->Error("IO Error saving source cache");
  607. Gen.GetCache().HeaderP->Dirty = true;
  608. // Merge in the source caches
  609. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  610. }
  611. if (PkgOk == true)
  612. {
  613. Progress.OverallProgress(1,1,1,"Reading Package Lists");
  614. return true;
  615. }
  616. // We use the source cache to generate the package cache
  617. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  618. FileFd SCacheF(SCacheFile,FileFd::ReadOnly);
  619. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  620. DynamicMMap Map(CacheF,MMap::Public);
  621. if (_error->PendingError() == true)
  622. return false;
  623. // Preload the map with the source cache
  624. if (SCacheF.Read((unsigned char *)Map.Data() + Map.RawAllocate(SCacheF.Size()),
  625. SCacheF.Size()) == false)
  626. return false;
  627. pkgCacheGenerator Gen(Map,Progress);
  628. // Compute the progress
  629. unsigned long TotalSize = 0;
  630. if (pkgAddSourcesSize(TotalSize) == false)
  631. return false;
  632. unsigned long CurrentSize = 0;
  633. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  634. }
  635. /*}}}*/