]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/deblistparser.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: deblistparser.cc,v 1.29.2.5 2004/01/06 01:43:44 mdz Exp $
4 /* ######################################################################
6 Package Cache Generator - Generator for the cache structure.
8 This builds the cache structure from the abstract package list parser.
10 ##################################################################### */
12 // Include Files /*{{{*/
13 #include <apt-pkg/deblistparser.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/configuration.h>
16 #include <apt-pkg/aptconfiguration.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/crc-16.h>
19 #include <apt-pkg/md5.h>
26 static debListParser::WordList PrioList
[] = {{"important",pkgCache::State::Important
},
27 {"required",pkgCache::State::Required
},
28 {"standard",pkgCache::State::Standard
},
29 {"optional",pkgCache::State::Optional
},
30 {"extra",pkgCache::State::Extra
},
33 // ListParser::debListParser - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
36 debListParser::debListParser(FileFd
*File
) : Tags(File
)
38 Arch
= _config
->Find("APT::architecture");
41 // ListParser::UniqFindTagWrite - Find the tag and write a unq string /*{{{*/
42 // ---------------------------------------------------------------------
44 unsigned long debListParser::UniqFindTagWrite(const char *Tag
)
48 if (Section
.Find(Tag
,Start
,Stop
) == false)
50 return WriteUniqString(Start
,Stop
- Start
);
53 // ListParser::Package - Return the package name /*{{{*/
54 // ---------------------------------------------------------------------
55 /* This is to return the name of the package this section describes */
56 string
debListParser::Package()
58 string Result
= Section
.FindS("Package");
59 if (Result
.empty() == true)
60 _error
->Error("Encountered a section with no Package: header");
64 // ListParser::Version - Return the version string /*{{{*/
65 // ---------------------------------------------------------------------
66 /* This is to return the string describing the version in debian form,
67 epoch:upstream-release. If this returns the blank string then the
68 entry is assumed to only describe package properties */
69 string
debListParser::Version()
71 return Section
.FindS("Version");
74 // ListParser::NewVersion - Fill in the version structure /*{{{*/
75 // ---------------------------------------------------------------------
77 bool debListParser::NewVersion(pkgCache::VerIterator Ver
)
80 Ver
->Section
= UniqFindTagWrite("Section");
81 Ver
->Arch
= UniqFindTagWrite("Architecture");
84 Ver
->Size
= (unsigned)Section
.FindI("Size");
86 // Unpacked Size (in K)
87 Ver
->InstalledSize
= (unsigned)Section
.FindI("Installed-Size");
88 Ver
->InstalledSize
*= 1024;
93 if (Section
.Find("Priority",Start
,Stop
) == true)
95 if (GrabWord(string(Start
,Stop
-Start
),PrioList
,Ver
->Priority
) == false)
96 Ver
->Priority
= pkgCache::State::Extra
;
99 if (ParseDepends(Ver
,"Depends",pkgCache::Dep::Depends
) == false)
101 if (ParseDepends(Ver
,"Pre-Depends",pkgCache::Dep::PreDepends
) == false)
103 if (ParseDepends(Ver
,"Suggests",pkgCache::Dep::Suggests
) == false)
105 if (ParseDepends(Ver
,"Recommends",pkgCache::Dep::Recommends
) == false)
107 if (ParseDepends(Ver
,"Conflicts",pkgCache::Dep::Conflicts
) == false)
109 if (ParseDepends(Ver
,"Breaks",pkgCache::Dep::DpkgBreaks
) == false)
111 if (ParseDepends(Ver
,"Replaces",pkgCache::Dep::Replaces
) == false)
113 if (ParseDepends(Ver
,"Enhances",pkgCache::Dep::Enhances
) == false)
117 if (ParseDepends(Ver
,"Optional",pkgCache::Dep::Suggests
) == false)
120 if (ParseProvides(Ver
) == false)
126 // ListParser::Description - Return the description string /*{{{*/
127 // ---------------------------------------------------------------------
128 /* This is to return the string describing the package in debian
129 form. If this returns the blank string then the entry is assumed to
130 only describe package properties */
131 string
debListParser::Description()
133 string
const lang
= DescriptionLanguage();
135 return Section
.FindS("Description");
137 return Section
.FindS(string("Description-").append(lang
).c_str());
140 // ListParser::DescriptionLanguage - Return the description lang string /*{{{*/
141 // ---------------------------------------------------------------------
142 /* This is to return the string describing the language of
143 description. If this returns the blank string then the entry is
144 assumed to describe original description. */
145 string
debListParser::DescriptionLanguage()
147 if (Section
.FindS("Description").empty() == false)
150 std::vector
<string
> const lang
= APT::Configuration::getLanguages();
151 for (std::vector
<string
>::const_iterator l
= lang
.begin();
152 l
!= lang
.end(); l
++)
153 if (Section
.FindS(string("Description-").append(*l
).c_str()).empty() == false)
159 // ListParser::Description - Return the description_md5 MD5SumValue /*{{{*/
160 // ---------------------------------------------------------------------
161 /* This is to return the md5 string to allow the check if it is the right
162 description. If no Description-md5 is found in the section it will be
165 MD5SumValue
debListParser::Description_md5()
167 string value
= Section
.FindS("Description-md5");
172 md5
.Add((Description() + "\n").c_str());
175 return MD5SumValue(value
);
178 // ListParser::UsePackage - Update a package structure /*{{{*/
179 // ---------------------------------------------------------------------
180 /* This is called to update the package with any new information
181 that might be found in the section */
182 bool debListParser::UsePackage(pkgCache::PkgIterator Pkg
,
183 pkgCache::VerIterator Ver
)
185 if (Pkg
->Section
== 0)
186 Pkg
->Section
= UniqFindTagWrite("Section");
187 if (Section
.FindFlag("Essential",Pkg
->Flags
,pkgCache::Flag::Essential
) == false)
189 if (Section
.FindFlag("Important",Pkg
->Flags
,pkgCache::Flag::Important
) == false)
192 if (strcmp(Pkg
.Name(),"apt") == 0)
193 Pkg
->Flags
|= pkgCache::Flag::Important
;
195 if (ParseStatus(Pkg
,Ver
) == false)
200 // ListParser::VersionHash - Compute a unique hash for this version /*{{{*/
201 // ---------------------------------------------------------------------
203 unsigned short debListParser::VersionHash()
205 const char *Sections
[] ={"Installed-Size",
213 unsigned long Result
= INIT_FCS
;
215 for (const char **I
= Sections
; *I
!= 0; I
++)
219 if (Section
.Find(*I
,Start
,End
) == false || End
- Start
>= (signed)sizeof(S
))
222 /* Strip out any spaces from the text, this undoes dpkgs reformatting
223 of certain fields. dpkg also has the rather interesting notion of
224 reformatting depends operators < -> <= */
226 for (; Start
!= End
; Start
++)
228 if (isspace(*Start
) == 0)
229 *I
++ = tolower_ascii(*Start
);
230 if (*Start
== '<' && Start
[1] != '<' && Start
[1] != '=')
232 if (*Start
== '>' && Start
[1] != '>' && Start
[1] != '=')
236 Result
= AddCRC16(Result
,S
,I
- S
);
242 // ListParser::ParseStatus - Parse the status field /*{{{*/
243 // ---------------------------------------------------------------------
244 /* Status lines are of the form,
245 Status: want flag status
246 want = unknown, install, hold, deinstall, purge
247 flag = ok, reinstreq, hold, hold-reinstreq
248 status = not-installed, unpacked, half-configured,
249 half-installed, config-files, post-inst-failed,
250 removal-failed, installed
252 Some of the above are obsolete (I think?) flag = hold-* and
253 status = post-inst-failed, removal-failed at least.
255 bool debListParser::ParseStatus(pkgCache::PkgIterator Pkg
,
256 pkgCache::VerIterator Ver
)
260 if (Section
.Find("Status",Start
,Stop
) == false)
263 // Isolate the first word
264 const char *I
= Start
;
265 for(; I
< Stop
&& *I
!= ' '; I
++);
266 if (I
>= Stop
|| *I
!= ' ')
267 return _error
->Error("Malformed Status line");
269 // Process the want field
270 WordList WantList
[] = {{"unknown",pkgCache::State::Unknown
},
271 {"install",pkgCache::State::Install
},
272 {"hold",pkgCache::State::Hold
},
273 {"deinstall",pkgCache::State::DeInstall
},
274 {"purge",pkgCache::State::Purge
},
276 if (GrabWord(string(Start
,I
-Start
),WantList
,Pkg
->SelectedState
) == false)
277 return _error
->Error("Malformed 1st word in the Status line");
279 // Isloate the next word
282 for(; I
< Stop
&& *I
!= ' '; I
++);
283 if (I
>= Stop
|| *I
!= ' ')
284 return _error
->Error("Malformed status line, no 2nd word");
286 // Process the flag field
287 WordList FlagList
[] = {{"ok",pkgCache::State::Ok
},
288 {"reinstreq",pkgCache::State::ReInstReq
},
289 {"hold",pkgCache::State::HoldInst
},
290 {"hold-reinstreq",pkgCache::State::HoldReInstReq
},
292 if (GrabWord(string(Start
,I
-Start
),FlagList
,Pkg
->InstState
) == false)
293 return _error
->Error("Malformed 2nd word in the Status line");
295 // Isloate the last word
298 for(; I
< Stop
&& *I
!= ' '; I
++);
300 return _error
->Error("Malformed Status line, no 3rd word");
302 // Process the flag field
303 WordList StatusList
[] = {{"not-installed",pkgCache::State::NotInstalled
},
304 {"unpacked",pkgCache::State::UnPacked
},
305 {"half-configured",pkgCache::State::HalfConfigured
},
306 {"installed",pkgCache::State::Installed
},
307 {"half-installed",pkgCache::State::HalfInstalled
},
308 {"config-files",pkgCache::State::ConfigFiles
},
309 {"triggers-awaited",pkgCache::State::TriggersAwaited
},
310 {"triggers-pending",pkgCache::State::TriggersPending
},
311 {"post-inst-failed",pkgCache::State::HalfConfigured
},
312 {"removal-failed",pkgCache::State::HalfInstalled
},
314 if (GrabWord(string(Start
,I
-Start
),StatusList
,Pkg
->CurrentState
) == false)
315 return _error
->Error("Malformed 3rd word in the Status line");
317 /* A Status line marks the package as indicating the current
318 version as well. Only if it is actually installed.. Otherwise
319 the interesting dpkg handling of the status file creates bogus
321 if (!(Pkg
->CurrentState
== pkgCache::State::NotInstalled
||
322 Pkg
->CurrentState
== pkgCache::State::ConfigFiles
))
324 if (Ver
.end() == true)
325 _error
->Warning("Encountered status field in a non-version description");
327 Pkg
->CurrentVer
= Ver
.Index();
333 const char *debListParser::ConvertRelation(const char *I
,unsigned int &Op
)
335 // Determine the operator
343 Op
= pkgCache::Dep::LessEq
;
350 Op
= pkgCache::Dep::Less
;
354 // < is the same as <= and << is really Cs < for some reason
355 Op
= pkgCache::Dep::LessEq
;
363 Op
= pkgCache::Dep::GreaterEq
;
370 Op
= pkgCache::Dep::Greater
;
374 // > is the same as >= and >> is really Cs > for some reason
375 Op
= pkgCache::Dep::GreaterEq
;
379 Op
= pkgCache::Dep::Equals
;
383 // HACK around bad package definitions
385 Op
= pkgCache::Dep::Equals
;
392 // ListParser::ParseDepends - Parse a dependency element /*{{{*/
393 // ---------------------------------------------------------------------
394 /* This parses the dependency elements out of a standard string in place,
396 const char *debListParser::ParseDepends(const char *Start
,const char *Stop
,
397 string
&Package
,string
&Ver
,
398 unsigned int &Op
, bool const &ParseArchFlags
,
399 bool const &StripMultiArch
)
401 // Strip off leading space
402 for (;Start
!= Stop
&& isspace(*Start
) != 0; Start
++);
404 // Parse off the package name
405 const char *I
= Start
;
406 for (;I
!= Stop
&& isspace(*I
) == 0 && *I
!= '(' && *I
!= ')' &&
407 *I
!= ',' && *I
!= '|'; I
++);
410 if (I
!= Stop
&& *I
== ')')
416 // Stash the package name
417 Package
.assign(Start
,I
- Start
);
419 // We don't want to confuse library users which can't handle MultiArch
420 if (StripMultiArch
== true) {
421 size_t const found
= Package
.rfind(':');
422 if (found
!= string::npos
)
423 Package
= Package
.substr(0,found
);
426 // Skip white space to the '('
427 for (;I
!= Stop
&& isspace(*I
) != 0 ; I
++);
430 if (I
!= Stop
&& *I
== '(')
433 for (I
++; I
!= Stop
&& isspace(*I
) != 0 ; I
++);
436 I
= ConvertRelation(I
,Op
);
439 for (;I
!= Stop
&& isspace(*I
) != 0; I
++);
441 for (;I
!= Stop
&& *I
!= ')'; I
++);
442 if (I
== Stop
|| Start
== I
)
445 // Skip trailing whitespace
447 for (; End
> Start
&& isspace(End
[-1]); End
--);
449 Ver
.assign(Start
,End
-Start
);
455 Op
= pkgCache::Dep::NoOp
;
459 for (;I
!= Stop
&& isspace(*I
) != 0; I
++);
461 if (ParseArchFlags
== true)
463 string arch
= _config
->Find("APT::Architecture");
465 // Parse an architecture
466 if (I
!= Stop
&& *I
== '[')
475 bool NegArch
= false;
478 // look for whitespace or ending ']'
479 while (End
!= Stop
&& !isspace(*End
) && *End
!= ']')
491 if (stringcmp(arch
,I
,End
) == 0)
500 for (;I
!= Stop
&& isspace(*I
) != 0; I
++);
507 Package
= ""; /* not for this arch */
511 for (;I
!= Stop
&& isspace(*I
) != 0; I
++);
514 if (I
!= Stop
&& *I
== '|')
515 Op
|= pkgCache::Dep::Or
;
517 if (I
== Stop
|| *I
== ',' || *I
== '|')
520 for (I
++; I
!= Stop
&& isspace(*I
) != 0; I
++);
527 // ListParser::ParseDepends - Parse a dependency list /*{{{*/
528 // ---------------------------------------------------------------------
529 /* This is the higher level depends parser. It takes a tag and generates
530 a complete depends tree for the given version. */
531 bool debListParser::ParseDepends(pkgCache::VerIterator Ver
,
532 const char *Tag
,unsigned int Type
)
536 if (Section
.Find(Tag
,Start
,Stop
) == false)
545 Start
= ParseDepends(Start
,Stop
,Package
,Version
,Op
);
547 return _error
->Error("Problem parsing dependency %s",Tag
);
549 if (NewDepends(Ver
,Package
,Version
,Op
,Type
) == false)
557 // ListParser::ParseProvides - Parse the provides list /*{{{*/
558 // ---------------------------------------------------------------------
560 bool debListParser::ParseProvides(pkgCache::VerIterator Ver
)
564 if (Section
.Find("Provides",Start
,Stop
) == false)
573 Start
= ParseDepends(Start
,Stop
,Package
,Version
,Op
);
575 return _error
->Error("Problem parsing Provides line");
576 if (Op
!= pkgCache::Dep::NoOp
) {
577 _error
->Warning("Ignoring Provides line with DepCompareOp for package %s", Package
.c_str());
579 if (NewProvides(Ver
,Package
,Version
) == false)
590 // ListParser::GrabWord - Matches a word and returns /*{{{*/
591 // ---------------------------------------------------------------------
592 /* Looks for a word in a list of words - for ParseStatus */
593 bool debListParser::GrabWord(string Word
,WordList
*List
,unsigned char &Out
)
595 for (unsigned int C
= 0; List
[C
].Str
!= 0; C
++)
597 if (strcasecmp(Word
.c_str(),List
[C
].Str
) == 0)
606 // ListParser::Step - Move to the next section in the file /*{{{*/
607 // ---------------------------------------------------------------------
608 /* This has to be carefull to only process the correct architecture */
609 bool debListParser::Step()
611 iOffset
= Tags
.Offset();
612 while (Tags
.Step(Section
) == true)
614 /* See if this is the correct Architecture, if it isn't then we
615 drop the whole section. A missing arch tag only happens (in theory)
616 inside the Status file, so that is a positive return */
619 if (Section
.Find("Architecture",Start
,Stop
) == false)
622 if (stringcmp(Arch
,Start
,Stop
) == 0)
625 if (stringcmp(Start
,Stop
,"all") == 0)
628 iOffset
= Tags
.Offset();
633 // ListParser::LoadReleaseInfo - Load the release information /*{{{*/
634 // ---------------------------------------------------------------------
636 bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator FileI
,
637 FileFd
&File
, string component
)
639 pkgTagFile
Tags(&File
, File
.Size() + 256); // XXX
640 pkgTagSection Section
;
641 if (Tags
.Step(Section
) == false)
644 //mvo: I don't think we need to fill that in (it's unused since apt-0.6)
645 //FileI->Architecture = WriteUniqString(Arch);
647 // apt-secure does no longer download individual (per-section) Release
648 // file. to provide Component pinning we use the section name now
649 FileI
->Component
= WriteUniqString(component
);
653 if (Section
.Find("Suite",Start
,Stop
) == true)
654 FileI
->Archive
= WriteUniqString(Start
,Stop
- Start
);
655 if (Section
.Find("Component",Start
,Stop
) == true)
656 FileI
->Component
= WriteUniqString(Start
,Stop
- Start
);
657 if (Section
.Find("Version",Start
,Stop
) == true)
658 FileI
->Version
= WriteUniqString(Start
,Stop
- Start
);
659 if (Section
.Find("Origin",Start
,Stop
) == true)
660 FileI
->Origin
= WriteUniqString(Start
,Stop
- Start
);
661 if (Section
.Find("Codename",Start
,Stop
) == true)
662 FileI
->Codename
= WriteUniqString(Start
,Stop
- Start
);
663 if (Section
.Find("Label",Start
,Stop
) == true)
664 FileI
->Label
= WriteUniqString(Start
,Stop
- Start
);
665 if (Section
.Find("Architecture",Start
,Stop
) == true)
666 FileI
->Architecture
= WriteUniqString(Start
,Stop
- Start
);
668 if (Section
.FindFlag("NotAutomatic",FileI
->Flags
,
669 pkgCache::Flag::NotAutomatic
) == false)
670 _error
->Warning("Bad NotAutomatic flag");
672 return !_error
->PendingError();
675 // ListParser::GetPrio - Convert the priority from a string /*{{{*/
676 // ---------------------------------------------------------------------
678 unsigned char debListParser::GetPrio(string Str
)
681 if (GrabWord(Str
,PrioList
,Out
) == false)
682 Out
= pkgCache::State::Extra
;