filelist.cc 18 KB

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