pkgcachegen.cc 27 KB

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