filelist.cc 18 KB

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