]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/deblistparser.cc
debListParser: ParseDepends: Only query native arch if needed
[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/cachefilter.h>
19 #include <apt-pkg/aptconfiguration.h>
20 #include <apt-pkg/strutl.h>
21 #include <apt-pkg/crc-16.h>
22 #include <apt-pkg/md5.h>
23 #include <apt-pkg/pkgcache.h>
24 #include <apt-pkg/cacheiterators.h>
25 #include <apt-pkg/tagfile.h>
26 #include <apt-pkg/macros.h>
27
28 #include <stddef.h>
29 #include <string.h>
30 #include <algorithm>
31 #include <string>
32 #include <vector>
33 #include <ctype.h>
34 /*}}}*/
35
36 using std::string;
37
38 static const debListParser::WordList PrioList[] = {
39 {"required",pkgCache::State::Required},
40 {"important",pkgCache::State::Important},
41 {"standard",pkgCache::State::Standard},
42 {"optional",pkgCache::State::Optional},
43 {"extra",pkgCache::State::Extra},
44 {NULL, 0}};
45
46 // ListParser::debListParser - Constructor /*{{{*/
47 // ---------------------------------------------------------------------
48 /* Provide an architecture and only this one and "all" will be accepted
49 in Step(), if no Architecture is given we will accept every arch
50 we would accept in general with checkArchitecture() */
51 debListParser::debListParser(FileFd *File) :
52 pkgCacheListParser(), d(NULL), Tags(File)
53 {
54 }
55 /*}}}*/
56 // ListParser::Package - Return the package name /*{{{*/
57 // ---------------------------------------------------------------------
58 /* This is to return the name of the package this section describes */
59 string debListParser::Package() {
60 string Result = Section.FindS("Package");
61
62 // Normalize mixed case package names to lower case, like dpkg does
63 // See Bug#807012 for details
64 std::transform(Result.begin(), Result.end(), Result.begin(), tolower_ascii);
65
66 if(unlikely(Result.empty() == true))
67 _error->Error("Encountered a section with no Package: header");
68 return Result;
69 }
70 /*}}}*/
71 // ListParser::Architecture - Return the package arch /*{{{*/
72 // ---------------------------------------------------------------------
73 /* This will return the Architecture of the package this section describes */
74 string debListParser::Architecture() {
75 std::string const Arch = Section.FindS("Architecture");
76 return Arch.empty() ? "none" : Arch;
77 }
78 /*}}}*/
79 // ListParser::ArchitectureAll /*{{{*/
80 // ---------------------------------------------------------------------
81 /* */
82 bool debListParser::ArchitectureAll() {
83 return Section.FindS("Architecture") == "all";
84 }
85 /*}}}*/
86 // ListParser::Version - Return the version string /*{{{*/
87 // ---------------------------------------------------------------------
88 /* This is to return the string describing the version in debian form,
89 epoch:upstream-release. If this returns the blank string then the
90 entry is assumed to only describe package properties */
91 string debListParser::Version()
92 {
93 return Section.FindS("Version");
94 }
95 /*}}}*/
96 unsigned char debListParser::ParseMultiArch(bool const showErrors) /*{{{*/
97 {
98 unsigned char MA;
99 string const MultiArch = Section.FindS("Multi-Arch");
100 if (MultiArch.empty() == true || MultiArch == "no")
101 MA = pkgCache::Version::No;
102 else if (MultiArch == "same") {
103 if (ArchitectureAll() == true)
104 {
105 if (showErrors == true)
106 _error->Warning("Architecture: all package '%s' can't be Multi-Arch: same",
107 Section.FindS("Package").c_str());
108 MA = pkgCache::Version::No;
109 }
110 else
111 MA = pkgCache::Version::Same;
112 }
113 else if (MultiArch == "foreign")
114 MA = pkgCache::Version::Foreign;
115 else if (MultiArch == "allowed")
116 MA = pkgCache::Version::Allowed;
117 else
118 {
119 if (showErrors == true)
120 _error->Warning("Unknown Multi-Arch type '%s' for package '%s'",
121 MultiArch.c_str(), Section.FindS("Package").c_str());
122 MA = pkgCache::Version::No;
123 }
124
125 if (ArchitectureAll() == true)
126 MA |= pkgCache::Version::All;
127
128 return MA;
129 }
130 /*}}}*/
131 // ListParser::NewVersion - Fill in the version structure /*{{{*/
132 // ---------------------------------------------------------------------
133 /* */
134 bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
135 {
136 const char *Start;
137 const char *Stop;
138
139 // Parse the section
140 if (Section.Find("Section",Start,Stop) == true)
141 {
142 map_stringitem_t const idx = StoreString(pkgCacheGenerator::SECTION, Start, Stop - Start);
143 Ver->Section = idx;
144 }
145 // Parse the source package name
146 pkgCache::GrpIterator const G = Ver.ParentPkg().Group();
147 Ver->SourcePkgName = G->Name;
148 Ver->SourceVerStr = Ver->VerStr;
149 if (Section.Find("Source",Start,Stop) == true)
150 {
151 const char * const Space = (const char * const) memchr(Start, ' ', Stop - Start);
152 pkgCache::VerIterator V;
153
154 if (Space != NULL)
155 {
156 Stop = Space;
157 const char * const Open = (const char * const) memchr(Space, '(', Stop - Space);
158 if (likely(Open != NULL))
159 {
160 const char * const Close = (const char * const) memchr(Open, ')', Stop - Open);
161 if (likely(Close != NULL))
162 {
163 std::string const version(Open + 1, (Close - Open) - 1);
164 if (version != Ver.VerStr())
165 {
166 map_stringitem_t const idx = StoreString(pkgCacheGenerator::VERSIONNUMBER, version);
167 Ver->SourceVerStr = idx;
168 }
169 }
170 }
171 }
172
173 std::string const pkgname(Start, Stop - Start);
174 if (pkgname != G.Name())
175 {
176 for (pkgCache::PkgIterator P = G.PackageList(); P.end() == false; P = G.NextPkg(P))
177 {
178 for (V = P.VersionList(); V.end() == false; ++V)
179 {
180 if (pkgname == V.SourcePkgName())
181 {
182 Ver->SourcePkgName = V->SourcePkgName;
183 break;
184 }
185 }
186 if (V.end() == false)
187 break;
188 }
189 if (V.end() == true)
190 {
191 map_stringitem_t const idx = StoreString(pkgCacheGenerator::PKGNAME, pkgname);
192 Ver->SourcePkgName = idx;
193 }
194 }
195 }
196
197 Ver->MultiArch = ParseMultiArch(true);
198 // Archive Size
199 Ver->Size = Section.FindULL("Size");
200 // Unpacked Size (in K)
201 Ver->InstalledSize = Section.FindULL("Installed-Size");
202 Ver->InstalledSize *= 1024;
203
204 // Priority
205 if (Section.Find("Priority",Start,Stop) == true)
206 {
207 if (GrabWord(string(Start,Stop-Start),PrioList,Ver->Priority) == false)
208 Ver->Priority = pkgCache::State::Extra;
209 }
210
211 if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
212 return false;
213 if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
214 return false;
215 if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
216 return false;
217 if (ParseDepends(Ver,"Breaks",pkgCache::Dep::DpkgBreaks) == false)
218 return false;
219 if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
220 return false;
221 if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
222 return false;
223 if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
224 return false;
225 if (ParseDepends(Ver,"Enhances",pkgCache::Dep::Enhances) == false)
226 return false;
227 // Obsolete.
228 if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false)
229 return false;
230
231 if (ParseProvides(Ver) == false)
232 return false;
233
234 return true;
235 }
236 /*}}}*/
237 // ListParser::Description - Return the description string /*{{{*/
238 // ---------------------------------------------------------------------
239 /* This is to return the string describing the package in debian
240 form. If this returns the blank string then the entry is assumed to
241 only describe package properties */
242 string debListParser::Description(std::string const &lang)
243 {
244 if (lang.empty())
245 return Section.FindS("Description");
246 else
247 return Section.FindS(string("Description-").append(lang).c_str());
248 }
249 /*}}}*/
250 // ListParser::AvailableDescriptionLanguages /*{{{*/
251 std::vector<std::string> debListParser::AvailableDescriptionLanguages()
252 {
253 std::vector<std::string> const understood = APT::Configuration::getLanguages();
254 std::vector<std::string> avail;
255 if (Section.Exists("Description") == true)
256 avail.push_back("");
257 for (std::vector<std::string>::const_iterator lang = understood.begin(); lang != understood.end(); ++lang)
258 {
259 std::string const tagname = "Description-" + *lang;
260 if (Section.Exists(tagname.c_str()) == true)
261 avail.push_back(*lang);
262 }
263 return avail;
264 }
265 /*}}}*/
266 // ListParser::Description_md5 - Return the description_md5 MD5SumValue /*{{{*/
267 // ---------------------------------------------------------------------
268 /* This is to return the md5 string to allow the check if it is the right
269 description. If no Description-md5 is found in the section it will be
270 calculated.
271 */
272 MD5SumValue debListParser::Description_md5()
273 {
274 string const value = Section.FindS("Description-md5");
275 if (value.empty() == true)
276 {
277 std::string const desc = Description("") + "\n";
278 if (desc == "\n")
279 return MD5SumValue();
280
281 MD5Summation md5;
282 md5.Add(desc.c_str());
283 return md5.Result();
284 }
285 else if (likely(value.size() == 32))
286 {
287 if (likely(value.find_first_not_of("0123456789abcdefABCDEF") == string::npos))
288 return MD5SumValue(value);
289 _error->Error("Malformed Description-md5 line; includes invalid character '%s'", value.c_str());
290 return MD5SumValue();
291 }
292 _error->Error("Malformed Description-md5 line; doesn't have the required length (32 != %d) '%s'", (int)value.size(), value.c_str());
293 return MD5SumValue();
294 }
295 /*}}}*/
296 // ListParser::UsePackage - Update a package structure /*{{{*/
297 // ---------------------------------------------------------------------
298 /* This is called to update the package with any new information
299 that might be found in the section */
300 bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg,
301 pkgCache::VerIterator &Ver)
302 {
303 string const static myArch = _config->Find("APT::Architecture");
304 // Possible values are: "all", "native", "installed" and "none"
305 // The "installed" mode is handled by ParseStatus(), See #544481 and friends.
306 string const static essential = _config->Find("pkgCacheGen::Essential", "all");
307 if (essential == "all" ||
308 (essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()))
309 if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
310 return false;
311 if (Section.FindFlag("Important",Pkg->Flags,pkgCache::Flag::Important) == false)
312 return false;
313
314 if (strcmp(Pkg.Name(),"apt") == 0)
315 {
316 if ((essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()) ||
317 essential == "all")
318 Pkg->Flags |= pkgCache::Flag::Essential | pkgCache::Flag::Important;
319 else
320 Pkg->Flags |= pkgCache::Flag::Important;
321 }
322
323 if (ParseStatus(Pkg,Ver) == false)
324 return false;
325 return true;
326 }
327 /*}}}*/
328 // ListParser::VersionHash - Compute a unique hash for this version /*{{{*/
329 // ---------------------------------------------------------------------
330 /* */
331 unsigned short debListParser::VersionHash()
332 {
333 const char *Sections[] ={"Installed-Size",
334 "Depends",
335 "Pre-Depends",
336 // "Suggests",
337 // "Recommends",
338 "Conflicts",
339 "Breaks",
340 "Replaces",0};
341 unsigned long Result = INIT_FCS;
342 char S[1024];
343 for (const char * const *I = Sections; *I != 0; ++I)
344 {
345 const char *Start;
346 const char *End;
347 if (Section.Find(*I,Start,End) == false || End - Start >= (signed)sizeof(S))
348 continue;
349
350 /* Strip out any spaces from the text, this undoes dpkgs reformatting
351 of certain fields. dpkg also has the rather interesting notion of
352 reformatting depends operators < -> <= */
353 char *J = S;
354 for (; Start != End; ++Start)
355 {
356 if (isspace_ascii(*Start) != 0)
357 continue;
358 *J++ = tolower_ascii(*Start);
359
360 if ((*Start == '<' || *Start == '>') && Start[1] != *Start && Start[1] != '=')
361 *J++ = '=';
362 }
363
364 Result = AddCRC16(Result,S,J - S);
365 }
366
367 return Result;
368 }
369 /*}}}*/
370 // StatusListParser::ParseStatus - Parse the status field /*{{{*/
371 // ---------------------------------------------------------------------
372 /* Status lines are of the form,
373 Status: want flag status
374 want = unknown, install, hold, deinstall, purge
375 flag = ok, reinstreq
376 status = not-installed, config-files, half-installed, unpacked,
377 half-configured, triggers-awaited, triggers-pending, installed
378 */
379 bool debListParser::ParseStatus(pkgCache::PkgIterator &,
380 pkgCache::VerIterator &)
381 {
382 return true;
383 }
384 bool debStatusListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
385 pkgCache::VerIterator &Ver)
386 {
387 const char *Start;
388 const char *Stop;
389 if (Section.Find("Status",Start,Stop) == false)
390 return true;
391
392 // UsePackage() is responsible for setting the flag in the default case
393 bool const static essential = _config->Find("pkgCacheGen::Essential", "") == "installed";
394 if (essential == true &&
395 Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
396 return false;
397
398 // Isolate the first word
399 const char *I = Start;
400 for(; I < Stop && *I != ' '; I++);
401 if (I >= Stop || *I != ' ')
402 return _error->Error("Malformed Status line");
403
404 // Process the want field
405 WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
406 {"install",pkgCache::State::Install},
407 {"hold",pkgCache::State::Hold},
408 {"deinstall",pkgCache::State::DeInstall},
409 {"purge",pkgCache::State::Purge},
410 {NULL, 0}};
411 if (GrabWord(string(Start,I-Start),WantList,Pkg->SelectedState) == false)
412 return _error->Error("Malformed 1st word in the Status line");
413
414 // Isloate the next word
415 I++;
416 Start = I;
417 for(; I < Stop && *I != ' '; I++);
418 if (I >= Stop || *I != ' ')
419 return _error->Error("Malformed status line, no 2nd word");
420
421 // Process the flag field
422 WordList FlagList[] = {{"ok",pkgCache::State::Ok},
423 {"reinstreq",pkgCache::State::ReInstReq},
424 {"hold",pkgCache::State::HoldInst},
425 {"hold-reinstreq",pkgCache::State::HoldReInstReq},
426 {NULL, 0}};
427 if (GrabWord(string(Start,I-Start),FlagList,Pkg->InstState) == false)
428 return _error->Error("Malformed 2nd word in the Status line");
429
430 // Isloate the last word
431 I++;
432 Start = I;
433 for(; I < Stop && *I != ' '; I++);
434 if (I != Stop)
435 return _error->Error("Malformed Status line, no 3rd word");
436
437 // Process the flag field
438 WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
439 {"config-files",pkgCache::State::ConfigFiles},
440 {"half-installed",pkgCache::State::HalfInstalled},
441 {"unpacked",pkgCache::State::UnPacked},
442 {"half-configured",pkgCache::State::HalfConfigured},
443 {"triggers-awaited",pkgCache::State::TriggersAwaited},
444 {"triggers-pending",pkgCache::State::TriggersPending},
445 {"installed",pkgCache::State::Installed},
446 {NULL, 0}};
447 if (GrabWord(string(Start,I-Start),StatusList,Pkg->CurrentState) == false)
448 return _error->Error("Malformed 3rd word in the Status line");
449
450 /* A Status line marks the package as indicating the current
451 version as well. Only if it is actually installed.. Otherwise
452 the interesting dpkg handling of the status file creates bogus
453 entries. */
454 if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
455 Pkg->CurrentState == pkgCache::State::ConfigFiles))
456 {
457 if (Ver.end() == true)
458 _error->Warning("Encountered status field in a non-version description");
459 else
460 Pkg->CurrentVer = Ver.Index();
461 }
462
463 return true;
464 }
465
466 const char *debListParser::ConvertRelation(const char *I,unsigned int &Op)
467 {
468 // Determine the operator
469 switch (*I)
470 {
471 case '<':
472 I++;
473 if (*I == '=')
474 {
475 I++;
476 Op = pkgCache::Dep::LessEq;
477 break;
478 }
479
480 if (*I == '<')
481 {
482 I++;
483 Op = pkgCache::Dep::Less;
484 break;
485 }
486
487 // < is the same as <= and << is really Cs < for some reason
488 Op = pkgCache::Dep::LessEq;
489 break;
490
491 case '>':
492 I++;
493 if (*I == '=')
494 {
495 I++;
496 Op = pkgCache::Dep::GreaterEq;
497 break;
498 }
499
500 if (*I == '>')
501 {
502 I++;
503 Op = pkgCache::Dep::Greater;
504 break;
505 }
506
507 // > is the same as >= and >> is really Cs > for some reason
508 Op = pkgCache::Dep::GreaterEq;
509 break;
510
511 case '=':
512 Op = pkgCache::Dep::Equals;
513 I++;
514 break;
515
516 // HACK around bad package definitions
517 default:
518 Op = pkgCache::Dep::Equals;
519 break;
520 }
521 return I;
522 }
523 /*}}}*/
524 // ListParser::ParseDepends - Parse a dependency element /*{{{*/
525 // ---------------------------------------------------------------------
526 /* This parses the dependency elements out of a standard string in place,
527 bit by bit. */
528 const char *debListParser::ParseDepends(const char *Start,const char *Stop,
529 std::string &Package,std::string &Ver,unsigned int &Op)
530 { return ParseDepends(Start, Stop, Package, Ver, Op, false, true, false); }
531 const char *debListParser::ParseDepends(const char *Start,const char *Stop,
532 std::string &Package,std::string &Ver,unsigned int &Op,
533 bool const &ParseArchFlags)
534 { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, true, false); }
535 const char *debListParser::ParseDepends(const char *Start,const char *Stop,
536 std::string &Package,std::string &Ver,unsigned int &Op,
537 bool const &ParseArchFlags, bool const &StripMultiArch)
538 { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, StripMultiArch, false); }
539 const char *debListParser::ParseDepends(const char *Start,const char *Stop,
540 string &Package,string &Ver,
541 unsigned int &Op, bool const &ParseArchFlags,
542 bool const &StripMultiArch,
543 bool const &ParseRestrictionsList)
544 {
545 // Strip off leading space
546 for (;Start != Stop && isspace_ascii(*Start) != 0; ++Start);
547
548 // Parse off the package name
549 const char *I = Start;
550 for (;I != Stop && isspace_ascii(*I) == 0 && *I != '(' && *I != ')' &&
551 *I != ',' && *I != '|' && *I != '[' && *I != ']' &&
552 *I != '<' && *I != '>'; ++I);
553
554 // Malformed, no '('
555 if (I != Stop && *I == ')')
556 return 0;
557
558 if (I == Start)
559 return 0;
560
561 // Stash the package name
562 Package.assign(Start,I - Start);
563
564 // We don't want to confuse library users which can't handle MultiArch
565 if (StripMultiArch == true) {
566 string const arch = _config->Find("APT::Architecture");
567 size_t const found = Package.rfind(':');
568 if (found != string::npos &&
569 (strcmp(Package.c_str() + found, ":any") == 0 ||
570 strcmp(Package.c_str() + found, ":native") == 0 ||
571 strcmp(Package.c_str() + found + 1, arch.c_str()) == 0))
572 Package = Package.substr(0,found);
573 }
574
575 // Skip white space to the '('
576 for (;I != Stop && isspace_ascii(*I) != 0 ; I++);
577
578 // Parse a version
579 if (I != Stop && *I == '(')
580 {
581 // Skip the '('
582 for (I++; I != Stop && isspace_ascii(*I) != 0 ; I++);
583 if (I + 3 >= Stop)
584 return 0;
585 I = ConvertRelation(I,Op);
586
587 // Skip whitespace
588 for (;I != Stop && isspace_ascii(*I) != 0; I++);
589 Start = I;
590 I = (const char*) memchr(I, ')', Stop - I);
591 if (I == NULL || Start == I)
592 return 0;
593
594 // Skip trailing whitespace
595 const char *End = I;
596 for (; End > Start && isspace_ascii(End[-1]); End--);
597
598 Ver.assign(Start,End-Start);
599 I++;
600 }
601 else
602 {
603 Ver.clear();
604 Op = pkgCache::Dep::NoOp;
605 }
606
607 // Skip whitespace
608 for (;I != Stop && isspace_ascii(*I) != 0; I++);
609
610 if (ParseArchFlags == true)
611 {
612 string const arch = _config->Find("APT::Architecture");
613 APT::CacheFilter::PackageArchitectureMatchesSpecification matchesArch(arch, false);
614
615 // Parse an architecture
616 if (I != Stop && *I == '[')
617 {
618 ++I;
619 // malformed
620 if (unlikely(I == Stop))
621 return 0;
622
623 const char *End = I;
624 bool Found = false;
625 bool NegArch = false;
626 while (I != Stop)
627 {
628 // look for whitespace or ending ']'
629 for (;End != Stop && !isspace_ascii(*End) && *End != ']'; ++End);
630
631 if (unlikely(End == Stop))
632 return 0;
633
634 if (*I == '!')
635 {
636 NegArch = true;
637 ++I;
638 }
639
640 std::string arch(I, End);
641 if (arch.empty() == false && matchesArch(arch.c_str()) == true)
642 {
643 Found = true;
644 if (I[-1] != '!')
645 NegArch = false;
646 // we found a match, so fast-forward to the end of the wildcards
647 for (; End != Stop && *End != ']'; ++End);
648 }
649
650 if (*End++ == ']') {
651 I = End;
652 break;
653 }
654
655 I = End;
656 for (;I != Stop && isspace_ascii(*I) != 0; I++);
657 }
658
659 if (NegArch == true)
660 Found = !Found;
661
662 if (Found == false)
663 Package = ""; /* not for this arch */
664 }
665
666 // Skip whitespace
667 for (;I != Stop && isspace_ascii(*I) != 0; I++);
668 }
669
670 if (ParseRestrictionsList == true)
671 {
672 // Parse a restrictions formula which is in disjunctive normal form:
673 // (foo AND bar) OR (blub AND bla)
674
675 std::vector<string> const profiles = APT::Configuration::getBuildProfiles();
676
677 // if the next character is a restriction list, then by default the
678 // dependency does not apply and the conditions have to be checked
679 // if the next character is not a restriction list, then by default the
680 // dependency applies
681 bool applies1 = (*I != '<');
682 while (I != Stop)
683 {
684 if (*I != '<')
685 break;
686
687 ++I;
688 // malformed
689 if (unlikely(I == Stop))
690 return 0;
691
692 const char *End = I;
693
694 // if of the prior restriction list is already fulfilled, then
695 // we can just skip to the end of the current list
696 if (applies1) {
697 for (;End != Stop && *End != '>'; ++End);
698 I = ++End;
699 // skip whitespace
700 for (;I != Stop && isspace_ascii(*I) != 0; I++);
701 } else {
702 bool applies2 = true;
703 // all the conditions inside a restriction list have to be
704 // met so once we find one that is not met, we can skip to
705 // the end of this list
706 while (I != Stop)
707 {
708 // look for whitespace or ending '>'
709 // End now points to the character after the current term
710 for (;End != Stop && !isspace_ascii(*End) && *End != '>'; ++End);
711
712 if (unlikely(End == Stop))
713 return 0;
714
715 bool NegRestriction = false;
716 if (*I == '!')
717 {
718 NegRestriction = true;
719 ++I;
720 }
721
722 std::string restriction(I, End);
723
724 if (restriction.empty() == false && profiles.empty() == false &&
725 std::find(profiles.begin(), profiles.end(), restriction) != profiles.end())
726 {
727 if (NegRestriction) {
728 applies2 = false;
729 // since one of the terms does not apply we don't have to check the others
730 for (; End != Stop && *End != '>'; ++End);
731 }
732 } else {
733 if (!NegRestriction) {
734 applies2 = false;
735 // since one of the terms does not apply we don't have to check the others
736 for (; End != Stop && *End != '>'; ++End);
737 }
738 }
739
740 if (*End++ == '>') {
741 I = End;
742 // skip whitespace
743 for (;I != Stop && isspace_ascii(*I) != 0; I++);
744 break;
745 }
746
747 I = End;
748 // skip whitespace
749 for (;I != Stop && isspace_ascii(*I) != 0; I++);
750 }
751 if (applies2) {
752 applies1 = true;
753 }
754 }
755 }
756
757 if (applies1 == false) {
758 Package = ""; //not for this restriction
759 }
760 }
761
762 if (I != Stop && *I == '|')
763 Op |= pkgCache::Dep::Or;
764
765 if (I == Stop || *I == ',' || *I == '|')
766 {
767 if (I != Stop)
768 for (I++; I != Stop && isspace_ascii(*I) != 0; I++);
769 return I;
770 }
771
772 return 0;
773 }
774 /*}}}*/
775 // ListParser::ParseDepends - Parse a dependency list /*{{{*/
776 // ---------------------------------------------------------------------
777 /* This is the higher level depends parser. It takes a tag and generates
778 a complete depends tree for the given version. */
779 bool debListParser::ParseDepends(pkgCache::VerIterator &Ver,
780 const char *Tag,unsigned int Type)
781 {
782 const char *Start;
783 const char *Stop;
784 if (Section.Find(Tag,Start,Stop) == false)
785 return true;
786
787 string const pkgArch = Ver.Arch();
788
789 while (1)
790 {
791 string Package;
792 string Version;
793 unsigned int Op;
794
795 Start = ParseDepends(Start, Stop, Package, Version, Op, false, false, false);
796 if (Start == 0)
797 return _error->Error("Problem parsing dependency %s",Tag);
798 size_t const found = Package.rfind(':');
799
800 if (found == string::npos)
801 {
802 if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false)
803 return false;
804 }
805 else if (strcmp(Package.c_str() + found, ":any") == 0)
806 {
807 if (NewDepends(Ver,Package,"any",Version,Op,Type) == false)
808 return false;
809 }
810 else
811 {
812 // Such dependencies are not supposed to be accepted …
813 // … but this is probably the best thing to do anyway
814 if (Package.substr(found + 1) == "native")
815 Package = Package.substr(0, found) + ':' + Ver.Cache()->NativeArch();
816
817 if (NewDepends(Ver, Package, "any", Version, Op | pkgCache::Dep::ArchSpecific, Type) == false)
818 return false;
819 }
820
821 if (Start == Stop)
822 break;
823 }
824 return true;
825 }
826 /*}}}*/
827 // ListParser::ParseProvides - Parse the provides list /*{{{*/
828 // ---------------------------------------------------------------------
829 /* */
830 bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
831 {
832 /* it is unlikely, but while parsing dependencies, we might have already
833 picked up multi-arch implicit provides which we do not want to duplicate here */
834 bool hasProvidesAlready = false;
835 std::string const spzName = Ver.ParentPkg().FullName(false);
836 {
837 for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv)
838 {
839 if (Prv.IsMultiArchImplicit() == false || (Prv->Flags & pkgCache::Flag::ArchSpecific) == 0)
840 continue;
841 if (spzName != Prv.OwnerPkg().FullName(false))
842 continue;
843 hasProvidesAlready = true;
844 break;
845 }
846 }
847
848 string const Arch = Ver.Arch();
849 const char *Start;
850 const char *Stop;
851 if (Section.Find("Provides",Start,Stop) == true)
852 {
853 string Package;
854 string Version;
855 unsigned int Op;
856
857 do
858 {
859 Start = ParseDepends(Start,Stop,Package,Version,Op, false, false, false);
860 const size_t archfound = Package.rfind(':');
861 if (Start == 0)
862 return _error->Error("Problem parsing Provides line");
863 if (Op != pkgCache::Dep::NoOp && Op != pkgCache::Dep::Equals) {
864 _error->Warning("Ignoring Provides line with non-equal DepCompareOp for package %s", Package.c_str());
865 } else if (archfound != string::npos) {
866 std::string spzArch = Package.substr(archfound + 1);
867 if (spzArch != "any")
868 {
869 if (NewProvides(Ver, Package.substr(0, archfound), spzArch, Version, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false)
870 return false;
871 }
872 if (NewProvides(Ver, Package, "any", Version, pkgCache::Flag::ArchSpecific) == false)
873 return false;
874 } else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) {
875 if (APT::Configuration::checkArchitecture(Arch))
876 if (NewProvidesAllArch(Ver, Package, Version, 0) == false)
877 return false;
878 } else {
879 if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
880 {
881 if (NewProvides(Ver, Package + ":any", "any", Version, pkgCache::Flag::MultiArchImplicit) == false)
882 return false;
883 }
884 if (NewProvides(Ver, Package, Arch, Version, 0) == false)
885 return false;
886 }
887 if (archfound == std::string::npos)
888 {
889 std::string const spzName = Package + ':' + Ver.ParentPkg().Arch();
890 pkgCache::PkgIterator const spzPkg = Ver.Cache()->FindPkg(spzName, "any");
891 if (spzPkg.end() == false)
892 {
893 if (NewProvides(Ver, spzName, "any", Version, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false)
894 return false;
895 }
896 }
897 } while (Start != Stop);
898 }
899
900 if (APT::Configuration::checkArchitecture(Arch))
901 {
902 if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
903 {
904 string const Package = string(Ver.ParentPkg().Name()).append(":").append("any");
905 if (NewProvides(Ver, Package, "any", Ver.VerStr(), pkgCache::Flag::MultiArchImplicit) == false)
906 return false;
907 }
908 else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
909 {
910 if (NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr(), pkgCache::Flag::MultiArchImplicit) == false)
911 return false;
912 }
913 }
914
915 if (hasProvidesAlready == false)
916 {
917 pkgCache::PkgIterator const spzPkg = Ver.Cache()->FindPkg(spzName, "any");
918 if (spzPkg.end() == false)
919 {
920 if (NewProvides(Ver, spzName, "any", Ver.VerStr(), pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false)
921 return false;
922 }
923 }
924 return true;
925 }
926 /*}}}*/
927 // ListParser::GrabWord - Matches a word and returns /*{{{*/
928 // ---------------------------------------------------------------------
929 /* Looks for a word in a list of words - for ParseStatus */
930 bool debListParser::GrabWord(string Word,const WordList *List,unsigned char &Out)
931 {
932 for (unsigned int C = 0; List[C].Str != 0; C++)
933 {
934 if (strcasecmp(Word.c_str(),List[C].Str) == 0)
935 {
936 Out = List[C].Val;
937 return true;
938 }
939 }
940 return false;
941 }
942 /*}}}*/
943 // ListParser::Step - Move to the next section in the file /*{{{*/
944 // ---------------------------------------------------------------------
945 /* This has to be careful to only process the correct architecture */
946 bool debListParser::Step()
947 {
948 iOffset = Tags.Offset();
949 return Tags.Step(Section);
950 }
951 /*}}}*/
952 // ListParser::GetPrio - Convert the priority from a string /*{{{*/
953 // ---------------------------------------------------------------------
954 /* */
955 unsigned char debListParser::GetPrio(string Str)
956 {
957 unsigned char Out;
958 if (GrabWord(Str,PrioList,Out) == false)
959 Out = pkgCache::State::Extra;
960
961 return Out;
962 }
963 /*}}}*/
964 bool debListParser::SameVersion(unsigned short const Hash, /*{{{*/
965 pkgCache::VerIterator const &Ver)
966 {
967 if (pkgCacheListParser::SameVersion(Hash, Ver) == false)
968 return false;
969 // status file has no (Download)Size, but all others are fair game
970 // status file is parsed last, so the first version we encounter is
971 // probably also the version we have downloaded
972 unsigned long long const Size = Section.FindULL("Size");
973 if (Size != 0 && Size != Ver->Size)
974 return false;
975 // available everywhere, but easier to check here than to include in VersionHash
976 unsigned char MultiArch = ParseMultiArch(false);
977 if (MultiArch != Ver->MultiArch)
978 return false;
979 // for all practical proposes (we can check): same version
980 return true;
981 }
982 /*}}}*/
983
984 debDebFileParser::debDebFileParser(FileFd *File, std::string const &DebFile)
985 : debListParser(File), DebFile(DebFile)
986 {
987 }
988
989 bool debDebFileParser::UsePackage(pkgCache::PkgIterator &Pkg,
990 pkgCache::VerIterator &Ver)
991 {
992 bool res = debListParser::UsePackage(Pkg, Ver);
993 // we use the full file path as a provides so that the file is found
994 // by its name
995 if(NewProvides(Ver, DebFile, Pkg.Cache()->NativeArch(), Ver.VerStr(), 0) == false)
996 return false;
997 return res;
998 }
999
1000 debListParser::~debListParser() {}