filelist.cc 18 KB

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