You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

416 lines
12 KiB

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