]>
git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz Exp $
4 /* ######################################################################
6 Fast scanner for RFC-822 type header information
8 This uses a rotating buffer to load the package information into.
9 The scanner runs over it and isolates and indexes a single section.
11 ##################################################################### */
13 // Include Files /*{{{*/
15 #pragma implementation "apt-pkg/tagfile.h"
18 #include <apt-pkg/tagfile.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/strutl.h>
31 // TagFile::pkgTagFile - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
34 pkgTagFile::pkgTagFile(FileFd
*pFd
,unsigned long Size
) :
38 if (Fd
.IsOpen() == false || Fd
.Size() == 0)
41 Start
= End
= Buffer
= 0;
47 Map
= new MMap (Fd
, MMap::Public
| MMap::ReadOnly
);
48 Buffer
= (char *) Map
->Data ();
50 End
= Buffer
+ Map
->Size ();
54 // TagFile::~pkgTagFile - Destructor /*{{{*/
55 // ---------------------------------------------------------------------
57 pkgTagFile::~pkgTagFile()
62 // TagFile::Step - Advance to the next section /*{{{*/
63 // ---------------------------------------------------------------------
64 /* If the Section Scanner fails we refill the buffer and try again. */
65 bool pkgTagFile::Step(pkgTagSection
&Tag
)
70 if (Tag
.Scan(Start
,End
- Start
) == false)
72 return _error
->Error(_("Unable to parse package file %s (1)"),
76 iOffset
+= Tag
.size();
82 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
83 // ---------------------------------------------------------------------
84 /* This jumps to a pre-recorded file location and reads the record
86 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long Offset
)
88 // We are within a buffer space of the next hit..
89 if (Offset
>= iOffset
&& iOffset
+ (End
- Start
) > Offset
)
91 unsigned long Dist
= Offset
- iOffset
;
97 // Reposition and reload..
99 Start
= Buffer
+ iOffset
;
101 if (Tag
.Scan(Start
,End
- Start
) == false)
102 return _error
->Error(_("Unable to parse package file %s (2)"),Fd
.Name().c_str());
107 // TagSection::Scan - Scan for the end of the header information /*{{{*/
108 // ---------------------------------------------------------------------
109 /* This looks for the first double new line in the data stream. It also
110 indexes the tags in the section. This very simple hash function for the
111 first 3 letters gives very good performance on the debian package files */
112 inline static unsigned long AlphaHash(const char *Text
, const char *End
= 0)
114 unsigned long Res
= 0;
115 for (; Text
!= End
&& *Text
!= ':' && *Text
!= 0; Text
++)
116 Res
= (unsigned long)(*Text
) ^ (Res
<< 2);
120 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
122 const char *End
= Start
+ MaxLength
;
123 Stop
= Section
= Start
;
124 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
126 if (Stop
== 0 || MaxLength
== 0)
130 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
132 // Start a new index and add it to the hash
133 if (isspace(Stop
[0]) == 0)
135 Indexes
[TagCount
++] = Stop
- Section
;
136 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
139 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
144 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
146 // Double newline marks the end of the record
147 if (Stop
+1 < End
&& Stop
[1] == '\n')
149 Indexes
[TagCount
] = Stop
- Section
;
150 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
157 if ((Stop
+1 >= End
) && (End
[-1] == '\n' || End
[-1] == '\r'))
159 Indexes
[TagCount
] = (End
- 1) - Section
;
166 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
167 // ---------------------------------------------------------------------
168 /* There should be exactly 1 newline at the end of the buffer, no more. */
169 void pkgTagSection::Trim()
171 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
174 // TagSection::Find - Locate a tag /*{{{*/
175 // ---------------------------------------------------------------------
176 /* This searches the section for a tag that matches the given string. */
177 bool pkgTagSection::Find(const char *Tag
,unsigned &Pos
) const
179 unsigned int Length
= strlen(Tag
);
180 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
185 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
189 St
= Section
+ Indexes
[I
];
190 if (strncasecmp(Tag
,St
,Length
) != 0)
193 // Make sure the colon is in the right place
194 const char *C
= St
+ Length
;
195 for (; isspace(*C
) != 0; C
++);
206 // TagSection::Find - Locate a tag /*{{{*/
207 // ---------------------------------------------------------------------
208 /* This searches the section for a tag that matches the given string. */
209 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
210 const char *&End
) const
212 unsigned int Length
= strlen(Tag
);
213 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
218 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
222 St
= Section
+ Indexes
[I
];
223 if (strncasecmp(Tag
,St
,Length
) != 0)
226 // Make sure the colon is in the right place
227 const char *C
= St
+ Length
;
228 for (; isspace(*C
) != 0; C
++);
232 // Strip off the gunk from the start end
234 End
= Section
+ Indexes
[I
+1];
236 return _error
->Error("Internal parsing error");
238 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
239 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
248 // TagSection::FindS - Find a string /*{{{*/
249 // ---------------------------------------------------------------------
251 string
pkgTagSection::FindS(const char *Tag
) const
255 if (Find(Tag
,Start
,End
) == false)
257 return string(Start
,End
);
260 // TagSection::FindI - Find an integer /*{{{*/
261 // ---------------------------------------------------------------------
263 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
267 if (Find(Tag
,Start
,Stop
) == false)
270 // Copy it into a temp buffer so we can use strtol
272 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
274 strncpy(S
,Start
,Stop
-Start
);
278 signed long Result
= strtol(S
,&End
,10);
284 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
285 // ---------------------------------------------------------------------
286 /* The bits marked in Flag are masked on/off in Flags */
287 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
288 unsigned long Flag
) const
292 if (Find(Tag
,Start
,Stop
) == false)
295 switch (StringToBool(string(Start
,Stop
)))
306 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
313 // TFRewrite - Rewrite a control record /*{{{*/
314 // ---------------------------------------------------------------------
315 /* This writes the control record to stdout rewriting it as necessary. The
316 override map item specificies the rewriting rules to follow. This also
317 takes the time to sort the feild list. */
319 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
321 static const char *iTFRewritePackageOrder
[] = {
332 "Revision", // Obsolete
333 "Config-Version", // Obsolete
346 "MSDOS-Filename", // Obsolete
349 static const char *iTFRewriteSourceOrder
[] = {"Package",
357 "Build-Depends-Indep",
359 "Build-Conflicts-Indep",
367 /* Two levels of initialization are used because gcc will set the symbol
368 size of an array to the length of the array, causing dynamic relinking
369 errors. Doing this makes the symbol size constant */
370 const char **TFRewritePackageOrder
= iTFRewritePackageOrder
;
371 const char **TFRewriteSourceOrder
= iTFRewriteSourceOrder
;
373 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
374 TFRewriteData
*Rewrite
)
376 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
377 for (unsigned I
= 0; I
!= 256; I
++)
380 // Set new tag up as necessary.
381 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
383 if (Rewrite
[J
].NewTag
== 0)
384 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
387 // Write all all of the tags, in order.
388 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
390 bool Rewritten
= false;
392 // See if this is a field that needs to be rewritten
393 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
395 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
398 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
400 if (isspace(Rewrite
[J
].Rewrite
[0]))
401 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
403 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
411 // See if it is in the fragment
413 if (Tags
.Find(Order
[I
],Pos
) == false)
417 if (Rewritten
== true)
420 /* Write out this element, taking a moment to rewrite the tag
421 in case of changes of case. */
424 Tags
.Get(Start
,Stop
,Pos
);
426 if (fputs(Order
[I
],Output
) < 0)
427 return _error
->Errno("fputs","IO Error to output");
428 Start
+= strlen(Order
[I
]);
429 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
430 return _error
->Errno("fwrite","IO Error to output");
431 if (Stop
[-1] != '\n')
432 fprintf(Output
,"\n");
435 // Now write all the old tags that were missed.
436 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
438 if ((Visited
[I
] & 1) == 1)
443 Tags
.Get(Start
,Stop
,I
);
444 const char *End
= Start
;
445 for (; End
< Stop
&& *End
!= ':'; End
++);
447 // See if this is a field that needs to be rewritten
448 bool Rewritten
= false;
449 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
451 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
454 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
456 if (isspace(Rewrite
[J
].Rewrite
[0]))
457 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
459 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
467 if (Rewritten
== true)
470 // Write out this element
471 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
472 return _error
->Errno("fwrite","IO Error to output");
473 if (Stop
[-1] != '\n')
474 fprintf(Output
,"\n");
477 // Now write all the rewrites that were missed
478 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
480 if ((Visited
[J
] & 2) == 2)
483 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
485 if (isspace(Rewrite
[J
].Rewrite
[0]))
486 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
488 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);