pkgcachegen.cc 28 KB

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