filelist.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: filelist.cc,v 1.4.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. File Listing - Manages a Cache of File -> Package names.
  6. Diversions add some significant complexity to the system. To keep
  7. storage space down in the very special case of a diverted file no
  8. extra bytes are allocated in the Node structure. Instead a diversion
  9. is inserted directly into the hash table and its flag bit set. Every
  10. lookup for that filename will always return the diversion.
  11. The hash buckets are stored in sorted form, with diversions having
  12. the highest sort order. Identical files are assigned the same file
  13. pointer, thus after a search all of the nodes owning that file can be
  14. found by iterating down the bucket.
  15. Re-updates of diversions (another extremely special case) are done by
  16. marking all diversions as untouched, then loading the entire diversion
  17. list again, touching each diversion and then finally going back and
  18. releasing all untouched diversions. It is assumed that the diversion
  19. table will always be quite small and be a very irregular case.
  20. Diversions that are user-installed are represented by a package with
  21. an empty name string.
  22. Conf files are handled like diversions by changing the meaning of the
  23. Pointer field to point to a conf file entry - again to reduce over
  24. head for a special case.
  25. ##################################################################### */
  26. /*}}}*/
  27. // Include Files /*{{{*/
  28. #include<config.h>
  29. #include <apt-pkg/filelist.h>
  30. #include <apt-pkg/mmap.h>
  31. #include <apt-pkg/error.h>
  32. #include <apt-pkg/strutl.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <iostream>
  37. #include <apti18n.h>
  38. /*}}}*/
  39. using namespace std;
  40. // FlCache::Header::Header - Constructor /*{{{*/
  41. // ---------------------------------------------------------------------
  42. /* Initialize the header variables. These are the defaults used when
  43. creating new caches */
  44. pkgFLCache::Header::Header()
  45. {
  46. Signature = 0xEA3F1295;
  47. /* Whenever the structures change the major version should be bumped,
  48. whenever the generator changes the minor version should be bumped. */
  49. MajorVersion = 1;
  50. MinorVersion = 0;
  51. Dirty = true;
  52. HeaderSz = sizeof(pkgFLCache::Header);
  53. NodeSz = sizeof(pkgFLCache::Node);
  54. DirSz = sizeof(pkgFLCache::Directory);
  55. PackageSz = sizeof(pkgFLCache::Package);
  56. DiversionSz = sizeof(pkgFLCache::Diversion);
  57. ConfFileSz = sizeof(pkgFLCache::ConfFile);
  58. NodeCount = 0;
  59. DirCount = 0;
  60. PackageCount = 0;
  61. DiversionCount = 0;
  62. ConfFileCount = 0;
  63. HashSize = 1 << 14;
  64. FileHash = 0;
  65. DirTree = 0;
  66. Packages = 0;
  67. Diversions = 0;
  68. UniqNodes = 0;
  69. memset(Pools,0,sizeof(Pools));
  70. }
  71. /*}}}*/
  72. // FLCache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
  73. // ---------------------------------------------------------------------
  74. /* Compare to make sure we are matching versions */
  75. bool pkgFLCache::Header::CheckSizes(Header &Against) const
  76. {
  77. if (HeaderSz == Against.HeaderSz &&
  78. NodeSz == Against.NodeSz &&
  79. DirSz == Against.DirSz &&
  80. DiversionSz == Against.DiversionSz &&
  81. PackageSz == Against.PackageSz &&
  82. ConfFileSz == Against.ConfFileSz)
  83. return true;
  84. return false;
  85. }
  86. /*}}}*/
  87. // FLCache::pkgFLCache - Constructor /*{{{*/
  88. // ---------------------------------------------------------------------
  89. /* If this is a new cache then a new header and hash table are instantaited
  90. otherwise the existing ones are mearly attached */
  91. pkgFLCache::pkgFLCache(DynamicMMap &Map) : Map(Map)
  92. {
  93. if (_error->PendingError() == true)
  94. return;
  95. LastTreeLookup = 0;
  96. LastLookupSize = 0;
  97. // Apply the typecasts
  98. HeaderP = (Header *)Map.Data();
  99. NodeP = (Node *)Map.Data();
  100. DirP = (Directory *)Map.Data();
  101. DiverP = (Diversion *)Map.Data();
  102. PkgP = (Package *)Map.Data();
  103. ConfP = (ConfFile *)Map.Data();
  104. StrP = (char *)Map.Data();
  105. AnyP = (unsigned char *)Map.Data();
  106. // New mapping, create the basic cache structures
  107. if (Map.Size() == 0)
  108. {
  109. Map.RawAllocate(sizeof(pkgFLCache::Header));
  110. *HeaderP = pkgFLCache::Header();
  111. HeaderP->FileHash = Map.RawAllocate(sizeof(pkgFLCache::Node)*HeaderP->HashSize,
  112. sizeof(pkgFLCache::Node))/sizeof(pkgFLCache::Node);
  113. }
  114. FileHash = NodeP + HeaderP->FileHash;
  115. // Setup the dynamic map manager
  116. HeaderP->Dirty = true;
  117. Map.Sync(0,sizeof(pkgFLCache::Header));
  118. Map.UsePools(*HeaderP->Pools,sizeof(HeaderP->Pools)/sizeof(HeaderP->Pools[0]));
  119. }
  120. /*}}}*/
  121. // FLCache::TreeLookup - Perform a lookup in a generic tree /*{{{*/
  122. // ---------------------------------------------------------------------
  123. /* This is a simple generic tree lookup. The first three entries of
  124. the Directory structure are used as a template, but any other similar
  125. structure could be used in it's place. */
  126. map_ptrloc pkgFLCache::TreeLookup(map_ptrloc *Base,const char *Text,
  127. const char *TextEnd,unsigned long Size,
  128. unsigned int *Count,bool Insert)
  129. {
  130. pkgFLCache::Directory *Dir;
  131. // Check our last entry cache
  132. if (LastTreeLookup != 0 && LastLookupSize == Size)
  133. {
  134. Dir = (pkgFLCache::Directory *)(AnyP + LastTreeLookup*Size);
  135. if (stringcmp(Text,TextEnd,StrP + Dir->Name) == 0)
  136. return LastTreeLookup;
  137. }
  138. while (1)
  139. {
  140. // Allocate a new one
  141. if (*Base == 0)
  142. {
  143. if (Insert == false)
  144. return 0;
  145. *Base = Map.Allocate(Size);
  146. if (*Base == 0)
  147. return 0;
  148. (*Count)++;
  149. Dir = (pkgFLCache::Directory *)(AnyP + *Base*Size);
  150. Dir->Name = Map.WriteString(Text,TextEnd - Text);
  151. LastTreeLookup = *Base;
  152. LastLookupSize = Size;
  153. return *Base;
  154. }
  155. // Compare this node
  156. Dir = (pkgFLCache::Directory *)(AnyP + *Base*Size);
  157. int Res = stringcmp(Text,TextEnd,StrP + Dir->Name);
  158. if (Res == 0)
  159. {
  160. LastTreeLookup = *Base;
  161. LastLookupSize = Size;
  162. return *Base;
  163. }
  164. if (Res > 0)
  165. Base = &Dir->Left;
  166. if (Res < 0)
  167. Base = &Dir->Right;
  168. }
  169. }
  170. /*}}}*/
  171. // FLCache::PrintTree - Print out a tree /*{{{*/
  172. // ---------------------------------------------------------------------
  173. /* This is a simple generic tree dumper, ment for debugging. */
  174. void pkgFLCache::PrintTree(map_ptrloc Base,unsigned long Size)
  175. {
  176. if (Base == 0)
  177. return;
  178. pkgFLCache::Directory *Dir = (pkgFLCache::Directory *)(AnyP + Base*Size);
  179. PrintTree(Dir->Left,Size);
  180. cout << (StrP + Dir->Name) << endl;
  181. PrintTree(Dir->Right,Size);
  182. }
  183. /*}}}*/
  184. // FLCache::GetPkg - Get a package pointer /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* Locate a package by name in it's tree, this is just a wrapper for
  187. TreeLookup */
  188. pkgFLCache::PkgIterator pkgFLCache::GetPkg(const char *Name,const char *NameEnd,
  189. bool Insert)
  190. {
  191. if (NameEnd == 0)
  192. NameEnd = Name + strlen(Name);
  193. map_ptrloc Pos = TreeLookup(&HeaderP->Packages,Name,NameEnd,
  194. sizeof(pkgFLCache::Package),
  195. &HeaderP->PackageCount,Insert);
  196. if (Pos == 0)
  197. return pkgFLCache::PkgIterator();
  198. return pkgFLCache::PkgIterator(*this,PkgP + Pos);
  199. }
  200. /*}}}*/
  201. // FLCache::GetNode - Get the node associated with the filename /*{{{*/
  202. // ---------------------------------------------------------------------
  203. /* Lookup a node in the hash table. If Insert is true then a new node is
  204. always inserted. The hash table can have multiple instances of a
  205. single name available. A search returns the first. It is important
  206. that additions for the same name insert after the first entry of
  207. the name group. */
  208. pkgFLCache::NodeIterator pkgFLCache::GetNode(const char *Name,
  209. const char *NameEnd,
  210. map_ptrloc Loc,
  211. bool Insert,bool Divert)
  212. {
  213. // Split the name into file and directory, hashing as it is copied
  214. const char *File = Name;
  215. unsigned long HashPos = 0;
  216. for (const char *I = Name; I < NameEnd; I++)
  217. {
  218. HashPos = 1637*HashPos + *I;
  219. if (*I == '/')
  220. File = I;
  221. }
  222. // Search for it
  223. Node *Hash = NodeP + HeaderP->FileHash + (HashPos % HeaderP->HashSize);
  224. int Res = 0;
  225. map_ptrloc FilePtr = 0;
  226. while (Hash->Pointer != 0)
  227. {
  228. // Compare
  229. Res = stringcmp(File+1,NameEnd,StrP + Hash->File);
  230. if (Res == 0)
  231. Res = stringcmp(Name,File,StrP + DirP[Hash->Dir].Name);
  232. // Diversion?
  233. if (Res == 0 && Insert == true)
  234. {
  235. /* Dir and File match exactly, we need to reuse the file name
  236. when we link it in */
  237. FilePtr = Hash->File;
  238. Res = Divert - ((Hash->Flags & Node::Diversion) == Node::Diversion);
  239. }
  240. // Is a match
  241. if (Res == 0)
  242. {
  243. if (Insert == false)
  244. return NodeIterator(*this,Hash);
  245. // Only one diversion per name!
  246. if (Divert == true)
  247. return NodeIterator(*this,Hash);
  248. break;
  249. }
  250. // Out of sort order
  251. if (Res > 0)
  252. break;
  253. if (Hash->Next != 0)
  254. Hash = NodeP + Hash->Next;
  255. else
  256. break;
  257. }
  258. // Fail, not found
  259. if (Insert == false)
  260. return NodeIterator(*this);
  261. // Find a directory node
  262. map_ptrloc Dir = TreeLookup(&HeaderP->DirTree,Name,File,
  263. sizeof(pkgFLCache::Directory),
  264. &HeaderP->DirCount,true);
  265. if (Dir == 0)
  266. return NodeIterator(*this);
  267. // Allocate a new node
  268. if (Hash->Pointer != 0)
  269. {
  270. // Overwrite or append
  271. if (Res > 0)
  272. {
  273. Node *Next = NodeP + Map.Allocate(sizeof(*Hash));
  274. if (Next == NodeP)
  275. return NodeIterator(*this);
  276. *Next = *Hash;
  277. Hash->Next = Next - NodeP;
  278. }
  279. else
  280. {
  281. unsigned long NewNext = Map.Allocate(sizeof(*Hash));
  282. if (NewNext == 0)
  283. return NodeIterator(*this);
  284. NodeP[NewNext].Next = Hash->Next;
  285. Hash->Next = NewNext;
  286. Hash = NodeP + Hash->Next;
  287. }
  288. }
  289. // Insert into the new item
  290. Hash->Dir = Dir;
  291. Hash->Pointer = Loc;
  292. Hash->Flags = 0;
  293. if (Divert == true)
  294. Hash->Flags |= Node::Diversion;
  295. if (FilePtr != 0)
  296. Hash->File = FilePtr;
  297. else
  298. {
  299. HeaderP->UniqNodes++;
  300. Hash->File = Map.WriteString(File+1,NameEnd - File-1);
  301. }
  302. // Link the node to the package list
  303. if (Divert == false && Loc == 0)
  304. {
  305. Hash->Next = PkgP[Loc].Files;
  306. PkgP[Loc].Files = Hash - NodeP;
  307. }
  308. HeaderP->NodeCount++;
  309. return NodeIterator(*this,Hash);
  310. }
  311. /*}}}*/
  312. // FLCache::HashNode - Return the hash bucket for the node /*{{{*/
  313. // ---------------------------------------------------------------------
  314. /* This is one of two hashing functions. The other is inlined into the
  315. GetNode routine. */
  316. pkgFLCache::Node *pkgFLCache::HashNode(NodeIterator const &Nde)
  317. {
  318. // Hash the node
  319. unsigned long HashPos = 0;
  320. for (const char *I = Nde.DirN(); *I != 0; I++)
  321. HashPos = 1637*HashPos + *I;
  322. HashPos = 1637*HashPos + '/';
  323. for (const char *I = Nde.File(); *I != 0; I++)
  324. HashPos = 1637*HashPos + *I;
  325. return NodeP + HeaderP->FileHash + (HashPos % HeaderP->HashSize);
  326. }
  327. /*}}}*/
  328. // FLCache::DropNode - Drop a node from the hash table /*{{{*/
  329. // ---------------------------------------------------------------------
  330. /* This erases a node from the hash table. Note that this does not unlink
  331. the node from the package linked list. */
  332. void pkgFLCache::DropNode(map_ptrloc N)
  333. {
  334. if (N == 0)
  335. return;
  336. NodeIterator Nde(*this,NodeP + N);
  337. if (Nde->NextPkg != 0)
  338. _error->Warning(_("DropNode called on still linked node"));
  339. // Locate it in the hash table
  340. Node *Last = 0;
  341. Node *Hash = HashNode(Nde);
  342. while (Hash->Pointer != 0)
  343. {
  344. // Got it
  345. if (Hash == Nde)
  346. {
  347. // Top of the bucket..
  348. if (Last == 0)
  349. {
  350. Hash->Pointer = 0;
  351. if (Hash->Next == 0)
  352. return;
  353. *Hash = NodeP[Hash->Next];
  354. // Release Hash->Next
  355. return;
  356. }
  357. Last->Next = Hash->Next;
  358. // Release Hash
  359. return;
  360. }
  361. Last = Hash;
  362. if (Hash->Next != 0)
  363. Hash = NodeP + Hash->Next;
  364. else
  365. break;
  366. }
  367. _error->Error(_("Failed to locate the hash element!"));
  368. }
  369. /*}}}*/
  370. // FLCache::BeginDiverLoad - Start reading new diversions /*{{{*/
  371. // ---------------------------------------------------------------------
  372. /* Tag all the diversions as untouched */
  373. void pkgFLCache::BeginDiverLoad()
  374. {
  375. for (DiverIterator I = DiverBegin(); I.end() == false; I++)
  376. I->Flags = 0;
  377. }
  378. /*}}}*/
  379. // FLCache::FinishDiverLoad - Finish up a new diversion load /*{{{*/
  380. // ---------------------------------------------------------------------
  381. /* This drops any untouched diversions. In effect removing any diversions
  382. that where not loaded (ie missing from the diversion file) */
  383. void pkgFLCache::FinishDiverLoad()
  384. {
  385. map_ptrloc *Cur = &HeaderP->Diversions;
  386. while (*Cur != 0)
  387. {
  388. Diversion *Div = DiverP + *Cur;
  389. if ((Div->Flags & Diversion::Touched) == Diversion::Touched)
  390. {
  391. Cur = &Div->Next;
  392. continue;
  393. }
  394. // Purge!
  395. DropNode(Div->DivertTo);
  396. DropNode(Div->DivertFrom);
  397. *Cur = Div->Next;
  398. }
  399. }
  400. /*}}}*/
  401. // FLCache::AddDiversion - Add a new diversion /*{{{*/
  402. // ---------------------------------------------------------------------
  403. /* Add a new diversion to the diverion tables and make sure that it is
  404. unique and non-chaining. */
  405. bool pkgFLCache::AddDiversion(PkgIterator const &Owner,
  406. const char *From,const char *To)
  407. {
  408. /* Locate the two hash nodes we are going to manipulate. If there
  409. are pre-existing diversions then they will be returned */
  410. NodeIterator FromN = GetNode(From,From+strlen(From),0,true,true);
  411. NodeIterator ToN = GetNode(To,To+strlen(To),0,true,true);
  412. if (FromN.end() == true || ToN.end() == true)
  413. return _error->Error(_("Failed to allocate diversion"));
  414. // Should never happen
  415. if ((FromN->Flags & Node::Diversion) != Node::Diversion ||
  416. (ToN->Flags & Node::Diversion) != Node::Diversion)
  417. return _error->Error(_("Internal error in AddDiversion"));
  418. // Now, try to reclaim an existing diversion..
  419. map_ptrloc Diver = 0;
  420. if (FromN->Pointer != 0)
  421. Diver = FromN->Pointer;
  422. /* Make sure from and to point to the same diversion, if they dont
  423. then we are trying to intermix diversions - very bad */
  424. if (ToN->Pointer != 0 && ToN->Pointer != Diver)
  425. {
  426. // It could be that the other diversion is no longer in use
  427. if ((DiverP[ToN->Pointer].Flags & Diversion::Touched) == Diversion::Touched)
  428. return _error->Error(_("Trying to overwrite a diversion, %s -> %s and %s/%s"),
  429. From,To,ToN.File(),ToN.Dir().Name());
  430. // We can erase it.
  431. Diversion *Div = DiverP + ToN->Pointer;
  432. ToN->Pointer = 0;
  433. if (Div->DivertTo == ToN.Offset())
  434. Div->DivertTo = 0;
  435. if (Div->DivertFrom == ToN.Offset())
  436. Div->DivertFrom = 0;
  437. // This diversion will be cleaned up by FinishDiverLoad
  438. }
  439. // Allocate a new diversion
  440. if (Diver == 0)
  441. {
  442. Diver = Map.Allocate(sizeof(Diversion));
  443. if (Diver == 0)
  444. return false;
  445. DiverP[Diver].Next = HeaderP->Diversions;
  446. HeaderP->Diversions = Diver;
  447. HeaderP->DiversionCount++;
  448. }
  449. // Can only have one diversion of the same files
  450. Diversion *Div = DiverP + Diver;
  451. if ((Div->Flags & Diversion::Touched) == Diversion::Touched)
  452. return _error->Error(_("Double add of diversion %s -> %s"),From,To);
  453. // Setup the From/To links
  454. if (Div->DivertFrom != FromN.Offset() && Div->DivertFrom != ToN.Offset())
  455. DropNode(Div->DivertFrom);
  456. Div->DivertFrom = FromN.Offset();
  457. if (Div->DivertTo != FromN.Offset() && Div->DivertTo != ToN.Offset())
  458. DropNode(Div->DivertTo);
  459. Div->DivertTo = ToN.Offset();
  460. // Link it to the two nodes
  461. FromN->Pointer = Diver;
  462. ToN->Pointer = Diver;
  463. // And the package
  464. Div->OwnerPkg = Owner.Offset();
  465. Div->Flags |= Diversion::Touched;
  466. return true;
  467. }
  468. /*}}}*/
  469. // FLCache::AddConfFile - Add a new configuration file /*{{{*/
  470. // ---------------------------------------------------------------------
  471. /* This simply adds a new conf file node to the hash table. This is only
  472. used by the status file reader. It associates a hash with each conf
  473. file entry that exists in the status file and the list file for
  474. the proper package. Duplicate conf files (across packages) are left
  475. up to other routines to deal with. */
  476. bool pkgFLCache::AddConfFile(const char *Name,const char *NameEnd,
  477. PkgIterator const &Owner,
  478. const unsigned char *Sum)
  479. {
  480. NodeIterator Nde = GetNode(Name,NameEnd,0,false,false);
  481. if (Nde.end() == true)
  482. return true;
  483. unsigned long File = Nde->File;
  484. for (; Nde->File == File && Nde.end() == false; Nde++)
  485. {
  486. if (Nde.RealPackage() != Owner)
  487. continue;
  488. if ((Nde->Flags & Node::ConfFile) == Node::ConfFile)
  489. return _error->Error(_("Duplicate conf file %s/%s"),Nde.DirN(),Nde.File());
  490. // Allocate a new conf file structure
  491. map_ptrloc Conf = Map.Allocate(sizeof(ConfFile));
  492. if (Conf == 0)
  493. return false;
  494. ConfP[Conf].OwnerPkg = Owner.Offset();
  495. memcpy(ConfP[Conf].MD5,Sum,sizeof(ConfP[Conf].MD5));
  496. Nde->Pointer = Conf;
  497. Nde->Flags |= Node::ConfFile;
  498. return true;
  499. }
  500. /* This means the conf file has been replaced, but the entry in the
  501. status file was not updated */
  502. return true;
  503. }
  504. /*}}}*/
  505. // NodeIterator::RealPackage - Return the package for this node /*{{{*/
  506. // ---------------------------------------------------------------------
  507. /* Since the package pointer is indirected in all sorts of interesting ways
  508. this is used to get a pointer to the owning package */
  509. pkgFLCache::Package *pkgFLCache::NodeIterator::RealPackage() const
  510. {
  511. if (Nde->Pointer == 0)
  512. return 0;
  513. if ((Nde->Flags & Node::ConfFile) == Node::ConfFile)
  514. return Owner->PkgP + Owner->ConfP[Nde->Pointer].OwnerPkg;
  515. // Diversions are ignored
  516. if ((Nde->Flags & Node::Diversion) == Node::Diversion)
  517. return 0;
  518. return Owner->PkgP + Nde->Pointer;
  519. }
  520. /*}}}*/