contents.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: contents.cc,v 1.4 2003/02/10 07:34:41 doogie Exp $
  4. /* ######################################################################
  5. contents - Archive contents generator
  6. The GenContents class is a back end for an archive contents generator.
  7. It takes a list of per-deb file name and merges it into a memory
  8. database of all previous output. This database is stored as a set
  9. of binary trees linked across directories to form a tree of all files+dirs
  10. given to it. The tree will also be sorted as it is built up thus
  11. removing the massive sort time overhead.
  12. By breaking all the pathnames into components and storing them
  13. separately a space saving is realized by not duplicating the string
  14. over and over again. Ultimately this saving is sacrificed to storage of
  15. the tree structure itself but the tree structure yields a speed gain
  16. in the sorting and processing. Ultimately it takes about 5 seconds to
  17. do 141000 nodes and about 5 meg of ram.
  18. The tree looks something like:
  19. usr/
  20. / \ / libslang
  21. bin/ lib/ --> libc6
  22. / \ \ libfoo
  23. games/ sbin/
  24. The ---> is the DirDown link
  25. ##################################################################### */
  26. /*}}}*/
  27. // Include Files /*{{{*/
  28. #include <config.h>
  29. #include <apt-pkg/debfile.h>
  30. #include <apt-pkg/dirstream.h>
  31. #include <apt-pkg/error.h>
  32. #include <apt-pkg/fileutl.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "contents.h"
  37. #include <apti18n.h>
  38. /*}}}*/
  39. // GenContents::~GenContents - Free allocated memory /*{{{*/
  40. // ---------------------------------------------------------------------
  41. /* Since all our allocations are static big-block allocations all that is
  42. needed is to free all of them. */
  43. GenContents::~GenContents()
  44. {
  45. while (BlockList != 0)
  46. {
  47. BigBlock *Old = BlockList;
  48. BlockList = Old->Next;
  49. free(Old->Block);
  50. delete Old;
  51. }
  52. }
  53. /*}}}*/
  54. // GenContents::Mystrdup - Custom strdup /*{{{*/
  55. // ---------------------------------------------------------------------
  56. /* This strdup also uses a large block allocator to eliminate glibc
  57. overhead */
  58. char *GenContents::Mystrdup(const char *From)
  59. {
  60. unsigned int Len = strlen(From) + 1;
  61. if (StrLeft <= Len)
  62. {
  63. StrLeft = 4096*10;
  64. StrPool = (char *)malloc(StrLeft);
  65. BigBlock *Block = new BigBlock;
  66. Block->Block = StrPool;
  67. Block->Next = BlockList;
  68. BlockList = Block;
  69. }
  70. memcpy(StrPool,From,Len);
  71. StrLeft -= Len;
  72. char *Res = StrPool;
  73. StrPool += Len;
  74. return Res;
  75. }
  76. /*}}}*/
  77. // GenContents::Node::operator new - Big block allocator /*{{{*/
  78. // ---------------------------------------------------------------------
  79. /* This eliminates glibc's malloc overhead by allocating large blocks and
  80. having a continuous set of Nodes. This takes about 8 bytes off each nodes
  81. space needs. Freeing is not supported. */
  82. void *GenContents::Node::operator new(size_t Amount,GenContents *Owner)
  83. {
  84. if (Owner->NodeLeft == 0)
  85. {
  86. Owner->NodeLeft = 10000;
  87. Owner->NodePool = static_cast<Node *>(malloc(Amount*Owner->NodeLeft));
  88. BigBlock *Block = new BigBlock;
  89. Block->Block = Owner->NodePool;
  90. Block->Next = Owner->BlockList;
  91. Owner->BlockList = Block;
  92. }
  93. Owner->NodeLeft--;
  94. return Owner->NodePool++;
  95. }
  96. /*}}}*/
  97. // GenContents::Grab - Grab a new node representing Name under Top /*{{{*/
  98. // ---------------------------------------------------------------------
  99. /* This grabs a new node representing the pathname component Name under
  100. the node Top. The node is given the name Package. It is assumed that Name
  101. is inside of top. If a duplicate already entered name is found then
  102. a note is made on the Dup list and the previous in-tree node is returned. */
  103. GenContents::Node *GenContents::Grab(GenContents::Node *Top,const char *Name,
  104. const char *Package)
  105. {
  106. /* We drop down to the next dir level each call. This simplifies
  107. the calling routine */
  108. if (Top->DirDown == 0)
  109. {
  110. Node *Item = new(this) Node;
  111. Item->Path = Mystrdup(Name);
  112. Item->Package = Package;
  113. Top->DirDown = Item;
  114. return Item;
  115. }
  116. Top = Top->DirDown;
  117. int Res;
  118. while (1)
  119. {
  120. Res = strcmp(Name,Top->Path);
  121. // Collision!
  122. if (Res == 0)
  123. {
  124. // See if this the the same package (multi-version dup)
  125. if (Top->Package == Package ||
  126. strcasecmp(Top->Package,Package) == 0)
  127. return Top;
  128. // Look for an already existing Dup
  129. for (Node *I = Top->Dups; I != 0; I = I->Dups)
  130. if (I->Package == Package ||
  131. strcasecmp(I->Package,Package) == 0)
  132. return Top;
  133. // Add the dup in
  134. Node *Item = new(this) Node;
  135. Item->Path = Top->Path;
  136. Item->Package = Package;
  137. Item->Dups = Top->Dups;
  138. Top->Dups = Item;
  139. return Top;
  140. }
  141. // Continue to traverse the tree
  142. if (Res < 0)
  143. {
  144. if (Top->BTreeLeft == 0)
  145. break;
  146. Top = Top->BTreeLeft;
  147. }
  148. else
  149. {
  150. if (Top->BTreeRight == 0)
  151. break;
  152. Top = Top->BTreeRight;
  153. }
  154. }
  155. // The item was not found in the tree
  156. Node *Item = new(this) Node;
  157. Item->Path = Mystrdup(Name);
  158. Item->Package = Package;
  159. // Link it into the tree
  160. if (Res < 0)
  161. {
  162. Item->BTreeLeft = Top->BTreeLeft;
  163. Top->BTreeLeft = Item;
  164. }
  165. else
  166. {
  167. Item->BTreeRight = Top->BTreeRight;
  168. Top->BTreeRight = Item;
  169. }
  170. return Item;
  171. }
  172. /*}}}*/
  173. // GenContents::Add - Add a path to the tree /*{{{*/
  174. // ---------------------------------------------------------------------
  175. /* This takes a full pathname and adds it into the tree. We split the
  176. pathname into directory fragments adding each one as we go. Technically
  177. in output from tar this should result in hitting previous items. */
  178. void GenContents::Add(const char *Dir,const char *Package)
  179. {
  180. Node *Root = &this->Root;
  181. // Drop leading slashes
  182. while (*Dir == '/' && *Dir != 0)
  183. Dir++;
  184. // Run over the string and grab out each bit up to and including a /
  185. const char *Start = Dir;
  186. const char *I = Dir;
  187. while (*I != 0)
  188. {
  189. if (*I != '/' || I - Start <= 1)
  190. {
  191. I++;
  192. continue;
  193. }
  194. I++;
  195. // Copy the path fragment over
  196. char Tmp[1024];
  197. strncpy(Tmp,Start,I - Start);
  198. Tmp[I - Start] = 0;
  199. // Grab a node for it
  200. Root = Grab(Root,Tmp,Package);
  201. Start = I;
  202. }
  203. // The final component if it does not have a trailing /
  204. if (I - Start >= 1)
  205. Grab(Root,Start,Package);
  206. }
  207. /*}}}*/
  208. // GenContents::WriteSpace - Write a given number of white space chars /*{{{*/
  209. // ---------------------------------------------------------------------
  210. /* We mod 8 it and write tabs where possible. */
  211. void GenContents::WriteSpace(std::string &out, size_t Current, size_t Target)
  212. {
  213. if (Target <= Current)
  214. Target = Current + 1;
  215. /* Now we write tabs so long as the next tab stop would not pass
  216. the target */
  217. for (; (Current/8 + 1)*8 < Target; Current = (Current/8 + 1)*8)
  218. out.append("\t");
  219. // Fill the last bit with spaces
  220. for (; Current < Target; Current++)
  221. out.append(" ");
  222. }
  223. /*}}}*/
  224. // GenContents::Print - Display the tree /*{{{*/
  225. // ---------------------------------------------------------------------
  226. /* This is the final result function. It takes the tree and recursively
  227. calls itself and runs over each section of the tree printing out
  228. the pathname and the hit packages. We use Buf to build the pathname
  229. summed over all the directory parents of this node. */
  230. void GenContents::Print(FileFd &Out)
  231. {
  232. char Buffer[1024];
  233. Buffer[0] = 0;
  234. DoPrint(Out,&Root,Buffer);
  235. }
  236. void GenContents::DoPrint(FileFd &Out,GenContents::Node *Top, char *Buf)
  237. {
  238. if (Top == 0)
  239. return;
  240. // Go left
  241. DoPrint(Out,Top->BTreeLeft,Buf);
  242. // Print the current dir location and then descend to lower dirs
  243. char *OldEnd = Buf + strlen(Buf);
  244. if (Top->Path != 0)
  245. {
  246. strcat(Buf,Top->Path);
  247. // Do not show the item if it is a directory with dups
  248. if (Top->Path[strlen(Top->Path)-1] != '/' /*|| Top->Dups == 0*/)
  249. {
  250. std::string out = Buf;
  251. WriteSpace(out, out.length(), 60);
  252. for (Node *I = Top; I != 0; I = I->Dups)
  253. {
  254. if (I != Top)
  255. out.append(",");
  256. out.append(I->Package);
  257. }
  258. out.append("\n");
  259. Out.Write(out.c_str(), out.length());
  260. }
  261. }
  262. // Go along the directory link
  263. DoPrint(Out,Top->DirDown,Buf);
  264. *OldEnd = 0;
  265. // Go right
  266. DoPrint(Out,Top->BTreeRight,Buf);
  267. }
  268. /*}}}*/
  269. // ContentsExtract Constructor /*{{{*/
  270. ContentsExtract::ContentsExtract()
  271. : Data(0), MaxSize(0), CurSize(0)
  272. {
  273. }
  274. /*}}}*/
  275. // ContentsExtract Destructor /*{{{*/
  276. ContentsExtract::~ContentsExtract()
  277. {
  278. free(Data);
  279. }
  280. /*}}}*/
  281. // ContentsExtract::Read - Read the archive /*{{{*/
  282. // ---------------------------------------------------------------------
  283. /* */
  284. bool ContentsExtract::Read(debDebFile &Deb)
  285. {
  286. Reset();
  287. return Deb.ExtractArchive(*this);
  288. }
  289. /*}}}*/
  290. // ContentsExtract::DoItem - Extract an item /*{{{*/
  291. // ---------------------------------------------------------------------
  292. /* This just tacks the name onto the end of our memory buffer */
  293. bool ContentsExtract::DoItem(Item &Itm, int &/*Fd*/)
  294. {
  295. unsigned long Len = strlen(Itm.Name);
  296. // Strip leading ./'s
  297. if (Itm.Name[0] == '.' && Itm.Name[1] == '/')
  298. {
  299. // == './'
  300. if (Len == 2)
  301. return true;
  302. Len -= 2;
  303. Itm.Name += 2;
  304. }
  305. // Allocate more storage for the string list
  306. if (CurSize + Len + 2 >= MaxSize || Data == 0)
  307. {
  308. if (MaxSize == 0)
  309. MaxSize = 512*1024/2;
  310. char *NewData = (char *)realloc(Data,MaxSize*2);
  311. if (NewData == 0)
  312. return _error->Error(_("realloc - Failed to allocate memory"));
  313. Data = NewData;
  314. MaxSize *= 2;
  315. }
  316. strcpy(Data+CurSize,Itm.Name);
  317. CurSize += Len + 1;
  318. return true;
  319. }
  320. /*}}}*/
  321. // ContentsExtract::TakeContents - Load the contents data /*{{{*/
  322. // ---------------------------------------------------------------------
  323. /* */
  324. bool ContentsExtract::TakeContents(const void *NewData,unsigned long long Length)
  325. {
  326. if (Length == 0)
  327. {
  328. CurSize = 0;
  329. return true;
  330. }
  331. // Allocate more storage for the string list
  332. if (Length + 2 >= MaxSize || Data == 0)
  333. {
  334. if (MaxSize == 0)
  335. MaxSize = 512*1024/2;
  336. while (MaxSize*2 <= Length)
  337. MaxSize *= 2;
  338. char *NewData = (char *)realloc(Data,MaxSize*2);
  339. if (NewData == 0)
  340. return _error->Error(_("realloc - Failed to allocate memory"));
  341. Data = NewData;
  342. MaxSize *= 2;
  343. }
  344. memcpy(Data,NewData,Length);
  345. CurSize = Length;
  346. return Data[CurSize-1] == 0;
  347. }
  348. /*}}}*/
  349. // ContentsExtract::Add - Read the contents data into the sorter /*{{{*/
  350. // ---------------------------------------------------------------------
  351. /* */
  352. void ContentsExtract::Add(GenContents &Contents,std::string const &Package)
  353. {
  354. const char *Start = Data;
  355. char *Pkg = Contents.Mystrdup(Package.c_str());
  356. for (const char *I = Data; I < Data + CurSize; I++)
  357. {
  358. if (*I == 0)
  359. {
  360. Contents.Add(Start,Pkg);
  361. Start = ++I;
  362. }
  363. }
  364. }
  365. /*}}}*/