]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/deblistparser.cc
Arranged to rename downloaded files to include all impo...
[apt.git] / apt-pkg / deb / deblistparser.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: deblistparser.cc,v 1.15 1999/02/01 02:22:11 jgg 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 <apt-pkg/deblistparser.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/configuration.h>
16 #include <apt-pkg/strutl.h>
17
18 #include <system.h>
19 /*}}}*/
20
21 // ListParser::debListParser - Constructor /*{{{*/
22 // ---------------------------------------------------------------------
23 /* */
24 debListParser::debListParser(FileFd &File) : Tags(File)
25 {
26 }
27 /*}}}*/
28 // ListParser::UniqFindTagWrite - Find the tag and write a unq string /*{{{*/
29 // ---------------------------------------------------------------------
30 /* */
31 unsigned long debListParser::UniqFindTagWrite(const char *Tag)
32 {
33 const char *Start;
34 const char *Stop;
35 if (Section.Find(Tag,Start,Stop) == false)
36 return 0;
37 return WriteUniqString(Start,Stop - Start);
38 }
39 /*}}}*/
40 // ListParser::Package - Return the package name /*{{{*/
41 // ---------------------------------------------------------------------
42 /* This is to return the name of the package this section describes */
43 string debListParser::Package()
44 {
45 string Result = Section.FindS("Package");
46 if (Result.empty() == true)
47 _error->Error("Encoutered a section with no Package: header");
48 return Result;
49 }
50 /*}}}*/
51 // ListParser::Version - Return the version string /*{{{*/
52 // ---------------------------------------------------------------------
53 /* This is to return the string describing the version in debian form,
54 epoch:upstream-release. If this returns the blank string then the
55 entry is assumed to only describe package properties */
56 string debListParser::Version()
57 {
58 return Section.FindS("Version");
59 }
60 /*}}}*/
61 // ListParser::NewVersion - Fill in the version structure /*{{{*/
62 // ---------------------------------------------------------------------
63 /* */
64 bool debListParser::NewVersion(pkgCache::VerIterator Ver)
65 {
66 // Parse the section
67 Ver->Section = UniqFindTagWrite("Section");
68 Ver->Arch = UniqFindTagWrite("Architecture");
69
70 // Archive Size
71 Ver->Size = (unsigned)Section.FindI("Size");
72
73 // Unpacked Size (in K)
74 Ver->InstalledSize = (unsigned)Section.FindI("Installed-Size");
75 Ver->InstalledSize *= 1024;
76
77 // Priority
78 const char *Start;
79 const char *Stop;
80 if (Section.Find("Priority",Start,Stop) == true)
81 {
82 WordList PrioList[] = {{"important",pkgCache::State::Important},
83 {"required",pkgCache::State::Required},
84 {"standard",pkgCache::State::Standard},
85 {"optional",pkgCache::State::Optional},
86 {"extra",pkgCache::State::Extra}};
87 if (GrabWord(string(Start,Stop-Start),PrioList,
88 _count(PrioList),Ver->Priority) == false)
89 return _error->Error("Malformed Priority line");
90 }
91
92 if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
93 return false;
94 if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
95 return false;
96 if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
97 return false;
98 if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
99 return false;
100 if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
101 return false;
102 if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
103 return false;
104
105 if (ParseProvides(Ver) == false)
106 return false;
107
108 return true;
109 }
110 /*}}}*/
111 // ListParser::UsePackage - Update a package structure /*{{{*/
112 // ---------------------------------------------------------------------
113 /* This is called to update the package with any new information
114 that might be found in the section */
115 bool debListParser::UsePackage(pkgCache::PkgIterator Pkg,
116 pkgCache::VerIterator Ver)
117 {
118 if (Pkg->Section == 0)
119 Pkg->Section = UniqFindTagWrite("Section");
120 if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
121 return false;
122 if (Section.FindFlag("Immediate-Configure",Pkg->Flags,pkgCache::Flag::ImmediateConf) == false)
123 return false;
124 if (ParseStatus(Pkg,Ver) == false)
125 return false;
126 return true;
127 }
128 /*}}}*/
129 // ListParser::ParseStatus - Parse the status field /*{{{*/
130 // ---------------------------------------------------------------------
131 /* Status lines are of the form,
132 Status: want flag status
133 want = unknown, install, hold, deinstall, purge
134 flag = ok, reinstreq, hold, hold-reinstreq
135 status = not-installed, unpacked, half-configured, uninstalled,
136 half-installed, config-files, post-inst-failed,
137 removal-failed, installed
138
139 Some of the above are obsolete (I think?) flag = hold-* and
140 status = post-inst-failed, removal-failed at least.
141 */
142 bool debListParser::ParseStatus(pkgCache::PkgIterator Pkg,
143 pkgCache::VerIterator Ver)
144 {
145 const char *Start;
146 const char *Stop;
147 if (Section.Find("Status",Start,Stop) == false)
148 return true;
149
150 // Isolate the first word
151 const char *I = Start;
152 for(; I < Stop && *I != ' '; I++);
153 if (I >= Stop || *I != ' ')
154 return _error->Error("Malformed Status line");
155
156 // Process the want field
157 WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
158 {"install",pkgCache::State::Install},
159 {"hold",pkgCache::State::Hold},
160 {"deinstall",pkgCache::State::DeInstall},
161 {"purge",pkgCache::State::Purge}};
162 if (GrabWord(string(Start,I-Start),WantList,
163 _count(WantList),Pkg->SelectedState) == false)
164 return _error->Error("Malformed 1st word in the Status line");
165
166 // Isloate the next word
167 I++;
168 Start = I;
169 for(; I < Stop && *I != ' '; I++);
170 if (I >= Stop || *I != ' ')
171 return _error->Error("Malformed status line, no 2nd word");
172
173 // Process the flag field
174 WordList FlagList[] = {{"ok",pkgCache::State::Ok},
175 {"reinstreq",pkgCache::State::ReInstReq},
176 {"hold",pkgCache::State::HoldInst},
177 {"hold-reinstreq",pkgCache::State::HoldReInstReq}};
178 if (GrabWord(string(Start,I-Start),FlagList,
179 _count(FlagList),Pkg->InstState) == false)
180 return _error->Error("Malformed 2nd word in the Status line");
181
182 // Isloate the last word
183 I++;
184 Start = I;
185 for(; I < Stop && *I != ' '; I++);
186 if (I != Stop)
187 return _error->Error("Malformed Status line, no 3rd word");
188
189 // Process the flag field
190 WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
191 {"unpacked",pkgCache::State::UnPacked},
192 {"half-configured",pkgCache::State::HalfConfigured},
193 {"installed",pkgCache::State::Installed},
194 {"uninstalled",pkgCache::State::UnInstalled},
195 {"half-installed",pkgCache::State::HalfInstalled},
196 {"config-files",pkgCache::State::ConfigFiles},
197 {"post-inst-failed",pkgCache::State::HalfConfigured},
198 {"removal-failed",pkgCache::State::HalfInstalled}};
199 if (GrabWord(string(Start,I-Start),StatusList,
200 _count(StatusList),Pkg->CurrentState) == false)
201 return _error->Error("Malformed 3rd word in the Status line");
202
203 /* A Status line marks the package as indicating the current
204 version as well. Only if it is actually installed.. Otherwise
205 the interesting dpkg handling of the status file creates bogus
206 entries. */
207 if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
208 Pkg->CurrentState == pkgCache::State::ConfigFiles))
209 {
210 if (Ver.end() == true)
211 _error->Warning("Encountered status field in a non-version description");
212 else
213 Pkg->CurrentVer = Ver.Index();
214 }
215
216 return true;
217 }
218 /*}}}*/
219 // ListParser::ParseDepends - Parse a dependency element /*{{{*/
220 // ---------------------------------------------------------------------
221 /* This parses the dependency elements out of a standard string in place,
222 bit by bit. */
223 const char *debListParser::ParseDepends(const char *Start,const char *Stop,
224 string &Package,string &Ver,
225 unsigned int &Op)
226 {
227 // Strip off leading space
228 for (;Start != Stop && isspace(*Start) != 0; Start++);
229
230 // Parse off the package name
231 const char *I = Start;
232 for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
233 *I != ',' && *I != '|'; I++);
234
235 // Malformed, no '('
236 if (I != Stop && *I == ')')
237 return 0;
238
239 if (I == Start)
240 return 0;
241
242 // Stash the package name
243 Package.assign(Start,I - Start);
244
245 // Skip white space to the '('
246 for (;I != Stop && isspace(*I) != 0 ; I++);
247
248 // Parse a version
249 if (I != Stop && *I == '(')
250 {
251 // Skip the '('
252 for (I++; I != Stop && isspace(*I) != 0 ; I++);
253 if (I + 3 >= Stop)
254 return 0;
255
256 // Determine the operator
257 switch (*I)
258 {
259 case '<':
260 I++;
261 if (*I == '=')
262 {
263 I++;
264 Op = pkgCache::Dep::LessEq;
265 break;
266 }
267
268 if (*I == '<')
269 {
270 I++;
271 Op = pkgCache::Dep::Less;
272 break;
273 }
274
275 // < is the same as <= and << is really Cs < for some reason
276 Op = pkgCache::Dep::LessEq;
277 break;
278
279 case '>':
280 I++;
281 if (*I == '=')
282 {
283 I++;
284 Op = pkgCache::Dep::GreaterEq;
285 break;
286 }
287
288 if (*I == '>')
289 {
290 I++;
291 Op = pkgCache::Dep::Greater;
292 break;
293 }
294
295 // > is the same as >= and >> is really Cs > for some reason
296 Op = pkgCache::Dep::GreaterEq;
297 break;
298
299 case '=':
300 Op = pkgCache::Dep::Equals;
301 I++;
302 break;
303
304 // HACK around bad package definitions
305 default:
306 Op = pkgCache::Dep::Equals;
307 break;
308 }
309
310 // Skip whitespace
311 for (;I != Stop && isspace(*I) != 0; I++);
312 Start = I;
313 for (;I != Stop && *I != ')'; I++);
314 if (I == Stop || Start == I)
315 return 0;
316
317 Ver = string(Start,I-Start);
318 I++;
319 }
320 else
321 {
322 Ver = string();
323 Op = pkgCache::Dep::NoOp;
324 }
325
326 // Skip whitespace
327 for (;I != Stop && isspace(*I) != 0; I++);
328 if (I != Stop && *I == '|')
329 Op |= pkgCache::Dep::Or;
330
331 if (I == Stop || *I == ',' || *I == '|')
332 {
333 if (I != Stop)
334 for (I++; I != Stop && isspace(*I) != 0; I++);
335 return I;
336 }
337
338 return 0;
339 }
340 /*}}}*/
341 // ListParser::ParseDepends - Parse a dependency list /*{{{*/
342 // ---------------------------------------------------------------------
343 /* This is the higher level depends parser. It takes a tag and generates
344 a complete depends tree for the given version. */
345 bool debListParser::ParseDepends(pkgCache::VerIterator Ver,
346 const char *Tag,unsigned int Type)
347 {
348 const char *Start;
349 const char *Stop;
350 if (Section.Find(Tag,Start,Stop) == false)
351 return true;
352
353 string Package;
354 string Version;
355 unsigned int Op;
356
357 while (1)
358 {
359 Start = ParseDepends(Start,Stop,Package,Version,Op);
360 if (Start == 0)
361 return _error->Error("Problem parsing dependency %s",Tag);
362
363 if (NewDepends(Ver,Package,Version,Op,Type) == false)
364 return false;
365 if (Start == Stop)
366 break;
367 }
368 return true;
369 }
370 /*}}}*/
371 // ListParser::ParseProvides - Parse the provides list /*{{{*/
372 // ---------------------------------------------------------------------
373 /* */
374 bool debListParser::ParseProvides(pkgCache::VerIterator Ver)
375 {
376 const char *Start;
377 const char *Stop;
378 if (Section.Find("Provides",Start,Stop) == false)
379 return true;
380
381 string Package;
382 string Version;
383 unsigned int Op;
384
385 while (1)
386 {
387 Start = ParseDepends(Start,Stop,Package,Version,Op);
388 if (Start == 0)
389 return _error->Error("Problem parsing Provides line");
390 if (Op != pkgCache::Dep::NoOp)
391 return _error->Error("Malformed provides line");
392
393 if (NewProvides(Ver,Package,Version) == false)
394 return false;
395
396 if (Start == Stop)
397 break;
398 }
399
400 return true;
401 }
402 /*}}}*/
403 // ListParser::GrabWord - Matches a word and returns /*{{{*/
404 // ---------------------------------------------------------------------
405 /* Looks for a word in a list of words - for ParseStatus */
406 bool debListParser::GrabWord(string Word,WordList *List,int Count,
407 unsigned char &Out)
408 {
409 for (int C = 0; C != Count; C++)
410 {
411 if (strcasecmp(Word.c_str(),List[C].Str) == 0)
412 {
413 Out = List[C].Val;
414 return true;
415 }
416 }
417 return false;
418 }
419 /*}}}*/
420 // ListParser::Step - Move to the next section in the file /*{{{*/
421 // ---------------------------------------------------------------------
422 /* This has to be carefull to only process the correct architecture */
423 bool debListParser::Step()
424 {
425 iOffset = Tags.Offset();
426 string Arch = _config->Find("APT::architecture");
427 while (Tags.Step(Section) == true)
428 {
429 /* See if this is the correct Architecture, if it isnt then we
430 drop the whole section */
431 const char *Start;
432 const char *Stop;
433 if (Section.Find("Architecture",Start,Stop) == false)
434 return true;
435
436 if (stringcmp(Start,Stop,Arch.begin(),Arch.end()) == 0)
437 return true;
438
439 if (stringcmp(Start,Stop,"all") == 0)
440 return true;
441
442 iOffset = Tags.Offset();
443 }
444 return false;
445 }
446 /*}}}*/
447 // ListParser::LoadReleaseInfo - Load the release information /*{{{*/
448 // ---------------------------------------------------------------------
449 /* */
450 bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator FileI,
451 FileFd &File)
452 {
453 pkgTagFile Tags(File);
454 pkgTagSection Section;
455 if (Tags.Step(Section) == false)
456 return false;
457
458 const char *Start;
459 const char *Stop;
460 if (Section.Find("Archive",Start,Stop) == true)
461 FileI->Archive = WriteUniqString(Start,Stop - Start);
462 if (Section.Find("Component",Start,Stop) == true)
463 FileI->Component = WriteUniqString(Start,Stop - Start);
464 if (Section.Find("Version",Start,Stop) == true)
465 FileI->Version = WriteUniqString(Start,Stop - Start);
466 if (Section.Find("Origin",Start,Stop) == true)
467 FileI->Origin = WriteUniqString(Start,Stop - Start);
468 if (Section.Find("Label",Start,Stop) == true)
469 FileI->Label = WriteUniqString(Start,Stop - Start);
470 if (Section.Find("Architecture",Start,Stop) == true)
471 FileI->Architecture = WriteUniqString(Start,Stop - Start);
472
473 if (Section.FindFlag("NotAutomatic",FileI->Flags,
474 pkgCache::Flag::NotAutomatic) == false)
475 _error->Warning("Bad NotAutomatic flag");
476
477 return !_error->PendingError();
478 }
479 /*}}}*/