]>
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 // Start != End is a special case to not fail on empty TagFiles
102 if (Start
!= End
&& Tag
.Scan(Start
,End
- Start
) == false)
103 return _error
->Error(_("Unable to parse package file %s (2)"),Fd
.Name().c_str());
108 // TagSection::Scan - Scan for the end of the header information /*{{{*/
109 // ---------------------------------------------------------------------
110 /* This looks for the first double new line in the data stream. It also
111 indexes the tags in the section. This very simple hash function for the
112 first 3 letters gives very good performance on the debian package files */
113 inline static unsigned long AlphaHash(const char *Text
, const char *End
= 0)
115 unsigned long Res
= 0;
116 for (; Text
!= End
&& *Text
!= ':' && *Text
!= 0; Text
++)
117 Res
= (unsigned long)(*Text
) ^ (Res
<< 2);
121 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
123 const char *End
= Start
+ MaxLength
;
124 Stop
= Section
= Start
;
125 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
127 if (Stop
== 0 || MaxLength
== 0)
131 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
133 // Start a new index and add it to the hash
134 if (isspace(Stop
[0]) == 0)
136 Indexes
[TagCount
++] = Stop
- Section
;
137 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
140 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
145 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
147 // Double newline marks the end of the record
148 if (Stop
+1 < End
&& Stop
[1] == '\n')
150 Indexes
[TagCount
] = Stop
- Section
;
151 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
158 if ((Stop
+1 >= End
) && (End
[-1] == '\n' || End
[-1] == '\r'))
160 Indexes
[TagCount
] = (End
- 1) - Section
;
167 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
168 // ---------------------------------------------------------------------
169 /* There should be exactly 1 newline at the end of the buffer, no more. */
170 void pkgTagSection::Trim()
172 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
175 // TagSection::Find - Locate a tag /*{{{*/
176 // ---------------------------------------------------------------------
177 /* This searches the section for a tag that matches the given string. */
178 bool pkgTagSection::Find(const char *Tag
,unsigned &Pos
) const
180 unsigned int Length
= strlen(Tag
);
181 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
186 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
190 St
= Section
+ Indexes
[I
];
191 if (strncasecmp(Tag
,St
,Length
) != 0)
194 // Make sure the colon is in the right place
195 const char *C
= St
+ Length
;
196 for (; isspace(*C
) != 0; C
++);
207 // TagSection::Find - Locate a tag /*{{{*/
208 // ---------------------------------------------------------------------
209 /* This searches the section for a tag that matches the given string. */
210 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
211 const char *&End
) const
213 unsigned int Length
= strlen(Tag
);
214 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
219 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
223 St
= Section
+ Indexes
[I
];
224 if (strncasecmp(Tag
,St
,Length
) != 0)
227 // Make sure the colon is in the right place
228 const char *C
= St
+ Length
;
229 for (; isspace(*C
) != 0; C
++);
233 // Strip off the gunk from the start end
235 End
= Section
+ Indexes
[I
+1];
237 return _error
->Error("Internal parsing error");
239 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
240 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
249 // TagSection::FindS - Find a string /*{{{*/
250 // ---------------------------------------------------------------------
252 string
pkgTagSection::FindS(const char *Tag
) const
256 if (Find(Tag
,Start
,End
) == false)
258 return string(Start
,End
);
261 // TagSection::FindI - Find an integer /*{{{*/
262 // ---------------------------------------------------------------------
264 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
268 if (Find(Tag
,Start
,Stop
) == false)
271 // Copy it into a temp buffer so we can use strtol
273 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
275 strncpy(S
,Start
,Stop
-Start
);
279 signed long Result
= strtol(S
,&End
,10);
285 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
286 // ---------------------------------------------------------------------
287 /* The bits marked in Flag are masked on/off in Flags */
288 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
289 unsigned long Flag
) const
293 if (Find(Tag
,Start
,Stop
) == false)
296 switch (StringToBool(string(Start
,Stop
)))
307 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
314 // TFRewrite - Rewrite a control record /*{{{*/
315 // ---------------------------------------------------------------------
316 /* This writes the control record to stdout rewriting it as necessary. The
317 override map item specificies the rewriting rules to follow. This also
318 takes the time to sort the feild list. */
320 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
322 static const char *iTFRewritePackageOrder
[] = {
333 "Revision", // Obsolete
334 "Config-Version", // Obsolete
348 "MSDOS-Filename", // Obsolete
351 static const char *iTFRewriteSourceOrder
[] = {"Package",
359 "Build-Depends-Indep",
361 "Build-Conflicts-Indep",
369 /* Two levels of initialization are used because gcc will set the symbol
370 size of an array to the length of the array, causing dynamic relinking
371 errors. Doing this makes the symbol size constant */
372 const char **TFRewritePackageOrder
= iTFRewritePackageOrder
;
373 const char **TFRewriteSourceOrder
= iTFRewriteSourceOrder
;
375 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
376 TFRewriteData
*Rewrite
)
378 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
379 for (unsigned I
= 0; I
!= 256; I
++)
382 // Set new tag up as necessary.
383 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
385 if (Rewrite
[J
].NewTag
== 0)
386 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
389 // Write all all of the tags, in order.
390 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
392 bool Rewritten
= false;
394 // See if this is a field that needs to be rewritten
395 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
397 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
400 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
402 if (isspace(Rewrite
[J
].Rewrite
[0]))
403 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
405 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
413 // See if it is in the fragment
415 if (Tags
.Find(Order
[I
],Pos
) == false)
419 if (Rewritten
== true)
422 /* Write out this element, taking a moment to rewrite the tag
423 in case of changes of case. */
426 Tags
.Get(Start
,Stop
,Pos
);
428 if (fputs(Order
[I
],Output
) < 0)
429 return _error
->Errno("fputs","IO Error to output");
430 Start
+= strlen(Order
[I
]);
431 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
432 return _error
->Errno("fwrite","IO Error to output");
433 if (Stop
[-1] != '\n')
434 fprintf(Output
,"\n");
437 // Now write all the old tags that were missed.
438 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
440 if ((Visited
[I
] & 1) == 1)
445 Tags
.Get(Start
,Stop
,I
);
446 const char *End
= Start
;
447 for (; End
< Stop
&& *End
!= ':'; End
++);
449 // See if this is a field that needs to be rewritten
450 bool Rewritten
= false;
451 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
453 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
456 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
458 if (isspace(Rewrite
[J
].Rewrite
[0]))
459 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
461 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
469 if (Rewritten
== true)
472 // Write out this element
473 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
474 return _error
->Errno("fwrite","IO Error to output");
475 if (Stop
[-1] != '\n')
476 fprintf(Output
,"\n");
479 // Now write all the rewrites that were missed
480 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
482 if ((Visited
[J
] & 2) == 2)
485 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
487 if (isspace(Rewrite
[J
].Rewrite
[0]))
488 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
490 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);