]>
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 d
= new pkgTagFilePrivate(pFd
, Size
);
55 if (d
->Fd
.IsOpen() == false)
57 d
->Start
= d
->End
= d
->Buffer
= 0;
63 d
->Buffer
= new char[Size
];
64 d
->Start
= d
->End
= d
->Buffer
;
70 // TagFile::~pkgTagFile - Destructor /*{{{*/
71 // ---------------------------------------------------------------------
73 pkgTagFile::~pkgTagFile()
79 // TagFile::Offset - Return the current offset in the buffer /*{{{*/
80 unsigned long pkgTagFile::Offset()
85 // TagFile::Resize - Resize the internal buffer /*{{{*/
86 // ---------------------------------------------------------------------
87 /* Resize the internal buffer (double it in size). Fail if a maximum size
90 bool pkgTagFile::Resize()
93 unsigned long long EndSize
= d
->End
- d
->Start
;
95 // fail is the buffer grows too big
96 if(d
->Size
> 1024*1024+1)
99 // get new buffer and use it
100 tmp
= new char[2*d
->Size
];
101 memcpy(tmp
, d
->Buffer
, d
->Size
);
106 // update the start/end pointers to the new buffer
107 d
->Start
= d
->Buffer
;
108 d
->End
= d
->Start
+ EndSize
;
112 // TagFile::Step - Advance to the next section /*{{{*/
113 // ---------------------------------------------------------------------
114 /* If the Section Scanner fails we refill the buffer and try again.
115 * If that fails too, double the buffer size and try again until a
116 * maximum buffer is reached.
118 bool pkgTagFile::Step(pkgTagSection
&Tag
)
120 while (Tag
.Scan(d
->Start
,d
->End
- d
->Start
) == false)
125 if(Tag
.Scan(d
->Start
,d
->End
- d
->Start
))
128 if (Resize() == false)
129 return _error
->Error(_("Unable to parse package file %s (1)"),
130 d
->Fd
.Name().c_str());
132 d
->Start
+= Tag
.size();
133 d
->iOffset
+= Tag
.size();
139 // TagFile::Fill - Top up the buffer /*{{{*/
140 // ---------------------------------------------------------------------
141 /* This takes the bit at the end of the buffer and puts it at the start
142 then fills the rest from the file */
143 bool pkgTagFile::Fill()
145 unsigned long long EndSize
= d
->End
- d
->Start
;
146 unsigned long long Actual
= 0;
148 memmove(d
->Buffer
,d
->Start
,EndSize
);
149 d
->Start
= d
->Buffer
;
150 d
->End
= d
->Buffer
+ EndSize
;
152 if (d
->Done
== false)
154 // See if only a bit of the file is left
155 if (d
->Fd
.Read(d
->End
, d
->Size
- (d
->End
- d
->Buffer
),&Actual
) == false)
157 if (Actual
!= d
->Size
- (d
->End
- d
->Buffer
))
164 if (EndSize
<= 3 && Actual
== 0)
166 if (d
->Size
- (d
->End
- d
->Buffer
) < 4)
169 // Append a double new line if one does not exist
170 unsigned int LineCount
= 0;
171 for (const char *E
= d
->End
- 1; E
- d
->End
< 6 && (*E
== '\n' || *E
== '\r'); E
--)
174 for (; LineCount
< 2; LineCount
++)
183 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
184 // ---------------------------------------------------------------------
185 /* This jumps to a pre-recorded file location and reads the record
187 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long long Offset
)
189 // We are within a buffer space of the next hit..
190 if (Offset
>= d
->iOffset
&& d
->iOffset
+ (d
->End
- d
->Start
) > Offset
)
192 unsigned long long Dist
= Offset
- d
->iOffset
;
198 // Reposition and reload..
201 if (d
->Fd
.Seek(Offset
) == false)
203 d
->End
= d
->Start
= d
->Buffer
;
208 if (Tag
.Scan(d
->Start
, d
->End
- d
->Start
) == true)
211 // This appends a double new line (for the real eof handling)
215 if (Tag
.Scan(d
->Start
, d
->End
- d
->Start
) == false)
216 return _error
->Error(_("Unable to parse package file %s (2)"),d
->Fd
.Name().c_str());
221 // TagSection::Scan - Scan for the end of the header information /*{{{*/
222 // ---------------------------------------------------------------------
223 /* This looks for the first double new line in the data stream.
224 It also indexes the tags in the section. */
225 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
227 const char *End
= Start
+ MaxLength
;
228 Stop
= Section
= Start
;
229 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
235 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
237 TrimRecord(true,End
);
239 // Start a new index and add it to the hash
240 if (isspace(Stop
[0]) == 0)
242 Indexes
[TagCount
++] = Stop
- Section
;
243 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
246 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
251 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
253 // Double newline marks the end of the record
254 if (Stop
+1 < End
&& Stop
[1] == '\n')
256 Indexes
[TagCount
] = Stop
- Section
;
257 TrimRecord(false,End
);
267 // TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/
268 // ---------------------------------------------------------------------
269 /* There should be exactly 2 newline at the end of the record, no more. */
270 void pkgTagSection::TrimRecord(bool BeforeRecord
, const char*& End
)
272 if (BeforeRecord
== true)
274 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
277 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
278 // ---------------------------------------------------------------------
279 /* There should be exactly 1 newline at the end of the buffer, no more. */
280 void pkgTagSection::Trim()
282 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
285 // TagSection::Exists - return True if a tag exists /*{{{*/
286 bool pkgTagSection::Exists(const char* const Tag
)
289 return Find(Tag
, tmp
);
292 // TagSection::Find - Locate a tag /*{{{*/
293 // ---------------------------------------------------------------------
294 /* This searches the section for a tag that matches the given string. */
295 bool pkgTagSection::Find(const char *Tag
,unsigned int &Pos
) const
297 unsigned int Length
= strlen(Tag
);
298 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
303 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
307 St
= Section
+ Indexes
[I
];
308 if (strncasecmp(Tag
,St
,Length
) != 0)
311 // Make sure the colon is in the right place
312 const char *C
= St
+ Length
;
313 for (; isspace(*C
) != 0; C
++);
324 // TagSection::Find - Locate a tag /*{{{*/
325 // ---------------------------------------------------------------------
326 /* This searches the section for a tag that matches the given string. */
327 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
328 const char *&End
) const
330 unsigned int Length
= strlen(Tag
);
331 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
336 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
340 St
= Section
+ Indexes
[I
];
341 if (strncasecmp(Tag
,St
,Length
) != 0)
344 // Make sure the colon is in the right place
345 const char *C
= St
+ Length
;
346 for (; isspace(*C
) != 0; C
++);
350 // Strip off the gunk from the start end
352 End
= Section
+ Indexes
[I
+1];
354 return _error
->Error("Internal parsing error");
356 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
357 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
366 // TagSection::FindS - Find a string /*{{{*/
367 // ---------------------------------------------------------------------
369 string
pkgTagSection::FindS(const char *Tag
) const
373 if (Find(Tag
,Start
,End
) == false)
375 return string(Start
,End
);
378 // TagSection::FindI - Find an integer /*{{{*/
379 // ---------------------------------------------------------------------
381 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
385 if (Find(Tag
,Start
,Stop
) == false)
388 // Copy it into a temp buffer so we can use strtol
390 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
392 strncpy(S
,Start
,Stop
-Start
);
396 signed long Result
= strtol(S
,&End
,10);
402 // TagSection::FindULL - Find an unsigned long long integer /*{{{*/
403 // ---------------------------------------------------------------------
405 unsigned long long pkgTagSection::FindULL(const char *Tag
, unsigned long long const &Default
) const
409 if (Find(Tag
,Start
,Stop
) == false)
412 // Copy it into a temp buffer so we can use strtoull
414 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
416 strncpy(S
,Start
,Stop
-Start
);
420 unsigned long long Result
= strtoull(S
,&End
,10);
426 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
427 // ---------------------------------------------------------------------
428 /* The bits marked in Flag are masked on/off in Flags */
429 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
430 unsigned long Flag
) const
434 if (Find(Tag
,Start
,Stop
) == false)
436 return FindFlag(Flags
, Flag
, Start
, Stop
);
438 bool const pkgTagSection::FindFlag(unsigned long &Flags
, unsigned long Flag
,
439 char const* Start
, char const* Stop
)
441 switch (StringToBool(string(Start
, Stop
)))
452 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
458 // TFRewrite - Rewrite a control record /*{{{*/
459 // ---------------------------------------------------------------------
460 /* This writes the control record to stdout rewriting it as necessary. The
461 override map item specificies the rewriting rules to follow. This also
462 takes the time to sort the feild list. */
464 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
466 static const char *iTFRewritePackageOrder
[] = {
474 "Original-Maintainer",
478 "Revision", // Obsolete
479 "Config-Version", // Obsolete
495 "MSDOS-Filename", // Obsolete
498 static const char *iTFRewriteSourceOrder
[] = {"Package",
505 "Original-Maintainer",
507 "Build-Depends-Indep",
509 "Build-Conflicts-Indep",
517 /* Two levels of initialization are used because gcc will set the symbol
518 size of an array to the length of the array, causing dynamic relinking
519 errors. Doing this makes the symbol size constant */
520 const char **TFRewritePackageOrder
= iTFRewritePackageOrder
;
521 const char **TFRewriteSourceOrder
= iTFRewriteSourceOrder
;
523 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
524 TFRewriteData
*Rewrite
)
526 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
527 for (unsigned I
= 0; I
!= 256; I
++)
530 // Set new tag up as necessary.
531 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
533 if (Rewrite
[J
].NewTag
== 0)
534 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
537 // Write all all of the tags, in order.
538 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
540 bool Rewritten
= false;
542 // See if this is a field that needs to be rewritten
543 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
545 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
548 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
550 if (isspace(Rewrite
[J
].Rewrite
[0]))
551 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
553 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
561 // See if it is in the fragment
563 if (Tags
.Find(Order
[I
],Pos
) == false)
567 if (Rewritten
== true)
570 /* Write out this element, taking a moment to rewrite the tag
571 in case of changes of case. */
574 Tags
.Get(Start
,Stop
,Pos
);
576 if (fputs(Order
[I
],Output
) < 0)
577 return _error
->Errno("fputs","IO Error to output");
578 Start
+= strlen(Order
[I
]);
579 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
580 return _error
->Errno("fwrite","IO Error to output");
581 if (Stop
[-1] != '\n')
582 fprintf(Output
,"\n");
585 // Now write all the old tags that were missed.
586 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
588 if ((Visited
[I
] & 1) == 1)
593 Tags
.Get(Start
,Stop
,I
);
594 const char *End
= Start
;
595 for (; End
< Stop
&& *End
!= ':'; End
++);
597 // See if this is a field that needs to be rewritten
598 bool Rewritten
= false;
599 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
601 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
604 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
606 if (isspace(Rewrite
[J
].Rewrite
[0]))
607 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
609 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
617 if (Rewritten
== true)
620 // Write out this element
621 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
622 return _error
->Errno("fwrite","IO Error to output");
623 if (Stop
[-1] != '\n')
624 fprintf(Output
,"\n");
627 // Now write all the rewrites that were missed
628 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
630 if ((Visited
[J
] & 2) == 2)
633 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
635 if (isspace(Rewrite
[J
].Rewrite
[0]))
636 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
638 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);