You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1342 lines
45 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.53.2.1 2003/12/24 23:09:17 mdz 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. #define APT_COMPATIBILITY 986
  11. #include <apt-pkg/pkgcachegen.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/version.h>
  14. #include <apt-pkg/progress.h>
  15. #include <apt-pkg/sourcelist.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <apt-pkg/aptconfiguration.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/sptr.h>
  20. #include <apt-pkg/pkgsystem.h>
  21. #include <apt-pkg/macros.h>
  22. #include <apt-pkg/tagfile.h>
  23. #include <apti18n.h>
  24. #include <vector>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <stdio.h>
  29. /*}}}*/
  30. typedef vector<pkgIndexFile *>::iterator FileIterator;
  31. template <typename Iter> std::vector<Iter*> pkgCacheGenerator::Dynamic<Iter>::toReMap(6);
  32. // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* We set the dirty flag and make sure that is written to the disk */
  35. pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
  36. Map(*pMap), Cache(pMap,false), Progress(Prog),
  37. FoundFileDeps(0)
  38. {
  39. CurrentFile = 0;
  40. memset(UniqHash,0,sizeof(UniqHash));
  41. if (_error->PendingError() == true)
  42. return;
  43. if (Map.Size() == 0)
  44. {
  45. // Setup the map interface..
  46. Cache.HeaderP = (pkgCache::Header *)Map.Data();
  47. if (Map.RawAllocate(sizeof(pkgCache::Header)) == 0 && _error->PendingError() == true)
  48. return;
  49. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  50. // Starting header
  51. *Cache.HeaderP = pkgCache::Header();
  52. map_ptrloc const idxVerSysName = WriteStringInMap(_system->VS->Label);
  53. Cache.HeaderP->VerSysName = idxVerSysName;
  54. map_ptrloc const idxArchitecture = WriteStringInMap(_config->Find("APT::Architecture"));
  55. Cache.HeaderP->Architecture = idxArchitecture;
  56. if (unlikely(idxVerSysName == 0 || idxArchitecture == 0))
  57. return;
  58. Cache.ReMap();
  59. }
  60. else
  61. {
  62. // Map directly from the existing file
  63. Cache.ReMap();
  64. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  65. if (Cache.VS != _system->VS)
  66. {
  67. _error->Error(_("Cache has an incompatible versioning system"));
  68. return;
  69. }
  70. }
  71. Cache.HeaderP->Dirty = true;
  72. Map.Sync(0,sizeof(pkgCache::Header));
  73. }
  74. /*}}}*/
  75. // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
  76. // ---------------------------------------------------------------------
  77. /* We sync the data then unset the dirty flag in two steps so as to
  78. advoid a problem during a crash */
  79. pkgCacheGenerator::~pkgCacheGenerator()
  80. {
  81. if (_error->PendingError() == true)
  82. return;
  83. if (Map.Sync() == false)
  84. return;
  85. Cache.HeaderP->Dirty = false;
  86. Map.Sync(0,sizeof(pkgCache::Header));
  87. }
  88. /*}}}*/
  89. void pkgCacheGenerator::ReMap(void const * const oldMap, void const * const newMap) {/*{{{*/
  90. if (oldMap == newMap)
  91. return;
  92. Cache.ReMap(false);
  93. CurrentFile += (pkgCache::PackageFile*) newMap - (pkgCache::PackageFile*) oldMap;
  94. for (size_t i = 0; i < _count(UniqHash); ++i)
  95. if (UniqHash[i] != 0)
  96. UniqHash[i] += (pkgCache::StringItem*) newMap - (pkgCache::StringItem*) oldMap;
  97. for (std::vector<pkgCache::GrpIterator*>::const_iterator i = Dynamic<pkgCache::GrpIterator>::toReMap.begin();
  98. i != Dynamic<pkgCache::GrpIterator>::toReMap.end(); ++i)
  99. (*i)->ReOwn(Cache, oldMap, newMap);
  100. for (std::vector<pkgCache::PkgIterator*>::const_iterator i = Dynamic<pkgCache::PkgIterator>::toReMap.begin();
  101. i != Dynamic<pkgCache::PkgIterator>::toReMap.end(); ++i)
  102. (*i)->ReOwn(Cache, oldMap, newMap);
  103. for (std::vector<pkgCache::VerIterator*>::const_iterator i = Dynamic<pkgCache::VerIterator>::toReMap.begin();
  104. i != Dynamic<pkgCache::VerIterator>::toReMap.end(); ++i)
  105. (*i)->ReOwn(Cache, oldMap, newMap);
  106. for (std::vector<pkgCache::DepIterator*>::const_iterator i = Dynamic<pkgCache::DepIterator>::toReMap.begin();
  107. i != Dynamic<pkgCache::DepIterator>::toReMap.end(); ++i)
  108. (*i)->ReOwn(Cache, oldMap, newMap);
  109. for (std::vector<pkgCache::DescIterator*>::const_iterator i = Dynamic<pkgCache::DescIterator>::toReMap.begin();
  110. i != Dynamic<pkgCache::DescIterator>::toReMap.end(); ++i)
  111. (*i)->ReOwn(Cache, oldMap, newMap);
  112. for (std::vector<pkgCache::PrvIterator*>::const_iterator i = Dynamic<pkgCache::PrvIterator>::toReMap.begin();
  113. i != Dynamic<pkgCache::PrvIterator>::toReMap.end(); ++i)
  114. (*i)->ReOwn(Cache, oldMap, newMap);
  115. for (std::vector<pkgCache::PkgFileIterator*>::const_iterator i = Dynamic<pkgCache::PkgFileIterator>::toReMap.begin();
  116. i != Dynamic<pkgCache::PkgFileIterator>::toReMap.end(); ++i)
  117. (*i)->ReOwn(Cache, oldMap, newMap);
  118. } /*}}}*/
  119. // CacheGenerator::WriteStringInMap /*{{{*/
  120. map_ptrloc pkgCacheGenerator::WriteStringInMap(const char *String,
  121. const unsigned long &Len) {
  122. void const * const oldMap = Map.Data();
  123. map_ptrloc const index = Map.WriteString(String, Len);
  124. if (index != 0)
  125. ReMap(oldMap, Map.Data());
  126. return index;
  127. }
  128. /*}}}*/
  129. // CacheGenerator::WriteStringInMap /*{{{*/
  130. map_ptrloc pkgCacheGenerator::WriteStringInMap(const char *String) {
  131. void const * const oldMap = Map.Data();
  132. map_ptrloc const index = Map.WriteString(String);
  133. if (index != 0)
  134. ReMap(oldMap, Map.Data());
  135. return index;
  136. }
  137. /*}}}*/
  138. map_ptrloc pkgCacheGenerator::AllocateInMap(const unsigned long &size) {/*{{{*/
  139. void const * const oldMap = Map.Data();
  140. map_ptrloc const index = Map.Allocate(size);
  141. if (index != 0)
  142. ReMap(oldMap, Map.Data());
  143. return index;
  144. }
  145. /*}}}*/
  146. // CacheGenerator::MergeList - Merge the package list /*{{{*/
  147. // ---------------------------------------------------------------------
  148. /* This provides the generation of the entries in the cache. Each loop
  149. goes through a single package record from the underlying parse engine. */
  150. bool pkgCacheGenerator::MergeList(ListParser &List,
  151. pkgCache::VerIterator *OutVer)
  152. {
  153. List.Owner = this;
  154. unsigned int Counter = 0;
  155. while (List.Step() == true)
  156. {
  157. string const PackageName = List.Package();
  158. if (PackageName.empty() == true)
  159. return false;
  160. /* As we handle Arch all packages as architecture bounded
  161. we add all information to every (simulated) arch package */
  162. std::vector<string> genArch;
  163. if (List.ArchitectureAll() == true) {
  164. genArch = APT::Configuration::getArchitectures();
  165. if (genArch.size() != 1)
  166. genArch.push_back("all");
  167. } else
  168. genArch.push_back(List.Architecture());
  169. for (std::vector<string>::const_iterator arch = genArch.begin();
  170. arch != genArch.end(); ++arch)
  171. {
  172. // Get a pointer to the package structure
  173. pkgCache::PkgIterator Pkg;
  174. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  175. if (NewPackage(Pkg, PackageName, *arch) == false)
  176. return _error->Error(_("Error occurred while processing %s (NewPackage)"),PackageName.c_str());
  177. Counter++;
  178. if (Counter % 100 == 0 && Progress != 0)
  179. Progress->Progress(List.Offset());
  180. /* Get a pointer to the version structure. We know the list is sorted
  181. so we use that fact in the search. Insertion of new versions is
  182. done with correct sorting */
  183. string Version = List.Version();
  184. if (Version.empty() == true)
  185. {
  186. // we first process the package, then the descriptions
  187. // (this has the bonus that we get MMap error when we run out
  188. // of MMap space)
  189. pkgCache::VerIterator Ver(Cache);
  190. Dynamic<pkgCache::VerIterator> DynVer(Ver);
  191. if (List.UsePackage(Pkg, Ver) == false)
  192. return _error->Error(_("Error occurred while processing %s (UsePackage1)"),
  193. PackageName.c_str());
  194. // Find the right version to write the description
  195. MD5SumValue CurMd5 = List.Description_md5();
  196. Ver = Pkg.VersionList();
  197. for (; Ver.end() == false; ++Ver)
  198. {
  199. pkgCache::DescIterator Desc = Ver.DescriptionList();
  200. Dynamic<pkgCache::DescIterator> DynDesc(Desc);
  201. map_ptrloc *LastDesc = &Ver->DescriptionList;
  202. bool duplicate=false;
  203. // don't add a new description if we have one for the given
  204. // md5 && language
  205. for ( ; Desc.end() == false; Desc++)
  206. if (MD5SumValue(Desc.md5()) == CurMd5 &&
  207. Desc.LanguageCode() == List.DescriptionLanguage())
  208. duplicate=true;
  209. if(duplicate)
  210. continue;
  211. for (Desc = Ver.DescriptionList();
  212. Desc.end() == false;
  213. LastDesc = &Desc->NextDesc, Desc++)
  214. {
  215. if (MD5SumValue(Desc.md5()) == CurMd5)
  216. {
  217. // Add new description
  218. void const * const oldMap = Map.Data();
  219. map_ptrloc const descindex = NewDescription(Desc, List.DescriptionLanguage(), CurMd5, *LastDesc);
  220. if (oldMap != Map.Data())
  221. LastDesc += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  222. *LastDesc = descindex;
  223. Desc->ParentPkg = Pkg.Index();
  224. if ((*LastDesc == 0 && _error->PendingError()) || NewFileDesc(Desc,List) == false)
  225. return _error->Error(_("Error occurred while processing %s (NewFileDesc1)"),PackageName.c_str());
  226. break;
  227. }
  228. }
  229. }
  230. continue;
  231. }
  232. pkgCache::VerIterator Ver = Pkg.VersionList();
  233. Dynamic<pkgCache::VerIterator> DynVer(Ver);
  234. map_ptrloc *LastVer = &Pkg->VersionList;
  235. void const * oldMap = Map.Data();
  236. int Res = 1;
  237. unsigned long const Hash = List.VersionHash();
  238. for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
  239. {
  240. Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
  241. // Version is higher as current version - insert here
  242. if (Res > 0)
  243. break;
  244. // Versionstrings are equal - is hash also equal?
  245. if (Res == 0 && Ver->Hash == Hash)
  246. break;
  247. // proceed with the next till we have either the right
  248. // or we found another version (which will be lower)
  249. }
  250. /* We already have a version for this item, record that we saw it */
  251. if (Res == 0 && Ver.end() == false && Ver->Hash == Hash)
  252. {
  253. if (List.UsePackage(Pkg,Ver) == false)
  254. return _error->Error(_("Error occurred while processing %s (UsePackage2)"),
  255. PackageName.c_str());
  256. if (NewFileVer(Ver,List) == false)
  257. return _error->Error(_("Error occurred while processing %s (NewFileVer1)"),
  258. PackageName.c_str());
  259. // Read only a single record and return
  260. if (OutVer != 0)
  261. {
  262. *OutVer = Ver;
  263. FoundFileDeps |= List.HasFileDeps();
  264. return true;
  265. }
  266. continue;
  267. }
  268. // Add a new version
  269. map_ptrloc const verindex = NewVersion(Ver,Version,*LastVer);
  270. if (verindex == 0 && _error->PendingError())
  271. return _error->Error(_("Error occurred while processing %s (NewVersion%d)"),
  272. PackageName.c_str(), 1);
  273. if (oldMap != Map.Data())
  274. LastVer += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  275. *LastVer = verindex;
  276. Ver->ParentPkg = Pkg.Index();
  277. Ver->Hash = Hash;
  278. if (List.NewVersion(Ver) == false)
  279. return _error->Error(_("Error occurred while processing %s (NewVersion%d)"),
  280. PackageName.c_str(), 2);
  281. if (List.UsePackage(Pkg,Ver) == false)
  282. return _error->Error(_("Error occurred while processing %s (UsePackage3)"),
  283. PackageName.c_str());
  284. if (NewFileVer(Ver,List) == false)
  285. return _error->Error(_("Error occurred while processing %s (NewVersion%d)"),
  286. PackageName.c_str(), 3);
  287. // Read only a single record and return
  288. if (OutVer != 0)
  289. {
  290. *OutVer = Ver;
  291. FoundFileDeps |= List.HasFileDeps();
  292. return true;
  293. }
  294. /* Record the Description data. Description data always exist in
  295. Packages and Translation-* files. */
  296. pkgCache::DescIterator Desc = Ver.DescriptionList();
  297. Dynamic<pkgCache::DescIterator> DynDesc(Desc);
  298. map_ptrloc *LastDesc = &Ver->DescriptionList;
  299. // Skip to the end of description set
  300. for (; Desc.end() == false; LastDesc = &Desc->NextDesc, Desc++);
  301. // Add new description
  302. oldMap = Map.Data();
  303. map_ptrloc const descindex = NewDescription(Desc, List.DescriptionLanguage(), List.Description_md5(), *LastDesc);
  304. if (oldMap != Map.Data())
  305. LastDesc += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  306. *LastDesc = descindex;
  307. Desc->ParentPkg = Pkg.Index();
  308. if ((*LastDesc == 0 && _error->PendingError()) || NewFileDesc(Desc,List) == false)
  309. return _error->Error(_("Error occurred while processing %s (NewFileDesc2)"),PackageName.c_str());
  310. }
  311. }
  312. FoundFileDeps |= List.HasFileDeps();
  313. if (Cache.HeaderP->PackageCount >= (1ULL<<sizeof(Cache.PkgP->ID)*8)-1)
  314. return _error->Error(_("Wow, you exceeded the number of package "
  315. "names this APT is capable of."));
  316. if (Cache.HeaderP->VersionCount >= (1ULL<<(sizeof(Cache.VerP->ID)*8))-1)
  317. return _error->Error(_("Wow, you exceeded the number of versions "
  318. "this APT is capable of."));
  319. if (Cache.HeaderP->DescriptionCount >= (1ULL<<(sizeof(Cache.DescP->ID)*8))-1)
  320. return _error->Error(_("Wow, you exceeded the number of descriptions "
  321. "this APT is capable of."));
  322. if (Cache.HeaderP->DependsCount >= (1ULL<<(sizeof(Cache.DepP->ID)*8))-1ULL)
  323. return _error->Error(_("Wow, you exceeded the number of dependencies "
  324. "this APT is capable of."));
  325. return true;
  326. }
  327. /*}}}*/
  328. // CacheGenerator::MergeFileProvides - Merge file provides /*{{{*/
  329. // ---------------------------------------------------------------------
  330. /* If we found any file depends while parsing the main list we need to
  331. resolve them. Since it is undesired to load the entire list of files
  332. into the cache as virtual packages we do a two stage effort. MergeList
  333. identifies the file depends and this creates Provdies for them by
  334. re-parsing all the indexs. */
  335. bool pkgCacheGenerator::MergeFileProvides(ListParser &List)
  336. {
  337. List.Owner = this;
  338. unsigned int Counter = 0;
  339. while (List.Step() == true)
  340. {
  341. string PackageName = List.Package();
  342. if (PackageName.empty() == true)
  343. return false;
  344. string Version = List.Version();
  345. if (Version.empty() == true)
  346. continue;
  347. pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
  348. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  349. if (Pkg.end() == true)
  350. return _error->Error(_("Error occurred while processing %s (FindPkg)"),
  351. PackageName.c_str());
  352. Counter++;
  353. if (Counter % 100 == 0 && Progress != 0)
  354. Progress->Progress(List.Offset());
  355. unsigned long Hash = List.VersionHash();
  356. pkgCache::VerIterator Ver = Pkg.VersionList();
  357. Dynamic<pkgCache::VerIterator> DynVer(Ver);
  358. for (; Ver.end() == false; Ver++)
  359. {
  360. if (Ver->Hash == Hash && Version.c_str() == Ver.VerStr())
  361. {
  362. if (List.CollectFileProvides(Cache,Ver) == false)
  363. return _error->Error(_("Error occurred while processing %s (CollectFileProvides)"),PackageName.c_str());
  364. break;
  365. }
  366. }
  367. if (Ver.end() == true)
  368. _error->Warning(_("Package %s %s was not found while processing file dependencies"),PackageName.c_str(),Version.c_str());
  369. }
  370. return true;
  371. }
  372. /*}}}*/
  373. // CacheGenerator::NewGroup - Add a new group /*{{{*/
  374. // ---------------------------------------------------------------------
  375. /* This creates a new group structure and adds it to the hash table */
  376. bool pkgCacheGenerator::NewGroup(pkgCache::GrpIterator &Grp, const string &Name)
  377. {
  378. Grp = Cache.FindGrp(Name);
  379. if (Grp.end() == false)
  380. return true;
  381. // Get a structure
  382. map_ptrloc const Group = AllocateInMap(sizeof(pkgCache::Group));
  383. if (unlikely(Group == 0))
  384. return false;
  385. Grp = pkgCache::GrpIterator(Cache, Cache.GrpP + Group);
  386. map_ptrloc const idxName = WriteStringInMap(Name);
  387. if (unlikely(idxName == 0))
  388. return false;
  389. Grp->Name = idxName;
  390. // Insert it into the hash table
  391. unsigned long const Hash = Cache.Hash(Name);
  392. Grp->Next = Cache.HeaderP->GrpHashTable[Hash];
  393. Cache.HeaderP->GrpHashTable[Hash] = Group;
  394. Grp->ID = Cache.HeaderP->GroupCount++;
  395. return true;
  396. }
  397. /*}}}*/
  398. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  399. // ---------------------------------------------------------------------
  400. /* This creates a new package structure and adds it to the hash table */
  401. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name,
  402. const string &Arch) {
  403. pkgCache::GrpIterator Grp;
  404. Dynamic<pkgCache::GrpIterator> DynGrp(Grp);
  405. if (unlikely(NewGroup(Grp, Name) == false))
  406. return false;
  407. Pkg = Grp.FindPkg(Arch);
  408. if (Pkg.end() == false)
  409. return true;
  410. // Get a structure
  411. map_ptrloc const Package = AllocateInMap(sizeof(pkgCache::Package));
  412. if (unlikely(Package == 0))
  413. return false;
  414. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  415. // Insert the package into our package list
  416. if (Grp->FirstPackage == 0) // the group is new
  417. {
  418. // Insert it into the hash table
  419. unsigned long const Hash = Cache.Hash(Name);
  420. Pkg->NextPackage = Cache.HeaderP->PkgHashTable[Hash];
  421. Cache.HeaderP->PkgHashTable[Hash] = Package;
  422. Grp->FirstPackage = Package;
  423. }
  424. else // Group the Packages together
  425. {
  426. // this package is the new last package
  427. pkgCache::PkgIterator LastPkg(Cache, Cache.PkgP + Grp->LastPackage);
  428. Pkg->NextPackage = LastPkg->NextPackage;
  429. LastPkg->NextPackage = Package;
  430. }
  431. Grp->LastPackage = Package;
  432. // Set the name, arch and the ID
  433. Pkg->Name = Grp->Name;
  434. Pkg->Group = Grp.Index();
  435. map_ptrloc const idxArch = WriteUniqString(Arch.c_str());
  436. if (unlikely(idxArch == 0))
  437. return false;
  438. Pkg->Arch = idxArch;
  439. Pkg->ID = Cache.HeaderP->PackageCount++;
  440. return true;
  441. }
  442. /*}}}*/
  443. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  444. // ---------------------------------------------------------------------
  445. /* */
  446. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  447. ListParser &List)
  448. {
  449. if (CurrentFile == 0)
  450. return true;
  451. // Get a structure
  452. map_ptrloc const VerFile = AllocateInMap(sizeof(pkgCache::VerFile));
  453. if (VerFile == 0)
  454. return 0;
  455. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  456. VF->File = CurrentFile - Cache.PkgFileP;
  457. // Link it to the end of the list
  458. map_ptrloc *Last = &Ver->FileList;
  459. for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; V++)
  460. Last = &V->NextFile;
  461. VF->NextFile = *Last;
  462. *Last = VF.Index();
  463. VF->Offset = List.Offset();
  464. VF->Size = List.Size();
  465. if (Cache.HeaderP->MaxVerFileSize < VF->Size)
  466. Cache.HeaderP->MaxVerFileSize = VF->Size;
  467. Cache.HeaderP->VerFileCount++;
  468. return true;
  469. }
  470. /*}}}*/
  471. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  472. // ---------------------------------------------------------------------
  473. /* This puts a version structure in the linked list */
  474. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  475. const string &VerStr,
  476. unsigned long Next)
  477. {
  478. // Get a structure
  479. map_ptrloc const Version = AllocateInMap(sizeof(pkgCache::Version));
  480. if (Version == 0)
  481. return 0;
  482. // Fill it in
  483. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  484. Ver->NextVer = Next;
  485. Ver->ID = Cache.HeaderP->VersionCount++;
  486. map_ptrloc const idxVerStr = WriteStringInMap(VerStr);
  487. if (unlikely(idxVerStr == 0))
  488. return 0;
  489. Ver->VerStr = idxVerStr;
  490. return Version;
  491. }
  492. /*}}}*/
  493. // CacheGenerator::NewFileDesc - Create a new File<->Desc association /*{{{*/
  494. // ---------------------------------------------------------------------
  495. /* */
  496. bool pkgCacheGenerator::NewFileDesc(pkgCache::DescIterator &Desc,
  497. ListParser &List)
  498. {
  499. if (CurrentFile == 0)
  500. return true;
  501. // Get a structure
  502. map_ptrloc const DescFile = AllocateInMap(sizeof(pkgCache::DescFile));
  503. if (DescFile == 0)
  504. return false;
  505. pkgCache::DescFileIterator DF(Cache,Cache.DescFileP + DescFile);
  506. DF->File = CurrentFile - Cache.PkgFileP;
  507. // Link it to the end of the list
  508. map_ptrloc *Last = &Desc->FileList;
  509. for (pkgCache::DescFileIterator D = Desc.FileList(); D.end() == false; D++)
  510. Last = &D->NextFile;
  511. DF->NextFile = *Last;
  512. *Last = DF.Index();
  513. DF->Offset = List.Offset();
  514. DF->Size = List.Size();
  515. if (Cache.HeaderP->MaxDescFileSize < DF->Size)
  516. Cache.HeaderP->MaxDescFileSize = DF->Size;
  517. Cache.HeaderP->DescFileCount++;
  518. return true;
  519. }
  520. /*}}}*/
  521. // CacheGenerator::NewDescription - Create a new Description /*{{{*/
  522. // ---------------------------------------------------------------------
  523. /* This puts a description structure in the linked list */
  524. map_ptrloc pkgCacheGenerator::NewDescription(pkgCache::DescIterator &Desc,
  525. const string &Lang,
  526. const MD5SumValue &md5sum,
  527. map_ptrloc Next)
  528. {
  529. // Get a structure
  530. map_ptrloc const Description = AllocateInMap(sizeof(pkgCache::Description));
  531. if (Description == 0)
  532. return 0;
  533. // Fill it in
  534. Desc = pkgCache::DescIterator(Cache,Cache.DescP + Description);
  535. Desc->NextDesc = Next;
  536. Desc->ID = Cache.HeaderP->DescriptionCount++;
  537. map_ptrloc const idxlanguage_code = WriteStringInMap(Lang);
  538. map_ptrloc const idxmd5sum = WriteStringInMap(md5sum.Value());
  539. if (unlikely(idxlanguage_code == 0 || idxmd5sum == 0))
  540. return 0;
  541. Desc->language_code = idxlanguage_code;
  542. Desc->md5sum = idxmd5sum;
  543. return Description;
  544. }
  545. /*}}}*/
  546. // CacheGenerator::FinishCache - do various finish operations /*{{{*/
  547. // ---------------------------------------------------------------------
  548. /* This prepares the Cache for delivery */
  549. bool pkgCacheGenerator::FinishCache(OpProgress *Progress)
  550. {
  551. // FIXME: add progress reporting for this operation
  552. // Do we have different architectures in your groups ?
  553. vector<string> archs = APT::Configuration::getArchitectures();
  554. if (archs.size() > 1)
  555. {
  556. // Create Conflicts in between the group
  557. pkgCache::GrpIterator G = GetCache().GrpBegin();
  558. Dynamic<pkgCache::GrpIterator> DynG(G);
  559. for (; G.end() != true; G++)
  560. {
  561. string const PkgName = G.Name();
  562. pkgCache::PkgIterator P = G.PackageList();
  563. Dynamic<pkgCache::PkgIterator> DynP(P);
  564. for (; P.end() != true; P = G.NextPkg(P))
  565. {
  566. if (strcmp(P.Arch(),"all") == 0)
  567. continue;
  568. pkgCache::PkgIterator allPkg;
  569. Dynamic<pkgCache::PkgIterator> DynallPkg(allPkg);
  570. pkgCache::VerIterator V = P.VersionList();
  571. Dynamic<pkgCache::VerIterator> DynV(V);
  572. for (; V.end() != true; V++)
  573. {
  574. string const Arch = V.Arch(true);
  575. map_ptrloc *OldDepLast = NULL;
  576. /* MultiArch handling introduces a lot of implicit Dependencies:
  577. - MultiArch: same → Co-Installable if they have the same version
  578. - Architecture: all → Need to be Co-Installable for internal reasons
  579. - All others conflict with all other group members */
  580. bool const coInstall = (V->MultiArch == pkgCache::Version::All ||
  581. V->MultiArch == pkgCache::Version::Same);
  582. if (V->MultiArch == pkgCache::Version::All && allPkg.end() == true)
  583. allPkg = G.FindPkg("all");
  584. for (vector<string>::const_iterator A = archs.begin(); A != archs.end(); ++A)
  585. {
  586. if (*A == Arch)
  587. continue;
  588. /* We allow only one installed arch at the time
  589. per group, therefore each group member conflicts
  590. with all other group members */
  591. pkgCache::PkgIterator D = G.FindPkg(*A);
  592. Dynamic<pkgCache::PkgIterator> DynD(D);
  593. if (D.end() == true)
  594. continue;
  595. if (coInstall == true)
  596. {
  597. // Replaces: ${self}:other ( << ${binary:Version})
  598. NewDepends(D, V, V.VerStr(),
  599. pkgCache::Dep::Less, pkgCache::Dep::Replaces,
  600. OldDepLast);
  601. // Breaks: ${self}:other (!= ${binary:Version})
  602. NewDepends(D, V, V.VerStr(),
  603. pkgCache::Dep::NotEquals, pkgCache::Dep::DpkgBreaks,
  604. OldDepLast);
  605. if (V->MultiArch == pkgCache::Version::All)
  606. {
  607. // Depend on ${self}:all which does depend on nothing
  608. NewDepends(allPkg, V, V.VerStr(),
  609. pkgCache::Dep::Equals, pkgCache::Dep::Depends,
  610. OldDepLast);
  611. }
  612. } else {
  613. // Conflicts: ${self}:other
  614. NewDepends(D, V, "",
  615. pkgCache::Dep::NoOp, pkgCache::Dep::Conflicts,
  616. OldDepLast);
  617. }
  618. }
  619. }
  620. }
  621. }
  622. }
  623. return true;
  624. }
  625. /*}}}*/
  626. // CacheGenerator::NewDepends - Create a dependency element /*{{{*/
  627. // ---------------------------------------------------------------------
  628. /* This creates a dependency element in the tree. It is linked to the
  629. version and to the package that it is pointing to. */
  630. bool pkgCacheGenerator::NewDepends(pkgCache::PkgIterator &Pkg,
  631. pkgCache::VerIterator &Ver,
  632. string const &Version,
  633. unsigned int const &Op,
  634. unsigned int const &Type,
  635. map_ptrloc *OldDepLast)
  636. {
  637. void const * const oldMap = Map.Data();
  638. // Get a structure
  639. map_ptrloc const Dependency = AllocateInMap(sizeof(pkgCache::Dependency));
  640. if (unlikely(Dependency == 0))
  641. return false;
  642. // Fill it in
  643. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  644. Dynamic<pkgCache::DepIterator> DynDep(Dep);
  645. Dep->ParentVer = Ver.Index();
  646. Dep->Type = Type;
  647. Dep->CompareOp = Op;
  648. Dep->ID = Cache.HeaderP->DependsCount++;
  649. // Probe the reverse dependency list for a version string that matches
  650. if (Version.empty() == false)
  651. {
  652. /* for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  653. if (I->Version != 0 && I.TargetVer() == Version)
  654. Dep->Version = I->Version;*/
  655. if (Dep->Version == 0) {
  656. map_ptrloc const index = WriteStringInMap(Version);
  657. if (unlikely(index == 0))
  658. return false;
  659. Dep->Version = index;
  660. }
  661. }
  662. // Link it to the package
  663. Dep->Package = Pkg.Index();
  664. Dep->NextRevDepends = Pkg->RevDepends;
  665. Pkg->RevDepends = Dep.Index();
  666. // Do we know where to link the Dependency to?
  667. if (OldDepLast == NULL)
  668. {
  669. OldDepLast = &Ver->DependsList;
  670. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  671. OldDepLast = &D->NextDepends;
  672. } else if (oldMap != Map.Data())
  673. OldDepLast += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  674. Dep->NextDepends = *OldDepLast;
  675. *OldDepLast = Dep.Index();
  676. OldDepLast = &Dep->NextDepends;
  677. return true;
  678. }
  679. /*}}}*/
  680. // ListParser::NewDepends - Create the environment for a new dependency /*{{{*/
  681. // ---------------------------------------------------------------------
  682. /* This creates a Group and the Package to link this dependency to if
  683. needed and handles also the caching of the old endpoint */
  684. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator &Ver,
  685. const string &PackageName,
  686. const string &Arch,
  687. const string &Version,
  688. unsigned int Op,
  689. unsigned int Type)
  690. {
  691. pkgCache::GrpIterator Grp;
  692. Dynamic<pkgCache::GrpIterator> DynGrp(Grp);
  693. if (unlikely(Owner->NewGroup(Grp, PackageName) == false))
  694. return false;
  695. // Locate the target package
  696. pkgCache::PkgIterator Pkg = Grp.FindPkg(Arch);
  697. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  698. if (Pkg.end() == true) {
  699. if (unlikely(Owner->NewPackage(Pkg, PackageName, Arch) == false))
  700. return false;
  701. }
  702. // Is it a file dependency?
  703. if (unlikely(PackageName[0] == '/'))
  704. FoundFileDeps = true;
  705. /* Caching the old end point speeds up generation substantially */
  706. if (OldDepVer != Ver) {
  707. OldDepLast = NULL;
  708. OldDepVer = Ver;
  709. }
  710. return Owner->NewDepends(Pkg, Ver, Version, Op, Type, OldDepLast);
  711. }
  712. /*}}}*/
  713. // ListParser::NewProvides - Create a Provides element /*{{{*/
  714. // ---------------------------------------------------------------------
  715. /* */
  716. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator &Ver,
  717. const string &PkgName,
  718. const string &PkgArch,
  719. const string &Version)
  720. {
  721. pkgCache &Cache = Owner->Cache;
  722. // We do not add self referencing provides
  723. if (Ver.ParentPkg().Name() == PkgName && PkgArch == Ver.Arch(true))
  724. return true;
  725. // Get a structure
  726. map_ptrloc const Provides = Owner->AllocateInMap(sizeof(pkgCache::Provides));
  727. if (unlikely(Provides == 0))
  728. return false;
  729. Cache.HeaderP->ProvidesCount++;
  730. // Fill it in
  731. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  732. Dynamic<pkgCache::PrvIterator> DynPrv(Prv);
  733. Prv->Version = Ver.Index();
  734. Prv->NextPkgProv = Ver->ProvidesList;
  735. Ver->ProvidesList = Prv.Index();
  736. if (Version.empty() == false && unlikely((Prv->ProvideVersion = WriteString(Version)) == 0))
  737. return false;
  738. // Locate the target package
  739. pkgCache::PkgIterator Pkg;
  740. Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
  741. if (unlikely(Owner->NewPackage(Pkg,PkgName, PkgArch) == false))
  742. return false;
  743. // Link it to the package
  744. Prv->ParentPkg = Pkg.Index();
  745. Prv->NextProvides = Pkg->ProvidesList;
  746. Pkg->ProvidesList = Prv.Index();
  747. return true;
  748. }
  749. /*}}}*/
  750. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  751. // ---------------------------------------------------------------------
  752. /* This is used to select which file is to be associated with all newly
  753. added versions. The caller is responsible for setting the IMS fields. */
  754. bool pkgCacheGenerator::SelectFile(const string &File,const string &Site,
  755. const pkgIndexFile &Index,
  756. unsigned long Flags)
  757. {
  758. // Get some space for the structure
  759. map_ptrloc const idxFile = AllocateInMap(sizeof(*CurrentFile));
  760. if (unlikely(idxFile == 0))
  761. return false;
  762. CurrentFile = Cache.PkgFileP + idxFile;
  763. // Fill it in
  764. map_ptrloc const idxFileName = WriteStringInMap(File);
  765. map_ptrloc const idxSite = WriteUniqString(Site);
  766. if (unlikely(idxFileName == 0 || idxSite == 0))
  767. return false;
  768. CurrentFile->FileName = idxFileName;
  769. CurrentFile->Site = idxSite;
  770. CurrentFile->NextFile = Cache.HeaderP->FileList;
  771. CurrentFile->Flags = Flags;
  772. CurrentFile->ID = Cache.HeaderP->PackageFileCount;
  773. map_ptrloc const idxIndexType = WriteUniqString(Index.GetType()->Label);
  774. if (unlikely(idxIndexType == 0))
  775. return false;
  776. CurrentFile->IndexType = idxIndexType;
  777. PkgFileName = File;
  778. Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
  779. Cache.HeaderP->PackageFileCount++;
  780. if (Progress != 0)
  781. Progress->SubProgress(Index.Size());
  782. return true;
  783. }
  784. /*}}}*/
  785. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  786. // ---------------------------------------------------------------------
  787. /* This is used to create handles to strings. Given the same text it
  788. always returns the same number */
  789. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  790. unsigned int Size)
  791. {
  792. /* We use a very small transient hash table here, this speeds up generation
  793. by a fair amount on slower machines */
  794. pkgCache::StringItem *&Bucket = UniqHash[(S[0]*5 + S[1]) % _count(UniqHash)];
  795. if (Bucket != 0 &&
  796. stringcmp(S,S+Size,Cache.StrP + Bucket->String) == 0)
  797. return Bucket->String;
  798. // Search for an insertion point
  799. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  800. int Res = 1;
  801. map_ptrloc *Last = &Cache.HeaderP->StringList;
  802. for (; I != Cache.StringItemP; Last = &I->NextItem,
  803. I = Cache.StringItemP + I->NextItem)
  804. {
  805. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  806. if (Res >= 0)
  807. break;
  808. }
  809. // Match
  810. if (Res == 0)
  811. {
  812. Bucket = I;
  813. return I->String;
  814. }
  815. // Get a structure
  816. void const * const oldMap = Map.Data();
  817. map_ptrloc const Item = AllocateInMap(sizeof(pkgCache::StringItem));
  818. if (Item == 0)
  819. return 0;
  820. map_ptrloc const idxString = WriteStringInMap(S,Size);
  821. if (unlikely(idxString == 0))
  822. return 0;
  823. if (oldMap != Map.Data()) {
  824. Last += (map_ptrloc*) Map.Data() - (map_ptrloc*) oldMap;
  825. I += (pkgCache::StringItem*) Map.Data() - (pkgCache::StringItem*) oldMap;
  826. }
  827. *Last = Item;
  828. // Fill in the structure
  829. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  830. ItemP->NextItem = I - Cache.StringItemP;
  831. ItemP->String = idxString;
  832. Bucket = ItemP;
  833. return ItemP->String;
  834. }
  835. /*}}}*/
  836. // CheckValidity - Check that a cache is up-to-date /*{{{*/
  837. // ---------------------------------------------------------------------
  838. /* This just verifies that each file in the list of index files exists,
  839. has matching attributes with the cache and the cache does not have
  840. any extra files. */
  841. static bool CheckValidity(const string &CacheFile, FileIterator Start,
  842. FileIterator End,MMap **OutMap = 0)
  843. {
  844. bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
  845. // No file, certainly invalid
  846. if (CacheFile.empty() == true || FileExists(CacheFile) == false)
  847. {
  848. if (Debug == true)
  849. std::clog << "CacheFile doesn't exist" << std::endl;
  850. return false;
  851. }
  852. // Map it
  853. FileFd CacheF(CacheFile,FileFd::ReadOnly);
  854. SPtr<MMap> Map = new MMap(CacheF,0);
  855. pkgCache Cache(Map);
  856. if (_error->PendingError() == true || Map->Size() == 0)
  857. {
  858. if (Debug == true)
  859. std::clog << "Errors are pending or Map is empty()" << std::endl;
  860. _error->Discard();
  861. return false;
  862. }
  863. /* Now we check every index file, see if it is in the cache,
  864. verify the IMS data and check that it is on the disk too.. */
  865. SPtrArray<bool> Visited = new bool[Cache.HeaderP->PackageFileCount];
  866. memset(Visited,0,sizeof(*Visited)*Cache.HeaderP->PackageFileCount);
  867. for (; Start != End; Start++)
  868. {
  869. if (Debug == true)
  870. std::clog << "Checking PkgFile " << (*Start)->Describe() << ": ";
  871. if ((*Start)->HasPackages() == false)
  872. {
  873. if (Debug == true)
  874. std::clog << "Has NO packages" << std::endl;
  875. continue;
  876. }
  877. if ((*Start)->Exists() == false)
  878. {
  879. #if 0 // mvo: we no longer give a message here (Default Sources spec)
  880. _error->WarningE("stat",_("Couldn't stat source package list %s"),
  881. (*Start)->Describe().c_str());
  882. #endif
  883. if (Debug == true)
  884. std::clog << "file doesn't exist" << std::endl;
  885. continue;
  886. }
  887. // FindInCache is also expected to do an IMS check.
  888. pkgCache::PkgFileIterator File = (*Start)->FindInCache(Cache);
  889. if (File.end() == true)
  890. {
  891. if (Debug == true)
  892. std::clog << "FindInCache returned end-Pointer" << std::endl;
  893. return false;
  894. }
  895. Visited[File->ID] = true;
  896. if (Debug == true)
  897. std::clog << "with ID " << File->ID << " is valid" << std::endl;
  898. }
  899. for (unsigned I = 0; I != Cache.HeaderP->PackageFileCount; I++)
  900. if (Visited[I] == false)
  901. {
  902. if (Debug == true)
  903. std::clog << "File with ID" << I << " wasn't visited" << std::endl;
  904. return false;
  905. }
  906. if (_error->PendingError() == true)
  907. {
  908. if (Debug == true)
  909. {
  910. std::clog << "Validity failed because of pending errors:" << std::endl;
  911. _error->DumpErrors();
  912. }
  913. _error->Discard();
  914. return false;
  915. }
  916. if (OutMap != 0)
  917. *OutMap = Map.UnGuard();
  918. return true;
  919. }
  920. /*}}}*/
  921. // ComputeSize - Compute the total size of a bunch of files /*{{{*/
  922. // ---------------------------------------------------------------------
  923. /* Size is kind of an abstract notion that is only used for the progress
  924. meter */
  925. static unsigned long ComputeSize(FileIterator Start,FileIterator End)
  926. {
  927. unsigned long TotalSize = 0;
  928. for (; Start != End; Start++)
  929. {
  930. if ((*Start)->HasPackages() == false)
  931. continue;
  932. TotalSize += (*Start)->Size();
  933. }
  934. return TotalSize;
  935. }
  936. /*}}}*/
  937. // BuildCache - Merge the list of index files into the cache /*{{{*/
  938. // ---------------------------------------------------------------------
  939. /* */
  940. static bool BuildCache(pkgCacheGenerator &Gen,
  941. OpProgress *Progress,
  942. unsigned long &CurrentSize,unsigned long TotalSize,
  943. FileIterator Start, FileIterator End)
  944. {
  945. FileIterator I;
  946. for (I = Start; I != End; I++)
  947. {
  948. if ((*I)->HasPackages() == false)
  949. continue;
  950. if ((*I)->Exists() == false)
  951. continue;
  952. if ((*I)->FindInCache(Gen.GetCache()).end() == false)
  953. {
  954. _error->Warning("Duplicate sources.list entry %s",
  955. (*I)->Describe().c_str());
  956. continue;
  957. }
  958. unsigned long Size = (*I)->Size();
  959. if (Progress != NULL)
  960. Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
  961. CurrentSize += Size;
  962. if ((*I)->Merge(Gen,Progress) == false)
  963. return false;
  964. }
  965. if (Gen.HasFileDeps() == true)
  966. {
  967. if (Progress != NULL)
  968. Progress->Done();
  969. TotalSize = ComputeSize(Start, End);
  970. CurrentSize = 0;
  971. for (I = Start; I != End; I++)
  972. {
  973. unsigned long Size = (*I)->Size();
  974. if (Progress != NULL)
  975. Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
  976. CurrentSize += Size;
  977. if ((*I)->MergeFileProvides(Gen,Progress) == false)
  978. return false;
  979. }
  980. }
  981. return true;
  982. }
  983. /*}}}*/
  984. DynamicMMap* pkgCacheGenerator::CreateDynamicMMap(FileFd *CacheF, unsigned long Flags) {
  985. unsigned long const MapStart = _config->FindI("APT::Cache-Start", 24*1024*1024);
  986. unsigned long const MapGrow = _config->FindI("APT::Cache-Grow", 1*1024*1024);
  987. unsigned long const MapLimit = _config->FindI("APT::Cache-Limit", 0);
  988. Flags |= MMap::Moveable;
  989. if (_config->FindB("APT::Cache-Fallback", false) == true)
  990. Flags |= MMap::Fallback;
  991. if (CacheF != NULL)
  992. return new DynamicMMap(*CacheF, Flags, MapStart, MapGrow, MapLimit);
  993. else
  994. return new DynamicMMap(Flags, MapStart, MapGrow, MapLimit);
  995. }
  996. // CacheGenerator::MakeStatusCache - Construct the status cache /*{{{*/
  997. // ---------------------------------------------------------------------
  998. /* This makes sure that the status cache (the cache that has all
  999. index files from the sources list and all local ones) is ready
  1000. to be mmaped. If OutMap is not zero then a MMap object representing
  1001. the cache will be stored there. This is pretty much mandetory if you
  1002. are using AllowMem. AllowMem lets the function be run as non-root
  1003. where it builds the cache 'fast' into a memory buffer. */
  1004. __deprecated bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
  1005. MMap **OutMap, bool AllowMem)
  1006. { return pkgCacheGenerator::MakeStatusCache(List, &Progress, OutMap, AllowMem); }
  1007. bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
  1008. MMap **OutMap,bool AllowMem)
  1009. {
  1010. bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
  1011. vector<pkgIndexFile *> Files;
  1012. for (vector<metaIndex *>::const_iterator i = List.begin();
  1013. i != List.end();
  1014. i++)
  1015. {
  1016. vector <pkgIndexFile *> *Indexes = (*i)->GetIndexFiles();
  1017. for (vector<pkgIndexFile *>::const_iterator j = Indexes->begin();
  1018. j != Indexes->end();
  1019. j++)
  1020. Files.push_back (*j);
  1021. }
  1022. unsigned long const EndOfSource = Files.size();
  1023. if (_system->AddStatusFiles(Files) == false)
  1024. return false;
  1025. // Decide if we can write to the files..
  1026. string const CacheFile = _config->FindFile("Dir::Cache::pkgcache");
  1027. string const SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
  1028. // ensure the cache directory exists
  1029. if (CacheFile.empty() == false || SrcCacheFile.empty() == false)
  1030. {
  1031. string dir = _config->FindDir("Dir::Cache");
  1032. size_t const len = dir.size();
  1033. if (len > 5 && dir.find("/apt/", len - 6, 5) == len - 5)
  1034. dir = dir.substr(0, len - 5);
  1035. if (CacheFile.empty() == false)
  1036. CreateDirectory(dir, flNotFile(CacheFile));
  1037. if (SrcCacheFile.empty() == false)
  1038. CreateDirectory(dir, flNotFile(SrcCacheFile));
  1039. }
  1040. // Decide if we can write to the cache
  1041. bool Writeable = false;
  1042. if (CacheFile.empty() == false)
  1043. Writeable = access(flNotFile(CacheFile).c_str(),W_OK) == 0;
  1044. else
  1045. if (SrcCacheFile.empty() == false)
  1046. Writeable = access(flNotFile(SrcCacheFile).c_str(),W_OK) == 0;
  1047. if (Debug == true)
  1048. std::clog << "Do we have write-access to the cache files? " << (Writeable ? "YES" : "NO") << std::endl;
  1049. if (Writeable == false && AllowMem == false && CacheFile.empty() == false)
  1050. return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
  1051. if (Progress != NULL)
  1052. Progress->OverallProgress(0,1,1,_("Reading package lists"));
  1053. // Cache is OK, Fin.
  1054. if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
  1055. {
  1056. if (Progress != NULL)
  1057. Progress->OverallProgress(1,1,1,_("Reading package lists"));
  1058. if (Debug == true)
  1059. std::clog << "pkgcache.bin is valid - no need to build anything" << std::endl;
  1060. return true;
  1061. }
  1062. else if (Debug == true)
  1063. std::clog << "pkgcache.bin is NOT valid" << std::endl;
  1064. /* At this point we know we need to reconstruct the package cache,
  1065. begin. */
  1066. SPtr<FileFd> CacheF;
  1067. SPtr<DynamicMMap> Map;
  1068. if (Writeable == true && CacheFile.empty() == false)
  1069. {
  1070. unlink(CacheFile.c_str());
  1071. CacheF = new FileFd(CacheFile,FileFd::WriteEmpty);
  1072. fchmod(CacheF->Fd(),0644);
  1073. Map = CreateDynamicMMap(CacheF, MMap::Public);
  1074. if (_error->PendingError() == true)
  1075. return false;
  1076. if (Debug == true)
  1077. std::clog << "Open filebased MMap" << std::endl;
  1078. }
  1079. else
  1080. {
  1081. // Just build it in memory..
  1082. Map = CreateDynamicMMap(NULL);
  1083. if (Debug == true)
  1084. std::clog << "Open memory Map (not filebased)" << std::endl;
  1085. }
  1086. // Lets try the source cache.
  1087. unsigned long CurrentSize = 0;
  1088. unsigned long TotalSize = 0;
  1089. if (CheckValidity(SrcCacheFile,Files.begin(),
  1090. Files.begin()+EndOfSource) == true)
  1091. {
  1092. if (Debug == true)
  1093. std::clog << "srcpkgcache.bin is valid - populate MMap with it." << std::endl;
  1094. // Preload the map with the source cache
  1095. FileFd SCacheF(SrcCacheFile,FileFd::ReadOnly);
  1096. unsigned long const alloc = Map->RawAllocate(SCacheF.Size());
  1097. if ((alloc == 0 && _error->PendingError())
  1098. || SCacheF.Read((unsigned char *)Map->Data() + alloc,
  1099. SCacheF.Size()) == false)
  1100. return false;
  1101. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  1102. // Build the status cache
  1103. pkgCacheGenerator Gen(Map.Get(),Progress);
  1104. if (_error->PendingError() == true)
  1105. return false;
  1106. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1107. Files.begin()+EndOfSource,Files.end()) == false)
  1108. return false;
  1109. // FIXME: move me to a better place
  1110. Gen.FinishCache(Progress);
  1111. }
  1112. else
  1113. {
  1114. if (Debug == true)
  1115. std::clog << "srcpkgcache.bin is NOT valid - rebuild" << std::endl;
  1116. TotalSize = ComputeSize(Files.begin(),Files.end());
  1117. // Build the source cache
  1118. pkgCacheGenerator Gen(Map.Get(),Progress);
  1119. if (_error->PendingError() == true)
  1120. return false;
  1121. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1122. Files.begin(),Files.begin()+EndOfSource) == false)
  1123. return false;
  1124. // Write it back
  1125. if (Writeable == true && SrcCacheFile.empty() == false)
  1126. {
  1127. FileFd SCacheF(SrcCacheFile,FileFd::WriteEmpty);
  1128. if (_error->PendingError() == true)
  1129. return false;
  1130. fchmod(SCacheF.Fd(),0644);
  1131. // Write out the main data
  1132. if (SCacheF.Write(Map->Data(),Map->Size()) == false)
  1133. return _error->Error(_("IO Error saving source cache"));
  1134. SCacheF.Sync();
  1135. // Write out the proper header
  1136. Gen.GetCache().HeaderP->Dirty = false;
  1137. if (SCacheF.Seek(0) == false ||
  1138. SCacheF.Write(Map->Data(),sizeof(*Gen.GetCache().HeaderP)) == false)
  1139. return _error->Error(_("IO Error saving source cache"));
  1140. Gen.GetCache().HeaderP->Dirty = true;
  1141. SCacheF.Sync();
  1142. }
  1143. // Build the status cache
  1144. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1145. Files.begin()+EndOfSource,Files.end()) == false)
  1146. return false;
  1147. // FIXME: move me to a better place
  1148. Gen.FinishCache(Progress);
  1149. }
  1150. if (Debug == true)
  1151. std::clog << "Caches are ready for shipping" << std::endl;
  1152. if (_error->PendingError() == true)
  1153. return false;
  1154. if (OutMap != 0)
  1155. {
  1156. if (CacheF != 0)
  1157. {
  1158. delete Map.UnGuard();
  1159. *OutMap = new MMap(*CacheF,0);
  1160. }
  1161. else
  1162. {
  1163. *OutMap = Map.UnGuard();
  1164. }
  1165. }
  1166. return true;
  1167. }
  1168. /*}}}*/
  1169. // CacheGenerator::MakeOnlyStatusCache - Build only a status files cache/*{{{*/
  1170. // ---------------------------------------------------------------------
  1171. /* */
  1172. __deprecated bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
  1173. { return pkgCacheGenerator::MakeOnlyStatusCache(&Progress, OutMap); }
  1174. bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap)
  1175. {
  1176. vector<pkgIndexFile *> Files;
  1177. unsigned long EndOfSource = Files.size();
  1178. if (_system->AddStatusFiles(Files) == false)
  1179. return false;
  1180. SPtr<DynamicMMap> Map = CreateDynamicMMap(NULL);
  1181. unsigned long CurrentSize = 0;
  1182. unsigned long TotalSize = 0;
  1183. TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
  1184. // Build the status cache
  1185. if (Progress != NULL)
  1186. Progress->OverallProgress(0,1,1,_("Reading package lists"));
  1187. pkgCacheGenerator Gen(Map.Get(),Progress);
  1188. if (_error->PendingError() == true)
  1189. return false;
  1190. if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
  1191. Files.begin()+EndOfSource,Files.end()) == false)
  1192. return false;
  1193. // FIXME: move me to a better place
  1194. Gen.FinishCache(Progress);
  1195. if (_error->PendingError() == true)
  1196. return false;
  1197. *OutMap = Map.UnGuard();
  1198. return true;
  1199. }
  1200. /*}}}*/