pkgcachegen.cc 27 KB

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