]>
git.saurik.com Git - apt.git/blob - ftparchive/contents.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: contents.cc,v 1.4 2003/02/10 07:34:41 doogie Exp $
4 /* ######################################################################
6 contents - Archive contents generator
8 The GenContents class is a back end for an archive contents generator.
9 It takes a list of per-deb file name and merges it into a memory
10 database of all previous output. This database is stored as a set
11 of binary trees linked across directories to form a tree of all files+dirs
12 given to it. The tree will also be sorted as it is built up thus
13 removing the massive sort time overhead.
15 By breaking all the pathnames into components and storing them
16 separately a space saving is realized by not duplicating the string
17 over and over again. Ultimately this saving is sacrificed to storage of
18 the tree structure itself but the tree structure yields a speed gain
19 in the sorting and processing. Ultimately it takes about 5 seconds to
20 do 141000 nodes and about 5 meg of ram.
22 The tree looks something like:
30 The ---> is the DirDown link
33 ##################################################################### */
35 // Include Files /*{{{*/
38 #include <apt-pkg/debfile.h>
39 #include <apt-pkg/extracttar.h>
40 #include <apt-pkg/error.h>
50 // GenContents::~GenContents - Free allocated memory /*{{{*/
51 // ---------------------------------------------------------------------
52 /* Since all our allocations are static big-block allocations all that is
53 needed is to free all of them. */
54 GenContents::~GenContents()
56 while (BlockList
!= 0)
58 BigBlock
*Old
= BlockList
;
59 BlockList
= Old
->Next
;
65 // GenContents::Mystrdup - Custom strdup /*{{{*/
66 // ---------------------------------------------------------------------
67 /* This strdup also uses a large block allocator to eliminate glibc
69 char *GenContents::Mystrdup(const char *From
)
71 unsigned int Len
= strlen(From
) + 1;
75 StrPool
= (char *)malloc(StrLeft
);
77 BigBlock
*Block
= new BigBlock
;
78 Block
->Block
= StrPool
;
79 Block
->Next
= BlockList
;
83 memcpy(StrPool
,From
,Len
);
91 // GenContents::Node::operator new - Big block allocator /*{{{*/
92 // ---------------------------------------------------------------------
93 /* This eliminates glibc's malloc overhead by allocating large blocks and
94 having a continuous set of Nodes. This takes about 8 bytes off each nodes
95 space needs. Freeing is not supported. */
96 void *GenContents::Node::operator new(size_t Amount
,GenContents
*Owner
)
98 if (Owner
->NodeLeft
== 0)
100 Owner
->NodeLeft
= 10000;
101 Owner
->NodePool
= (Node
*)malloc(Amount
*Owner
->NodeLeft
);
102 BigBlock
*Block
= new BigBlock
;
103 Block
->Block
= Owner
->NodePool
;
104 Block
->Next
= Owner
->BlockList
;
105 Owner
->BlockList
= Block
;
109 return Owner
->NodePool
++;
112 // GenContents::Grab - Grab a new node representing Name under Top /*{{{*/
113 // ---------------------------------------------------------------------
114 /* This grabs a new node representing the pathname component Name under
115 the node Top. The node is given the name Package. It is assumed that Name
116 is inside of top. If a duplicate already entered name is found then
117 a note is made on the Dup list and the previous in-tree node is returned. */
118 GenContents::Node
*GenContents::Grab(GenContents::Node
*Top
,const char *Name
,
121 /* We drop down to the next dir level each call. This simplifies
122 the calling routine */
123 if (Top
->DirDown
== 0)
125 Node
*Item
= new(this) Node
;
126 Item
->Path
= Mystrdup(Name
);
127 Item
->Package
= Package
;
136 Res
= strcmp(Name
,Top
->Path
);
141 // See if this the the same package (multi-version dup)
142 if (Top
->Package
== Package
||
143 strcasecmp(Top
->Package
,Package
) == 0)
146 // Look for an already existing Dup
147 for (Node
*I
= Top
->Dups
; I
!= 0; I
= I
->Dups
)
148 if (I
->Package
== Package
||
149 strcasecmp(I
->Package
,Package
) == 0)
153 Node
*Item
= new(this) Node
;
154 Item
->Path
= Top
->Path
;
155 Item
->Package
= Package
;
156 Item
->Dups
= Top
->Dups
;
161 // Continue to traverse the tree
164 if (Top
->BTreeLeft
== 0)
166 Top
= Top
->BTreeLeft
;
170 if (Top
->BTreeRight
== 0)
172 Top
= Top
->BTreeRight
;
176 // The item was not found in the tree
177 Node
*Item
= new(this) Node
;
178 Item
->Path
= Mystrdup(Name
);
179 Item
->Package
= Package
;
181 // Link it into the tree
184 Item
->BTreeLeft
= Top
->BTreeLeft
;
185 Top
->BTreeLeft
= Item
;
189 Item
->BTreeRight
= Top
->BTreeRight
;
190 Top
->BTreeRight
= Item
;
196 // GenContents::Add - Add a path to the tree /*{{{*/
197 // ---------------------------------------------------------------------
198 /* This takes a full pathname and adds it into the tree. We split the
199 pathname into directory fragments adding each one as we go. Technically
200 in output from tar this should result in hitting previous items. */
201 void GenContents::Add(const char *Dir
,const char *Package
)
203 Node
*Root
= &this->Root
;
205 // Drop leading slashes
206 while (*Dir
== '/' && *Dir
!= 0)
209 // Run over the string and grab out each bit up to and including a /
210 const char *Start
= Dir
;
214 if (*I
!= '/' || I
- Start
<= 1)
221 // Copy the path fragment over
223 strncpy(Tmp
,Start
,I
- Start
);
226 // Grab a node for it
227 Root
= Grab(Root
,Tmp
,Package
);
232 // The final component if it does not have a trailing /
234 Root
= Grab(Root
,Start
,Package
);
237 // GenContents::WriteSpace - Write a given number of white space chars /*{{{*/
238 // ---------------------------------------------------------------------
239 /* We mod 8 it and write tabs where possible. */
240 void GenContents::WriteSpace(FILE *Out
,unsigned int Current
,unsigned int Target
)
242 if (Target
<= Current
)
243 Target
= Current
+ 1;
245 /* Now we write tabs so long as the next tab stop would not pass
247 for (; (Current
/8 + 1)*8 < Target
; Current
= (Current
/8 + 1)*8)
250 // Fill the last bit with spaces
251 for (; Current
< Target
; Current
++)
255 // GenContents::Print - Display the tree /*{{{*/
256 // ---------------------------------------------------------------------
257 /* This is the final result function. It takes the tree and recursively
258 calls itself and runs over each section of the tree printing out
259 the pathname and the hit packages. We use Buf to build the pathname
260 summed over all the directory parents of this node. */
261 void GenContents::Print(FILE *Out
)
265 DoPrint(Out
,&Root
,Buffer
);
267 void GenContents::DoPrint(FILE *Out
,GenContents::Node
*Top
, char *Buf
)
273 DoPrint(Out
,Top
->BTreeLeft
,Buf
);
275 // Print the current dir location and then descend to lower dirs
276 char *OldEnd
= Buf
+ strlen(Buf
);
279 strcat(Buf
,Top
->Path
);
281 // Do not show the item if it is a directory with dups
282 if (Top
->Path
[strlen(Top
->Path
)-1] != '/' /*|| Top->Dups == 0*/)
285 WriteSpace(Out
,strlen(Buf
),60);
286 for (Node
*I
= Top
; I
!= 0; I
= I
->Dups
)
290 fputs(I
->Package
,Out
);
296 // Go along the directory link
297 DoPrint(Out
,Top
->DirDown
,Buf
);
301 DoPrint(Out
,Top
->BTreeRight
,Buf
);
305 // ContentsExtract::Read - Read the archive /*{{{*/
306 // ---------------------------------------------------------------------
308 bool ContentsExtract::Read(debDebFile
&Deb
)
311 return Deb
.ExtractArchive(*this);
314 // ContentsExtract::DoItem - Extract an item /*{{{*/
315 // ---------------------------------------------------------------------
316 /* This just tacks the name onto the end of our memory buffer */
317 bool ContentsExtract::DoItem(Item
&Itm
,int &Fd
)
319 unsigned long Len
= strlen(Itm
.Name
);
321 // Strip leading ./'s
322 if (Itm
.Name
[0] == '.' && Itm
.Name
[1] == '/')
332 // Allocate more storage for the string list
333 if (CurSize
+ Len
+ 2 >= MaxSize
|| Data
== 0)
336 MaxSize
= 512*1024/2;
337 char *NewData
= (char *)realloc(Data
,MaxSize
*2);
339 return _error
->Error(_("realloc - Failed to allocate memory"));
344 strcpy(Data
+CurSize
,Itm
.Name
);
349 // ContentsExtract::TakeContents - Load the contents data /*{{{*/
350 // ---------------------------------------------------------------------
352 bool ContentsExtract::TakeContents(const void *NewData
,unsigned long long Length
)
360 // Allocate more storage for the string list
361 if (Length
+ 2 >= MaxSize
|| Data
== 0)
364 MaxSize
= 512*1024/2;
365 while (MaxSize
*2 <= Length
)
368 char *NewData
= (char *)realloc(Data
,MaxSize
*2);
370 return _error
->Error(_("realloc - Failed to allocate memory"));
374 memcpy(Data
,NewData
,Length
);
377 return Data
[CurSize
-1] == 0;
380 // ContentsExtract::Add - Read the contents data into the sorter /*{{{*/
381 // ---------------------------------------------------------------------
383 void ContentsExtract::Add(GenContents
&Contents
,string
const &Package
)
385 const char *Start
= Data
;
386 char *Pkg
= Contents
.Mystrdup(Package
.c_str());
387 for (const char *I
= Data
; I
< Data
+ CurSize
; I
++)
391 Contents
.Add(Start
,Pkg
);