* deb/deblistparser.cc:
[ntk/apt.git] / apt-pkg / deb / deblistparser.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: deblistparser.cc,v 1.29.2.5 2004/01/06 01:43:44 mdz Exp $
4 /* ######################################################################
5
6 Package Cache Generator - Generator for the cache structure.
7
8 This builds the cache structure from the abstract package list parser.
9
10 ##################################################################### */
11 /*}}}*/
12 // Include Files /*{{{*/
13 #include <config.h>
14
15 #include <apt-pkg/deblistparser.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/aptconfiguration.h>
19 #include <apt-pkg/strutl.h>
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/crc-16.h>
22 #include <apt-pkg/md5.h>
23 #include <apt-pkg/macros.h>
24
25 #include <fnmatch.h>
26 #include <ctype.h>
27 /*}}}*/
28
29 using std::string;
30
31 static debListParser::WordList PrioList[] = {{"important",pkgCache::State::Important},
32 {"required",pkgCache::State::Required},
33 {"standard",pkgCache::State::Standard},
34 {"optional",pkgCache::State::Optional},
35 {"extra",pkgCache::State::Extra},
36 {}};
37
38 // ListParser::debListParser - Constructor /*{{{*/
39 // ---------------------------------------------------------------------
40 /* Provide an architecture and only this one and "all" will be accepted
41 in Step(), if no Architecture is given we will accept every arch
42 we would accept in general with checkArchitecture() */
43 debListParser::debListParser(FileFd *File, string const &Arch) : Tags(File),
44 Arch(Arch) {
45 if (Arch == "native")
46 this->Arch = _config->Find("APT::Architecture");
47 Architectures = APT::Configuration::getArchitectures();
48 MultiArchEnabled = Architectures.size() > 1;
49 }
50 /*}}}*/
51 // ListParser::UniqFindTagWrite - Find the tag and write a unq string /*{{{*/
52 // ---------------------------------------------------------------------
53 /* */
54 unsigned long debListParser::UniqFindTagWrite(const char *Tag)
55 {
56 const char *Start;
57 const char *Stop;
58 if (Section.Find(Tag,Start,Stop) == false)
59 return 0;
60 return WriteUniqString(Start,Stop - Start);
61 }
62 /*}}}*/
63 // ListParser::Package - Return the package name /*{{{*/
64 // ---------------------------------------------------------------------
65 /* This is to return the name of the package this section describes */
66 string debListParser::Package() {
67 string const Result = Section.FindS("Package");
68 if(unlikely(Result.empty() == true))
69 _error->Error("Encountered a section with no Package: header");
70 return Result;
71 }
72 /*}}}*/
73 // ListParser::Architecture - Return the package arch /*{{{*/
74 // ---------------------------------------------------------------------
75 /* This will return the Architecture of the package this section describes */
76 string debListParser::Architecture() {
77 return Section.FindS("Architecture");
78 }
79 /*}}}*/
80 // ListParser::ArchitectureAll /*{{{*/
81 // ---------------------------------------------------------------------
82 /* */
83 bool debListParser::ArchitectureAll() {
84 return Section.FindS("Architecture") == "all";
85 }
86 /*}}}*/
87 // ListParser::Version - Return the version string /*{{{*/
88 // ---------------------------------------------------------------------
89 /* This is to return the string describing the version in debian form,
90 epoch:upstream-release. If this returns the blank string then the
91 entry is assumed to only describe package properties */
92 string debListParser::Version()
93 {
94 return Section.FindS("Version");
95 }
96 /*}}}*/
97 // ListParser::NewVersion - Fill in the version structure /*{{{*/
98 // ---------------------------------------------------------------------
99 /* */
100 bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
101 {
102 // Parse the section
103 Ver->Section = UniqFindTagWrite("Section");
104
105 // Parse multi-arch
106 string const MultiArch = Section.FindS("Multi-Arch");
107 if (MultiArch.empty() == true)
108 Ver->MultiArch = pkgCache::Version::None;
109 else if (MultiArch == "same") {
110 // Parse multi-arch
111 if (ArchitectureAll() == true)
112 {
113 /* Arch all packages can't be Multi-Arch: same */
114 _error->Warning("Architecture: all package '%s' can't be Multi-Arch: same",
115 Section.FindS("Package").c_str());
116 Ver->MultiArch = pkgCache::Version::None;
117 }
118 else
119 Ver->MultiArch = pkgCache::Version::Same;
120 }
121 else if (MultiArch == "foreign")
122 Ver->MultiArch = pkgCache::Version::Foreign;
123 else if (MultiArch == "allowed")
124 Ver->MultiArch = pkgCache::Version::Allowed;
125 else
126 {
127 _error->Warning("Unknown Multi-Arch type '%s' for package '%s'",
128 MultiArch.c_str(), Section.FindS("Package").c_str());
129 Ver->MultiArch = pkgCache::Version::None;
130 }
131
132 if (ArchitectureAll() == true)
133 Ver->MultiArch |= pkgCache::Version::All;
134
135 // Archive Size
136 Ver->Size = Section.FindULL("Size");
137 // Unpacked Size (in K)
138 Ver->InstalledSize = Section.FindULL("Installed-Size");
139 Ver->InstalledSize *= 1024;
140
141 // Priority
142 const char *Start;
143 const char *Stop;
144 if (Section.Find("Priority",Start,Stop) == true)
145 {
146 if (GrabWord(string(Start,Stop-Start),PrioList,Ver->Priority) == false)
147 Ver->Priority = pkgCache::State::Extra;
148 }
149
150 if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
151 return false;
152 if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
153 return false;
154 if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
155 return false;
156 if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
157 return false;
158 if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
159 return false;
160 if (ParseDepends(Ver,"Breaks",pkgCache::Dep::DpkgBreaks) == false)
161 return false;
162 if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
163 return false;
164 if (ParseDepends(Ver,"Enhances",pkgCache::Dep::Enhances) == false)
165 return false;
166
167 // Obsolete.
168 if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false)
169 return false;
170
171 if (ParseProvides(Ver) == false)
172 return false;
173
174 return true;
175 }
176 /*}}}*/
177 // ListParser::Description - Return the description string /*{{{*/
178 // ---------------------------------------------------------------------
179 /* This is to return the string describing the package in debian
180 form. If this returns the blank string then the entry is assumed to
181 only describe package properties */
182 string debListParser::Description()
183 {
184 string const lang = DescriptionLanguage();
185 if (lang.empty())
186 return Section.FindS("Description");
187 else
188 return Section.FindS(string("Description-").append(lang).c_str());
189 }
190 /*}}}*/
191 // ListParser::DescriptionLanguage - Return the description lang string /*{{{*/
192 // ---------------------------------------------------------------------
193 /* This is to return the string describing the language of
194 description. If this returns the blank string then the entry is
195 assumed to describe original description. */
196 string debListParser::DescriptionLanguage()
197 {
198 if (Section.FindS("Description").empty() == false)
199 return "";
200
201 std::vector<string> const lang = APT::Configuration::getLanguages(true);
202 for (std::vector<string>::const_iterator l = lang.begin();
203 l != lang.end(); ++l)
204 if (Section.FindS(string("Description-").append(*l).c_str()).empty() == false)
205 return *l;
206
207 return "";
208 }
209 /*}}}*/
210 // ListParser::Description - Return the description_md5 MD5SumValue /*{{{*/
211 // ---------------------------------------------------------------------
212 /* This is to return the md5 string to allow the check if it is the right
213 description. If no Description-md5 is found in the section it will be
214 calculated.
215 */
216 MD5SumValue debListParser::Description_md5()
217 {
218 string const value = Section.FindS("Description-md5");
219 if (value.empty() == true)
220 {
221 MD5Summation md5;
222 md5.Add((Description() + "\n").c_str());
223 return md5.Result();
224 }
225 else if (likely(value.size() == 32))
226 {
227 if (likely(value.find_first_not_of("0123456789abcdefABCDEF") == string::npos))
228 return MD5SumValue(value);
229 _error->Error("Malformed Description-md5 line; includes invalid character '%s'", value.c_str());
230 return MD5SumValue();
231 }
232 _error->Error("Malformed Description-md5 line; doesn't have the required length (32 != %d) '%s'", (int)value.size(), value.c_str());
233 return MD5SumValue();
234 }
235 /*}}}*/
236 // ListParser::UsePackage - Update a package structure /*{{{*/
237 // ---------------------------------------------------------------------
238 /* This is called to update the package with any new information
239 that might be found in the section */
240 bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg,
241 pkgCache::VerIterator &Ver)
242 {
243 if (Pkg->Section == 0)
244 Pkg->Section = UniqFindTagWrite("Section");
245
246 string const static myArch = _config->Find("APT::Architecture");
247 // Possible values are: "all", "native", "installed" and "none"
248 // The "installed" mode is handled by ParseStatus(), See #544481 and friends.
249 string const static essential = _config->Find("pkgCacheGen::Essential", "all");
250 if (essential == "all" ||
251 (essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()))
252 if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
253 return false;
254 if (Section.FindFlag("Important",Pkg->Flags,pkgCache::Flag::Important) == false)
255 return false;
256
257 if (strcmp(Pkg.Name(),"apt") == 0)
258 {
259 if ((essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()) ||
260 essential == "all")
261 Pkg->Flags |= pkgCache::Flag::Essential | pkgCache::Flag::Important;
262 else
263 Pkg->Flags |= pkgCache::Flag::Important;
264 }
265
266 if (ParseStatus(Pkg,Ver) == false)
267 return false;
268 return true;
269 }
270 /*}}}*/
271 // ListParser::VersionHash - Compute a unique hash for this version /*{{{*/
272 // ---------------------------------------------------------------------
273 /* */
274 unsigned short debListParser::VersionHash()
275 {
276 const char *Sections[] ={"Installed-Size",
277 "Depends",
278 "Pre-Depends",
279 // "Suggests",
280 // "Recommends",
281 "Conflicts",
282 "Breaks",
283 "Replaces",0};
284 unsigned long Result = INIT_FCS;
285 char S[1024];
286 for (const char **I = Sections; *I != 0; I++)
287 {
288 const char *Start;
289 const char *End;
290 if (Section.Find(*I,Start,End) == false || End - Start >= (signed)sizeof(S))
291 continue;
292
293 /* Strip out any spaces from the text, this undoes dpkgs reformatting
294 of certain fields. dpkg also has the rather interesting notion of
295 reformatting depends operators < -> <= */
296 char *J = S;
297 for (; Start != End; Start++)
298 {
299 if (isspace(*Start) == 0)
300 *J++ = tolower_ascii(*Start);
301 if (*Start == '<' && Start[1] != '<' && Start[1] != '=')
302 *J++ = '=';
303 if (*Start == '>' && Start[1] != '>' && Start[1] != '=')
304 *J++ = '=';
305 }
306
307 Result = AddCRC16(Result,S,J - S);
308 }
309
310 return Result;
311 }
312 /*}}}*/
313 // ListParser::ParseStatus - Parse the status field /*{{{*/
314 // ---------------------------------------------------------------------
315 /* Status lines are of the form,
316 Status: want flag status
317 want = unknown, install, hold, deinstall, purge
318 flag = ok, reinstreq, hold, hold-reinstreq
319 status = not-installed, unpacked, half-configured,
320 half-installed, config-files, post-inst-failed,
321 removal-failed, installed
322
323 Some of the above are obsolete (I think?) flag = hold-* and
324 status = post-inst-failed, removal-failed at least.
325 */
326 bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
327 pkgCache::VerIterator &Ver)
328 {
329 const char *Start;
330 const char *Stop;
331 if (Section.Find("Status",Start,Stop) == false)
332 return true;
333
334 // UsePackage() is responsible for setting the flag in the default case
335 bool const static essential = _config->Find("pkgCacheGen::Essential", "") == "installed";
336 if (essential == true &&
337 Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
338 return false;
339
340 // Isolate the first word
341 const char *I = Start;
342 for(; I < Stop && *I != ' '; I++);
343 if (I >= Stop || *I != ' ')
344 return _error->Error("Malformed Status line");
345
346 // Process the want field
347 WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
348 {"install",pkgCache::State::Install},
349 {"hold",pkgCache::State::Hold},
350 {"deinstall",pkgCache::State::DeInstall},
351 {"purge",pkgCache::State::Purge},
352 {}};
353 if (GrabWord(string(Start,I-Start),WantList,Pkg->SelectedState) == false)
354 return _error->Error("Malformed 1st word in the Status line");
355
356 // Isloate the next word
357 I++;
358 Start = I;
359 for(; I < Stop && *I != ' '; I++);
360 if (I >= Stop || *I != ' ')
361 return _error->Error("Malformed status line, no 2nd word");
362
363 // Process the flag field
364 WordList FlagList[] = {{"ok",pkgCache::State::Ok},
365 {"reinstreq",pkgCache::State::ReInstReq},
366 {"hold",pkgCache::State::HoldInst},
367 {"hold-reinstreq",pkgCache::State::HoldReInstReq},
368 {}};
369 if (GrabWord(string(Start,I-Start),FlagList,Pkg->InstState) == false)
370 return _error->Error("Malformed 2nd word in the Status line");
371
372 // Isloate the last word
373 I++;
374 Start = I;
375 for(; I < Stop && *I != ' '; I++);
376 if (I != Stop)
377 return _error->Error("Malformed Status line, no 3rd word");
378
379 // Process the flag field
380 WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
381 {"unpacked",pkgCache::State::UnPacked},
382 {"half-configured",pkgCache::State::HalfConfigured},
383 {"installed",pkgCache::State::Installed},
384 {"half-installed",pkgCache::State::HalfInstalled},
385 {"config-files",pkgCache::State::ConfigFiles},
386 {"triggers-awaited",pkgCache::State::TriggersAwaited},
387 {"triggers-pending",pkgCache::State::TriggersPending},
388 {"post-inst-failed",pkgCache::State::HalfConfigured},
389 {"removal-failed",pkgCache::State::HalfInstalled},
390 {}};
391 if (GrabWord(string(Start,I-Start),StatusList,Pkg->CurrentState) == false)
392 return _error->Error("Malformed 3rd word in the Status line");
393
394 /* A Status line marks the package as indicating the current
395 version as well. Only if it is actually installed.. Otherwise
396 the interesting dpkg handling of the status file creates bogus
397 entries. */
398 if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
399 Pkg->CurrentState == pkgCache::State::ConfigFiles))
400 {
401 if (Ver.end() == true)
402 _error->Warning("Encountered status field in a non-version description");
403 else
404 Pkg->CurrentVer = Ver.Index();
405 }
406
407 return true;
408 }
409
410 const char *debListParser::ConvertRelation(const char *I,unsigned int &Op)
411 {
412 // Determine the operator
413 switch (*I)
414 {
415 case '<':
416 I++;
417 if (*I == '=')
418 {
419 I++;
420 Op = pkgCache::Dep::LessEq;
421 break;
422 }
423
424 if (*I == '<')
425 {
426 I++;
427 Op = pkgCache::Dep::Less;
428 break;
429 }
430
431 // < is the same as <= and << is really Cs < for some reason
432 Op = pkgCache::Dep::LessEq;
433 break;
434
435 case '>':
436 I++;
437 if (*I == '=')
438 {
439 I++;
440 Op = pkgCache::Dep::GreaterEq;
441 break;
442 }
443
444 if (*I == '>')
445 {
446 I++;
447 Op = pkgCache::Dep::Greater;
448 break;
449 }
450
451 // > is the same as >= and >> is really Cs > for some reason
452 Op = pkgCache::Dep::GreaterEq;
453 break;
454
455 case '=':
456 Op = pkgCache::Dep::Equals;
457 I++;
458 break;
459
460 // HACK around bad package definitions
461 default:
462 Op = pkgCache::Dep::Equals;
463 break;
464 }
465 return I;
466 }
467
468 /*
469 * CompleteArch:
470 *
471 * The complete architecture, consisting of <kernel>-<cpu>.
472 */
473 static string CompleteArch(std::string const &arch) {
474 if (arch == "armel") return "linux-arm";
475 if (arch == "armhf") return "linux-arm";
476 if (arch == "lpia") return "linux-i386";
477 if (arch == "powerpcspe") return "linux-powerpc";
478 if (arch == "uclibc-linux-armel") return "linux-arm";
479 if (arch == "uclinux-armel") return "uclinux-arm";
480
481 return (arch.find("-") != string::npos) ? arch : "linux-" + arch;
482 }
483 /*}}}*/
484 // ListParser::ParseDepends - Parse a dependency element /*{{{*/
485 // ---------------------------------------------------------------------
486 /* This parses the dependency elements out of a standard string in place,
487 bit by bit. */
488 const char *debListParser::ParseDepends(const char *Start,const char *Stop,
489 string &Package,string &Ver,
490 unsigned int &Op, bool const &ParseArchFlags,
491 bool const &StripMultiArch)
492 {
493 // Strip off leading space
494 for (;Start != Stop && isspace(*Start) != 0; Start++);
495
496 // Parse off the package name
497 const char *I = Start;
498 for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
499 *I != ',' && *I != '|' && *I != '[' && *I != ']'; I++);
500
501 // Malformed, no '('
502 if (I != Stop && *I == ')')
503 return 0;
504
505 if (I == Start)
506 return 0;
507
508 // Stash the package name
509 Package.assign(Start,I - Start);
510
511 // We don't want to confuse library users which can't handle MultiArch
512 string const arch = _config->Find("APT::Architecture");
513 if (StripMultiArch == true) {
514 size_t const found = Package.rfind(':');
515 if (found != string::npos &&
516 (strcmp(Package.c_str() + found, ":any") == 0 ||
517 strcmp(Package.c_str() + found, ":native") == 0 ||
518 strcmp(Package.c_str() + found + 1, arch.c_str()) == 0))
519 Package = Package.substr(0,found);
520 }
521
522 // Skip white space to the '('
523 for (;I != Stop && isspace(*I) != 0 ; I++);
524
525 // Parse a version
526 if (I != Stop && *I == '(')
527 {
528 // Skip the '('
529 for (I++; I != Stop && isspace(*I) != 0 ; I++);
530 if (I + 3 >= Stop)
531 return 0;
532 I = ConvertRelation(I,Op);
533
534 // Skip whitespace
535 for (;I != Stop && isspace(*I) != 0; I++);
536 Start = I;
537 I = (const char*) memchr(I, ')', Stop - I);
538 if (I == NULL || Start == I)
539 return 0;
540
541 // Skip trailing whitespace
542 const char *End = I;
543 for (; End > Start && isspace(End[-1]); End--);
544
545 Ver.assign(Start,End-Start);
546 I++;
547 }
548 else
549 {
550 Ver.clear();
551 Op = pkgCache::Dep::NoOp;
552 }
553
554 // Skip whitespace
555 for (;I != Stop && isspace(*I) != 0; I++);
556
557 if (ParseArchFlags == true)
558 {
559 string completeArch = CompleteArch(arch);
560
561 // Parse an architecture
562 if (I != Stop && *I == '[')
563 {
564 ++I;
565 // malformed
566 if (unlikely(I == Stop))
567 return 0;
568
569 const char *End = I;
570 bool Found = false;
571 bool NegArch = false;
572 while (I != Stop)
573 {
574 // look for whitespace or ending ']'
575 for (;End != Stop && !isspace(*End) && *End != ']'; ++End);
576
577 if (unlikely(End == Stop))
578 return 0;
579
580 if (*I == '!')
581 {
582 NegArch = true;
583 ++I;
584 }
585
586 if (stringcmp(arch,I,End) == 0) {
587 Found = true;
588 } else {
589 std::string wildcard = SubstVar(string(I, End), "any", "*");
590 if (fnmatch(wildcard.c_str(), completeArch.c_str(), 0) == 0)
591 Found = true;
592 }
593
594 if (Found == true)
595 {
596 if (I[-1] != '!')
597 NegArch = false;
598 // we found a match, so fast-forward to the end of the wildcards
599 for (; End != Stop && *End != ']'; ++End);
600 }
601
602 if (*End++ == ']') {
603 I = End;
604 break;
605 }
606
607 I = End;
608 for (;I != Stop && isspace(*I) != 0; I++);
609 }
610
611 if (NegArch == true)
612 Found = !Found;
613
614 if (Found == false)
615 Package = ""; /* not for this arch */
616 }
617
618 // Skip whitespace
619 for (;I != Stop && isspace(*I) != 0; I++);
620 }
621
622 if (I != Stop && *I == '|')
623 Op |= pkgCache::Dep::Or;
624
625 if (I == Stop || *I == ',' || *I == '|')
626 {
627 if (I != Stop)
628 for (I++; I != Stop && isspace(*I) != 0; I++);
629 return I;
630 }
631
632 return 0;
633 }
634 /*}}}*/
635 // ListParser::ParseDepends - Parse a dependency list /*{{{*/
636 // ---------------------------------------------------------------------
637 /* This is the higher level depends parser. It takes a tag and generates
638 a complete depends tree for the given version. */
639 bool debListParser::ParseDepends(pkgCache::VerIterator &Ver,
640 const char *Tag,unsigned int Type)
641 {
642 const char *Start;
643 const char *Stop;
644 if (Section.Find(Tag,Start,Stop) == false)
645 return true;
646
647 string const pkgArch = Ver.Arch();
648
649 while (1)
650 {
651 string Package;
652 string Version;
653 unsigned int Op;
654
655 Start = ParseDepends(Start,Stop,Package,Version,Op,false,!MultiArchEnabled);
656 if (Start == 0)
657 return _error->Error("Problem parsing dependency %s",Tag);
658 size_t const found = Package.rfind(':');
659
660 if (MultiArchEnabled == true &&
661 (Type == pkgCache::Dep::Conflicts ||
662 Type == pkgCache::Dep::DpkgBreaks ||
663 Type == pkgCache::Dep::Replaces))
664 {
665 for (std::vector<std::string>::const_iterator a = Architectures.begin();
666 a != Architectures.end(); ++a)
667 if (NewDepends(Ver,Package,*a,Version,Op,Type) == false)
668 return false;
669 }
670 else if (MultiArchEnabled == true && found != string::npos &&
671 strcmp(Package.c_str() + found, ":any") != 0)
672 {
673 string Arch = Package.substr(found+1, string::npos);
674 Package = Package.substr(0, found);
675 // Such dependencies are not supposed to be accepted …
676 // … but this is probably the best thing to do.
677 if (Arch == "native")
678 Arch = _config->Find("APT::Architecture");
679 if (NewDepends(Ver,Package,Arch,Version,Op,Type) == false)
680 return false;
681 }
682 else if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false)
683 return false;
684 if (Start == Stop)
685 break;
686 }
687 return true;
688 }
689 /*}}}*/
690 // ListParser::ParseProvides - Parse the provides list /*{{{*/
691 // ---------------------------------------------------------------------
692 /* */
693 bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
694 {
695 const char *Start;
696 const char *Stop;
697 if (Section.Find("Provides",Start,Stop) == true)
698 {
699 string Package;
700 string Version;
701 string const Arch = Ver.Arch();
702 unsigned int Op;
703
704 while (1)
705 {
706 Start = ParseDepends(Start,Stop,Package,Version,Op);
707 if (Start == 0)
708 return _error->Error("Problem parsing Provides line");
709 if (Op != pkgCache::Dep::NoOp) {
710 _error->Warning("Ignoring Provides line with DepCompareOp for package %s", Package.c_str());
711 } else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) {
712 if (NewProvidesAllArch(Ver, Package, Version) == false)
713 return false;
714 } else {
715 if (NewProvides(Ver, Package, Arch, Version) == false)
716 return false;
717 }
718
719 if (Start == Stop)
720 break;
721 }
722 }
723
724 if (MultiArchEnabled == false)
725 return true;
726 else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
727 {
728 string const Package = string(Ver.ParentPkg().Name()).append(":").append("any");
729 return NewProvidesAllArch(Ver, Package, Ver.VerStr());
730 }
731 else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
732 return NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr());
733
734 return true;
735 }
736 /*}}}*/
737 // ListParser::NewProvides - add provides for all architectures /*{{{*/
738 bool debListParser::NewProvidesAllArch(pkgCache::VerIterator &Ver, string const &Package,
739 string const &Version) {
740 for (std::vector<string>::const_iterator a = Architectures.begin();
741 a != Architectures.end(); ++a)
742 {
743 if (NewProvides(Ver, Package, *a, Version) == false)
744 return false;
745 }
746 return true;
747 }
748 /*}}}*/
749 // ListParser::GrabWord - Matches a word and returns /*{{{*/
750 // ---------------------------------------------------------------------
751 /* Looks for a word in a list of words - for ParseStatus */
752 bool debListParser::GrabWord(string Word,WordList *List,unsigned char &Out)
753 {
754 for (unsigned int C = 0; List[C].Str != 0; C++)
755 {
756 if (strcasecmp(Word.c_str(),List[C].Str) == 0)
757 {
758 Out = List[C].Val;
759 return true;
760 }
761 }
762 return false;
763 }
764 /*}}}*/
765 // ListParser::Step - Move to the next section in the file /*{{{*/
766 // ---------------------------------------------------------------------
767 /* This has to be carefull to only process the correct architecture */
768 bool debListParser::Step()
769 {
770 iOffset = Tags.Offset();
771 while (Tags.Step(Section) == true)
772 {
773 /* See if this is the correct Architecture, if it isn't then we
774 drop the whole section. A missing arch tag only happens (in theory)
775 inside the Status file, so that is a positive return */
776 string const Architecture = Section.FindS("Architecture");
777 if (Architecture.empty() == true)
778 return true;
779
780 if (Arch.empty() == true || Arch == "any" || MultiArchEnabled == false)
781 {
782 if (APT::Configuration::checkArchitecture(Architecture) == true)
783 return true;
784 }
785 else
786 {
787 if (Architecture == Arch)
788 return true;
789
790 if (Architecture == "all" && Arch == _config->Find("APT::Architecture"))
791 return true;
792 }
793
794 iOffset = Tags.Offset();
795 }
796 return false;
797 }
798 /*}}}*/
799 // ListParser::LoadReleaseInfo - Load the release information /*{{{*/
800 // ---------------------------------------------------------------------
801 /* */
802 bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator &FileI,
803 FileFd &File, string component)
804 {
805 // apt-secure does no longer download individual (per-section) Release
806 // file. to provide Component pinning we use the section name now
807 FileI->Component = WriteUniqString(component);
808
809 // FIXME: Code depends on the fact that Release files aren't compressed
810 FILE* release = fdopen(dup(File.Fd()), "r");
811 if (release == NULL)
812 return false;
813
814 char buffer[101];
815 bool gpgClose = false;
816 while (fgets(buffer, sizeof(buffer), release) != NULL)
817 {
818 size_t len = 0;
819
820 // Skip empty lines
821 for (; buffer[len] == '\r' && buffer[len] == '\n'; ++len)
822 /* nothing */
823 ;
824 if (buffer[len] == '\0')
825 continue;
826
827 // only evalute the first GPG section
828 if (strncmp("-----", buffer, 5) == 0)
829 {
830 if (gpgClose == true)
831 break;
832 gpgClose = true;
833 continue;
834 }
835
836 // seperate the tag from the data
837 const char* dataStart = strchr(buffer + len, ':');
838 if (dataStart == NULL)
839 continue;
840 len = dataStart - buffer;
841 for (++dataStart; *dataStart == ' '; ++dataStart)
842 /* nothing */
843 ;
844 const char* dataEnd = (const char*)rawmemchr(dataStart, '\0');
845 // The last char should be a newline, but we can never be sure: #633350
846 const char* lineEnd = dataEnd;
847 for (--lineEnd; *lineEnd == '\r' || *lineEnd == '\n'; --lineEnd)
848 /* nothing */
849 ;
850 ++lineEnd;
851
852 // which datastorage need to be updated
853 enum { Suite, Component, Version, Origin, Codename, Label, None } writeTo = None;
854 if (buffer[0] == ' ')
855 ;
856 #define APT_PARSER_WRITETO(X) else if (strncmp(#X, buffer, len) == 0) writeTo = X;
857 APT_PARSER_WRITETO(Suite)
858 APT_PARSER_WRITETO(Component)
859 APT_PARSER_WRITETO(Version)
860 APT_PARSER_WRITETO(Origin)
861 APT_PARSER_WRITETO(Codename)
862 APT_PARSER_WRITETO(Label)
863 #undef APT_PARSER_WRITETO
864 #define APT_PARSER_FLAGIT(X) else if (strncmp(#X, buffer, len) == 0) \
865 pkgTagSection::FindFlag(FileI->Flags, pkgCache::Flag:: X, dataStart, lineEnd);
866 APT_PARSER_FLAGIT(NotAutomatic)
867 APT_PARSER_FLAGIT(ButAutomaticUpgrades)
868 #undef APT_PARSER_FLAGIT
869
870 // load all data from the line and save it
871 string data;
872 if (writeTo != None)
873 data.append(dataStart, dataEnd);
874 if (sizeof(buffer) - 1 == (dataEnd - buffer))
875 {
876 while (fgets(buffer, sizeof(buffer), release) != NULL)
877 {
878 if (writeTo != None)
879 data.append(buffer);
880 if (strlen(buffer) != sizeof(buffer) - 1)
881 break;
882 }
883 }
884 if (writeTo != None)
885 {
886 // remove spaces and stuff from the end of the data line
887 for (std::string::reverse_iterator s = data.rbegin();
888 s != data.rend(); ++s)
889 {
890 if (*s != '\r' && *s != '\n' && *s != ' ')
891 break;
892 *s = '\0';
893 }
894 switch (writeTo) {
895 case Suite: FileI->Archive = WriteUniqString(data); break;
896 case Component: FileI->Component = WriteUniqString(data); break;
897 case Version: FileI->Version = WriteUniqString(data); break;
898 case Origin: FileI->Origin = WriteUniqString(data); break;
899 case Codename: FileI->Codename = WriteUniqString(data); break;
900 case Label: FileI->Label = WriteUniqString(data); break;
901 case None: break;
902 }
903 }
904 }
905 fclose(release);
906
907 return !_error->PendingError();
908 }
909 /*}}}*/
910 // ListParser::GetPrio - Convert the priority from a string /*{{{*/
911 // ---------------------------------------------------------------------
912 /* */
913 unsigned char debListParser::GetPrio(string Str)
914 {
915 unsigned char Out;
916 if (GrabWord(Str,PrioList,Out) == false)
917 Out = pkgCache::State::Extra;
918
919 return Out;
920 }
921 /*}}}*/