]> git.saurik.com Git - apt.git/blame - apt-pkg/tagfile.cc
* added Debug::pkgDepCache::AutoInstall debug option
[apt.git] / apt-pkg / tagfile.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 3// $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz Exp $
578bfd0a
AL
4/* ######################################################################
5
6 Fast scanner for RFC-822 type header information
7
ad00ae81 8 This uses a rotating buffer to load the package information into.
578bfd0a
AL
9 The scanner runs over it and isolates and indexes a single section.
10
11 ##################################################################### */
12 /*}}}*/
13// Include Files /*{{{*/
6c139d6e 14#ifdef __GNUG__
094a497d 15#pragma implementation "apt-pkg/tagfile.h"
6c139d6e
AL
16#endif
17
094a497d
AL
18#include <apt-pkg/tagfile.h>
19#include <apt-pkg/error.h>
cdcc6d34 20#include <apt-pkg/strutl.h>
578bfd0a 21
b2e465d6
AL
22#include <apti18n.h>
23
578bfd0a
AL
24#include <string>
25#include <stdio.h>
851a45a8 26#include <ctype.h>
578bfd0a
AL
27 /*}}}*/
28
851a45a8
AL
29using std::string;
30
578bfd0a
AL
31// TagFile::pkgTagFile - Constructor /*{{{*/
32// ---------------------------------------------------------------------
33/* */
b3d44315
MV
34pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) :
35 Fd(*pFd),
36 Size(Size)
578bfd0a 37{
432b168c 38 if (Fd.IsOpen() == false || Fd.Size() == 0)
0e72dd52
AL
39 {
40 Buffer = 0;
41 Start = End = Buffer = 0;
0e72dd52 42 iOffset = 0;
432b168c 43 Map = NULL;
0e72dd52
AL
44 return;
45 }
46
432b168c
MV
47 Map = new MMap (Fd, MMap::Public | MMap::ReadOnly);
48 Buffer = (char *) Map->Data ();
49 Start = Buffer;
50 End = Buffer + Map->Size ();
dcb79bae 51 iOffset = 0;
578bfd0a
AL
52}
53 /*}}}*/
b2e465d6 54// TagFile::~pkgTagFile - Destructor /*{{{*/
29f7b36c
AL
55// ---------------------------------------------------------------------
56/* */
57pkgTagFile::~pkgTagFile()
58{
432b168c 59 delete Map;
29f7b36c
AL
60}
61 /*}}}*/
578bfd0a
AL
62// TagFile::Step - Advance to the next section /*{{{*/
63// ---------------------------------------------------------------------
64/* If the Section Scanner fails we refill the buffer and try again. */
65bool pkgTagFile::Step(pkgTagSection &Tag)
66{
432b168c
MV
67 if (Start == End)
68 return false;
69
0852eaef
AL
70 if (Tag.Scan(Start,End - Start) == false)
71 {
432b168c
MV
72 return _error->Error(_("Unable to parse package file %s (1)"),
73 Fd.Name().c_str());
613f9499 74 }
dcb79bae
AL
75 Start += Tag.size();
76 iOffset += Tag.size();
b2e465d6
AL
77
78 Tag.Trim();
578bfd0a
AL
79 return true;
80}
81 /*}}}*/
ad00ae81
AL
82// TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
83// ---------------------------------------------------------------------
03e39e59
AL
84/* This jumps to a pre-recorded file location and reads the record
85 that is there */
ad00ae81
AL
86bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
87{
b2e465d6
AL
88 // We are within a buffer space of the next hit..
89 if (Offset >= iOffset && iOffset + (End - Start) > Offset)
90 {
91 unsigned long Dist = Offset - iOffset;
92 Start += Dist;
93 iOffset += Dist;
94 return Step(Tag);
95 }
96
97 // Reposition and reload..
ad00ae81 98 iOffset = Offset;
432b168c 99 Start = Buffer + iOffset;
0852eaef
AL
100
101 if (Tag.Scan(Start,End - Start) == false)
b2e465d6 102 return _error->Error(_("Unable to parse package file %s (2)"),Fd.Name().c_str());
06bba740 103
ad00ae81
AL
104 return true;
105}
106 /*}}}*/
578bfd0a
AL
107// TagSection::Scan - Scan for the end of the header information /*{{{*/
108// ---------------------------------------------------------------------
109/* This looks for the first double new line in the data stream. It also
c1a22377
AL
110 indexes the tags in the section. This very simple hash function for the
111 first 3 letters gives very good performance on the debian package files */
b2e465d6
AL
112inline static unsigned long AlphaHash(const char *Text, const char *End = 0)
113{
114 unsigned long Res = 0;
115 for (; Text != End && *Text != ':' && *Text != 0; Text++)
116 Res = (unsigned long)(*Text) ^ (Res << 2);
117 return Res & 0xFF;
118}
119
0852eaef 120bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
578bfd0a
AL
121{
122 const char *End = Start + MaxLength;
123 Stop = Section = Start;
c1a22377 124 memset(AlphaIndexes,0,sizeof(AlphaIndexes));
c7b5ce1c 125
432b168c 126 if (Stop == 0 || MaxLength == 0)
0852eaef 127 return false;
578bfd0a
AL
128
129 TagCount = 0;
fd71171a 130 while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
578bfd0a 131 {
90d64280 132 // Start a new index and add it to the hash
c1a22377
AL
133 if (isspace(Stop[0]) == 0)
134 {
135 Indexes[TagCount++] = Stop - Section;
b2e465d6 136 AlphaIndexes[AlphaHash(Stop,End)] = TagCount;
c1a22377 137 }
0a8e3465 138
c1a22377 139 Stop = (const char *)memchr(Stop,'\n',End - Stop);
0a8e3465 140
c1a22377 141 if (Stop == 0)
0852eaef 142 return false;
138d4b3d 143
b8b7c37d 144 for (; Stop+1 < End && Stop[1] == '\r'; Stop++);
c1a22377 145
f3bcc383
AL
146 // Double newline marks the end of the record
147 if (Stop+1 < End && Stop[1] == '\n')
578bfd0a 148 {
578bfd0a 149 Indexes[TagCount] = Stop - Section;
fd71171a 150 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
0852eaef 151 return true;
578bfd0a
AL
152 }
153
c1a22377
AL
154 Stop++;
155 }
138d4b3d 156
432b168c
MV
157 if ((Stop+1 >= End) && (End[-1] == '\n' || End[-1] == '\r'))
158 {
159 Indexes[TagCount] = (End - 1) - Section;
160 return true;
161 }
162
0852eaef 163 return false;
578bfd0a
AL
164}
165 /*}}}*/
b2e465d6
AL
166// TagSection::Trim - Trim off any trailing garbage /*{{{*/
167// ---------------------------------------------------------------------
168/* There should be exactly 1 newline at the end of the buffer, no more. */
169void pkgTagSection::Trim()
170{
171 for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
172}
173 /*}}}*/
578bfd0a
AL
174// TagSection::Find - Locate a tag /*{{{*/
175// ---------------------------------------------------------------------
176/* This searches the section for a tag that matches the given string. */
b2e465d6 177bool pkgTagSection::Find(const char *Tag,unsigned &Pos) const
578bfd0a
AL
178{
179 unsigned int Length = strlen(Tag);
b2e465d6 180 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
c1a22377
AL
181 if (I == 0)
182 return false;
183 I--;
184
185 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
186 I = (I+1)%TagCount)
578bfd0a 187 {
c1a22377
AL
188 const char *St;
189 St = Section + Indexes[I];
190 if (strncasecmp(Tag,St,Length) != 0)
578bfd0a
AL
191 continue;
192
b2e465d6
AL
193 // Make sure the colon is in the right place
194 const char *C = St + Length;
195 for (; isspace(*C) != 0; C++);
196 if (*C != ':')
197 continue;
198 Pos = I;
199 return true;
200 }
201
202 Pos = 0;
203 return false;
204}
205 /*}}}*/
206// TagSection::Find - Locate a tag /*{{{*/
207// ---------------------------------------------------------------------
208/* This searches the section for a tag that matches the given string. */
209bool pkgTagSection::Find(const char *Tag,const char *&Start,
210 const char *&End) const
211{
212 unsigned int Length = strlen(Tag);
213 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
214 if (I == 0)
215 return false;
216 I--;
217
218 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
219 I = (I+1)%TagCount)
220 {
221 const char *St;
222 St = Section + Indexes[I];
223 if (strncasecmp(Tag,St,Length) != 0)
224 continue;
225
578bfd0a 226 // Make sure the colon is in the right place
c1a22377 227 const char *C = St + Length;
578bfd0a
AL
228 for (; isspace(*C) != 0; C++);
229 if (*C != ':')
230 continue;
231
232 // Strip off the gunk from the start end
233 Start = C;
234 End = Section + Indexes[I+1];
06bba740
AL
235 if (Start >= End)
236 return _error->Error("Internal parsing error");
237
578bfd0a
AL
238 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
239 for (; isspace(End[-1]) != 0 && End > Start; End--);
06bba740 240
578bfd0a
AL
241 return true;
242 }
c1a22377 243
578bfd0a
AL
244 Start = End = 0;
245 return false;
246}
247 /*}}}*/
0e66b144 248// TagSection::FindS - Find a string /*{{{*/
a05599f1
AL
249// ---------------------------------------------------------------------
250/* */
b2e465d6 251string pkgTagSection::FindS(const char *Tag) const
a05599f1
AL
252{
253 const char *Start;
254 const char *End;
255 if (Find(Tag,Start,End) == false)
256 return string();
257 return string(Start,End);
258}
259 /*}}}*/
260// TagSection::FindI - Find an integer /*{{{*/
261// ---------------------------------------------------------------------
262/* */
b2e465d6 263signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
a05599f1
AL
264{
265 const char *Start;
b0b4efb9
AL
266 const char *Stop;
267 if (Find(Tag,Start,Stop) == false)
268 return Default;
269
270 // Copy it into a temp buffer so we can use strtol
271 char S[300];
272 if ((unsigned)(Stop - Start) >= sizeof(S))
273 return Default;
274 strncpy(S,Start,Stop-Start);
275 S[Stop - Start] = 0;
276
277 char *End;
278 signed long Result = strtol(S,&End,10);
279 if (S == End)
280 return Default;
281 return Result;
282}
283 /*}}}*/
284// TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
285// ---------------------------------------------------------------------
286/* The bits marked in Flag are masked on/off in Flags */
287bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
b2e465d6 288 unsigned long Flag) const
b0b4efb9
AL
289{
290 const char *Start;
291 const char *Stop;
292 if (Find(Tag,Start,Stop) == false)
293 return true;
a05599f1 294
b0b4efb9
AL
295 switch (StringToBool(string(Start,Stop)))
296 {
297 case 0:
298 Flags &= ~Flag;
299 return true;
300
301 case 1:
302 Flags |= Flag;
303 return true;
304
305 default:
b2e465d6 306 _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
b0b4efb9
AL
307 return true;
308 }
309 return true;
a05599f1
AL
310}
311 /*}}}*/
b2e465d6
AL
312
313// TFRewrite - Rewrite a control record /*{{{*/
314// ---------------------------------------------------------------------
315/* This writes the control record to stdout rewriting it as necessary. The
316 override map item specificies the rewriting rules to follow. This also
317 takes the time to sort the feild list. */
318
319/* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
320 array. */
321static const char *iTFRewritePackageOrder[] = {
322 "Package",
323 "Essential",
324 "Status",
325 "Priority",
326 "Section",
327 "Installed-Size",
328 "Maintainer",
329 "Architecture",
330 "Source",
331 "Version",
332 "Revision", // Obsolete
333 "Config-Version", // Obsolete
334 "Replaces",
335 "Provides",
336 "Depends",
337 "Pre-Depends",
338 "Recommends",
339 "Suggests",
340 "Conflicts",
341 "Conffiles",
342 "Filename",
343 "Size",
344 "MD5Sum",
a7c835af 345 "SHA1Sum",
b2e465d6
AL
346 "MSDOS-Filename", // Obsolete
347 "Description",
348 0};
349static const char *iTFRewriteSourceOrder[] = {"Package",
350 "Source",
351 "Binary",
352 "Version",
353 "Priority",
354 "Section",
355 "Maintainer",
356 "Build-Depends",
357 "Build-Depends-Indep",
358 "Build-Conflicts",
359 "Build-Conflicts-Indep",
360 "Architecture",
361 "Standards-Version",
362 "Format",
363 "Directory",
364 "Files",
365 0};
366
367/* Two levels of initialization are used because gcc will set the symbol
368 size of an array to the length of the array, causing dynamic relinking
369 errors. Doing this makes the symbol size constant */
370const char **TFRewritePackageOrder = iTFRewritePackageOrder;
371const char **TFRewriteSourceOrder = iTFRewriteSourceOrder;
372
373bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
374 TFRewriteData *Rewrite)
375{
376 unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
377 for (unsigned I = 0; I != 256; I++)
378 Visited[I] = 0;
379
380 // Set new tag up as necessary.
381 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
382 {
383 if (Rewrite[J].NewTag == 0)
384 Rewrite[J].NewTag = Rewrite[J].Tag;
385 }
386
387 // Write all all of the tags, in order.
388 for (unsigned int I = 0; Order[I] != 0; I++)
389 {
390 bool Rewritten = false;
391
392 // See if this is a field that needs to be rewritten
393 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
394 {
395 if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
396 {
397 Visited[J] |= 2;
398 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
399 {
400 if (isspace(Rewrite[J].Rewrite[0]))
401 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
402 else
403 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
404 }
405
406 Rewritten = true;
407 break;
408 }
409 }
410
411 // See if it is in the fragment
412 unsigned Pos;
413 if (Tags.Find(Order[I],Pos) == false)
414 continue;
415 Visited[Pos] |= 1;
416
417 if (Rewritten == true)
418 continue;
419
420 /* Write out this element, taking a moment to rewrite the tag
421 in case of changes of case. */
422 const char *Start;
423 const char *Stop;
424 Tags.Get(Start,Stop,Pos);
425
426 if (fputs(Order[I],Output) < 0)
427 return _error->Errno("fputs","IO Error to output");
428 Start += strlen(Order[I]);
429 if (fwrite(Start,Stop - Start,1,Output) != 1)
430 return _error->Errno("fwrite","IO Error to output");
431 if (Stop[-1] != '\n')
432 fprintf(Output,"\n");
433 }
434
435 // Now write all the old tags that were missed.
436 for (unsigned int I = 0; I != Tags.Count(); I++)
437 {
438 if ((Visited[I] & 1) == 1)
439 continue;
440
441 const char *Start;
442 const char *Stop;
443 Tags.Get(Start,Stop,I);
444 const char *End = Start;
445 for (; End < Stop && *End != ':'; End++);
446
447 // See if this is a field that needs to be rewritten
448 bool Rewritten = false;
449 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
450 {
451 if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
452 {
453 Visited[J] |= 2;
454 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
455 {
456 if (isspace(Rewrite[J].Rewrite[0]))
457 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
458 else
459 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
460 }
461
462 Rewritten = true;
463 break;
464 }
465 }
466
467 if (Rewritten == true)
468 continue;
469
470 // Write out this element
471 if (fwrite(Start,Stop - Start,1,Output) != 1)
472 return _error->Errno("fwrite","IO Error to output");
473 if (Stop[-1] != '\n')
474 fprintf(Output,"\n");
475 }
476
477 // Now write all the rewrites that were missed
478 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
479 {
480 if ((Visited[J] & 2) == 2)
481 continue;
482
483 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
484 {
485 if (isspace(Rewrite[J].Rewrite[0]))
486 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
487 else
488 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
489 }
490 }
491
492 return true;
493}
494 /*}}}*/