]> git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
initial Breaks implementation
[apt.git] / apt-pkg / tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz Exp $
4 /* ######################################################################
5
6 Fast scanner for RFC-822 type header information
7
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.
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #ifdef __GNUG__
15 #pragma implementation "apt-pkg/tagfile.h"
16 #endif
17
18 #include <apt-pkg/tagfile.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/strutl.h>
21
22 #include <apti18n.h>
23
24 #include <string>
25 #include <stdio.h>
26 #include <ctype.h>
27 /*}}}*/
28
29 using std::string;
30
31 // TagFile::pkgTagFile - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
33 /* */
34 pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) :
35 Fd(*pFd),
36 Size(Size),
37 Map(NULL),
38 Buffer(0)
39 {
40 if (Fd.IsOpen() == false)
41 {
42 Start = End = Buffer = 0;
43 Done = true;
44 iOffset = 0;
45 Map = NULL;
46 return;
47 }
48
49 // check if we can MMap it
50 if(Fd.Size() == 0)
51 {
52 Buffer = new char[Size];
53 Start = End = Buffer;
54 Done = false;
55 Fill();
56 } else {
57 Map = new MMap (Fd, MMap::Public | MMap::ReadOnly);
58 Buffer = (char *) Map->Data ();
59 Start = Buffer;
60 End = Buffer + Map->Size ();
61 }
62 iOffset = 0;
63 }
64 /*}}}*/
65 // TagFile::~pkgTagFile - Destructor /*{{{*/
66 // ---------------------------------------------------------------------
67 /* */
68 pkgTagFile::~pkgTagFile()
69 {
70 if(!Map) delete [] Buffer;
71 delete Map;
72 }
73 /*}}}*/
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)
78 {
79 if ((Map != NULL) && (Start == End))
80 return false;
81
82 if (Tag.Scan(Start,End - Start) == false)
83 {
84 if (Map != NULL)
85 return _error->Error(_("Unable to parse package file %s (1)"),
86 Fd.Name().c_str());
87
88 if (Fill() == false)
89 return false;
90
91 if (Tag.Scan(Start,End - Start) == false)
92 return _error->Error(_("Unable to parse package file %s (1)"),
93 Fd.Name().c_str());
94 }
95 Start += Tag.size();
96 iOffset += Tag.size();
97
98 Tag.Trim();
99 return true;
100 }
101 /*}}}*/
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()
107 {
108 unsigned long EndSize = End - Start;
109 unsigned long Actual = 0;
110
111 memmove(Buffer,Start,EndSize);
112 Start = Buffer;
113 End = Buffer + EndSize;
114
115 if (Done == false)
116 {
117 // See if only a bit of the file is left
118 if (Fd.Read(End,Size - (End - Buffer),&Actual) == false)
119 return false;
120 if (Actual != Size - (End - Buffer))
121 Done = true;
122 End += Actual;
123 }
124
125 if (Done == true)
126 {
127 if (EndSize <= 3 && Actual == 0)
128 return false;
129 if (Size - (End - Buffer) < 4)
130 return true;
131
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--)
135 if (*E == '\n')
136 LineCount++;
137 for (; LineCount < 2; LineCount++)
138 *End++ = '\n';
139
140 return true;
141 }
142
143 return true;
144 }
145 /*}}}*/
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
149 that is there */
150 bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
151 {
152 // We are within a buffer space of the next hit..
153 if (Offset >= iOffset && iOffset + (End - Start) > Offset)
154 {
155 unsigned long Dist = Offset - iOffset;
156 Start += Dist;
157 iOffset += Dist;
158 return Step(Tag);
159 }
160
161 iOffset = Offset;
162 if (Map != NULL)
163 {
164 Start = Buffer + iOffset;
165 }
166 else
167 {
168 // Reposition and reload..
169 Done = false;
170 if (Fd.Seek(Offset) == false)
171 return false;
172 End = Start = Buffer;
173
174 if (Fill() == false)
175 return false;
176
177 if (Tag.Scan(Start,End - Start) == true)
178 return true;
179
180 // This appends a double new line (for the real eof handling)
181 if (Fill() == false)
182 return false;
183 }
184
185 if (Tag.Scan(Start,End - Start) == false)
186 return _error->Error(_("Unable to parse package file %s (2)"),Fd.Name().c_str());
187
188 return true;
189 }
190 /*}}}*/
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)
197 {
198 unsigned long Res = 0;
199 for (; Text != End && *Text != ':' && *Text != 0; Text++)
200 Res = (unsigned long)(*Text) ^ (Res << 2);
201 return Res & 0xFF;
202 }
203
204 bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
205 {
206 const char *End = Start + MaxLength;
207 Stop = Section = Start;
208 memset(AlphaIndexes,0,sizeof(AlphaIndexes));
209
210 if (Stop == 0 || MaxLength == 0)
211 return false;
212
213 TagCount = 0;
214 while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
215 {
216 // Start a new index and add it to the hash
217 if (isspace(Stop[0]) == 0)
218 {
219 Indexes[TagCount++] = Stop - Section;
220 AlphaIndexes[AlphaHash(Stop,End)] = TagCount;
221 }
222
223 Stop = (const char *)memchr(Stop,'\n',End - Stop);
224
225 if (Stop == 0)
226 return false;
227
228 for (; Stop+1 < End && Stop[1] == '\r'; Stop++);
229
230 // Double newline marks the end of the record
231 if (Stop+1 < End && Stop[1] == '\n')
232 {
233 Indexes[TagCount] = Stop - Section;
234 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
235 return true;
236 }
237
238 Stop++;
239 }
240
241 if ((Stop+1 >= End) && (End[-1] == '\n' || End[-1] == '\r'))
242 {
243 Indexes[TagCount] = (End - 1) - Section;
244 return true;
245 }
246
247 return false;
248 }
249 /*}}}*/
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()
254 {
255 for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
256 }
257 /*}}}*/
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
262 {
263 unsigned int Length = strlen(Tag);
264 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
265 if (I == 0)
266 return false;
267 I--;
268
269 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
270 I = (I+1)%TagCount)
271 {
272 const char *St;
273 St = Section + Indexes[I];
274 if (strncasecmp(Tag,St,Length) != 0)
275 continue;
276
277 // Make sure the colon is in the right place
278 const char *C = St + Length;
279 for (; isspace(*C) != 0; C++);
280 if (*C != ':')
281 continue;
282 Pos = I;
283 return true;
284 }
285
286 Pos = 0;
287 return false;
288 }
289 /*}}}*/
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
295 {
296 unsigned int Length = strlen(Tag);
297 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
298 if (I == 0)
299 return false;
300 I--;
301
302 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
303 I = (I+1)%TagCount)
304 {
305 const char *St;
306 St = Section + Indexes[I];
307 if (strncasecmp(Tag,St,Length) != 0)
308 continue;
309
310 // Make sure the colon is in the right place
311 const char *C = St + Length;
312 for (; isspace(*C) != 0; C++);
313 if (*C != ':')
314 continue;
315
316 // Strip off the gunk from the start end
317 Start = C;
318 End = Section + Indexes[I+1];
319 if (Start >= End)
320 return _error->Error("Internal parsing error");
321
322 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
323 for (; isspace(End[-1]) != 0 && End > Start; End--);
324
325 return true;
326 }
327
328 Start = End = 0;
329 return false;
330 }
331 /*}}}*/
332 // TagSection::FindS - Find a string /*{{{*/
333 // ---------------------------------------------------------------------
334 /* */
335 string pkgTagSection::FindS(const char *Tag) const
336 {
337 const char *Start;
338 const char *End;
339 if (Find(Tag,Start,End) == false)
340 return string();
341 return string(Start,End);
342 }
343 /*}}}*/
344 // TagSection::FindI - Find an integer /*{{{*/
345 // ---------------------------------------------------------------------
346 /* */
347 signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
348 {
349 const char *Start;
350 const char *Stop;
351 if (Find(Tag,Start,Stop) == false)
352 return Default;
353
354 // Copy it into a temp buffer so we can use strtol
355 char S[300];
356 if ((unsigned)(Stop - Start) >= sizeof(S))
357 return Default;
358 strncpy(S,Start,Stop-Start);
359 S[Stop - Start] = 0;
360
361 char *End;
362 signed long Result = strtol(S,&End,10);
363 if (S == End)
364 return Default;
365 return Result;
366 }
367 /*}}}*/
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
373 {
374 const char *Start;
375 const char *Stop;
376 if (Find(Tag,Start,Stop) == false)
377 return true;
378
379 switch (StringToBool(string(Start,Stop)))
380 {
381 case 0:
382 Flags &= ~Flag;
383 return true;
384
385 case 1:
386 Flags |= Flag;
387 return true;
388
389 default:
390 _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
391 return true;
392 }
393 return true;
394 }
395 /*}}}*/
396
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. */
402
403 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
404 array. */
405 static const char *iTFRewritePackageOrder[] = {
406 "Package",
407 "Essential",
408 "Status",
409 "Priority",
410 "Section",
411 "Installed-Size",
412 "Maintainer",
413 "Architecture",
414 "Source",
415 "Version",
416 "Revision", // Obsolete
417 "Config-Version", // Obsolete
418 "Replaces",
419 "Provides",
420 "Depends",
421 "Pre-Depends",
422 "Recommends",
423 "Suggests",
424 "Conflicts",
425 "Breaks",
426 "Conffiles",
427 "Filename",
428 "Size",
429 "MD5Sum",
430 "SHA1",
431 "SHA256",
432 "MSDOS-Filename", // Obsolete
433 "Description",
434 0};
435 static const char *iTFRewriteSourceOrder[] = {"Package",
436 "Source",
437 "Binary",
438 "Version",
439 "Priority",
440 "Section",
441 "Maintainer",
442 "Build-Depends",
443 "Build-Depends-Indep",
444 "Build-Conflicts",
445 "Build-Conflicts-Indep",
446 "Architecture",
447 "Standards-Version",
448 "Format",
449 "Directory",
450 "Files",
451 0};
452
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;
458
459 bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
460 TFRewriteData *Rewrite)
461 {
462 unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
463 for (unsigned I = 0; I != 256; I++)
464 Visited[I] = 0;
465
466 // Set new tag up as necessary.
467 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
468 {
469 if (Rewrite[J].NewTag == 0)
470 Rewrite[J].NewTag = Rewrite[J].Tag;
471 }
472
473 // Write all all of the tags, in order.
474 for (unsigned int I = 0; Order[I] != 0; I++)
475 {
476 bool Rewritten = false;
477
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++)
480 {
481 if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
482 {
483 Visited[J] |= 2;
484 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
485 {
486 if (isspace(Rewrite[J].Rewrite[0]))
487 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
488 else
489 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
490 }
491
492 Rewritten = true;
493 break;
494 }
495 }
496
497 // See if it is in the fragment
498 unsigned Pos;
499 if (Tags.Find(Order[I],Pos) == false)
500 continue;
501 Visited[Pos] |= 1;
502
503 if (Rewritten == true)
504 continue;
505
506 /* Write out this element, taking a moment to rewrite the tag
507 in case of changes of case. */
508 const char *Start;
509 const char *Stop;
510 Tags.Get(Start,Stop,Pos);
511
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");
519 }
520
521 // Now write all the old tags that were missed.
522 for (unsigned int I = 0; I != Tags.Count(); I++)
523 {
524 if ((Visited[I] & 1) == 1)
525 continue;
526
527 const char *Start;
528 const char *Stop;
529 Tags.Get(Start,Stop,I);
530 const char *End = Start;
531 for (; End < Stop && *End != ':'; End++);
532
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++)
536 {
537 if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
538 {
539 Visited[J] |= 2;
540 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
541 {
542 if (isspace(Rewrite[J].Rewrite[0]))
543 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
544 else
545 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
546 }
547
548 Rewritten = true;
549 break;
550 }
551 }
552
553 if (Rewritten == true)
554 continue;
555
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");
561 }
562
563 // Now write all the rewrites that were missed
564 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
565 {
566 if ((Visited[J] & 2) == 2)
567 continue;
568
569 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
570 {
571 if (isspace(Rewrite[J].Rewrite[0]))
572 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
573 else
574 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
575 }
576 }
577
578 return true;
579 }
580 /*}}}*/