]>
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
) :
40 if (Fd
.IsOpen() == false)
42 Start
= End
= Buffer
= 0;
49 // check if we can MMap it
52 Buffer
= new char[Size
];
57 Map
= new MMap (Fd
, MMap::Public
| MMap::ReadOnly
);
58 Buffer
= (char *) Map
->Data ();
60 End
= Buffer
+ Map
->Size ();
65 // TagFile::~pkgTagFile - Destructor /*{{{*/
66 // ---------------------------------------------------------------------
68 pkgTagFile::~pkgTagFile()
70 if(!Map
) delete [] Buffer
;
74 // TagFile::Step - Advance to the next section /*{{{*/
75 // ---------------------------------------------------------------------
76 /* If the Section Scanner fails we refill the buffer and try again. */
77 bool pkgTagFile::Step(pkgTagSection
&Tag
)
79 if ((Map
!= NULL
) && (Start
== End
))
82 if (Tag
.Scan(Start
,End
- Start
) == false)
85 return _error
->Error(_("Unable to parse package file %s (1)"),
91 if (Tag
.Scan(Start
,End
- Start
) == false)
92 return _error
->Error(_("Unable to parse package file %s (1)"),
96 iOffset
+= Tag
.size();
102 // TagFile::Fill - Top up the buffer /*{{{*/
103 // ---------------------------------------------------------------------
104 /* This takes the bit at the end of the buffer and puts it at the start
105 then fills the rest from the file */
106 bool pkgTagFile::Fill()
108 unsigned long EndSize
= End
- Start
;
109 unsigned long Actual
= 0;
111 memmove(Buffer
,Start
,EndSize
);
113 End
= Buffer
+ EndSize
;
117 // See if only a bit of the file is left
118 if (Fd
.Read(End
,Size
- (End
- Buffer
),&Actual
) == false)
120 if (Actual
!= Size
- (End
- Buffer
))
127 if (EndSize
<= 3 && Actual
== 0)
129 if (Size
- (End
- Buffer
) < 4)
132 // Append a double new line if one does not exist
133 unsigned int LineCount
= 0;
134 for (const char *E
= End
- 1; E
- End
< 6 && (*E
== '\n' || *E
== '\r'); E
--)
137 for (; LineCount
< 2; LineCount
++)
146 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
147 // ---------------------------------------------------------------------
148 /* This jumps to a pre-recorded file location and reads the record
150 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long Offset
)
152 // We are within a buffer space of the next hit..
153 if (Offset
>= iOffset
&& iOffset
+ (End
- Start
) > Offset
)
155 unsigned long Dist
= Offset
- iOffset
;
164 Start
= Buffer
+ iOffset
;
168 // Reposition and reload..
170 if (Fd
.Seek(Offset
) == false)
172 End
= Start
= Buffer
;
177 if (Tag
.Scan(Start
,End
- Start
) == true)
180 // This appends a double new line (for the real eof handling)
185 if (Tag
.Scan(Start
,End
- Start
) == false)
186 return _error
->Error(_("Unable to parse package file %s (2)"),Fd
.Name().c_str());
191 // TagSection::Scan - Scan for the end of the header information /*{{{*/
192 // ---------------------------------------------------------------------
193 /* This looks for the first double new line in the data stream. It also
194 indexes the tags in the section. This very simple hash function for the
195 first 3 letters gives very good performance on the debian package files */
196 inline static unsigned long AlphaHash(const char *Text
, const char *End
= 0)
198 unsigned long Res
= 0;
199 for (; Text
!= End
&& *Text
!= ':' && *Text
!= 0; Text
++)
200 Res
= (unsigned long)(*Text
) ^ (Res
<< 2);
204 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
206 const char *End
= Start
+ MaxLength
;
207 Stop
= Section
= Start
;
208 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
210 if (Stop
== 0 || MaxLength
== 0)
214 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
216 // Start a new index and add it to the hash
217 if (isspace(Stop
[0]) == 0)
219 Indexes
[TagCount
++] = Stop
- Section
;
220 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
223 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
228 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
230 // Double newline marks the end of the record
231 if (Stop
+1 < End
&& Stop
[1] == '\n')
233 Indexes
[TagCount
] = Stop
- Section
;
234 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
241 if ((Stop
+1 >= End
) && (End
[-1] == '\n' || End
[-1] == '\r'))
243 Indexes
[TagCount
] = (End
- 1) - Section
;
250 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
251 // ---------------------------------------------------------------------
252 /* There should be exactly 1 newline at the end of the buffer, no more. */
253 void pkgTagSection::Trim()
255 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
258 // TagSection::Find - Locate a tag /*{{{*/
259 // ---------------------------------------------------------------------
260 /* This searches the section for a tag that matches the given string. */
261 bool pkgTagSection::Find(const char *Tag
,unsigned &Pos
) const
263 unsigned int Length
= strlen(Tag
);
264 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
269 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
273 St
= Section
+ Indexes
[I
];
274 if (strncasecmp(Tag
,St
,Length
) != 0)
277 // Make sure the colon is in the right place
278 const char *C
= St
+ Length
;
279 for (; isspace(*C
) != 0; C
++);
290 // TagSection::Find - Locate a tag /*{{{*/
291 // ---------------------------------------------------------------------
292 /* This searches the section for a tag that matches the given string. */
293 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
294 const char *&End
) const
296 unsigned int Length
= strlen(Tag
);
297 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
302 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
306 St
= Section
+ Indexes
[I
];
307 if (strncasecmp(Tag
,St
,Length
) != 0)
310 // Make sure the colon is in the right place
311 const char *C
= St
+ Length
;
312 for (; isspace(*C
) != 0; C
++);
316 // Strip off the gunk from the start end
318 End
= Section
+ Indexes
[I
+1];
320 return _error
->Error("Internal parsing error");
322 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
323 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
332 // TagSection::FindS - Find a string /*{{{*/
333 // ---------------------------------------------------------------------
335 string
pkgTagSection::FindS(const char *Tag
) const
339 if (Find(Tag
,Start
,End
) == false)
341 return string(Start
,End
);
344 // TagSection::FindI - Find an integer /*{{{*/
345 // ---------------------------------------------------------------------
347 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
351 if (Find(Tag
,Start
,Stop
) == false)
354 // Copy it into a temp buffer so we can use strtol
356 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
358 strncpy(S
,Start
,Stop
-Start
);
362 signed long Result
= strtol(S
,&End
,10);
368 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
369 // ---------------------------------------------------------------------
370 /* The bits marked in Flag are masked on/off in Flags */
371 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
372 unsigned long Flag
) const
376 if (Find(Tag
,Start
,Stop
) == false)
379 switch (StringToBool(string(Start
,Stop
)))
390 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
397 // TFRewrite - Rewrite a control record /*{{{*/
398 // ---------------------------------------------------------------------
399 /* This writes the control record to stdout rewriting it as necessary. The
400 override map item specificies the rewriting rules to follow. This also
401 takes the time to sort the feild list. */
403 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
405 static const char *iTFRewritePackageOrder
[] = {
416 "Revision", // Obsolete
417 "Config-Version", // Obsolete
432 "MSDOS-Filename", // Obsolete
435 static const char *iTFRewriteSourceOrder
[] = {"Package",
443 "Build-Depends-Indep",
445 "Build-Conflicts-Indep",
453 /* Two levels of initialization are used because gcc will set the symbol
454 size of an array to the length of the array, causing dynamic relinking
455 errors. Doing this makes the symbol size constant */
456 const char **TFRewritePackageOrder
= iTFRewritePackageOrder
;
457 const char **TFRewriteSourceOrder
= iTFRewriteSourceOrder
;
459 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
460 TFRewriteData
*Rewrite
)
462 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
463 for (unsigned I
= 0; I
!= 256; I
++)
466 // Set new tag up as necessary.
467 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
469 if (Rewrite
[J
].NewTag
== 0)
470 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
473 // Write all all of the tags, in order.
474 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
476 bool Rewritten
= false;
478 // See if this is a field that needs to be rewritten
479 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
481 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
484 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
486 if (isspace(Rewrite
[J
].Rewrite
[0]))
487 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
489 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
497 // See if it is in the fragment
499 if (Tags
.Find(Order
[I
],Pos
) == false)
503 if (Rewritten
== true)
506 /* Write out this element, taking a moment to rewrite the tag
507 in case of changes of case. */
510 Tags
.Get(Start
,Stop
,Pos
);
512 if (fputs(Order
[I
],Output
) < 0)
513 return _error
->Errno("fputs","IO Error to output");
514 Start
+= strlen(Order
[I
]);
515 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
516 return _error
->Errno("fwrite","IO Error to output");
517 if (Stop
[-1] != '\n')
518 fprintf(Output
,"\n");
521 // Now write all the old tags that were missed.
522 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
524 if ((Visited
[I
] & 1) == 1)
529 Tags
.Get(Start
,Stop
,I
);
530 const char *End
= Start
;
531 for (; End
< Stop
&& *End
!= ':'; End
++);
533 // See if this is a field that needs to be rewritten
534 bool Rewritten
= false;
535 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
537 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
540 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
542 if (isspace(Rewrite
[J
].Rewrite
[0]))
543 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
545 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
553 if (Rewritten
== true)
556 // Write out this element
557 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
558 return _error
->Errno("fwrite","IO Error to output");
559 if (Stop
[-1] != '\n')
560 fprintf(Output
,"\n");
563 // Now write all the rewrites that were missed
564 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
566 if ((Visited
[J
] & 2) == 2)
569 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
571 if (isspace(Rewrite
[J
].Rewrite
[0]))
572 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
574 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);