]>
git.saurik.com Git - apt.git/blob - ftparchive/contents.cc
eadced626125cfb574d0dca5544f8412d769131d
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 /*{{{*/
39 #include <apt-pkg/debfile.h>
40 #include <apt-pkg/extracttar.h>
41 #include <apt-pkg/error.h>
48 // GenContents::~GenContents - Free allocated memory /*{{{*/
49 // ---------------------------------------------------------------------
50 /* Since all our allocations are static big-block allocations all that is
51 needed is to free all of them. */
52 GenContents::~GenContents()
54 while (BlockList
!= 0)
56 BigBlock
*Old
= BlockList
;
57 BlockList
= Old
->Next
;
63 // GenContents::Mystrdup - Custom strdup /*{{{*/
64 // ---------------------------------------------------------------------
65 /* This strdup also uses a large block allocator to eliminate glibc
67 char *GenContents::Mystrdup(const char *From
)
69 unsigned int Len
= strlen(From
) + 1;
73 StrPool
= (char *)malloc(StrLeft
);
75 BigBlock
*Block
= new BigBlock
;
76 Block
->Block
= StrPool
;
77 Block
->Next
= BlockList
;
81 memcpy(StrPool
,From
,Len
);
89 // GenContents::Node::operator new - Big block allocator /*{{{*/
90 // ---------------------------------------------------------------------
91 /* This eliminates glibc's malloc overhead by allocating large blocks and
92 having a continuous set of Nodes. This takes about 8 bytes off each nodes
93 space needs. Freeing is not supported. */
94 void *GenContents::Node::operator new(size_t Amount
,GenContents
*Owner
)
96 if (Owner
->NodeLeft
== 0)
98 Owner
->NodeLeft
= 10000;
99 Owner
->NodePool
= (Node
*)malloc(Amount
*Owner
->NodeLeft
);
100 BigBlock
*Block
= new BigBlock
;
101 Block
->Block
= Owner
->NodePool
;
102 Block
->Next
= Owner
->BlockList
;
103 Owner
->BlockList
= Block
;
107 return Owner
->NodePool
++;
110 // GenContents::Grab - Grab a new node representing Name under Top /*{{{*/
111 // ---------------------------------------------------------------------
112 /* This grabs a new node representing the pathname component Name under
113 the node Top. The node is given the name Package. It is assumed that Name
114 is inside of top. If a duplicate already entered name is found then
115 a note is made on the Dup list and the previous in-tree node is returned. */
116 GenContents::Node
*GenContents::Grab(GenContents::Node
*Top
,const char *Name
,
119 /* We drop down to the next dir level each call. This simplifies
120 the calling routine */
121 if (Top
->DirDown
== 0)
123 Node
*Item
= new(this) Node
;
124 Item
->Path
= Mystrdup(Name
);
125 Item
->Package
= Package
;
134 Res
= strcmp(Name
,Top
->Path
);
139 // See if this the the same package (multi-version dup)
140 if (Top
->Package
== Package
||
141 strcasecmp(Top
->Package
,Package
) == 0)
144 // Look for an already existing Dup
145 for (Node
*I
= Top
->Dups
; I
!= 0; I
= I
->Dups
)
146 if (I
->Package
== Package
||
147 strcasecmp(I
->Package
,Package
) == 0)
151 Node
*Item
= new(this) Node
;
152 Item
->Path
= Top
->Path
;
153 Item
->Package
= Package
;
154 Item
->Dups
= Top
->Dups
;
159 // Continue to traverse the tree
162 if (Top
->BTreeLeft
== 0)
164 Top
= Top
->BTreeLeft
;
168 if (Top
->BTreeRight
== 0)
170 Top
= Top
->BTreeRight
;
174 // The item was not found in the tree
175 Node
*Item
= new(this) Node
;
176 Item
->Path
= Mystrdup(Name
);
177 Item
->Package
= Package
;
179 // Link it into the tree
182 Item
->BTreeLeft
= Top
->BTreeLeft
;
183 Top
->BTreeLeft
= Item
;
187 Item
->BTreeRight
= Top
->BTreeRight
;
188 Top
->BTreeRight
= Item
;
194 // GenContents::Add - Add a path to the tree /*{{{*/
195 // ---------------------------------------------------------------------
196 /* This takes a full pathname and adds it into the tree. We split the
197 pathname into directory fragments adding each one as we go. Technically
198 in output from tar this should result in hitting previous items. */
199 void GenContents::Add(const char *Dir
,const char *Package
)
201 Node
*Root
= &this->Root
;
203 // Drop leading slashes
204 while (*Dir
== '/' && *Dir
!= 0)
207 // Run over the string and grab out each bit up to and including a /
208 const char *Start
= Dir
;
212 if (*I
!= '/' || I
- Start
<= 1)
219 // Copy the path fragment over
221 strncpy(Tmp
,Start
,I
- Start
);
224 // Grab a node for it
225 Root
= Grab(Root
,Tmp
,Package
);
230 // The final component if it does not have a trailing /
232 Root
= Grab(Root
,Start
,Package
);
235 // GenContents::WriteSpace - Write a given number of white space chars /*{{{*/
236 // ---------------------------------------------------------------------
237 /* We mod 8 it and write tabs where possible. */
238 void GenContents::WriteSpace(FILE *Out
,unsigned int Current
,unsigned int Target
)
240 if (Target
<= Current
)
241 Target
= Current
+ 1;
243 /* Now we write tabs so long as the next tab stop would not pass
245 for (; (Current
/8 + 1)*8 < Target
; Current
= (Current
/8 + 1)*8)
248 // Fill the last bit with spaces
249 for (; Current
< Target
; Current
++)
253 // GenContents::Print - Display the tree /*{{{*/
254 // ---------------------------------------------------------------------
255 /* This is the final result function. It takes the tree and recursively
256 calls itself and runs over each section of the tree printing out
257 the pathname and the hit packages. We use Buf to build the pathname
258 summed over all the directory parents of this node. */
259 void GenContents::Print(FILE *Out
)
263 DoPrint(Out
,&Root
,Buffer
);
265 void GenContents::DoPrint(FILE *Out
,GenContents::Node
*Top
, char *Buf
)
271 DoPrint(Out
,Top
->BTreeLeft
,Buf
);
273 // Print the current dir location and then descend to lower dirs
274 char *OldEnd
= Buf
+ strlen(Buf
);
277 strcat(Buf
,Top
->Path
);
279 // Do not show the item if it is a directory with dups
280 if (Top
->Path
[strlen(Top
->Path
)-1] != '/' /*|| Top->Dups == 0*/)
283 WriteSpace(Out
,strlen(Buf
),60);
284 for (Node
*I
= Top
; I
!= 0; I
= I
->Dups
)
288 fputs(I
->Package
,Out
);
294 // Go along the directory link
295 DoPrint(Out
,Top
->DirDown
,Buf
);
299 DoPrint(Out
,Top
->BTreeRight
,Buf
);
303 // ContentsExtract::Read - Read the archive /*{{{*/
304 // ---------------------------------------------------------------------
306 bool ContentsExtract::Read(debDebFile
&Deb
)
309 return Deb
.ExtractArchive(*this);
312 // ContentsExtract::DoItem - Extract an item /*{{{*/
313 // ---------------------------------------------------------------------
314 /* This just tacks the name onto the end of our memory buffer */
315 bool ContentsExtract::DoItem(Item
&Itm
,int &Fd
)
317 unsigned long Len
= strlen(Itm
.Name
);
319 // Strip leading ./'s
320 if (Itm
.Name
[0] == '.' && Itm
.Name
[1] == '/')
330 // Allocate more storage for the string list
331 if (CurSize
+ Len
+ 2 >= MaxSize
|| Data
== 0)
334 MaxSize
= 512*1024/2;
335 char *NewData
= (char *)realloc(Data
,MaxSize
*2);
337 return _error
->Error(_("realloc - Failed to allocate memory"));
342 strcpy(Data
+CurSize
,Itm
.Name
);
347 // ContentsExtract::TakeContents - Load the contents data /*{{{*/
348 // ---------------------------------------------------------------------
350 bool ContentsExtract::TakeContents(const void *NewData
,unsigned long Length
)
358 // Allocate more storage for the string list
359 if (Length
+ 2 >= MaxSize
|| Data
== 0)
362 MaxSize
= 512*1024/2;
363 while (MaxSize
*2 <= Length
)
366 char *NewData
= (char *)realloc(Data
,MaxSize
*2);
368 return _error
->Error(_("realloc - Failed to allocate memory"));
372 memcpy(Data
,NewData
,Length
);
375 return Data
[CurSize
-1] == 0;
378 // ContentsExtract::Add - Read the contents data into the sorter /*{{{*/
379 // ---------------------------------------------------------------------
381 void ContentsExtract::Add(GenContents
&Contents
,string
const &Package
)
383 const char *Start
= Data
;
384 char *Pkg
= Contents
.Mystrdup(Package
.c_str());
385 for (const char *I
= Data
; I
< Data
+ CurSize
; I
++)
389 Contents
.Add(Start
,Pkg
);