pkgcachegen.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.35 1999/04/18 06:36:36 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. // GenerateSrcCache - Write the source package lists to the map /*{{{*/
  540. // ---------------------------------------------------------------------
  541. /* This puts the source package cache into the given generator. */
  542. bool pkgGenerateSrcCache(pkgSourceList &List,OpProgress &Progress,
  543. pkgCacheGenerator &Gen,
  544. unsigned long &CurrentSize,unsigned long TotalSize)
  545. {
  546. string ListDir = _config->FindDir("Dir::State::lists");
  547. // Prepare the progress indicator
  548. TotalSize = 0;
  549. struct stat Buf;
  550. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  551. {
  552. string File = ListDir + URItoFileName(I->PackagesURI());
  553. if (stat(File.c_str(),&Buf) != 0)
  554. continue;
  555. TotalSize += Buf.st_size;
  556. }
  557. if (pkgAddSourcesSize(TotalSize) == false)
  558. return false;
  559. // Generate the pkg source cache
  560. CurrentSize = 0;
  561. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  562. {
  563. // Only cache deb source types.
  564. if (I->Type != pkgSourceList::Item::Deb)
  565. continue;
  566. string File = ListDir + URItoFileName(I->PackagesURI());
  567. if (FileExists(File) == false)
  568. continue;
  569. FileFd Pkg(File,FileFd::ReadOnly);
  570. debListParser Parser(Pkg);
  571. Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
  572. if (_error->PendingError() == true)
  573. return _error->Error("Problem opening %s",File.c_str());
  574. CurrentSize += Pkg.Size();
  575. Progress.SubProgress(0,I->PackagesInfo());
  576. if (Gen.SelectFile(File) == false)
  577. return _error->Error("Problem with SelectFile %s",File.c_str());
  578. if (Gen.MergeList(Parser) == false)
  579. return _error->Error("Problem with MergeList %s",File.c_str());
  580. // Check the release file
  581. string RFile = ListDir + URItoFileName(I->ReleaseURI());
  582. if (FileExists(RFile) == true)
  583. {
  584. FileFd Rel(RFile,FileFd::ReadOnly);
  585. if (_error->PendingError() == true)
  586. return false;
  587. Parser.LoadReleaseInfo(Gen.GetCurFile(),Rel);
  588. }
  589. }
  590. return true;
  591. }
  592. /*}}}*/
  593. // MakeStatusCache - Generates a cache that includes the status files /*{{{*/
  594. // ---------------------------------------------------------------------
  595. /* This copies the package source cache and then merges the status and
  596. xstatus files into it. */
  597. bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress)
  598. {
  599. Progress.OverallProgress(0,1,1,"Reading Package Lists");
  600. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  601. bool SrcOk = pkgSrcCacheCheck(List);
  602. bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile);
  603. // Rebuild the source and package caches
  604. if (SrcOk == false)
  605. {
  606. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  607. FileFd SCacheF(SCacheFile,FileFd::WriteEmpty);
  608. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  609. DynamicMMap Map(CacheF,MMap::Public);
  610. if (_error->PendingError() == true)
  611. return false;
  612. pkgCacheGenerator Gen(Map,Progress);
  613. unsigned long CurrentSize = 0;
  614. unsigned long TotalSize = 0;
  615. if (pkgGenerateSrcCache(List,Progress,Gen,CurrentSize,TotalSize) == false)
  616. return false;
  617. // Write the src cache
  618. Gen.GetCache().HeaderP->Dirty = false;
  619. if (SCacheF.Write(Map.Data(),Map.Size()) == false)
  620. return _error->Error("IO Error saving source cache");
  621. Gen.GetCache().HeaderP->Dirty = true;
  622. // Merge in the source caches
  623. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  624. }
  625. if (PkgOk == true)
  626. {
  627. Progress.OverallProgress(1,1,1,"Reading Package Lists");
  628. return true;
  629. }
  630. // We use the source cache to generate the package cache
  631. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  632. FileFd SCacheF(SCacheFile,FileFd::ReadOnly);
  633. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  634. DynamicMMap Map(CacheF,MMap::Public);
  635. if (_error->PendingError() == true)
  636. return false;
  637. // Preload the map with the source cache
  638. if (SCacheF.Read((unsigned char *)Map.Data() + Map.RawAllocate(SCacheF.Size()),
  639. SCacheF.Size()) == false)
  640. return false;
  641. pkgCacheGenerator Gen(Map,Progress);
  642. // Compute the progress
  643. unsigned long TotalSize = 0;
  644. if (pkgAddSourcesSize(TotalSize) == false)
  645. return false;
  646. unsigned long CurrentSize = 0;
  647. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  648. }
  649. /*}}}*/
  650. // MakeStatusCacheMem - Returns a map for the status cache /*{{{*/
  651. // ---------------------------------------------------------------------
  652. /* This creates a map object for the status cache. If the process has write
  653. access to the caches then it is the same as MakeStatusCache, otherwise it
  654. creates a memory block and puts the cache in there. */
  655. MMap *pkgMakeStatusCacheMem(pkgSourceList &List,OpProgress &Progress)
  656. {
  657. /* If the cache file is writeable this is just a wrapper for
  658. MakeStatusCache */
  659. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  660. bool Writeable = access(CacheFile.c_str(),W_OK) == 0;
  661. if (Writeable == true)
  662. {
  663. if (pkgMakeStatusCache(List,Progress) == false)
  664. return 0;
  665. // Open the cache file
  666. FileFd File(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly);
  667. if (_error->PendingError() == true)
  668. return 0;
  669. MMap *Map = new MMap(File,MMap::Public | MMap::ReadOnly);
  670. if (_error->PendingError() == true)
  671. {
  672. delete Map;
  673. return 0;
  674. }
  675. return Map;
  676. }
  677. // Mostly from MakeStatusCache..
  678. Progress.OverallProgress(0,1,1,"Reading Package Lists");
  679. bool SrcOk = pkgSrcCacheCheck(List);
  680. bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile);
  681. // Rebuild the source and package caches
  682. if (SrcOk == false)
  683. {
  684. DynamicMMap *Map = new DynamicMMap(MMap::Public);
  685. if (_error->PendingError() == true)
  686. {
  687. delete Map;
  688. return 0;
  689. }
  690. pkgCacheGenerator Gen(*Map,Progress);
  691. unsigned long CurrentSize = 0;
  692. unsigned long TotalSize = 0;
  693. if (pkgGenerateSrcCache(List,Progress,Gen,CurrentSize,TotalSize) == false)
  694. {
  695. delete Map;
  696. return 0;
  697. }
  698. // Merge in the source caches
  699. if (pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize) == false)
  700. {
  701. delete Map;
  702. return 0;
  703. }
  704. return Map;
  705. }
  706. if (PkgOk == true)
  707. {
  708. Progress.OverallProgress(1,1,1,"Reading Package Lists");
  709. // Open the cache file
  710. FileFd File(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly);
  711. if (_error->PendingError() == true)
  712. return 0;
  713. MMap *Map = new MMap(File,MMap::Public | MMap::ReadOnly);
  714. if (_error->PendingError() == true)
  715. {
  716. delete Map;
  717. return 0;
  718. }
  719. return Map;
  720. }
  721. // We use the source cache to generate the package cache
  722. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  723. FileFd SCacheF(SCacheFile,FileFd::ReadOnly);
  724. DynamicMMap *Map = new DynamicMMap(MMap::Public);
  725. if (_error->PendingError() == true)
  726. {
  727. delete Map;
  728. return 0;
  729. }
  730. // Preload the map with the source cache
  731. if (SCacheF.Read((unsigned char *)Map->Data() + Map->RawAllocate(SCacheF.Size()),
  732. SCacheF.Size()) == false)
  733. {
  734. delete Map;
  735. return 0;
  736. }
  737. pkgCacheGenerator Gen(*Map,Progress);
  738. // Compute the progress
  739. unsigned long TotalSize = 0;
  740. if (pkgAddSourcesSize(TotalSize) == false)
  741. {
  742. delete Map;
  743. return 0;
  744. }
  745. unsigned long CurrentSize = 0;
  746. if (pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize) == false)
  747. {
  748. delete Map;
  749. return 0;
  750. }
  751. return Map;
  752. }
  753. /*}}}*/