pkgcachegen.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.41 1999/07/26 17:46:07 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. #include <errno.h>
  25. /*}}}*/
  26. // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* We set the diry flag and make sure that is written to the disk */
  29. pkgCacheGenerator::pkgCacheGenerator(DynamicMMap &Map,OpProgress &Prog) :
  30. Map(Map), Cache(Map), Progress(&Prog)
  31. {
  32. CurrentFile = 0;
  33. if (_error->PendingError() == true)
  34. return;
  35. if (Map.Size() == 0)
  36. {
  37. Map.RawAllocate(sizeof(pkgCache::Header));
  38. *Cache.HeaderP = pkgCache::Header();
  39. }
  40. Cache.HeaderP->Dirty = true;
  41. Map.Sync(0,sizeof(pkgCache::Header));
  42. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  43. memset(UniqHash,0,sizeof(UniqHash));
  44. }
  45. /*}}}*/
  46. // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
  47. // ---------------------------------------------------------------------
  48. /* We sync the data then unset the dirty flag in two steps so as to
  49. advoid a problem during a crash */
  50. pkgCacheGenerator::~pkgCacheGenerator()
  51. {
  52. if (_error->PendingError() == true)
  53. return;
  54. if (Map.Sync() == false)
  55. return;
  56. Cache.HeaderP->Dirty = false;
  57. Map.Sync(0,sizeof(pkgCache::Header));
  58. }
  59. /*}}}*/
  60. // CacheGenerator::MergeList - Merge the package list /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* This provides the generation of the entries in the cache. Each loop
  63. goes through a single package record from the underlying parse engine. */
  64. bool pkgCacheGenerator::MergeList(ListParser &List,
  65. pkgCache::VerIterator *OutVer)
  66. {
  67. List.Owner = this;
  68. unsigned int Counter = 0;
  69. while (List.Step() == true)
  70. {
  71. // Get a pointer to the package structure
  72. string PackageName = List.Package();
  73. if (PackageName.empty() == true)
  74. return false;
  75. pkgCache::PkgIterator Pkg;
  76. if (NewPackage(Pkg,PackageName) == false)
  77. return _error->Error("Error occured while processing %s (NewPackage)",PackageName.c_str());
  78. Counter++;
  79. if (Counter % 100 == 0 && Progress != 0)
  80. Progress->Progress(List.Offset());
  81. /* Get a pointer to the version structure. We know the list is sorted
  82. so we use that fact in the search. Insertion of new versions is
  83. done with correct sorting */
  84. string Version = List.Version();
  85. if (Version.empty() == true)
  86. {
  87. if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
  88. return _error->Error("Error occured while processing %s (UsePackage1)",PackageName.c_str());
  89. continue;
  90. }
  91. pkgCache::VerIterator Ver = Pkg.VersionList();
  92. map_ptrloc *Last = &Pkg->VersionList;
  93. int Res = 1;
  94. for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
  95. {
  96. Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
  97. Ver.VerStr() + strlen(Ver.VerStr()));
  98. if (Res >= 0)
  99. break;
  100. }
  101. /* We already have a version for this item, record that we
  102. saw it */
  103. unsigned long Hash = List.VersionHash();
  104. if (Res == 0 && Ver->Hash == Hash)
  105. {
  106. if (List.UsePackage(Pkg,Ver) == false)
  107. return _error->Error("Error occured while processing %s (UsePackage2)",PackageName.c_str());
  108. if (NewFileVer(Ver,List) == false)
  109. return _error->Error("Error occured while processing %s (NewFileVer1)",PackageName.c_str());
  110. // Read only a single record and return
  111. if (OutVer != 0)
  112. {
  113. *OutVer = Ver;
  114. return true;
  115. }
  116. continue;
  117. }
  118. // Skip to the end of the same version set.
  119. if (Res == 0)
  120. {
  121. for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
  122. {
  123. Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
  124. Ver.VerStr() + strlen(Ver.VerStr()));
  125. if (Res != 0)
  126. break;
  127. }
  128. }
  129. // Add a new version
  130. *Last = NewVersion(Ver,Version,*Last);
  131. Ver->ParentPkg = Pkg.Index();
  132. Ver->Hash = Hash;
  133. if (List.NewVersion(Ver) == false)
  134. return _error->Error("Error occured while processing %s (NewVersion1)",PackageName.c_str());
  135. if (List.UsePackage(Pkg,Ver) == false)
  136. return _error->Error("Error occured while processing %s (UsePackage3)",PackageName.c_str());
  137. if (NewFileVer(Ver,List) == false)
  138. return _error->Error("Error occured while processing %s (NewVersion2)",PackageName.c_str());
  139. // Read only a single record and return
  140. if (OutVer != 0)
  141. {
  142. *OutVer = Ver;
  143. return true;
  144. }
  145. }
  146. return true;
  147. }
  148. /*}}}*/
  149. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* This creates a new package structure and adds it to the hash table */
  152. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
  153. {
  154. Pkg = Cache.FindPkg(Name);
  155. if (Pkg.end() == false)
  156. return true;
  157. // Get a structure
  158. unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
  159. if (Package == 0)
  160. return false;
  161. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  162. // Insert it into the hash table
  163. unsigned long Hash = Cache.Hash(Name);
  164. Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
  165. Cache.HeaderP->HashTable[Hash] = Package;
  166. // Set the name and the ID
  167. Pkg->Name = Map.WriteString(Name);
  168. if (Pkg->Name == 0)
  169. return false;
  170. Pkg->ID = Cache.HeaderP->PackageCount++;
  171. return true;
  172. }
  173. /*}}}*/
  174. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  175. // ---------------------------------------------------------------------
  176. /* */
  177. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  178. ListParser &List)
  179. {
  180. if (CurrentFile == 0)
  181. return true;
  182. // Get a structure
  183. unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
  184. if (VerFile == 0)
  185. return 0;
  186. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  187. VF->File = CurrentFile - Cache.PkgFileP;
  188. // Link it to the end of the list
  189. map_ptrloc *Last = &Ver->FileList;
  190. for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; V++)
  191. Last = &V->NextFile;
  192. VF->NextFile = *Last;
  193. *Last = VF.Index();
  194. VF->Offset = List.Offset();
  195. VF->Size = List.Size();
  196. if (Cache.HeaderP->MaxVerFileSize < VF->Size)
  197. Cache.HeaderP->MaxVerFileSize = VF->Size;
  198. Cache.HeaderP->VerFileCount++;
  199. return true;
  200. }
  201. /*}}}*/
  202. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  203. // ---------------------------------------------------------------------
  204. /* This puts a version structure in the linked list */
  205. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  206. string VerStr,
  207. unsigned long Next)
  208. {
  209. // Get a structure
  210. unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
  211. if (Version == 0)
  212. return 0;
  213. // Fill it in
  214. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  215. Ver->NextVer = Next;
  216. Ver->ID = Cache.HeaderP->VersionCount++;
  217. Ver->VerStr = Map.WriteString(VerStr);
  218. if (Ver->VerStr == 0)
  219. return 0;
  220. return Version;
  221. }
  222. /*}}}*/
  223. // ListParser::NewDepends - Create a dependency element /*{{{*/
  224. // ---------------------------------------------------------------------
  225. /* This creates a dependency element in the tree. It is linked to the
  226. version and to the package that it is pointing to. */
  227. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
  228. string PackageName,
  229. string Version,
  230. unsigned int Op,
  231. unsigned int Type)
  232. {
  233. pkgCache &Cache = Owner->Cache;
  234. // Get a structure
  235. unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
  236. if (Dependency == 0)
  237. return false;
  238. // Fill it in
  239. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  240. Dep->ParentVer = Ver.Index();
  241. Dep->Type = Type;
  242. Dep->CompareOp = Op;
  243. Dep->ID = Cache.HeaderP->DependsCount++;
  244. // Locate the target package
  245. pkgCache::PkgIterator Pkg;
  246. if (Owner->NewPackage(Pkg,PackageName) == false)
  247. return false;
  248. // Probe the reverse dependency list for a version string that matches
  249. if (Version.empty() == false)
  250. {
  251. /* for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++, Hit++)
  252. if (I->Version != 0 && I.TargetVer() == Version)
  253. Dep->Version = I->Version;*/
  254. if (Dep->Version == 0)
  255. if ((Dep->Version = WriteString(Version)) == 0)
  256. return false;
  257. }
  258. // Link it to the package
  259. Dep->Package = Pkg.Index();
  260. Dep->NextRevDepends = Pkg->RevDepends;
  261. Pkg->RevDepends = Dep.Index();
  262. /* Link it to the version (at the end of the list)
  263. Caching the old end point speeds up generation substantially */
  264. if (OldDepVer != Ver)
  265. {
  266. OldDepLast = &Ver->DependsList;
  267. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  268. OldDepLast = &D->NextDepends;
  269. OldDepVer = Ver;
  270. }
  271. Dep->NextDepends = *OldDepLast;
  272. *OldDepLast = Dep.Index();
  273. OldDepLast = &Dep->NextDepends;
  274. return true;
  275. }
  276. /*}}}*/
  277. // ListParser::NewProvides - Create a Provides element /*{{{*/
  278. // ---------------------------------------------------------------------
  279. /* */
  280. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
  281. string PackageName,
  282. string Version)
  283. {
  284. pkgCache &Cache = Owner->Cache;
  285. // We do not add self referencing provides
  286. if (Ver.ParentPkg().Name() == PackageName)
  287. return true;
  288. // Get a structure
  289. unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
  290. if (Provides == 0)
  291. return false;
  292. Cache.HeaderP->ProvidesCount++;
  293. // Fill it in
  294. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  295. Prv->Version = Ver.Index();
  296. Prv->NextPkgProv = Ver->ProvidesList;
  297. Ver->ProvidesList = Prv.Index();
  298. if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
  299. return false;
  300. // Locate the target package
  301. pkgCache::PkgIterator Pkg;
  302. if (Owner->NewPackage(Pkg,PackageName) == false)
  303. return false;
  304. // Link it to the package
  305. Prv->ParentPkg = Pkg.Index();
  306. Prv->NextProvides = Pkg->ProvidesList;
  307. Pkg->ProvidesList = Prv.Index();
  308. return true;
  309. }
  310. /*}}}*/
  311. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  312. // ---------------------------------------------------------------------
  313. /* This is used to select which file is to be associated with all newly
  314. added versions. */
  315. bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
  316. {
  317. struct stat Buf;
  318. if (stat(File.c_str(),&Buf) == -1)
  319. return _error->Errno("stat","Couldn't stat ",File.c_str());
  320. // Get some space for the structure
  321. CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
  322. if (CurrentFile == Cache.PkgFileP)
  323. return false;
  324. // Fill it in
  325. CurrentFile->FileName = Map.WriteString(File);
  326. CurrentFile->Size = Buf.st_size;
  327. CurrentFile->mtime = Buf.st_mtime;
  328. CurrentFile->NextFile = Cache.HeaderP->FileList;
  329. CurrentFile->Flags = Flags;
  330. CurrentFile->ID = Cache.HeaderP->PackageFileCount;
  331. PkgFileName = File;
  332. Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
  333. Cache.HeaderP->PackageFileCount++;
  334. if (CurrentFile->FileName == 0)
  335. return false;
  336. if (Progress != 0)
  337. Progress->SubProgress(Buf.st_size);
  338. return true;
  339. }
  340. /*}}}*/
  341. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  342. // ---------------------------------------------------------------------
  343. /* This is used to create handles to strings. Given the same text it
  344. always returns the same number */
  345. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  346. unsigned int Size)
  347. {
  348. /* We use a very small transient hash table here, this speeds up generation
  349. by a fair amount on slower machines */
  350. pkgCache::StringItem *&Bucket = UniqHash[(S[0]*5 + S[1]) % _count(UniqHash)];
  351. if (Bucket != 0 &&
  352. stringcmp(S,S+Size,Cache.StrP + Bucket->String) == 0)
  353. return Bucket->String;
  354. // Search for an insertion point
  355. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  356. int Res = 1;
  357. map_ptrloc *Last = &Cache.HeaderP->StringList;
  358. for (; I != Cache.StringItemP; Last = &I->NextItem,
  359. I = Cache.StringItemP + I->NextItem)
  360. {
  361. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  362. if (Res >= 0)
  363. break;
  364. }
  365. // Match
  366. if (Res == 0)
  367. {
  368. Bucket = I;
  369. return I->String;
  370. }
  371. // Get a structure
  372. unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
  373. if (Item == 0)
  374. return 0;
  375. // Fill in the structure
  376. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  377. ItemP->NextItem = I - Cache.StringItemP;
  378. *Last = Item;
  379. ItemP->String = Map.WriteString(S,Size);
  380. if (ItemP->String == 0)
  381. return 0;
  382. Bucket = ItemP;
  383. return ItemP->String;
  384. }
  385. /*}}}*/
  386. // SrcCacheCheck - Check if the source package cache is uptodate /*{{{*/
  387. // ---------------------------------------------------------------------
  388. /* The source cache is checked against the source list and the files
  389. on disk, any difference results in a false. */
  390. bool pkgSrcCacheCheck(pkgSourceList &List)
  391. {
  392. if (_error->PendingError() == true)
  393. return false;
  394. string CacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  395. string ListDir = _config->FindDir("Dir::State::lists");
  396. // Count the number of missing files
  397. int Missing = 0;
  398. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  399. {
  400. // Only cache deb source types.
  401. if (I->Type != pkgSourceList::Item::Deb)
  402. {
  403. Missing++;
  404. continue;
  405. }
  406. string File = ListDir + URItoFileName(I->PackagesURI());
  407. struct stat Buf;
  408. if (stat(File.c_str(),&Buf) != 0)
  409. {
  410. _error->WarningE("stat","Couldn't stat source package list '%s' (%s)",
  411. I->PackagesInfo().c_str(),File.c_str());
  412. Missing++;
  413. }
  414. }
  415. // Open the source package cache
  416. if (FileExists(CacheFile) == false)
  417. return false;
  418. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  419. if (_error->PendingError() == true)
  420. {
  421. _error->Discard();
  422. return false;
  423. }
  424. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  425. if (_error->PendingError() == true || Map.Size() == 0)
  426. {
  427. _error->Discard();
  428. return false;
  429. }
  430. pkgCache Cache(Map);
  431. if (_error->PendingError() == true)
  432. {
  433. _error->Discard();
  434. return false;
  435. }
  436. // They are certianly out of sync
  437. if (Cache.Head().PackageFileCount != List.size() - Missing)
  438. return false;
  439. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  440. {
  441. // Search for a match in the source list
  442. bool Bad = true;
  443. for (pkgSourceList::const_iterator I = List.begin();
  444. I != List.end(); I++)
  445. {
  446. // Only cache deb source types.
  447. if (I->Type != pkgSourceList::Item::Deb)
  448. continue;
  449. string File = ListDir + URItoFileName(I->PackagesURI());
  450. if (F.FileName() == File)
  451. {
  452. Bad = false;
  453. break;
  454. }
  455. }
  456. // Check if the file matches what was cached
  457. Bad |= !F.IsOk();
  458. if (Bad == true)
  459. return false;
  460. }
  461. return true;
  462. }
  463. /*}}}*/
  464. // PkgCacheCheck - Check if the package cache is uptodate /*{{{*/
  465. // ---------------------------------------------------------------------
  466. /* This does a simple check of all files used to compose the cache */
  467. bool pkgPkgCacheCheck(string CacheFile)
  468. {
  469. if (_error->PendingError() == true)
  470. return false;
  471. // Open the source package cache
  472. if (FileExists(CacheFile) == false)
  473. return false;
  474. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  475. if (_error->PendingError() == true)
  476. {
  477. _error->Discard();
  478. return false;
  479. }
  480. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  481. if (_error->PendingError() == true || Map.Size() == 0)
  482. {
  483. _error->Discard();
  484. return false;
  485. }
  486. pkgCache Cache(Map);
  487. if (_error->PendingError() == true)
  488. {
  489. _error->Discard();
  490. return false;
  491. }
  492. // Status files that must be in the cache
  493. string Status[3];
  494. Status[0] = _config->FindFile("Dir::State::xstatus");
  495. Status[1]= _config->FindFile("Dir::State::userstatus");
  496. Status[2] = _config->FindFile("Dir::State::status");
  497. // Cheack each file
  498. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  499. {
  500. if (F.IsOk() == false)
  501. return false;
  502. // See if this is one of the status files
  503. for (int I = 0; I != 3; I++)
  504. if (F.FileName() == Status[I])
  505. Status[I] = string();
  506. }
  507. // Make sure all the status files are loaded.
  508. for (int I = 0; I != 3; I++)
  509. {
  510. if (Status[I].empty() == false && FileExists(Status[I]) == true)
  511. return false;
  512. }
  513. return true;
  514. }
  515. /*}}}*/
  516. // AddStatusSize - Add the size of the status files /*{{{*/
  517. // ---------------------------------------------------------------------
  518. /* This adds the size of all the status files to the size counter */
  519. bool pkgAddStatusSize(unsigned long &TotalSize)
  520. {
  521. // Grab the file names
  522. string xstatus = _config->FindFile("Dir::State::xstatus");
  523. string userstatus = _config->FindFile("Dir::State::userstatus");
  524. string status = _config->FindFile("Dir::State::status");
  525. // Grab the sizes
  526. struct stat Buf;
  527. if (stat(xstatus.c_str(),&Buf) == 0)
  528. TotalSize += Buf.st_size;
  529. if (stat(userstatus.c_str(),&Buf) == 0)
  530. TotalSize += Buf.st_size;
  531. if (stat(status.c_str(),&Buf) != 0)
  532. return _error->Errno("stat","Couldn't stat the status file %s",status.c_str());
  533. TotalSize += Buf.st_size;
  534. return true;
  535. }
  536. /*}}}*/
  537. // MergeStatus - Add the status files to the cache /*{{{*/
  538. // ---------------------------------------------------------------------
  539. /* This adds the status files to the map */
  540. bool pkgMergeStatus(OpProgress &Progress,pkgCacheGenerator &Gen,
  541. unsigned long &CurrentSize,unsigned long TotalSize)
  542. {
  543. // Grab the file names
  544. string Status[3];
  545. Status[0] = _config->FindFile("Dir::State::xstatus");
  546. Status[1]= _config->FindFile("Dir::State::userstatus");
  547. Status[2] = _config->FindFile("Dir::State::status");
  548. for (int I = 0; I != 3; I++)
  549. {
  550. // Check if the file exists and it is not the primary status file.
  551. string File = Status[I];
  552. if (I != 2 && FileExists(File) == false)
  553. continue;
  554. FileFd Pkg(File,FileFd::ReadOnly);
  555. debListParser Parser(Pkg);
  556. Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
  557. if (_error->PendingError() == true)
  558. return _error->Error("Problem opening %s",File.c_str());
  559. CurrentSize += Pkg.Size();
  560. Progress.SubProgress(0,"Local Package State - " + flNotDir(File));
  561. if (Gen.SelectFile(File,pkgCache::Flag::NotSource) == false)
  562. return _error->Error("Problem with SelectFile %s",File.c_str());
  563. if (Gen.MergeList(Parser) == false)
  564. return _error->Error("Problem with MergeList %s",File.c_str());
  565. Progress.Progress(Pkg.Size());
  566. }
  567. return true;
  568. }
  569. /*}}}*/
  570. // GenerateSrcCache - Write the source package lists to the map /*{{{*/
  571. // ---------------------------------------------------------------------
  572. /* This puts the source package cache into the given generator. */
  573. bool pkgGenerateSrcCache(pkgSourceList &List,OpProgress &Progress,
  574. pkgCacheGenerator &Gen,
  575. unsigned long &CurrentSize,unsigned long &TotalSize)
  576. {
  577. string ListDir = _config->FindDir("Dir::State::lists");
  578. // Prepare the progress indicator
  579. TotalSize = 0;
  580. struct stat Buf;
  581. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  582. {
  583. string File = ListDir + URItoFileName(I->PackagesURI());
  584. if (stat(File.c_str(),&Buf) != 0)
  585. continue;
  586. TotalSize += Buf.st_size;
  587. }
  588. if (pkgAddStatusSize(TotalSize) == false)
  589. return false;
  590. // Generate the pkg source cache
  591. CurrentSize = 0;
  592. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
  593. {
  594. // Only cache deb source types.
  595. if (I->Type != pkgSourceList::Item::Deb)
  596. continue;
  597. string File = ListDir + URItoFileName(I->PackagesURI());
  598. if (FileExists(File) == false)
  599. continue;
  600. FileFd Pkg(File,FileFd::ReadOnly);
  601. debListParser Parser(Pkg);
  602. Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
  603. if (_error->PendingError() == true)
  604. return _error->Error("Problem opening %s",File.c_str());
  605. CurrentSize += Pkg.Size();
  606. Progress.SubProgress(0,I->PackagesInfo());
  607. if (Gen.SelectFile(File) == false)
  608. return _error->Error("Problem with SelectFile %s",File.c_str());
  609. if (Gen.MergeList(Parser) == false)
  610. return _error->Error("Problem with MergeList %s",File.c_str());
  611. // Check the release file
  612. string RFile = ListDir + URItoFileName(I->ReleaseURI());
  613. if (FileExists(RFile) == true)
  614. {
  615. FileFd Rel(RFile,FileFd::ReadOnly);
  616. if (_error->PendingError() == true)
  617. return false;
  618. Parser.LoadReleaseInfo(Gen.GetCurFile(),Rel);
  619. }
  620. }
  621. return true;
  622. }
  623. /*}}}*/
  624. // MakeStatusCache - Generates a cache that includes the status files /*{{{*/
  625. // ---------------------------------------------------------------------
  626. /* This copies the package source cache and then merges the status and
  627. xstatus files into it. */
  628. bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress)
  629. {
  630. Progress.OverallProgress(0,1,1,"Reading Package Lists");
  631. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  632. bool SrcOk = pkgSrcCacheCheck(List);
  633. bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile);
  634. // Rebuild the source and package caches
  635. if (SrcOk == false)
  636. {
  637. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  638. FileFd SCacheF(SCacheFile,FileFd::WriteEmpty);
  639. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  640. DynamicMMap Map(CacheF,MMap::Public);
  641. if (_error->PendingError() == true)
  642. return false;
  643. pkgCacheGenerator Gen(Map,Progress);
  644. unsigned long CurrentSize = 0;
  645. unsigned long TotalSize = 0;
  646. if (pkgGenerateSrcCache(List,Progress,Gen,CurrentSize,TotalSize) == false)
  647. return false;
  648. // Write the src cache
  649. Gen.GetCache().HeaderP->Dirty = false;
  650. if (SCacheF.Write(Map.Data(),Map.Size()) == false)
  651. return _error->Error("IO Error saving source cache");
  652. Gen.GetCache().HeaderP->Dirty = true;
  653. // Merge in the source caches
  654. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  655. }
  656. if (PkgOk == true)
  657. {
  658. Progress.OverallProgress(1,1,1,"Reading Package Lists");
  659. return true;
  660. }
  661. // We use the source cache to generate the package cache
  662. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  663. FileFd SCacheF(SCacheFile,FileFd::ReadOnly);
  664. FileFd CacheF(CacheFile,FileFd::WriteEmpty);
  665. DynamicMMap Map(CacheF,MMap::Public);
  666. if (_error->PendingError() == true)
  667. return false;
  668. // Preload the map with the source cache
  669. if (SCacheF.Read((unsigned char *)Map.Data() + Map.RawAllocate(SCacheF.Size()),
  670. SCacheF.Size()) == false)
  671. return false;
  672. pkgCacheGenerator Gen(Map,Progress);
  673. // Compute the progress
  674. unsigned long TotalSize = 0;
  675. if (pkgAddStatusSize(TotalSize) == false)
  676. return false;
  677. unsigned long CurrentSize = 0;
  678. return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
  679. }
  680. /*}}}*/
  681. // MakeStatusCacheMem - Returns a map for the status cache /*{{{*/
  682. // ---------------------------------------------------------------------
  683. /* This creates a map object for the status cache. If the process has write
  684. access to the caches then it is the same as MakeStatusCache, otherwise it
  685. creates a memory block and puts the cache in there. */
  686. MMap *pkgMakeStatusCacheMem(pkgSourceList &List,OpProgress &Progress)
  687. {
  688. /* If the cache file is writeable this is just a wrapper for
  689. MakeStatusCache */
  690. string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  691. bool Writeable = (access(CacheFile.c_str(),W_OK) == 0) ||
  692. (errno == ENOENT);
  693. if (Writeable == true)
  694. {
  695. if (pkgMakeStatusCache(List,Progress) == false)
  696. return 0;
  697. // Open the cache file
  698. FileFd File(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly);
  699. if (_error->PendingError() == true)
  700. return 0;
  701. MMap *Map = new MMap(File,MMap::Public | MMap::ReadOnly);
  702. if (_error->PendingError() == true)
  703. {
  704. delete Map;
  705. return 0;
  706. }
  707. return Map;
  708. }
  709. // Mostly from MakeStatusCache..
  710. Progress.OverallProgress(0,1,1,"Reading Package Lists");
  711. bool SrcOk = pkgSrcCacheCheck(List);
  712. bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile);
  713. // Rebuild the source and package caches
  714. if (SrcOk == false)
  715. {
  716. DynamicMMap *Map = new DynamicMMap(MMap::Public);
  717. if (_error->PendingError() == true)
  718. {
  719. delete Map;
  720. return 0;
  721. }
  722. pkgCacheGenerator Gen(*Map,Progress);
  723. unsigned long CurrentSize = 0;
  724. unsigned long TotalSize = 0;
  725. if (pkgGenerateSrcCache(List,Progress,Gen,CurrentSize,TotalSize) == false)
  726. {
  727. delete Map;
  728. return 0;
  729. }
  730. // Merge in the source caches
  731. if (pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize) == false)
  732. {
  733. delete Map;
  734. return 0;
  735. }
  736. return Map;
  737. }
  738. if (PkgOk == true)
  739. {
  740. Progress.OverallProgress(1,1,1,"Reading Package Lists");
  741. // Open the cache file
  742. FileFd File(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly);
  743. if (_error->PendingError() == true)
  744. return 0;
  745. MMap *Map = new MMap(File,MMap::Public | MMap::ReadOnly);
  746. if (_error->PendingError() == true)
  747. {
  748. delete Map;
  749. return 0;
  750. }
  751. return Map;
  752. }
  753. // We use the source cache to generate the package cache
  754. string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  755. FileFd SCacheF(SCacheFile,FileFd::ReadOnly);
  756. DynamicMMap *Map = new DynamicMMap(MMap::Public);
  757. if (_error->PendingError() == true)
  758. {
  759. delete Map;
  760. return 0;
  761. }
  762. // Preload the map with the source cache
  763. if (SCacheF.Read((unsigned char *)Map->Data() + Map->RawAllocate(SCacheF.Size()),
  764. SCacheF.Size()) == false)
  765. {
  766. delete Map;
  767. return 0;
  768. }
  769. pkgCacheGenerator Gen(*Map,Progress);
  770. // Compute the progress
  771. unsigned long TotalSize = 0;
  772. if (pkgAddStatusSize(TotalSize) == false)
  773. {
  774. delete Map;
  775. return 0;
  776. }
  777. unsigned long CurrentSize = 0;
  778. if (pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize) == false)
  779. {
  780. delete Map;
  781. return 0;
  782. }
  783. return Map;
  784. }
  785. /*}}}*/