]>
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
), Buffer(NULL
),
34 Start(NULL
), End(NULL
),
35 Done(false), iOffset(0),
44 unsigned long long iOffset
;
45 unsigned long long Size
;
48 // TagFile::pkgTagFile - Constructor /*{{{*/
49 // ---------------------------------------------------------------------
51 pkgTagFile::pkgTagFile(FileFd
*pFd
,unsigned long long Size
)
53 /* The size is increased by 4 because if we start with the Size of the
54 filename we need to try to read 1 char more to see an EOF faster, 1
55 char the end-pointer can be on and maybe 2 newlines need to be added
56 to the end of the file -> 4 extra chars */
58 d
= new pkgTagFilePrivate(pFd
, Size
);
60 if (d
->Fd
.IsOpen() == false)
61 d
->Start
= d
->End
= d
->Buffer
= 0;
63 d
->Buffer
= (char*)malloc(sizeof(char) * Size
);
65 if (d
->Buffer
== NULL
)
70 d
->Start
= d
->End
= d
->Buffer
;
76 // TagFile::~pkgTagFile - Destructor /*{{{*/
77 // ---------------------------------------------------------------------
79 pkgTagFile::~pkgTagFile()
85 // TagFile::Offset - Return the current offset in the buffer /*{{{*/
86 unsigned long pkgTagFile::Offset()
91 // TagFile::Resize - Resize the internal buffer /*{{{*/
92 // ---------------------------------------------------------------------
93 /* Resize the internal buffer (double it in size). Fail if a maximum size
96 bool pkgTagFile::Resize()
98 // fail is the buffer grows too big
99 if(d
->Size
> 1024*1024+1)
102 return Resize(d
->Size
* 2);
104 bool pkgTagFile::Resize(unsigned long long const newSize
)
106 unsigned long long const EndSize
= d
->End
- d
->Start
;
108 // get new buffer and use it
109 char* newBuffer
= (char*)realloc(d
->Buffer
, sizeof(char) * newSize
);
110 if (newBuffer
== NULL
)
112 d
->Buffer
= newBuffer
;
115 // update the start/end pointers to the new buffer
116 d
->Start
= d
->Buffer
;
117 d
->End
= d
->Start
+ EndSize
;
121 // TagFile::Step - Advance to the next section /*{{{*/
122 // ---------------------------------------------------------------------
123 /* If the Section Scanner fails we refill the buffer and try again.
124 * If that fails too, double the buffer size and try again until a
125 * maximum buffer is reached.
127 bool pkgTagFile::Step(pkgTagSection
&Tag
)
129 while (Tag
.Scan(d
->Start
,d
->End
- d
->Start
) == false)
134 if(Tag
.Scan(d
->Start
,d
->End
- d
->Start
))
137 if (Resize() == false)
138 return _error
->Error(_("Unable to parse package file %s (1)"),
139 d
->Fd
.Name().c_str());
141 d
->Start
+= Tag
.size();
142 d
->iOffset
+= Tag
.size();
148 // TagFile::Fill - Top up the buffer /*{{{*/
149 // ---------------------------------------------------------------------
150 /* This takes the bit at the end of the buffer and puts it at the start
151 then fills the rest from the file */
152 bool pkgTagFile::Fill()
154 unsigned long long EndSize
= d
->End
- d
->Start
;
155 unsigned long long Actual
= 0;
157 memmove(d
->Buffer
,d
->Start
,EndSize
);
158 d
->Start
= d
->Buffer
;
159 d
->End
= d
->Buffer
+ EndSize
;
161 if (d
->Done
== false)
163 // See if only a bit of the file is left
164 unsigned long long const dataSize
= d
->Size
- ((d
->End
- d
->Buffer
) + 1);
165 if (d
->Fd
.Read(d
->End
, dataSize
, &Actual
) == false)
167 if (Actual
!= dataSize
)
174 if (EndSize
<= 3 && Actual
== 0)
176 if (d
->Size
- (d
->End
- d
->Buffer
) < 4)
179 // Append a double new line if one does not exist
180 unsigned int LineCount
= 0;
181 for (const char *E
= d
->End
- 1; E
- d
->End
< 6 && (*E
== '\n' || *E
== '\r'); E
--)
186 if ((unsigned)(d
->End
- d
->Buffer
) >= d
->Size
)
188 for (; LineCount
< 2; LineCount
++)
198 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This jumps to a pre-recorded file location and reads the record
202 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long long Offset
)
204 // We are within a buffer space of the next hit..
205 if (Offset
>= d
->iOffset
&& d
->iOffset
+ (d
->End
- d
->Start
) > Offset
)
207 unsigned long long Dist
= Offset
- d
->iOffset
;
213 // Reposition and reload..
216 if (d
->Fd
.Seek(Offset
) == false)
218 d
->End
= d
->Start
= d
->Buffer
;
223 if (Tag
.Scan(d
->Start
, d
->End
- d
->Start
) == true)
226 // This appends a double new line (for the real eof handling)
230 if (Tag
.Scan(d
->Start
, d
->End
- d
->Start
) == false)
231 return _error
->Error(_("Unable to parse package file %s (2)"),d
->Fd
.Name().c_str());
236 // pkgTagSection::pkgTagSection - Constructor /*{{{*/
237 // ---------------------------------------------------------------------
239 pkgTagSection::pkgTagSection()
240 : Section(0), TagCount(0), d(NULL
), Stop(0)
242 memset(&Indexes
, 0, sizeof(Indexes
));
243 memset(&AlphaIndexes
, 0, sizeof(AlphaIndexes
));
246 // TagSection::Scan - Scan for the end of the header information /*{{{*/
247 // ---------------------------------------------------------------------
248 /* This looks for the first double new line in the data stream.
249 It also indexes the tags in the section. */
250 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
252 const char *End
= Start
+ MaxLength
;
253 Stop
= Section
= Start
;
254 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
260 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
262 TrimRecord(true,End
);
264 // Start a new index and add it to the hash
265 if (isspace(Stop
[0]) == 0)
267 Indexes
[TagCount
++] = Stop
- Section
;
268 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
271 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
276 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
278 // Double newline marks the end of the record
279 if (Stop
+1 < End
&& Stop
[1] == '\n')
281 Indexes
[TagCount
] = Stop
- Section
;
282 TrimRecord(false,End
);
292 // TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/
293 // ---------------------------------------------------------------------
294 /* There should be exactly 2 newline at the end of the record, no more. */
295 void pkgTagSection::TrimRecord(bool BeforeRecord
, const char*& End
)
297 if (BeforeRecord
== true)
299 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
302 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
303 // ---------------------------------------------------------------------
304 /* There should be exactly 1 newline at the end of the buffer, no more. */
305 void pkgTagSection::Trim()
307 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
310 // TagSection::Exists - return True if a tag exists /*{{{*/
311 bool pkgTagSection::Exists(const char* const Tag
)
314 return Find(Tag
, tmp
);
317 // TagSection::Find - Locate a tag /*{{{*/
318 // ---------------------------------------------------------------------
319 /* This searches the section for a tag that matches the given string. */
320 bool pkgTagSection::Find(const char *Tag
,unsigned int &Pos
) const
322 unsigned int Length
= strlen(Tag
);
323 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
328 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
332 St
= Section
+ Indexes
[I
];
333 if (strncasecmp(Tag
,St
,Length
) != 0)
336 // Make sure the colon is in the right place
337 const char *C
= St
+ Length
;
338 for (; isspace(*C
) != 0; C
++);
349 // TagSection::Find - Locate a tag /*{{{*/
350 // ---------------------------------------------------------------------
351 /* This searches the section for a tag that matches the given string. */
352 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
353 const char *&End
) const
355 unsigned int Length
= strlen(Tag
);
356 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
361 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
365 St
= Section
+ Indexes
[I
];
366 if (strncasecmp(Tag
,St
,Length
) != 0)
369 // Make sure the colon is in the right place
370 const char *C
= St
+ Length
;
371 for (; isspace(*C
) != 0; C
++);
375 // Strip off the gunk from the start end
377 End
= Section
+ Indexes
[I
+1];
379 return _error
->Error("Internal parsing error");
381 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
382 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
391 // TagSection::FindS - Find a string /*{{{*/
392 // ---------------------------------------------------------------------
394 string
pkgTagSection::FindS(const char *Tag
) const
398 if (Find(Tag
,Start
,End
) == false)
400 return string(Start
,End
);
403 // TagSection::FindI - Find an integer /*{{{*/
404 // ---------------------------------------------------------------------
406 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
410 if (Find(Tag
,Start
,Stop
) == false)
413 // Copy it into a temp buffer so we can use strtol
415 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
417 strncpy(S
,Start
,Stop
-Start
);
421 signed long Result
= strtol(S
,&End
,10);
427 // TagSection::FindULL - Find an unsigned long long integer /*{{{*/
428 // ---------------------------------------------------------------------
430 unsigned long long pkgTagSection::FindULL(const char *Tag
, unsigned long long const &Default
) const
434 if (Find(Tag
,Start
,Stop
) == false)
437 // Copy it into a temp buffer so we can use strtoull
439 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
441 strncpy(S
,Start
,Stop
-Start
);
445 unsigned long long Result
= strtoull(S
,&End
,10);
451 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
452 // ---------------------------------------------------------------------
453 /* The bits marked in Flag are masked on/off in Flags */
454 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
455 unsigned long Flag
) const
459 if (Find(Tag
,Start
,Stop
) == false)
461 return FindFlag(Flags
, Flag
, Start
, Stop
);
463 bool const pkgTagSection::FindFlag(unsigned long &Flags
, unsigned long Flag
,
464 char const* Start
, char const* Stop
)
466 switch (StringToBool(string(Start
, Stop
)))
477 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
483 // TFRewrite - Rewrite a control record /*{{{*/
484 // ---------------------------------------------------------------------
485 /* This writes the control record to stdout rewriting it as necessary. The
486 override map item specificies the rewriting rules to follow. This also
487 takes the time to sort the feild list. */
489 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
491 static const char *iTFRewritePackageOrder
[] = {
499 "Original-Maintainer",
503 "Revision", // Obsolete
504 "Config-Version", // Obsolete
520 "MSDOS-Filename", // Obsolete
523 static const char *iTFRewriteSourceOrder
[] = {"Package",
530 "Original-Maintainer",
532 "Build-Depends-Indep",
534 "Build-Conflicts-Indep",
542 /* Two levels of initialization are used because gcc will set the symbol
543 size of an array to the length of the array, causing dynamic relinking
544 errors. Doing this makes the symbol size constant */
545 const char **TFRewritePackageOrder
= iTFRewritePackageOrder
;
546 const char **TFRewriteSourceOrder
= iTFRewriteSourceOrder
;
548 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
549 TFRewriteData
*Rewrite
)
551 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
552 for (unsigned I
= 0; I
!= 256; I
++)
555 // Set new tag up as necessary.
556 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
558 if (Rewrite
[J
].NewTag
== 0)
559 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
562 // Write all all of the tags, in order.
563 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
565 bool Rewritten
= false;
567 // See if this is a field that needs to be rewritten
568 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
570 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
573 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
575 if (isspace(Rewrite
[J
].Rewrite
[0]))
576 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
578 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
586 // See if it is in the fragment
588 if (Tags
.Find(Order
[I
],Pos
) == false)
592 if (Rewritten
== true)
595 /* Write out this element, taking a moment to rewrite the tag
596 in case of changes of case. */
599 Tags
.Get(Start
,Stop
,Pos
);
601 if (fputs(Order
[I
],Output
) < 0)
602 return _error
->Errno("fputs","IO Error to output");
603 Start
+= strlen(Order
[I
]);
604 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
605 return _error
->Errno("fwrite","IO Error to output");
606 if (Stop
[-1] != '\n')
607 fprintf(Output
,"\n");
610 // Now write all the old tags that were missed.
611 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
613 if ((Visited
[I
] & 1) == 1)
618 Tags
.Get(Start
,Stop
,I
);
619 const char *End
= Start
;
620 for (; End
< Stop
&& *End
!= ':'; End
++);
622 // See if this is a field that needs to be rewritten
623 bool Rewritten
= false;
624 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
626 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
629 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
631 if (isspace(Rewrite
[J
].Rewrite
[0]))
632 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
634 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
642 if (Rewritten
== true)
645 // Write out this element
646 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
647 return _error
->Errno("fwrite","IO Error to output");
648 if (Stop
[-1] != '\n')
649 fprintf(Output
,"\n");
652 // Now write all the rewrites that were missed
653 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
655 if ((Visited
[J
] & 2) == 2)
658 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
660 if (isspace(Rewrite
[J
].Rewrite
[0]))
661 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
663 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);