]> git.saurik.com Git - apt.git/blame - apt-pkg/deb/deblistparser.cc
Merge remote-tracking branch 'upstream/debian/experimental' into feature/acq-trans
[apt.git] / apt-pkg / deb / deblistparser.cc
CommitLineData
f55a958f
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 3// $Id: deblistparser.cc,v 1.29.2.5 2004/01/06 01:43:44 mdz Exp $
f55a958f
AL
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 /*{{{*/
ea542140
DK
13#include <config.h>
14
094a497d
AL
15#include <apt-pkg/deblistparser.h>
16#include <apt-pkg/error.h>
17#include <apt-pkg/configuration.h>
424ff669 18#include <apt-pkg/cachefilter.h>
45df0ad2 19#include <apt-pkg/aptconfiguration.h>
cdcc6d34 20#include <apt-pkg/strutl.h>
472ff00e 21#include <apt-pkg/fileutl.h>
204fbdcc 22#include <apt-pkg/crc-16.h>
a52f938b 23#include <apt-pkg/md5.h>
453b82a3
DK
24#include <apt-pkg/mmap.h>
25#include <apt-pkg/pkgcache.h>
26#include <apt-pkg/cacheiterators.h>
27#include <apt-pkg/tagfile.h>
5c0d3668 28#include <apt-pkg/macros.h>
9c14e3d6 29
453b82a3
DK
30#include <stddef.h>
31#include <string.h>
32#include <algorithm>
33#include <string>
34#include <vector>
e7b470ee 35#include <ctype.h>
f55a958f
AL
36 /*}}}*/
37
8f3ba4e8
DK
38using std::string;
39
84de0cea
MV
40static debListParser::WordList PrioList[] = {
41 {"required",pkgCache::State::Required},
42 {"important",pkgCache::State::Important},
43 {"standard",pkgCache::State::Standard},
44 {"optional",pkgCache::State::Optional},
45 {"extra",pkgCache::State::Extra},
67c3067f 46 {NULL, 0}};
b2e465d6 47
f55a958f
AL
48// ListParser::debListParser - Constructor /*{{{*/
49// ---------------------------------------------------------------------
5dd4c8b8
DK
50/* Provide an architecture and only this one and "all" will be accepted
51 in Step(), if no Architecture is given we will accept every arch
52 we would accept in general with checkArchitecture() */
53debListParser::debListParser(FileFd *File, string const &Arch) : Tags(File),
54 Arch(Arch) {
33dd02e3
DK
55 if (Arch == "native")
56 this->Arch = _config->Find("APT::Architecture");
dcfa253f
DK
57 Architectures = APT::Configuration::getArchitectures();
58 MultiArchEnabled = Architectures.size() > 1;
0149949b
AL
59}
60 /*}}}*/
f55a958f
AL
61// ListParser::Package - Return the package name /*{{{*/
62// ---------------------------------------------------------------------
63/* This is to return the name of the package this section describes */
5bf15716 64string debListParser::Package() {
33dd02e3
DK
65 string const Result = Section.FindS("Package");
66 if(unlikely(Result.empty() == true))
67 _error->Error("Encountered a section with no Package: header");
68 return Result;
5bf15716
DK
69}
70 /*}}}*/
71// ListParser::Architecture - Return the package arch /*{{{*/
72// ---------------------------------------------------------------------
28166356 73/* This will return the Architecture of the package this section describes */
5bf15716 74string debListParser::Architecture() {
99a2ea5a 75 return Section.FindS("Architecture");
f55a958f
AL
76}
77 /*}}}*/
857e9c13
DK
78// ListParser::ArchitectureAll /*{{{*/
79// ---------------------------------------------------------------------
80/* */
81bool debListParser::ArchitectureAll() {
33dd02e3 82 return Section.FindS("Architecture") == "all";
f55a958f
AL
83}
84 /*}}}*/
85// ListParser::Version - Return the version string /*{{{*/
86// ---------------------------------------------------------------------
87/* This is to return the string describing the version in debian form,
88 epoch:upstream-release. If this returns the blank string then the
89 entry is assumed to only describe package properties */
90string debListParser::Version()
91{
b0b4efb9 92 return Section.FindS("Version");
f55a958f
AL
93}
94 /*}}}*/
e33dbfe5 95unsigned char debListParser::ParseMultiArch(bool const showErrors) /*{{{*/
0149949b 96{
e33dbfe5 97 unsigned char MA;
28166356 98 string const MultiArch = Section.FindS("Multi-Arch");
19fb04e8 99 if (MultiArch.empty() == true || MultiArch == "no")
e33dbfe5 100 MA = pkgCache::Version::None;
28166356 101 else if (MultiArch == "same") {
ca238ede 102 if (ArchitectureAll() == true)
25396fb0 103 {
e33dbfe5
DK
104 if (showErrors == true)
105 _error->Warning("Architecture: all package '%s' can't be Multi-Arch: same",
106 Section.FindS("Package").c_str());
107 MA = pkgCache::Version::None;
25396fb0 108 }
28166356 109 else
e33dbfe5 110 MA = pkgCache::Version::Same;
28166356
DK
111 }
112 else if (MultiArch == "foreign")
e33dbfe5 113 MA = pkgCache::Version::Foreign;
28166356 114 else if (MultiArch == "allowed")
e33dbfe5 115 MA = pkgCache::Version::Allowed;
28166356
DK
116 else
117 {
e33dbfe5
DK
118 if (showErrors == true)
119 _error->Warning("Unknown Multi-Arch type '%s' for package '%s'",
120 MultiArch.c_str(), Section.FindS("Package").c_str());
121 MA = pkgCache::Version::None;
25396fb0
DK
122 }
123
ca238ede 124 if (ArchitectureAll() == true)
e33dbfe5 125 MA |= pkgCache::Version::All;
ca238ede 126
e33dbfe5
DK
127 return MA;
128}
129 /*}}}*/
130// ListParser::NewVersion - Fill in the version structure /*{{{*/
131// ---------------------------------------------------------------------
132/* */
133bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
134{
78a5476f
DK
135 const char *Start;
136 const char *Stop;
137
e33dbfe5 138 // Parse the section
78a5476f
DK
139 if (Section.Find("Section",Start,Stop) == true)
140 {
141 map_stringitem_t const idx = StoreString(pkgCacheGenerator::SECTION, Start, Stop - Start);
142 Ver->Section = idx;
143 }
a221efc3
DK
144 // Parse the source package name
145 pkgCache::GrpIterator const G = Ver.ParentPkg().Group();
146 Ver->SourcePkgName = G->Name;
147 Ver->SourceVerStr = Ver->VerStr;
148 if (Section.Find("Source",Start,Stop) == true)
149 {
150 const char * const Space = (const char * const) memchr(Start, ' ', Stop - Start);
151 pkgCache::VerIterator V;
152
153 if (Space != NULL)
154 {
155 Stop = Space;
156 const char * const Open = (const char * const) memchr(Space, '(', Stop - Space);
157 if (likely(Open != NULL))
158 {
159 const char * const Close = (const char * const) memchr(Open, ')', Stop - Open);
160 if (likely(Close != NULL))
161 {
162 std::string const version(Open + 1, (Close - Open) - 1);
163 if (version != Ver.VerStr())
164 {
30b45652 165 map_stringitem_t const idx = StoreString(pkgCacheGenerator::VERSIONNUMBER, version);
a221efc3
DK
166 Ver->SourceVerStr = idx;
167 }
168 }
169 }
170 }
171
172 std::string const pkgname(Start, Stop - Start);
173 if (pkgname != G.Name())
174 {
175 for (pkgCache::PkgIterator P = G.PackageList(); P.end() == false; P = G.NextPkg(P))
176 {
177 for (V = P.VersionList(); V.end() == false; ++V)
178 {
179 if (pkgname == V.SourcePkgName())
180 {
181 Ver->SourcePkgName = V->SourcePkgName;
182 break;
183 }
184 }
185 if (V.end() == false)
186 break;
187 }
188 if (V.end() == true)
189 {
190 map_stringitem_t const idx = StoreString(pkgCacheGenerator::PKGNAME, pkgname);
191 Ver->SourcePkgName = idx;
192 }
193 }
194 }
78a5476f 195
e33dbfe5 196 Ver->MultiArch = ParseMultiArch(true);
0149949b 197 // Archive Size
e2c66de5 198 Ver->Size = Section.FindULL("Size");
0149949b 199 // Unpacked Size (in K)
e2c66de5 200 Ver->InstalledSize = Section.FindULL("Installed-Size");
0149949b
AL
201 Ver->InstalledSize *= 1024;
202
203 // Priority
0149949b 204 if (Section.Find("Priority",Start,Stop) == true)
78a5476f 205 {
b2e465d6 206 if (GrabWord(string(Start,Stop-Start),PrioList,Ver->Priority) == false)
421c8d10 207 Ver->Priority = pkgCache::State::Extra;
0149949b 208 }
dcb79bae 209
6c139d6e 210 if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
dcb79bae 211 return false;
8efa2a3b 212 if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
dcb79bae 213 return false;
6c139d6e 214 if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
dcb79bae 215 return false;
6c139d6e 216 if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
dcb79bae 217 return false;
6c139d6e 218 if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
dcb79bae 219 return false;
308c7d30
IJ
220 if (ParseDepends(Ver,"Breaks",pkgCache::Dep::DpkgBreaks) == false)
221 return false;
f55ece0e 222 if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
dcb79bae 223 return false;
f8ae7e8b 224 if (ParseDepends(Ver,"Enhances",pkgCache::Dep::Enhances) == false)
225 return false;
dcb79bae 226
b2e465d6
AL
227 // Obsolete.
228 if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false)
229 return false;
230
dcb79bae
AL
231 if (ParseProvides(Ver) == false)
232 return false;
0149949b 233
f55a958f
AL
234 return true;
235}
236 /*}}}*/
a52f938b
OS
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 */
ffe3c68e 242string debListParser::Description(std::string const &lang)
a52f938b 243{
45df0ad2 244 if (lang.empty())
a52f938b
OS
245 return Section.FindS("Description");
246 else
45df0ad2 247 return Section.FindS(string("Description-").append(lang).c_str());
a52f938b 248}
ffe3c68e
DK
249 /*}}}*/
250// ListParser::AvailableDescriptionLanguages /*{{{*/
251std::vector<std::string> debListParser::AvailableDescriptionLanguages()
a52f938b 252{
ffe3c68e
DK
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;
a52f938b 264}
ffe3c68e
DK
265 /*}}}*/
266// ListParser::Description_md5 - Return the description_md5 MD5SumValue /*{{{*/
a52f938b 267// ---------------------------------------------------------------------
770c32ec
MV
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 */
a52f938b
OS
272MD5SumValue debListParser::Description_md5()
273{
fc691496
DK
274 string const value = Section.FindS("Description-md5");
275 if (value.empty() == true)
770c32ec 276 {
ffe3c68e 277 std::string const desc = Description("") + "\n";
99359751
DK
278 if (desc == "\n")
279 return MD5SumValue();
280
a52f938b 281 MD5Summation md5;
99359751 282 md5.Add(desc.c_str());
a52f938b 283 return md5.Result();
fc691496
DK
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();
a52f938b
OS
294}
295 /*}}}*/
f55a958f
AL
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 */
32b9a14c
DK
300bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg,
301 pkgCache::VerIterator &Ver)
f55a958f 302{
5dd4c8b8 303 string const static myArch = _config->Find("APT::Architecture");
e5a91f7e
DK
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()))
5dd4c8b8
DK
309 if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
310 return false;
138d4b3d 311 if (Section.FindFlag("Important",Pkg->Flags,pkgCache::Flag::Important) == false)
f55a958f 312 return false;
138d4b3d
AL
313
314 if (strcmp(Pkg.Name(),"apt") == 0)
a552f37e
DK
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
f55a958f
AL
323 if (ParseStatus(Pkg,Ver) == false)
324 return false;
325 return true;
326}
327 /*}}}*/
204fbdcc
AL
328// ListParser::VersionHash - Compute a unique hash for this version /*{{{*/
329// ---------------------------------------------------------------------
330/* */
331unsigned short debListParser::VersionHash()
332{
333 const char *Sections[] ={"Installed-Size",
334 "Depends",
335 "Pre-Depends",
f78439bf
AL
336// "Suggests",
337// "Recommends",
204fbdcc 338 "Conflicts",
308c7d30 339 "Breaks",
204fbdcc
AL
340 "Replaces",0};
341 unsigned long Result = INIT_FCS;
418a471f 342 char S[1024];
d9682cf8 343 for (const char * const *I = Sections; *I != 0; ++I)
204fbdcc
AL
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
421c8d10
AL
351 of certain fields. dpkg also has the rather interesting notion of
352 reformatting depends operators < -> <= */
470a5c38 353 char *J = S;
d9682cf8 354 for (; Start != End; ++Start)
421c8d10 355 {
d9682cf8
DK
356 if (isspace(*Start) != 0)
357 continue;
358 *J++ = tolower_ascii(*Start);
359
360 if ((*Start == '<' || *Start == '>') && Start[1] != *Start && Start[1] != '=')
470a5c38 361 *J++ = '=';
421c8d10 362 }
418a471f 363
470a5c38 364 Result = AddCRC16(Result,S,J - S);
204fbdcc
AL
365 }
366
367 return Result;
368}
369 /*}}}*/
dcb79bae 370// ListParser::ParseStatus - Parse the status field /*{{{*/
f55a958f
AL
371// ---------------------------------------------------------------------
372/* Status lines are of the form,
373 Status: want flag status
374 want = unknown, install, hold, deinstall, purge
375 flag = ok, reinstreq, hold, hold-reinstreq
a005475e 376 status = not-installed, unpacked, half-configured,
f55a958f
AL
377 half-installed, config-files, post-inst-failed,
378 removal-failed, installed
379
380 Some of the above are obsolete (I think?) flag = hold-* and
381 status = post-inst-failed, removal-failed at least.
382 */
32b9a14c
DK
383bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
384 pkgCache::VerIterator &Ver)
f55a958f
AL
385{
386 const char *Start;
387 const char *Stop;
388 if (Section.Find("Status",Start,Stop) == false)
389 return true;
6bc703c2
DK
390
391 // UsePackage() is responsible for setting the flag in the default case
392 bool const static essential = _config->Find("pkgCacheGen::Essential", "") == "installed";
393 if (essential == true &&
394 Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
395 return false;
396
f55a958f
AL
397 // Isolate the first word
398 const char *I = Start;
399 for(; I < Stop && *I != ' '; I++);
400 if (I >= Stop || *I != ' ')
401 return _error->Error("Malformed Status line");
402
403 // Process the want field
6c139d6e
AL
404 WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
405 {"install",pkgCache::State::Install},
406 {"hold",pkgCache::State::Hold},
407 {"deinstall",pkgCache::State::DeInstall},
b2e465d6 408 {"purge",pkgCache::State::Purge},
67c3067f 409 {NULL, 0}};
b2e465d6 410 if (GrabWord(string(Start,I-Start),WantList,Pkg->SelectedState) == false)
f55a958f
AL
411 return _error->Error("Malformed 1st word in the Status line");
412
413 // Isloate the next word
414 I++;
415 Start = I;
416 for(; I < Stop && *I != ' '; I++);
417 if (I >= Stop || *I != ' ')
418 return _error->Error("Malformed status line, no 2nd word");
419
420 // Process the flag field
6c139d6e
AL
421 WordList FlagList[] = {{"ok",pkgCache::State::Ok},
422 {"reinstreq",pkgCache::State::ReInstReq},
423 {"hold",pkgCache::State::HoldInst},
b2e465d6 424 {"hold-reinstreq",pkgCache::State::HoldReInstReq},
67c3067f 425 {NULL, 0}};
b2e465d6 426 if (GrabWord(string(Start,I-Start),FlagList,Pkg->InstState) == false)
f55a958f
AL
427 return _error->Error("Malformed 2nd word in the Status line");
428
429 // Isloate the last word
430 I++;
431 Start = I;
432 for(; I < Stop && *I != ' '; I++);
433 if (I != Stop)
434 return _error->Error("Malformed Status line, no 3rd word");
435
436 // Process the flag field
6c139d6e
AL
437 WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
438 {"unpacked",pkgCache::State::UnPacked},
439 {"half-configured",pkgCache::State::HalfConfigured},
440 {"installed",pkgCache::State::Installed},
6c139d6e
AL
441 {"half-installed",pkgCache::State::HalfInstalled},
442 {"config-files",pkgCache::State::ConfigFiles},
9d06bc80
MV
443 {"triggers-awaited",pkgCache::State::TriggersAwaited},
444 {"triggers-pending",pkgCache::State::TriggersPending},
6c139d6e 445 {"post-inst-failed",pkgCache::State::HalfConfigured},
b2e465d6 446 {"removal-failed",pkgCache::State::HalfInstalled},
67c3067f 447 {NULL, 0}};
b2e465d6 448 if (GrabWord(string(Start,I-Start),StatusList,Pkg->CurrentState) == false)
f55a958f
AL
449 return _error->Error("Malformed 3rd word in the Status line");
450
451 /* A Status line marks the package as indicating the current
452 version as well. Only if it is actually installed.. Otherwise
453 the interesting dpkg handling of the status file creates bogus
454 entries. */
6c139d6e
AL
455 if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
456 Pkg->CurrentState == pkgCache::State::ConfigFiles))
f55a958f
AL
457 {
458 if (Ver.end() == true)
459 _error->Warning("Encountered status field in a non-version description");
460 else
461 Pkg->CurrentVer = Ver.Index();
462 }
463
dcb79bae
AL
464 return true;
465}
a1826878 466
b2e465d6
AL
467const char *debListParser::ConvertRelation(const char *I,unsigned int &Op)
468{
469 // Determine the operator
470 switch (*I)
471 {
472 case '<':
473 I++;
474 if (*I == '=')
475 {
476 I++;
477 Op = pkgCache::Dep::LessEq;
478 break;
479 }
480
481 if (*I == '<')
482 {
483 I++;
484 Op = pkgCache::Dep::Less;
485 break;
486 }
487
488 // < is the same as <= and << is really Cs < for some reason
489 Op = pkgCache::Dep::LessEq;
490 break;
491
492 case '>':
493 I++;
494 if (*I == '=')
495 {
496 I++;
497 Op = pkgCache::Dep::GreaterEq;
498 break;
499 }
500
501 if (*I == '>')
502 {
503 I++;
504 Op = pkgCache::Dep::Greater;
505 break;
506 }
507
508 // > is the same as >= and >> is really Cs > for some reason
509 Op = pkgCache::Dep::GreaterEq;
510 break;
511
512 case '=':
513 Op = pkgCache::Dep::Equals;
514 I++;
515 break;
516
517 // HACK around bad package definitions
518 default:
519 Op = pkgCache::Dep::Equals;
520 break;
521 }
522 return I;
523}
a1826878
AL
524 /*}}}*/
525// ListParser::ParseDepends - Parse a dependency element /*{{{*/
526// ---------------------------------------------------------------------
527/* This parses the dependency elements out of a standard string in place,
528 bit by bit. */
565ded7b
JS
529const char *debListParser::ParseDepends(const char *Start,const char *Stop,
530 std::string &Package,std::string &Ver,unsigned int &Op)
531 { return ParseDepends(Start, Stop, Package, Ver, Op, false, true, false); }
532const char *debListParser::ParseDepends(const char *Start,const char *Stop,
533 std::string &Package,std::string &Ver,unsigned int &Op,
534 bool const &ParseArchFlags)
535 { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, true, false); }
536const char *debListParser::ParseDepends(const char *Start,const char *Stop,
537 std::string &Package,std::string &Ver,unsigned int &Op,
538 bool const &ParseArchFlags, bool const &StripMultiArch)
539 { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, StripMultiArch, false); }
dcb79bae
AL
540const char *debListParser::ParseDepends(const char *Start,const char *Stop,
541 string &Package,string &Ver,
41c81fd8 542 unsigned int &Op, bool const &ParseArchFlags,
565ded7b
JS
543 bool const &StripMultiArch,
544 bool const &ParseRestrictionsList)
dcb79bae
AL
545{
546 // Strip off leading space
565ded7b 547 for (;Start != Stop && isspace(*Start) != 0; ++Start);
dcb79bae
AL
548
549 // Parse off the package name
550 const char *I = Start;
551 for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
565ded7b
JS
552 *I != ',' && *I != '|' && *I != '[' && *I != ']' &&
553 *I != '<' && *I != '>'; ++I);
dcb79bae
AL
554
555 // Malformed, no '('
556 if (I != Stop && *I == ')')
557 return 0;
558
559 if (I == Start)
560 return 0;
561
562 // Stash the package name
563 Package.assign(Start,I - Start);
41c81fd8
DK
564
565 // We don't want to confuse library users which can't handle MultiArch
550f6493 566 string const arch = _config->Find("APT::Architecture");
41c81fd8
DK
567 if (StripMultiArch == true) {
568 size_t const found = Package.rfind(':');
550f6493
DK
569 if (found != string::npos &&
570 (strcmp(Package.c_str() + found, ":any") == 0 ||
571 strcmp(Package.c_str() + found, ":native") == 0 ||
572 strcmp(Package.c_str() + found + 1, arch.c_str()) == 0))
41c81fd8
DK
573 Package = Package.substr(0,found);
574 }
575
dcb79bae
AL
576 // Skip white space to the '('
577 for (;I != Stop && isspace(*I) != 0 ; I++);
578
579 // Parse a version
580 if (I != Stop && *I == '(')
581 {
582 // Skip the '('
583 for (I++; I != Stop && isspace(*I) != 0 ; I++);
584 if (I + 3 >= Stop)
585 return 0;
b2e465d6 586 I = ConvertRelation(I,Op);
dcb79bae
AL
587
588 // Skip whitespace
589 for (;I != Stop && isspace(*I) != 0; I++);
590 Start = I;
404528bd
DK
591 I = (const char*) memchr(I, ')', Stop - I);
592 if (I == NULL || Start == I)
593 return 0;
dcb79bae 594
02f000a9
AL
595 // Skip trailing whitespace
596 const char *End = I;
597 for (; End > Start && isspace(End[-1]); End--);
598
171c75f1 599 Ver.assign(Start,End-Start);
dcb79bae
AL
600 I++;
601 }
602 else
603 {
171c75f1 604 Ver.clear();
6c139d6e 605 Op = pkgCache::Dep::NoOp;
dcb79bae
AL
606 }
607
608 // Skip whitespace
609 for (;I != Stop && isspace(*I) != 0; I++);
b2e465d6
AL
610
611 if (ParseArchFlags == true)
612 {
424ff669 613 APT::CacheFilter::PackageArchitectureMatchesSpecification matchesArch(arch, false);
9e10ad8a 614
b2e465d6
AL
615 // Parse an architecture
616 if (I != Stop && *I == '[')
617 {
cd9694bf 618 ++I;
b2e465d6 619 // malformed
cd9694bf
DK
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)
b2e465d6 627 {
cd9694bf
DK
628 // look for whitespace or ending ']'
629 for (;End != Stop && !isspace(*End) && *End != ']'; ++End);
630
631 if (unlikely(End == Stop))
b2e465d6 632 return 0;
9e10ad8a
AL
633
634 if (*I == '!')
cd9694bf 635 {
9e10ad8a 636 NegArch = true;
cd9694bf
DK
637 ++I;
638 }
9e10ad8a 639
424ff669
DK
640 std::string arch(I, End);
641 if (arch.empty() == false && matchesArch(arch.c_str()) == true)
cd9694bf 642 {
424ff669 643 Found = true;
cd9694bf
DK
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
b2e465d6
AL
650 if (*End++ == ']') {
651 I = End;
652 break;
653 }
cd9694bf 654
b2e465d6
AL
655 I = End;
656 for (;I != Stop && isspace(*I) != 0; I++);
cd9694bf 657 }
9e10ad8a 658
cd9694bf 659 if (NegArch == true)
9e10ad8a 660 Found = !Found;
cd9694bf
DK
661
662 if (Found == false)
b2e465d6
AL
663 Package = ""; /* not for this arch */
664 }
cd9694bf 665
b2e465d6
AL
666 // Skip whitespace
667 for (;I != Stop && isspace(*I) != 0; I++);
668 }
669
565ded7b
JS
670 if (ParseRestrictionsList == true)
671 {
672 // Parse a restrictions list
673 if (I != Stop && *I == '<')
674 {
675 ++I;
676 // malformed
677 if (unlikely(I == Stop))
678 return 0;
679
ce7f128c 680 std::vector<string> const profiles = APT::Configuration::getBuildProfiles();
565ded7b
JS
681
682 const char *End = I;
683 bool Found = false;
684 bool NegRestriction = false;
685 while (I != Stop)
686 {
687 // look for whitespace or ending '>'
688 for (;End != Stop && !isspace(*End) && *End != '>'; ++End);
689
690 if (unlikely(End == Stop))
691 return 0;
692
693 if (*I == '!')
694 {
695 NegRestriction = true;
696 ++I;
697 }
698
699 std::string restriction(I, End);
700
701 std::string prefix = "profile.";
702 // only support for "profile" prefix, ignore others
703 if (restriction.size() > prefix.size() &&
704 restriction.substr(0, prefix.size()) == prefix)
705 {
706 // get the name of the profile
707 restriction = restriction.substr(prefix.size());
708
709 if (restriction.empty() == false && profiles.empty() == false &&
710 std::find(profiles.begin(), profiles.end(), restriction) != profiles.end())
711 {
712 Found = true;
713 if (I[-1] != '!')
714 NegRestriction = false;
715 // we found a match, so fast-forward to the end of the wildcards
716 for (; End != Stop && *End != '>'; ++End);
717 }
718 }
719
720 if (*End++ == '>') {
721 I = End;
722 break;
723 }
724
725 I = End;
726 for (;I != Stop && isspace(*I) != 0; I++);
727 }
728
729 if (NegRestriction == true)
730 Found = !Found;
731
732 if (Found == false)
733 Package = ""; /* not for this restriction */
734 }
735
736 // Skip whitespace
737 for (;I != Stop && isspace(*I) != 0; I++);
738 }
739
dcb79bae 740 if (I != Stop && *I == '|')
6c139d6e 741 Op |= pkgCache::Dep::Or;
dcb79bae
AL
742
743 if (I == Stop || *I == ',' || *I == '|')
744 {
745 if (I != Stop)
746 for (I++; I != Stop && isspace(*I) != 0; I++);
747 return I;
748 }
749
750 return 0;
751}
752 /*}}}*/
753// ListParser::ParseDepends - Parse a dependency list /*{{{*/
754// ---------------------------------------------------------------------
755/* This is the higher level depends parser. It takes a tag and generates
756 a complete depends tree for the given version. */
32b9a14c 757bool debListParser::ParseDepends(pkgCache::VerIterator &Ver,
dcb79bae
AL
758 const char *Tag,unsigned int Type)
759{
760 const char *Start;
761 const char *Stop;
762 if (Section.Find(Tag,Start,Stop) == false)
763 return true;
306eacf6 764
28166356 765 string const pkgArch = Ver.Arch();
dcb79bae 766
8efa2a3b 767 while (1)
dcb79bae 768 {
0f485ee5
TG
769 string Package;
770 string Version;
771 unsigned int Op;
772
565ded7b 773 Start = ParseDepends(Start, Stop, Package, Version, Op, false, false, false);
dcb79bae
AL
774 if (Start == 0)
775 return _error->Error("Problem parsing dependency %s",Tag);
0f485ee5 776 size_t const found = Package.rfind(':');
306eacf6 777
cef094c2
DK
778 // If negative is unspecific it needs to apply on all architectures
779 if (MultiArchEnabled == true && found == string::npos &&
306eacf6
DK
780 (Type == pkgCache::Dep::Conflicts ||
781 Type == pkgCache::Dep::DpkgBreaks ||
782 Type == pkgCache::Dep::Replaces))
783 {
dcfa253f
DK
784 for (std::vector<std::string>::const_iterator a = Architectures.begin();
785 a != Architectures.end(); ++a)
306eacf6
DK
786 if (NewDepends(Ver,Package,*a,Version,Op,Type) == false)
787 return false;
c919ad6e
DK
788 if (NewDepends(Ver,Package,"none",Version,Op,Type) == false)
789 return false;
306eacf6 790 }
0f485ee5
TG
791 else if (MultiArchEnabled == true && found != string::npos &&
792 strcmp(Package.c_str() + found, ":any") != 0)
793 {
794 string Arch = Package.substr(found+1, string::npos);
795 Package = Package.substr(0, found);
796 // Such dependencies are not supposed to be accepted …
797 // … but this is probably the best thing to do.
798 if (Arch == "native")
799 Arch = _config->Find("APT::Architecture");
800 if (NewDepends(Ver,Package,Arch,Version,Op,Type) == false)
801 return false;
802 }
c919ad6e
DK
803 else
804 {
805 if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false)
806 return false;
807 if ((Type == pkgCache::Dep::Conflicts ||
808 Type == pkgCache::Dep::DpkgBreaks ||
809 Type == pkgCache::Dep::Replaces) &&
810 NewDepends(Ver, Package,
811 (pkgArch != "none") ? "none" : _config->Find("APT::Architecture"),
812 Version,Op,Type) == false)
813 return false;
814 }
8efa2a3b
AL
815 if (Start == Stop)
816 break;
dcb79bae
AL
817 }
818 return true;
819}
820 /*}}}*/
821// ListParser::ParseProvides - Parse the provides list /*{{{*/
822// ---------------------------------------------------------------------
823/* */
32b9a14c 824bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
dcb79bae
AL
825{
826 const char *Start;
827 const char *Stop;
67e0766f 828 if (Section.Find("Provides",Start,Stop) == true)
dcb79bae 829 {
67e0766f
DK
830 string Package;
831 string Version;
28166356 832 string const Arch = Ver.Arch();
67e0766f
DK
833 unsigned int Op;
834
835 while (1)
836 {
837 Start = ParseDepends(Start,Stop,Package,Version,Op);
838 if (Start == 0)
839 return _error->Error("Problem parsing Provides line");
40faab46
DK
840 if (Op != pkgCache::Dep::NoOp && Op != pkgCache::Dep::Equals) {
841 _error->Warning("Ignoring Provides line with non-equal DepCompareOp for package %s", Package.c_str());
04340db3
DK
842 } else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) {
843 if (NewProvidesAllArch(Ver, Package, Version) == false)
844 return false;
67e0766f
DK
845 } else {
846 if (NewProvides(Ver, Package, Arch, Version) == false)
847 return false;
848 }
849
850 if (Start == Stop)
851 break;
b63380b0 852 }
67e0766f 853 }
dcb79bae 854
b4140ecf 855 if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
4d174dc8
DK
856 {
857 string const Package = string(Ver.ParentPkg().Name()).append(":").append("any");
60dcec6d 858 return NewProvidesAllArch(Ver, Package, Ver.VerStr());
dcb79bae 859 }
894d672e 860 else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
60dcec6d 861 return NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr());
dcb79bae 862
60dcec6d
DK
863 return true;
864}
865 /*}}}*/
866// ListParser::NewProvides - add provides for all architectures /*{{{*/
867bool debListParser::NewProvidesAllArch(pkgCache::VerIterator &Ver, string const &Package,
868 string const &Version) {
dcfa253f
DK
869 for (std::vector<string>::const_iterator a = Architectures.begin();
870 a != Architectures.end(); ++a)
67e0766f
DK
871 {
872 if (NewProvides(Ver, Package, *a, Version) == false)
873 return false;
dcb79bae 874 }
f55a958f
AL
875 return true;
876}
877 /*}}}*/
878// ListParser::GrabWord - Matches a word and returns /*{{{*/
879// ---------------------------------------------------------------------
880/* Looks for a word in a list of words - for ParseStatus */
b2e465d6 881bool debListParser::GrabWord(string Word,WordList *List,unsigned char &Out)
f55a958f 882{
b2e465d6 883 for (unsigned int C = 0; List[C].Str != 0; C++)
f55a958f
AL
884 {
885 if (strcasecmp(Word.c_str(),List[C].Str) == 0)
886 {
887 Out = List[C].Val;
888 return true;
889 }
890 }
891 return false;
892}
893 /*}}}*/
894// ListParser::Step - Move to the next section in the file /*{{{*/
895// ---------------------------------------------------------------------
1e3f4083 896/* This has to be careful to only process the correct architecture */
f55a958f
AL
897bool debListParser::Step()
898{
dcb79bae 899 iOffset = Tags.Offset();
0149949b 900 while (Tags.Step(Section) == true)
ddc1d8d0
AL
901 {
902 /* See if this is the correct Architecture, if it isn't then we
903 drop the whole section. A missing arch tag only happens (in theory)
904 inside the Status file, so that is a positive return */
5dd4c8b8 905 string const Architecture = Section.FindS("Architecture");
9c14e3d6 906
dd13742e 907 if (Arch.empty() == true || Arch == "any" || MultiArchEnabled == false)
5dd4c8b8
DK
908 {
909 if (APT::Configuration::checkArchitecture(Architecture) == true)
910 return true;
c919ad6e
DK
911 /* parse version stanzas without an architecture only in the status file
912 (and as misfortune bycatch flat-archives) */
913 if ((Arch.empty() == true || Arch == "any") && Architecture.empty() == true)
914 return true;
5dd4c8b8
DK
915 }
916 else
917 {
918 if (Architecture == Arch)
919 return true;
0149949b 920
28166356 921 if (Architecture == "all" && Arch == _config->Find("APT::Architecture"))
5dd4c8b8
DK
922 return true;
923 }
dcb79bae
AL
924
925 iOffset = Tags.Offset();
0149949b
AL
926 }
927 return false;
f55a958f
AL
928}
929 /*}}}*/
b0b4efb9
AL
930// ListParser::LoadReleaseInfo - Load the release information /*{{{*/
931// ---------------------------------------------------------------------
932/* */
32b9a14c 933bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator &FileI,
f2152f03 934 FileFd &File, string component)
b0b4efb9 935{
f2152f03
MV
936 // apt-secure does no longer download individual (per-section) Release
937 // file. to provide Component pinning we use the section name now
78a5476f 938 map_stringitem_t const storage = StoreString(pkgCacheGenerator::MIXED, component);
2b803d40 939 FileI->Component = storage;
f2152f03 940
233d79a5 941 pkgTagFile TagFile(&File, File.Size());
e9737c7f
DK
942 pkgTagSection Section;
943 if (_error->PendingError() == true || TagFile.Step(Section) == false)
fe0f7911
DK
944 return false;
945
e9737c7f 946 std::string data;
78a5476f 947 #define APT_INRELEASE(TYPE, TAG, STORE) \
e9737c7f
DK
948 data = Section.FindS(TAG); \
949 if (data.empty() == false) \
950 { \
78a5476f 951 map_stringitem_t const storage = StoreString(pkgCacheGenerator::TYPE, data); \
e9737c7f 952 STORE = storage; \
fe0f7911 953 }
78a5476f
DK
954 APT_INRELEASE(MIXED, "Suite", FileI->Archive)
955 APT_INRELEASE(MIXED, "Component", FileI->Component)
30b45652 956 APT_INRELEASE(VERSIONNUMBER, "Version", FileI->Version)
78a5476f
DK
957 APT_INRELEASE(MIXED, "Origin", FileI->Origin)
958 APT_INRELEASE(MIXED, "Codename", FileI->Codename)
959 APT_INRELEASE(MIXED, "Label", FileI->Label)
e9737c7f
DK
960 #undef APT_INRELEASE
961 Section.FindFlag("NotAutomatic", FileI->Flags, pkgCache::Flag::NotAutomatic);
962 Section.FindFlag("ButAutomaticUpgrades", FileI->Flags, pkgCache::Flag::ButAutomaticUpgrades);
f2152f03 963
b0b4efb9
AL
964 return !_error->PendingError();
965}
966 /*}}}*/
b2e465d6
AL
967// ListParser::GetPrio - Convert the priority from a string /*{{{*/
968// ---------------------------------------------------------------------
969/* */
970unsigned char debListParser::GetPrio(string Str)
971{
972 unsigned char Out;
973 if (GrabWord(Str,PrioList,Out) == false)
974 Out = pkgCache::State::Extra;
975
976 return Out;
977}
978 /*}}}*/
68134bda
DK
979#if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
980bool debListParser::SameVersion(unsigned short const Hash, /*{{{*/
981 pkgCache::VerIterator const &Ver)
982{
983 if (pkgCacheGenerator::ListParser::SameVersion(Hash, Ver) == false)
984 return false;
985 // status file has no (Download)Size, but all others are fair game
986 // status file is parsed last, so the first version we encounter is
987 // probably also the version we have downloaded
988 unsigned long long const Size = Section.FindULL("Size");
989 if (Size != 0 && Size != Ver->Size)
990 return false;
991 // available everywhere, but easier to check here than to include in VersionHash
992 unsigned char MultiArch = ParseMultiArch(false);
993 if (MultiArch != Ver->MultiArch)
994 return false;
995 // for all practical proposes (we can check): same version
996 return true;
997}
998 /*}}}*/
999#endif
0d29b9d4
MV
1000
1001
1002debDebFileParser::debDebFileParser(FileFd *File, std::string const &DebFile)
1003 : debListParser(File, ""), DebFile(DebFile)
1004{
1005}
1006
1007bool debDebFileParser::UsePackage(pkgCache::PkgIterator &Pkg,
1008 pkgCache::VerIterator &Ver)
1009{
1010 bool res = debListParser::UsePackage(Pkg, Ver);
1011 // we use the full file path as a provides so that the file is found
1012 // by its name
1013 if(NewProvidesAllArch(Ver, DebFile, Ver.VerStr()) == false)
1014 return false;
1015 return res;
1016}
1017
1018
1019