]>
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 /*{{{*/
16 #include <apt-pkg/tagfile.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/strutl.h>
19 #include <apt-pkg/fileutl.h>
30 class pkgTagFilePrivate
33 pkgTagFilePrivate(FileFd
*pFd
, unsigned long long Size
) : Fd(*pFd
), Size(Size
)
41 unsigned long long iOffset
;
42 unsigned long long Size
;
45 // TagFile::pkgTagFile - Constructor /*{{{*/
46 // ---------------------------------------------------------------------
48 pkgTagFile::pkgTagFile(FileFd
*pFd
,unsigned long long Size
)
50 d
= new pkgTagFilePrivate(pFd
, Size
);
52 if (d
->Fd
.IsOpen() == false)
54 d
->Start
= d
->End
= d
->Buffer
= 0;
60 d
->Buffer
= new char[Size
];
61 d
->Start
= d
->End
= d
->Buffer
;
67 // TagFile::~pkgTagFile - Destructor /*{{{*/
68 // ---------------------------------------------------------------------
70 pkgTagFile::~pkgTagFile()
76 // TagFile::Offset - Return the current offset in the buffer /*{{{*/
77 unsigned long pkgTagFile::Offset()
82 // TagFile::Resize - Resize the internal buffer /*{{{*/
83 // ---------------------------------------------------------------------
84 /* Resize the internal buffer (double it in size). Fail if a maximum size
87 bool pkgTagFile::Resize()
90 unsigned long long EndSize
= d
->End
- d
->Start
;
92 // fail is the buffer grows too big
93 if(d
->Size
> 1024*1024+1)
96 // get new buffer and use it
97 tmp
= new char[2*d
->Size
];
98 memcpy(tmp
, d
->Buffer
, d
->Size
);
103 // update the start/end pointers to the new buffer
104 d
->Start
= d
->Buffer
;
105 d
->End
= d
->Start
+ EndSize
;
109 // TagFile::Step - Advance to the next section /*{{{*/
110 // ---------------------------------------------------------------------
111 /* If the Section Scanner fails we refill the buffer and try again.
112 * If that fails too, double the buffer size and try again until a
113 * maximum buffer is reached.
115 bool pkgTagFile::Step(pkgTagSection
&Tag
)
117 while (Tag
.Scan(d
->Start
,d
->End
- d
->Start
) == false)
122 if(Tag
.Scan(d
->Start
,d
->End
- d
->Start
))
125 if (Resize() == false)
126 return _error
->Error(_("Unable to parse package file %s (1)"),
127 d
->Fd
.Name().c_str());
129 d
->Start
+= Tag
.size();
130 d
->iOffset
+= Tag
.size();
136 // TagFile::Fill - Top up the buffer /*{{{*/
137 // ---------------------------------------------------------------------
138 /* This takes the bit at the end of the buffer and puts it at the start
139 then fills the rest from the file */
140 bool pkgTagFile::Fill()
142 unsigned long long EndSize
= d
->End
- d
->Start
;
143 unsigned long long Actual
= 0;
145 memmove(d
->Buffer
,d
->Start
,EndSize
);
146 d
->Start
= d
->Buffer
;
147 d
->End
= d
->Buffer
+ EndSize
;
149 if (d
->Done
== false)
151 // See if only a bit of the file is left
152 if (d
->Fd
.Read(d
->End
, d
->Size
- (d
->End
- d
->Buffer
),&Actual
) == false)
154 if (Actual
!= d
->Size
- (d
->End
- d
->Buffer
))
161 if (EndSize
<= 3 && Actual
== 0)
163 if (d
->Size
- (d
->End
- d
->Buffer
) < 4)
166 // Append a double new line if one does not exist
167 unsigned int LineCount
= 0;
168 for (const char *E
= d
->End
- 1; E
- d
->End
< 6 && (*E
== '\n' || *E
== '\r'); E
--)
171 for (; LineCount
< 2; LineCount
++)
180 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
181 // ---------------------------------------------------------------------
182 /* This jumps to a pre-recorded file location and reads the record
184 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long long Offset
)
186 // We are within a buffer space of the next hit..
187 if (Offset
>= d
->iOffset
&& d
->iOffset
+ (d
->End
- d
->Start
) > Offset
)
189 unsigned long long Dist
= Offset
- d
->iOffset
;
195 // Reposition and reload..
198 if (d
->Fd
.Seek(Offset
) == false)
200 d
->End
= d
->Start
= d
->Buffer
;
205 if (Tag
.Scan(d
->Start
, d
->End
- d
->Start
) == true)
208 // This appends a double new line (for the real eof handling)
212 if (Tag
.Scan(d
->Start
, d
->End
- d
->Start
) == false)
213 return _error
->Error(_("Unable to parse package file %s (2)"),d
->Fd
.Name().c_str());
218 // TagSection::Scan - Scan for the end of the header information /*{{{*/
219 // ---------------------------------------------------------------------
220 /* This looks for the first double new line in the data stream.
221 It also indexes the tags in the section. */
222 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
224 const char *End
= Start
+ MaxLength
;
225 Stop
= Section
= Start
;
226 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
232 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
234 TrimRecord(true,End
);
236 // Start a new index and add it to the hash
237 if (isspace(Stop
[0]) == 0)
239 Indexes
[TagCount
++] = Stop
- Section
;
240 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
243 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
248 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
250 // Double newline marks the end of the record
251 if (Stop
+1 < End
&& Stop
[1] == '\n')
253 Indexes
[TagCount
] = Stop
- Section
;
254 TrimRecord(false,End
);
264 // TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/
265 // ---------------------------------------------------------------------
266 /* There should be exactly 2 newline at the end of the record, no more. */
267 void pkgTagSection::TrimRecord(bool BeforeRecord
, const char*& End
)
269 if (BeforeRecord
== true)
271 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
274 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
275 // ---------------------------------------------------------------------
276 /* There should be exactly 1 newline at the end of the buffer, no more. */
277 void pkgTagSection::Trim()
279 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
282 // TagSection::Find - Locate a tag /*{{{*/
283 // ---------------------------------------------------------------------
284 /* This searches the section for a tag that matches the given string. */
285 bool pkgTagSection::Find(const char *Tag
,unsigned &Pos
) const
287 unsigned int Length
= strlen(Tag
);
288 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
293 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
297 St
= Section
+ Indexes
[I
];
298 if (strncasecmp(Tag
,St
,Length
) != 0)
301 // Make sure the colon is in the right place
302 const char *C
= St
+ Length
;
303 for (; isspace(*C
) != 0; C
++);
314 // TagSection::Find - Locate a tag /*{{{*/
315 // ---------------------------------------------------------------------
316 /* This searches the section for a tag that matches the given string. */
317 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
318 const char *&End
) const
320 unsigned int Length
= strlen(Tag
);
321 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
326 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
330 St
= Section
+ Indexes
[I
];
331 if (strncasecmp(Tag
,St
,Length
) != 0)
334 // Make sure the colon is in the right place
335 const char *C
= St
+ Length
;
336 for (; isspace(*C
) != 0; C
++);
340 // Strip off the gunk from the start end
342 End
= Section
+ Indexes
[I
+1];
344 return _error
->Error("Internal parsing error");
346 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
347 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
356 // TagSection::FindS - Find a string /*{{{*/
357 // ---------------------------------------------------------------------
359 string
pkgTagSection::FindS(const char *Tag
) const
363 if (Find(Tag
,Start
,End
) == false)
365 return string(Start
,End
);
368 // TagSection::FindI - Find an integer /*{{{*/
369 // ---------------------------------------------------------------------
371 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
375 if (Find(Tag
,Start
,Stop
) == false)
378 // Copy it into a temp buffer so we can use strtol
380 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
382 strncpy(S
,Start
,Stop
-Start
);
386 signed long Result
= strtol(S
,&End
,10);
392 // TagSection::FindULL - Find an unsigned long long integer /*{{{*/
393 // ---------------------------------------------------------------------
395 unsigned long long pkgTagSection::FindULL(const char *Tag
, unsigned long long const &Default
) const
399 if (Find(Tag
,Start
,Stop
) == false)
402 // Copy it into a temp buffer so we can use strtoull
404 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
406 strncpy(S
,Start
,Stop
-Start
);
410 unsigned long long Result
= strtoull(S
,&End
,10);
416 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
417 // ---------------------------------------------------------------------
418 /* The bits marked in Flag are masked on/off in Flags */
419 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
420 unsigned long Flag
) const
424 if (Find(Tag
,Start
,Stop
) == false)
426 return FindFlag(Flags
, Flag
, Start
, Stop
);
428 bool const pkgTagSection::FindFlag(unsigned long &Flags
, unsigned long Flag
,
429 char const* Start
, char const* Stop
)
431 switch (StringToBool(string(Start
, Stop
)))
442 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
448 // TFRewrite - Rewrite a control record /*{{{*/
449 // ---------------------------------------------------------------------
450 /* This writes the control record to stdout rewriting it as necessary. The
451 override map item specificies the rewriting rules to follow. This also
452 takes the time to sort the feild list. */
454 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
456 static const char *iTFRewritePackageOrder
[] = {
464 "Original-Maintainer",
468 "Revision", // Obsolete
469 "Config-Version", // Obsolete
485 "MSDOS-Filename", // Obsolete
488 static const char *iTFRewriteSourceOrder
[] = {"Package",
495 "Original-Maintainer",
497 "Build-Depends-Indep",
499 "Build-Conflicts-Indep",
507 /* Two levels of initialization are used because gcc will set the symbol
508 size of an array to the length of the array, causing dynamic relinking
509 errors. Doing this makes the symbol size constant */
510 const char **TFRewritePackageOrder
= iTFRewritePackageOrder
;
511 const char **TFRewriteSourceOrder
= iTFRewriteSourceOrder
;
513 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
514 TFRewriteData
*Rewrite
)
516 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
517 for (unsigned I
= 0; I
!= 256; I
++)
520 // Set new tag up as necessary.
521 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
523 if (Rewrite
[J
].NewTag
== 0)
524 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
527 // Write all all of the tags, in order.
528 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
530 bool Rewritten
= false;
532 // See if this is a field that needs to be rewritten
533 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
535 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
538 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
540 if (isspace(Rewrite
[J
].Rewrite
[0]))
541 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
543 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
551 // See if it is in the fragment
553 if (Tags
.Find(Order
[I
],Pos
) == false)
557 if (Rewritten
== true)
560 /* Write out this element, taking a moment to rewrite the tag
561 in case of changes of case. */
564 Tags
.Get(Start
,Stop
,Pos
);
566 if (fputs(Order
[I
],Output
) < 0)
567 return _error
->Errno("fputs","IO Error to output");
568 Start
+= strlen(Order
[I
]);
569 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
570 return _error
->Errno("fwrite","IO Error to output");
571 if (Stop
[-1] != '\n')
572 fprintf(Output
,"\n");
575 // Now write all the old tags that were missed.
576 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
578 if ((Visited
[I
] & 1) == 1)
583 Tags
.Get(Start
,Stop
,I
);
584 const char *End
= Start
;
585 for (; End
< Stop
&& *End
!= ':'; End
++);
587 // See if this is a field that needs to be rewritten
588 bool Rewritten
= false;
589 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
591 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
594 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
596 if (isspace(Rewrite
[J
].Rewrite
[0]))
597 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
599 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
607 if (Rewritten
== true)
610 // Write out this element
611 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
612 return _error
->Errno("fwrite","IO Error to output");
613 if (Stop
[-1] != '\n')
614 fprintf(Output
,"\n");
617 // Now write all the rewrites that were missed
618 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
620 if ((Visited
[J
] & 2) == 2)
623 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
625 if (isspace(Rewrite
[J
].Rewrite
[0]))
626 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
628 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);