]> git.saurik.com Git - apt.git/blob - cmdline/apt-cache.cc
* Apply --important option to apt-cache depends (LP: #16947)
[apt.git] / cmdline / apt-cache.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-cache.cc,v 1.72 2004/04/30 04:34:03 mdz Exp $
4 /* ######################################################################
5
6 apt-cache - Manages the cache files
7
8 apt-cache provides some functions fo manipulating the cache files.
9 It uses the command line interface common to all the APT tools.
10
11 Returns 100 on failure, 0 on success.
12
13 ##################################################################### */
14 /*}}}*/
15 // Include Files /*{{{*/
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/pkgcachegen.h>
18 #include <apt-pkg/init.h>
19 #include <apt-pkg/progress.h>
20 #include <apt-pkg/sourcelist.h>
21 #include <apt-pkg/cmndline.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/pkgrecords.h>
24 #include <apt-pkg/srcrecords.h>
25 #include <apt-pkg/version.h>
26 #include <apt-pkg/policy.h>
27 #include <apt-pkg/tagfile.h>
28 #include <apt-pkg/algorithms.h>
29 #include <apt-pkg/sptr.h>
30
31 #include <config.h>
32 #include <apti18n.h>
33
34 #include <locale.h>
35 #include <iostream>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <regex.h>
39 #include <stdio.h>
40
41 #include <iomanip>
42 /*}}}*/
43
44 using namespace std;
45
46 pkgCache *GCache = 0;
47 pkgSourceList *SrcList = 0;
48
49 // LocalitySort - Sort a version list by package file locality /*{{{*/
50 // ---------------------------------------------------------------------
51 /* */
52 int LocalityCompare(const void *a, const void *b)
53 {
54 pkgCache::VerFile *A = *(pkgCache::VerFile **)a;
55 pkgCache::VerFile *B = *(pkgCache::VerFile **)b;
56
57 if (A == 0 && B == 0)
58 return 0;
59 if (A == 0)
60 return 1;
61 if (B == 0)
62 return -1;
63
64 if (A->File == B->File)
65 return A->Offset - B->Offset;
66 return A->File - B->File;
67 }
68
69 void LocalitySort(pkgCache::VerFile **begin,
70 unsigned long Count,size_t Size)
71 {
72 qsort(begin,Count,Size,LocalityCompare);
73 }
74
75 void LocalitySort(pkgCache::DescFile **begin,
76 unsigned long Count,size_t Size)
77 {
78 qsort(begin,Count,Size,LocalityCompare);
79 }
80 /*}}}*/
81 // UnMet - Show unmet dependencies /*{{{*/
82 // ---------------------------------------------------------------------
83 /* */
84 bool UnMet(CommandLine &CmdL)
85 {
86 pkgCache &Cache = *GCache;
87 bool Important = _config->FindB("APT::Cache::Important",false);
88
89 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
90 {
91 for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
92 {
93 bool Header = false;
94 for (pkgCache::DepIterator D = V.DependsList(); D.end() == false;)
95 {
96 // Collect or groups
97 pkgCache::DepIterator Start;
98 pkgCache::DepIterator End;
99 D.GlobOr(Start,End);
100
101 // Skip conflicts and replaces
102 if (End->Type != pkgCache::Dep::PreDepends &&
103 End->Type != pkgCache::Dep::Depends &&
104 End->Type != pkgCache::Dep::Suggests &&
105 End->Type != pkgCache::Dep::Recommends)
106 continue;
107
108 // Important deps only
109 if (Important == true)
110 if (End->Type != pkgCache::Dep::PreDepends &&
111 End->Type != pkgCache::Dep::Depends)
112 continue;
113
114 // Verify the or group
115 bool OK = false;
116 pkgCache::DepIterator RealStart = Start;
117 do
118 {
119 // See if this dep is Ok
120 pkgCache::Version **VList = Start.AllTargets();
121 if (*VList != 0)
122 {
123 OK = true;
124 delete [] VList;
125 break;
126 }
127 delete [] VList;
128
129 if (Start == End)
130 break;
131 Start++;
132 }
133 while (1);
134
135 // The group is OK
136 if (OK == true)
137 continue;
138
139 // Oops, it failed..
140 if (Header == false)
141 ioprintf(cout,_("Package %s version %s has an unmet dep:\n"),
142 P.Name(),V.VerStr());
143 Header = true;
144
145 // Print out the dep type
146 cout << " " << End.DepType() << ": ";
147
148 // Show the group
149 Start = RealStart;
150 do
151 {
152 cout << Start.TargetPkg().Name();
153 if (Start.TargetVer() != 0)
154 cout << " (" << Start.CompType() << " " << Start.TargetVer() <<
155 ")";
156 if (Start == End)
157 break;
158 cout << " | ";
159 Start++;
160 }
161 while (1);
162
163 cout << endl;
164 }
165 }
166 }
167 return true;
168 }
169 /*}}}*/
170 // DumpPackage - Show a dump of a package record /*{{{*/
171 // ---------------------------------------------------------------------
172 /* */
173 bool DumpPackage(CommandLine &CmdL)
174 {
175 pkgCache &Cache = *GCache;
176 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
177 {
178 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
179 if (Pkg.end() == true)
180 {
181 _error->Warning(_("Unable to locate package %s"),*I);
182 continue;
183 }
184
185 cout << "Package: " << Pkg.Name() << endl;
186 cout << "Versions: " << endl;
187 for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
188 {
189 cout << Cur.VerStr();
190 for (pkgCache::VerFileIterator Vf = Cur.FileList(); Vf.end() == false; Vf++)
191 cout << " (" << Vf.File().FileName() << ")";
192 cout << endl;
193 for (pkgCache::DescIterator D = Cur.DescriptionList(); D.end() == false; D++)
194 {
195 cout << " Description Language: " << D.LanguageCode() << endl
196 << " File: " << D.FileList().File().FileName() << endl
197 << " MD5: " << D.md5() << endl;
198 }
199 cout << endl;
200 }
201
202 cout << endl;
203
204 cout << "Reverse Depends: " << endl;
205 for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() != true; D++)
206 {
207 cout << " " << D.ParentPkg().Name() << ',' << D.TargetPkg().Name();
208 if (D->Version != 0)
209 cout << ' ' << DeNull(D.TargetVer()) << endl;
210 else
211 cout << endl;
212 }
213
214 cout << "Dependencies: " << endl;
215 for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
216 {
217 cout << Cur.VerStr() << " - ";
218 for (pkgCache::DepIterator Dep = Cur.DependsList(); Dep.end() != true; Dep++)
219 cout << Dep.TargetPkg().Name() << " (" << (int)Dep->CompareOp << " " << DeNull(Dep.TargetVer()) << ") ";
220 cout << endl;
221 }
222
223 cout << "Provides: " << endl;
224 for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
225 {
226 cout << Cur.VerStr() << " - ";
227 for (pkgCache::PrvIterator Prv = Cur.ProvidesList(); Prv.end() != true; Prv++)
228 cout << Prv.ParentPkg().Name() << " ";
229 cout << endl;
230 }
231 cout << "Reverse Provides: " << endl;
232 for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; Prv++)
233 cout << Prv.OwnerPkg().Name() << " " << Prv.OwnerVer().VerStr() << endl;
234 }
235
236 return true;
237 }
238 /*}}}*/
239 // Stats - Dump some nice statistics /*{{{*/
240 // ---------------------------------------------------------------------
241 /* */
242 bool Stats(CommandLine &Cmd)
243 {
244 pkgCache &Cache = *GCache;
245 cout << _("Total package names: ") << Cache.Head().PackageCount << " (" <<
246 SizeToStr(Cache.Head().PackageCount*Cache.Head().PackageSz) << ')' << endl;
247
248 int Normal = 0;
249 int Virtual = 0;
250 int NVirt = 0;
251 int DVirt = 0;
252 int Missing = 0;
253 pkgCache::PkgIterator I = Cache.PkgBegin();
254 for (;I.end() != true; I++)
255 {
256 if (I->VersionList != 0 && I->ProvidesList == 0)
257 {
258 Normal++;
259 continue;
260 }
261
262 if (I->VersionList != 0 && I->ProvidesList != 0)
263 {
264 NVirt++;
265 continue;
266 }
267
268 if (I->VersionList == 0 && I->ProvidesList != 0)
269 {
270 // Only 1 provides
271 if (I.ProvidesList()->NextProvides == 0)
272 {
273 DVirt++;
274 }
275 else
276 Virtual++;
277 continue;
278 }
279 if (I->VersionList == 0 && I->ProvidesList == 0)
280 {
281 Missing++;
282 continue;
283 }
284 }
285 cout << _(" Normal packages: ") << Normal << endl;
286 cout << _(" Pure virtual packages: ") << Virtual << endl;
287 cout << _(" Single virtual packages: ") << DVirt << endl;
288 cout << _(" Mixed virtual packages: ") << NVirt << endl;
289 cout << _(" Missing: ") << Missing << endl;
290
291 cout << _("Total distinct versions: ") << Cache.Head().VersionCount << " (" <<
292 SizeToStr(Cache.Head().VersionCount*Cache.Head().VersionSz) << ')' << endl;
293 cout << _("Total distinct descriptions: ") << Cache.Head().DescriptionCount << " (" <<
294 SizeToStr(Cache.Head().DescriptionCount*Cache.Head().DescriptionSz) << ')' << endl;
295 cout << _("Total dependencies: ") << Cache.Head().DependsCount << " (" <<
296 SizeToStr(Cache.Head().DependsCount*Cache.Head().DependencySz) << ')' << endl;
297
298 cout << _("Total ver/file relations: ") << Cache.Head().VerFileCount << " (" <<
299 SizeToStr(Cache.Head().VerFileCount*Cache.Head().VerFileSz) << ')' << endl;
300 cout << _("Total Desc/File relations: ") << Cache.Head().DescFileCount << " (" <<
301 SizeToStr(Cache.Head().DescFileCount*Cache.Head().DescFileSz) << ')' << endl;
302 cout << _("Total Provides mappings: ") << Cache.Head().ProvidesCount << " (" <<
303 SizeToStr(Cache.Head().ProvidesCount*Cache.Head().ProvidesSz) << ')' << endl;
304
305 // String list stats
306 unsigned long Size = 0;
307 unsigned long Count = 0;
308 for (pkgCache::StringItem *I = Cache.StringItemP + Cache.Head().StringList;
309 I!= Cache.StringItemP; I = Cache.StringItemP + I->NextItem)
310 {
311 Count++;
312 Size += strlen(Cache.StrP + I->String) + 1;
313 }
314 cout << _("Total globbed strings: ") << Count << " (" << SizeToStr(Size) << ')' << endl;
315
316 unsigned long DepVerSize = 0;
317 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
318 {
319 for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
320 {
321 for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; D++)
322 {
323 if (D->Version != 0)
324 DepVerSize += strlen(D.TargetVer()) + 1;
325 }
326 }
327 }
328 cout << _("Total dependency version space: ") << SizeToStr(DepVerSize) << endl;
329
330 unsigned long Slack = 0;
331 for (int I = 0; I != 7; I++)
332 Slack += Cache.Head().Pools[I].ItemSize*Cache.Head().Pools[I].Count;
333 cout << _("Total slack space: ") << SizeToStr(Slack) << endl;
334
335 unsigned long Total = 0;
336 Total = Slack + Size + Cache.Head().DependsCount*Cache.Head().DependencySz +
337 Cache.Head().VersionCount*Cache.Head().VersionSz +
338 Cache.Head().PackageCount*Cache.Head().PackageSz +
339 Cache.Head().VerFileCount*Cache.Head().VerFileSz +
340 Cache.Head().ProvidesCount*Cache.Head().ProvidesSz;
341 cout << _("Total space accounted for: ") << SizeToStr(Total) << endl;
342
343 return true;
344 }
345 /*}}}*/
346 // Dump - show everything /*{{{*/
347 // ---------------------------------------------------------------------
348 /* This is worthless except fer debugging things */
349 bool Dump(CommandLine &Cmd)
350 {
351 pkgCache &Cache = *GCache;
352 cout << "Using Versioning System: " << Cache.VS->Label << endl;
353
354 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
355 {
356 cout << "Package: " << P.Name() << endl;
357 for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
358 {
359 cout << " Version: " << V.VerStr() << endl;
360 cout << " File: " << V.FileList().File().FileName() << endl;
361 for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; D++)
362 cout << " Depends: " << D.TargetPkg().Name() << ' ' <<
363 DeNull(D.TargetVer()) << endl;
364 for (pkgCache::DescIterator D = V.DescriptionList(); D.end() == false; D++)
365 {
366 cout << " Description Language: " << D.LanguageCode() << endl
367 << " File: " << D.FileList().File().FileName() << endl
368 << " MD5: " << D.md5() << endl;
369 }
370 }
371 }
372
373 for (pkgCache::PkgFileIterator F = Cache.FileBegin(); F.end() == false; F++)
374 {
375 cout << "File: " << F.FileName() << endl;
376 cout << " Type: " << F.IndexType() << endl;
377 cout << " Size: " << F->Size << endl;
378 cout << " ID: " << F->ID << endl;
379 cout << " Flags: " << F->Flags << endl;
380 cout << " Time: " << TimeRFC1123(F->mtime) << endl;
381 cout << " Archive: " << DeNull(F.Archive()) << endl;
382 cout << " Component: " << DeNull(F.Component()) << endl;
383 cout << " Version: " << DeNull(F.Version()) << endl;
384 cout << " Origin: " << DeNull(F.Origin()) << endl;
385 cout << " Site: " << DeNull(F.Site()) << endl;
386 cout << " Label: " << DeNull(F.Label()) << endl;
387 cout << " Architecture: " << DeNull(F.Architecture()) << endl;
388 }
389
390 return true;
391 }
392 /*}}}*/
393 // DumpAvail - Print out the available list /*{{{*/
394 // ---------------------------------------------------------------------
395 /* This is needed to make dpkg --merge happy.. I spent a bit of time to
396 make this run really fast, perhaps I went a little overboard.. */
397 bool DumpAvail(CommandLine &Cmd)
398 {
399 pkgCache &Cache = *GCache;
400
401 pkgPolicy Plcy(&Cache);
402 if (ReadPinFile(Plcy) == false)
403 return false;
404
405 unsigned long Count = Cache.HeaderP->PackageCount+1;
406 pkgCache::VerFile **VFList = new pkgCache::VerFile *[Count];
407 memset(VFList,0,sizeof(*VFList)*Count);
408
409 // Map versions that we want to write out onto the VerList array.
410 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
411 {
412 if (P->VersionList == 0)
413 continue;
414
415 /* Find the proper version to use. If the policy says there are no
416 possible selections we return the installed version, if available..
417 This prevents dselect from making it obsolete. */
418 pkgCache::VerIterator V = Plcy.GetCandidateVer(P);
419 if (V.end() == true)
420 {
421 if (P->CurrentVer == 0)
422 continue;
423 V = P.CurrentVer();
424 }
425
426 pkgCache::VerFileIterator VF = V.FileList();
427 for (; VF.end() == false ; VF++)
428 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == 0)
429 break;
430
431 /* Okay, here we have a bit of a problem.. The policy has selected the
432 currently installed package - however it only exists in the
433 status file.. We need to write out something or dselect will mark
434 the package as obsolete! Thus we emit the status file entry, but
435 below we remove the status line to make it valid for the
436 available file. However! We only do this if their do exist *any*
437 non-source versions of the package - that way the dselect obsolete
438 handling works OK. */
439 if (VF.end() == true)
440 {
441 for (pkgCache::VerIterator Cur = P.VersionList(); Cur.end() != true; Cur++)
442 {
443 for (VF = Cur.FileList(); VF.end() == false; VF++)
444 {
445 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == 0)
446 {
447 VF = V.FileList();
448 break;
449 }
450 }
451
452 if (VF.end() == false)
453 break;
454 }
455 }
456
457 VFList[P->ID] = VF;
458 }
459
460 LocalitySort(VFList,Count,sizeof(*VFList));
461
462 // Iterate over all the package files and write them out.
463 char *Buffer = new char[Cache.HeaderP->MaxVerFileSize+10];
464 for (pkgCache::VerFile **J = VFList; *J != 0;)
465 {
466 pkgCache::PkgFileIterator File(Cache,(*J)->File + Cache.PkgFileP);
467 if (File.IsOk() == false)
468 {
469 _error->Error(_("Package file %s is out of sync."),File.FileName());
470 break;
471 }
472
473 FileFd PkgF(File.FileName(),FileFd::ReadOnly);
474 if (_error->PendingError() == true)
475 break;
476
477 /* Write all of the records from this package file, since we
478 already did locality sorting we can now just seek through the
479 file in read order. We apply 1 more optimization here, since often
480 there will be < 1 byte gaps between records (for the \n) we read that
481 into the next buffer and offset a bit.. */
482 unsigned long Pos = 0;
483 for (; *J != 0; J++)
484 {
485 if ((*J)->File + Cache.PkgFileP != File)
486 break;
487
488 const pkgCache::VerFile &VF = **J;
489
490 // Read the record and then write it out again.
491 unsigned long Jitter = VF.Offset - Pos;
492 if (Jitter > 8)
493 {
494 if (PkgF.Seek(VF.Offset) == false)
495 break;
496 Jitter = 0;
497 }
498
499 if (PkgF.Read(Buffer,VF.Size + Jitter) == false)
500 break;
501 Buffer[VF.Size + Jitter] = '\n';
502
503 // See above..
504 if ((File->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
505 {
506 pkgTagSection Tags;
507 TFRewriteData RW[] = {{"Status",0},{"Config-Version",0},{}};
508 const char *Zero = 0;
509 if (Tags.Scan(Buffer+Jitter,VF.Size+1) == false ||
510 TFRewrite(stdout,Tags,&Zero,RW) == false)
511 {
512 _error->Error("Internal Error, Unable to parse a package record");
513 break;
514 }
515 fputc('\n',stdout);
516 }
517 else
518 {
519 if (fwrite(Buffer+Jitter,VF.Size+1,1,stdout) != 1)
520 break;
521 }
522
523 Pos = VF.Offset + VF.Size;
524 }
525
526 fflush(stdout);
527 if (_error->PendingError() == true)
528 break;
529 }
530
531 delete [] Buffer;
532 delete [] VFList;
533 return !_error->PendingError();
534 }
535 /*}}}*/
536 // Depends - Print out a dependency tree /*{{{*/
537 // ---------------------------------------------------------------------
538 /* */
539 bool Depends(CommandLine &CmdL)
540 {
541 pkgCache &Cache = *GCache;
542 SPtrArray<unsigned> Colours = new unsigned[Cache.Head().PackageCount];
543 memset(Colours,0,sizeof(*Colours)*Cache.Head().PackageCount);
544
545 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
546 {
547 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
548 if (Pkg.end() == true)
549 {
550 _error->Warning(_("Unable to locate package %s"),*I);
551 continue;
552 }
553 Colours[Pkg->ID] = 1;
554 }
555
556 bool Recurse = _config->FindB("APT::Cache::RecurseDepends",false);
557 bool Installed = _config->FindB("APT::Cache::Installed",false);
558 bool Important = _config->FindB("APT::Cache::Important",false);
559 bool DidSomething;
560 do
561 {
562 DidSomething = false;
563 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
564 {
565 if (Colours[Pkg->ID] != 1)
566 continue;
567 Colours[Pkg->ID] = 2;
568 DidSomething = true;
569
570 pkgCache::VerIterator Ver = Pkg.VersionList();
571 if (Ver.end() == true)
572 {
573 cout << '<' << Pkg.Name() << '>' << endl;
574 continue;
575 }
576
577 cout << Pkg.Name() << endl;
578
579 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
580 {
581 // Important deps only
582 if (Important == true)
583 if (D->Type != pkgCache::Dep::PreDepends &&
584 D->Type != pkgCache::Dep::Depends)
585 continue;
586
587 pkgCache::PkgIterator Trg = D.TargetPkg();
588
589 if((Installed && Trg->CurrentVer != 0) || !Installed)
590 {
591
592 if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
593 cout << " |";
594 else
595 cout << " ";
596
597 // Show the package
598 if (Trg->VersionList == 0)
599 cout << D.DepType() << ": <" << Trg.Name() << ">" << endl;
600 else
601 cout << D.DepType() << ": " << Trg.Name() << endl;
602
603 if (Recurse == true)
604 Colours[D.TargetPkg()->ID]++;
605
606 }
607
608 // Display all solutions
609 SPtrArray<pkgCache::Version *> List = D.AllTargets();
610 pkgPrioSortList(Cache,List);
611 for (pkgCache::Version **I = List; *I != 0; I++)
612 {
613 pkgCache::VerIterator V(Cache,*I);
614 if (V != Cache.VerP + V.ParentPkg()->VersionList ||
615 V->ParentPkg == D->Package)
616 continue;
617 cout << " " << V.ParentPkg().Name() << endl;
618
619 if (Recurse == true)
620 Colours[D.ParentPkg()->ID]++;
621 }
622 }
623 }
624 }
625 while (DidSomething == true);
626
627 return true;
628 }
629
630 // RDepends - Print out a reverse dependency tree - mbc /*{{{*/
631 // ---------------------------------------------------------------------
632 /* */
633 bool RDepends(CommandLine &CmdL)
634 {
635 pkgCache &Cache = *GCache;
636 SPtrArray<unsigned> Colours = new unsigned[Cache.Head().PackageCount];
637 memset(Colours,0,sizeof(*Colours)*Cache.Head().PackageCount);
638
639 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
640 {
641 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
642 if (Pkg.end() == true)
643 {
644 _error->Warning(_("Unable to locate package %s"),*I);
645 continue;
646 }
647 Colours[Pkg->ID] = 1;
648 }
649
650 bool Recurse = _config->FindB("APT::Cache::RecurseDepends",false);
651 bool Installed = _config->FindB("APT::Cache::Installed",false);
652 bool DidSomething;
653 do
654 {
655 DidSomething = false;
656 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
657 {
658 if (Colours[Pkg->ID] != 1)
659 continue;
660 Colours[Pkg->ID] = 2;
661 DidSomething = true;
662
663 pkgCache::VerIterator Ver = Pkg.VersionList();
664 if (Ver.end() == true)
665 {
666 cout << '<' << Pkg.Name() << '>' << endl;
667 continue;
668 }
669
670 cout << Pkg.Name() << endl;
671
672 cout << "Reverse Depends:" << endl;
673 for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() == false; D++)
674 {
675 // Show the package
676 pkgCache::PkgIterator Trg = D.ParentPkg();
677
678 if((Installed && Trg->CurrentVer != 0) || !Installed)
679 {
680
681 if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
682 cout << " |";
683 else
684 cout << " ";
685
686 if (Trg->VersionList == 0)
687 cout << D.DepType() << ": <" << Trg.Name() << ">" << endl;
688 else
689 cout << Trg.Name() << endl;
690
691 if (Recurse == true)
692 Colours[D.ParentPkg()->ID]++;
693
694 }
695
696 // Display all solutions
697 SPtrArray<pkgCache::Version *> List = D.AllTargets();
698 pkgPrioSortList(Cache,List);
699 for (pkgCache::Version **I = List; *I != 0; I++)
700 {
701 pkgCache::VerIterator V(Cache,*I);
702 if (V != Cache.VerP + V.ParentPkg()->VersionList ||
703 V->ParentPkg == D->Package)
704 continue;
705 cout << " " << V.ParentPkg().Name() << endl;
706
707 if (Recurse == true)
708 Colours[D.ParentPkg()->ID]++;
709 }
710 }
711 }
712 }
713 while (DidSomething == true);
714
715 return true;
716 }
717
718 /*}}}*/
719
720
721 // xvcg - Generate a graph for xvcg /*{{{*/
722 // ---------------------------------------------------------------------
723 // Code contributed from Junichi Uekawa <dancer@debian.org> on 20 June 2002.
724
725 bool XVcg(CommandLine &CmdL)
726 {
727 pkgCache &Cache = *GCache;
728 bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
729
730 /* Normal packages are boxes
731 Pure Provides are triangles
732 Mixed are diamonds
733 rhomb are missing packages*/
734 const char *Shapes[] = {"ellipse","triangle","box","rhomb"};
735
736 /* Initialize the list of packages to show.
737 1 = To Show
738 2 = To Show no recurse
739 3 = Emitted no recurse
740 4 = Emitted
741 0 = None */
742 enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
743 enum TheFlags {ForceNR=(1<<0)};
744 unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
745 unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
746 unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
747
748 // Show everything if no arguments given
749 if (CmdL.FileList[1] == 0)
750 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
751 Show[I] = ToShow;
752 else
753 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
754 Show[I] = None;
755 memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
756
757 // Map the shapes
758 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
759 {
760 if (Pkg->VersionList == 0)
761 {
762 // Missing
763 if (Pkg->ProvidesList == 0)
764 ShapeMap[Pkg->ID] = 0;
765 else
766 ShapeMap[Pkg->ID] = 1;
767 }
768 else
769 {
770 // Normal
771 if (Pkg->ProvidesList == 0)
772 ShapeMap[Pkg->ID] = 2;
773 else
774 ShapeMap[Pkg->ID] = 3;
775 }
776 }
777
778 // Load the list of packages from the command line into the show list
779 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
780 {
781 // Process per-package flags
782 string P = *I;
783 bool Force = false;
784 if (P.length() > 3)
785 {
786 if (P.end()[-1] == '^')
787 {
788 Force = true;
789 P.erase(P.end()-1);
790 }
791
792 if (P.end()[-1] == ',')
793 P.erase(P.end()-1);
794 }
795
796 // Locate the package
797 pkgCache::PkgIterator Pkg = Cache.FindPkg(P);
798 if (Pkg.end() == true)
799 {
800 _error->Warning(_("Unable to locate package %s"),*I);
801 continue;
802 }
803 Show[Pkg->ID] = ToShow;
804
805 if (Force == true)
806 Flags[Pkg->ID] |= ForceNR;
807 }
808
809 // Little header
810 cout << "graph: { title: \"packages\"" << endl <<
811 "xmax: 700 ymax: 700 x: 30 y: 30" << endl <<
812 "layout_downfactor: 8" << endl;
813
814 bool Act = true;
815 while (Act == true)
816 {
817 Act = false;
818 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
819 {
820 // See we need to show this package
821 if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
822 continue;
823
824 //printf ("node: { title: \"%s\" label: \"%s\" }\n", Pkg.Name(), Pkg.Name());
825
826 // Colour as done
827 if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
828 {
829 // Pure Provides and missing packages have no deps!
830 if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
831 Show[Pkg->ID] = Done;
832 else
833 Show[Pkg->ID] = DoneNR;
834 }
835 else
836 Show[Pkg->ID] = Done;
837 Act = true;
838
839 // No deps to map out
840 if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
841 continue;
842
843 pkgCache::VerIterator Ver = Pkg.VersionList();
844 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
845 {
846 // See if anything can meet this dep
847 // Walk along the actual package providing versions
848 bool Hit = false;
849 pkgCache::PkgIterator DPkg = D.TargetPkg();
850 for (pkgCache::VerIterator I = DPkg.VersionList();
851 I.end() == false && Hit == false; I++)
852 {
853 if (Cache.VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
854 Hit = true;
855 }
856
857 // Follow all provides
858 for (pkgCache::PrvIterator I = DPkg.ProvidesList();
859 I.end() == false && Hit == false; I++)
860 {
861 if (Cache.VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
862 Hit = true;
863 }
864
865
866 // Only graph critical deps
867 if (D.IsCritical() == true)
868 {
869 printf ("edge: { sourcename: \"%s\" targetname: \"%s\" class: 2 ",Pkg.Name(), D.TargetPkg().Name() );
870
871 // Colour the node for recursion
872 if (Show[D.TargetPkg()->ID] <= DoneNR)
873 {
874 /* If a conflicts does not meet anything in the database
875 then show the relation but do not recurse */
876 if (Hit == false &&
877 (D->Type == pkgCache::Dep::Conflicts ||
878 D->Type == pkgCache::Dep::DpkgBreaks ||
879 D->Type == pkgCache::Dep::Obsoletes))
880 {
881 if (Show[D.TargetPkg()->ID] == None &&
882 Show[D.TargetPkg()->ID] != ToShow)
883 Show[D.TargetPkg()->ID] = ToShowNR;
884 }
885 else
886 {
887 if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
888 Show[D.TargetPkg()->ID] = ToShowNR;
889 else
890 Show[D.TargetPkg()->ID] = ToShow;
891 }
892 }
893
894 // Edge colour
895 switch(D->Type)
896 {
897 case pkgCache::Dep::Conflicts:
898 printf("label: \"conflicts\" color: lightgreen }\n");
899 break;
900 case pkgCache::Dep::DpkgBreaks:
901 printf("label: \"breaks\" color: lightgreen }\n");
902 break;
903 case pkgCache::Dep::Obsoletes:
904 printf("label: \"obsoletes\" color: lightgreen }\n");
905 break;
906
907 case pkgCache::Dep::PreDepends:
908 printf("label: \"predepends\" color: blue }\n");
909 break;
910
911 default:
912 printf("}\n");
913 break;
914 }
915 }
916 }
917 }
918 }
919
920 /* Draw the box colours after the fact since we can not tell what colour
921 they should be until everything is finished drawing */
922 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
923 {
924 if (Show[Pkg->ID] < DoneNR)
925 continue;
926
927 if (Show[Pkg->ID] == DoneNR)
928 printf("node: { title: \"%s\" label: \"%s\" color: orange shape: %s }\n", Pkg.Name(), Pkg.Name(),
929 Shapes[ShapeMap[Pkg->ID]]);
930 else
931 printf("node: { title: \"%s\" label: \"%s\" shape: %s }\n", Pkg.Name(), Pkg.Name(),
932 Shapes[ShapeMap[Pkg->ID]]);
933
934 }
935
936 printf("}\n");
937 return true;
938 }
939 /*}}}*/
940
941
942 // Dotty - Generate a graph for Dotty /*{{{*/
943 // ---------------------------------------------------------------------
944 /* Dotty is the graphvis program for generating graphs. It is a fairly
945 simple queuing algorithm that just writes dependencies and nodes.
946 http://www.research.att.com/sw/tools/graphviz/ */
947 bool Dotty(CommandLine &CmdL)
948 {
949 pkgCache &Cache = *GCache;
950 bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
951
952 /* Normal packages are boxes
953 Pure Provides are triangles
954 Mixed are diamonds
955 Hexagons are missing packages*/
956 const char *Shapes[] = {"hexagon","triangle","box","diamond"};
957
958 /* Initialize the list of packages to show.
959 1 = To Show
960 2 = To Show no recurse
961 3 = Emitted no recurse
962 4 = Emitted
963 0 = None */
964 enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
965 enum TheFlags {ForceNR=(1<<0)};
966 unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
967 unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
968 unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
969
970 // Show everything if no arguments given
971 if (CmdL.FileList[1] == 0)
972 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
973 Show[I] = ToShow;
974 else
975 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
976 Show[I] = None;
977 memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
978
979 // Map the shapes
980 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
981 {
982 if (Pkg->VersionList == 0)
983 {
984 // Missing
985 if (Pkg->ProvidesList == 0)
986 ShapeMap[Pkg->ID] = 0;
987 else
988 ShapeMap[Pkg->ID] = 1;
989 }
990 else
991 {
992 // Normal
993 if (Pkg->ProvidesList == 0)
994 ShapeMap[Pkg->ID] = 2;
995 else
996 ShapeMap[Pkg->ID] = 3;
997 }
998 }
999
1000 // Load the list of packages from the command line into the show list
1001 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1002 {
1003 // Process per-package flags
1004 string P = *I;
1005 bool Force = false;
1006 if (P.length() > 3)
1007 {
1008 if (P.end()[-1] == '^')
1009 {
1010 Force = true;
1011 P.erase(P.end()-1);
1012 }
1013
1014 if (P.end()[-1] == ',')
1015 P.erase(P.end()-1);
1016 }
1017
1018 // Locate the package
1019 pkgCache::PkgIterator Pkg = Cache.FindPkg(P);
1020 if (Pkg.end() == true)
1021 {
1022 _error->Warning(_("Unable to locate package %s"),*I);
1023 continue;
1024 }
1025 Show[Pkg->ID] = ToShow;
1026
1027 if (Force == true)
1028 Flags[Pkg->ID] |= ForceNR;
1029 }
1030
1031 // Little header
1032 printf("digraph packages {\n");
1033 printf("concentrate=true;\n");
1034 printf("size=\"30,40\";\n");
1035
1036 bool Act = true;
1037 while (Act == true)
1038 {
1039 Act = false;
1040 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
1041 {
1042 // See we need to show this package
1043 if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
1044 continue;
1045
1046 // Colour as done
1047 if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
1048 {
1049 // Pure Provides and missing packages have no deps!
1050 if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
1051 Show[Pkg->ID] = Done;
1052 else
1053 Show[Pkg->ID] = DoneNR;
1054 }
1055 else
1056 Show[Pkg->ID] = Done;
1057 Act = true;
1058
1059 // No deps to map out
1060 if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
1061 continue;
1062
1063 pkgCache::VerIterator Ver = Pkg.VersionList();
1064 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
1065 {
1066 // See if anything can meet this dep
1067 // Walk along the actual package providing versions
1068 bool Hit = false;
1069 pkgCache::PkgIterator DPkg = D.TargetPkg();
1070 for (pkgCache::VerIterator I = DPkg.VersionList();
1071 I.end() == false && Hit == false; I++)
1072 {
1073 if (Cache.VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
1074 Hit = true;
1075 }
1076
1077 // Follow all provides
1078 for (pkgCache::PrvIterator I = DPkg.ProvidesList();
1079 I.end() == false && Hit == false; I++)
1080 {
1081 if (Cache.VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
1082 Hit = true;
1083 }
1084
1085 // Only graph critical deps
1086 if (D.IsCritical() == true)
1087 {
1088 printf("\"%s\" -> \"%s\"",Pkg.Name(),D.TargetPkg().Name());
1089
1090 // Colour the node for recursion
1091 if (Show[D.TargetPkg()->ID] <= DoneNR)
1092 {
1093 /* If a conflicts does not meet anything in the database
1094 then show the relation but do not recurse */
1095 if (Hit == false &&
1096 (D->Type == pkgCache::Dep::Conflicts ||
1097 D->Type == pkgCache::Dep::Obsoletes))
1098 {
1099 if (Show[D.TargetPkg()->ID] == None &&
1100 Show[D.TargetPkg()->ID] != ToShow)
1101 Show[D.TargetPkg()->ID] = ToShowNR;
1102 }
1103 else
1104 {
1105 if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
1106 Show[D.TargetPkg()->ID] = ToShowNR;
1107 else
1108 Show[D.TargetPkg()->ID] = ToShow;
1109 }
1110 }
1111
1112 // Edge colour
1113 switch(D->Type)
1114 {
1115 case pkgCache::Dep::Conflicts:
1116 case pkgCache::Dep::Obsoletes:
1117 printf("[color=springgreen];\n");
1118 break;
1119
1120 case pkgCache::Dep::PreDepends:
1121 printf("[color=blue];\n");
1122 break;
1123
1124 default:
1125 printf(";\n");
1126 break;
1127 }
1128 }
1129 }
1130 }
1131 }
1132
1133 /* Draw the box colours after the fact since we can not tell what colour
1134 they should be until everything is finished drawing */
1135 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
1136 {
1137 if (Show[Pkg->ID] < DoneNR)
1138 continue;
1139
1140 // Orange box for early recursion stoppage
1141 if (Show[Pkg->ID] == DoneNR)
1142 printf("\"%s\" [color=orange,shape=%s];\n",Pkg.Name(),
1143 Shapes[ShapeMap[Pkg->ID]]);
1144 else
1145 printf("\"%s\" [shape=%s];\n",Pkg.Name(),
1146 Shapes[ShapeMap[Pkg->ID]]);
1147 }
1148
1149 printf("}\n");
1150 return true;
1151 }
1152 /*}}}*/
1153 // DoAdd - Perform an adding operation /*{{{*/
1154 // ---------------------------------------------------------------------
1155 /* */
1156 bool DoAdd(CommandLine &CmdL)
1157 {
1158 return _error->Error("Unimplemented");
1159 #if 0
1160 // Make sure there is at least one argument
1161 if (CmdL.FileSize() <= 1)
1162 return _error->Error("You must give at least one file name");
1163
1164 // Open the cache
1165 FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::WriteAny);
1166 if (_error->PendingError() == true)
1167 return false;
1168
1169 DynamicMMap Map(CacheF,MMap::Public);
1170 if (_error->PendingError() == true)
1171 return false;
1172
1173 OpTextProgress Progress(*_config);
1174 pkgCacheGenerator Gen(Map,Progress);
1175 if (_error->PendingError() == true)
1176 return false;
1177
1178 unsigned long Length = CmdL.FileSize() - 1;
1179 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1180 {
1181 Progress.OverallProgress(I - CmdL.FileList,Length,1,"Generating cache");
1182 Progress.SubProgress(Length);
1183
1184 // Do the merge
1185 FileFd TagF(*I,FileFd::ReadOnly);
1186 debListParser Parser(TagF);
1187 if (_error->PendingError() == true)
1188 return _error->Error("Problem opening %s",*I);
1189
1190 if (Gen.SelectFile(*I,"") == false)
1191 return _error->Error("Problem with SelectFile");
1192
1193 if (Gen.MergeList(Parser) == false)
1194 return _error->Error("Problem with MergeList");
1195 }
1196
1197 Progress.Done();
1198 GCache = &Gen.GetCache();
1199 Stats(CmdL);
1200
1201 return true;
1202 #endif
1203 }
1204 /*}}}*/
1205 // DisplayRecord - Displays the complete record for the package /*{{{*/
1206 // ---------------------------------------------------------------------
1207 /* This displays the package record from the proper package index file.
1208 It is not used by DumpAvail for performance reasons. */
1209 bool DisplayRecord(pkgCache::VerIterator V)
1210 {
1211 // Find an appropriate file
1212 pkgCache::VerFileIterator Vf = V.FileList();
1213 for (; Vf.end() == false; Vf++)
1214 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
1215 break;
1216 if (Vf.end() == true)
1217 Vf = V.FileList();
1218
1219 // Check and load the package list file
1220 pkgCache::PkgFileIterator I = Vf.File();
1221 if (I.IsOk() == false)
1222 return _error->Error(_("Package file %s is out of sync."),I.FileName());
1223
1224 FileFd PkgF(I.FileName(),FileFd::ReadOnly);
1225 if (_error->PendingError() == true)
1226 return false;
1227
1228 // Read the record
1229 unsigned char *Buffer = new unsigned char[GCache->HeaderP->MaxVerFileSize+1];
1230 Buffer[V.FileList()->Size] = '\n';
1231 if (PkgF.Seek(V.FileList()->Offset) == false ||
1232 PkgF.Read(Buffer,V.FileList()->Size) == false)
1233 {
1234 delete [] Buffer;
1235 return false;
1236 }
1237
1238 // Get a pointer to start of Description field
1239 const unsigned char *DescP = (unsigned char*)strstr((char*)Buffer, "Description:");
1240
1241 // Write all but Description
1242 if (fwrite(Buffer,1,DescP - Buffer,stdout) < (size_t)(DescP - Buffer))
1243 {
1244 delete [] Buffer;
1245 return false;
1246 }
1247
1248 // Show the right description
1249 pkgRecords Recs(*GCache);
1250 pkgCache::DescIterator Desc = V.TranslatedDescription();
1251 pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
1252 cout << "Description" << ( (strcmp(Desc.LanguageCode(),"") != 0) ? "-" : "" ) << Desc.LanguageCode() << ": " << P.LongDesc();
1253
1254 // Find the first field after the description (if there is any)
1255 for(DescP++;DescP != &Buffer[V.FileList()->Size];DescP++)
1256 {
1257 if(*DescP == '\n' && *(DescP+1) != ' ')
1258 {
1259 // write the rest of the buffer
1260 const unsigned char *end=&Buffer[V.FileList()->Size];
1261 if (fwrite(DescP,1,end-DescP,stdout) < (size_t)(end-DescP))
1262 {
1263 delete [] Buffer;
1264 return false;
1265 }
1266
1267 break;
1268 }
1269 }
1270 // write a final newline (after the description)
1271 cout<<endl;
1272 delete [] Buffer;
1273
1274 return true;
1275 }
1276 /*}}}*/
1277 // Search - Perform a search /*{{{*/
1278 // ---------------------------------------------------------------------
1279 /* This searches the package names and pacakge descriptions for a pattern */
1280 struct ExDescFile
1281 {
1282 pkgCache::DescFile *Df;
1283 bool NameMatch;
1284 };
1285
1286 bool Search(CommandLine &CmdL)
1287 {
1288 pkgCache &Cache = *GCache;
1289 bool ShowFull = _config->FindB("APT::Cache::ShowFull",false);
1290 bool NamesOnly = _config->FindB("APT::Cache::NamesOnly",false);
1291 unsigned NumPatterns = CmdL.FileSize() -1;
1292
1293 pkgDepCache::Policy Plcy;
1294
1295 // Make sure there is at least one argument
1296 if (NumPatterns < 1)
1297 return _error->Error(_("You must give exactly one pattern"));
1298
1299 // Compile the regex pattern
1300 regex_t *Patterns = new regex_t[NumPatterns];
1301 memset(Patterns,0,sizeof(*Patterns)*NumPatterns);
1302 for (unsigned I = 0; I != NumPatterns; I++)
1303 {
1304 if (regcomp(&Patterns[I],CmdL.FileList[I+1],REG_EXTENDED | REG_ICASE |
1305 REG_NOSUB) != 0)
1306 {
1307 for (; I != 0; I--)
1308 regfree(&Patterns[I]);
1309 return _error->Error("Regex compilation error");
1310 }
1311 }
1312
1313 // Create the text record parser
1314 pkgRecords Recs(Cache);
1315 if (_error->PendingError() == true)
1316 {
1317 for (unsigned I = 0; I != NumPatterns; I++)
1318 regfree(&Patterns[I]);
1319 return false;
1320 }
1321
1322 ExDescFile *DFList = new ExDescFile[Cache.HeaderP->PackageCount+1];
1323 memset(DFList,0,sizeof(*DFList)*Cache.HeaderP->PackageCount+1);
1324
1325 // Map versions that we want to write out onto the VerList array.
1326 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
1327 {
1328 DFList[P->ID].NameMatch = NumPatterns != 0;
1329 for (unsigned I = 0; I != NumPatterns; I++)
1330 {
1331 if (regexec(&Patterns[I],P.Name(),0,0,0) == 0)
1332 DFList[P->ID].NameMatch &= true;
1333 else
1334 DFList[P->ID].NameMatch = false;
1335 }
1336
1337 // Doing names only, drop any that dont match..
1338 if (NamesOnly == true && DFList[P->ID].NameMatch == false)
1339 continue;
1340
1341 // Find the proper version to use.
1342 pkgCache::VerIterator V = Plcy.GetCandidateVer(P);
1343 if (V.end() == false)
1344 DFList[P->ID].Df = V.DescriptionList().FileList();
1345 }
1346
1347 // Include all the packages that provide matching names too
1348 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
1349 {
1350 if (DFList[P->ID].NameMatch == false)
1351 continue;
1352
1353 for (pkgCache::PrvIterator Prv = P.ProvidesList() ; Prv.end() == false; Prv++)
1354 {
1355 pkgCache::VerIterator V = Plcy.GetCandidateVer(Prv.OwnerPkg());
1356 if (V.end() == false)
1357 {
1358 DFList[Prv.OwnerPkg()->ID].Df = V.DescriptionList().FileList();
1359 DFList[Prv.OwnerPkg()->ID].NameMatch = true;
1360 }
1361 }
1362 }
1363
1364 LocalitySort(&DFList->Df,Cache.HeaderP->PackageCount,sizeof(*DFList));
1365
1366 // Iterate over all the version records and check them
1367 for (ExDescFile *J = DFList; J->Df != 0; J++)
1368 {
1369 pkgRecords::Parser &P = Recs.Lookup(pkgCache::DescFileIterator(Cache,J->Df));
1370
1371 bool Match = true;
1372 if (J->NameMatch == false)
1373 {
1374 string LongDesc = P.LongDesc();
1375 Match = NumPatterns != 0;
1376 for (unsigned I = 0; I != NumPatterns; I++)
1377 {
1378 if (regexec(&Patterns[I],LongDesc.c_str(),0,0,0) == 0)
1379 Match &= true;
1380 else
1381 Match = false;
1382 }
1383 }
1384
1385 if (Match == true)
1386 {
1387 if (ShowFull == true)
1388 {
1389 const char *Start;
1390 const char *End;
1391 P.GetRec(Start,End);
1392 fwrite(Start,End-Start,1,stdout);
1393 putc('\n',stdout);
1394 }
1395 else
1396 printf("%s - %s\n",P.Name().c_str(),P.ShortDesc().c_str());
1397 }
1398 }
1399
1400 delete [] DFList;
1401 for (unsigned I = 0; I != NumPatterns; I++)
1402 regfree(&Patterns[I]);
1403 if (ferror(stdout))
1404 return _error->Error("Write to stdout failed");
1405 return true;
1406 }
1407 /*}}}*/
1408 // ShowPackage - Dump the package record to the screen /*{{{*/
1409 // ---------------------------------------------------------------------
1410 /* */
1411 bool ShowPackage(CommandLine &CmdL)
1412 {
1413 pkgCache &Cache = *GCache;
1414 pkgDepCache::Policy Plcy;
1415
1416 unsigned found = 0;
1417
1418 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1419 {
1420 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
1421 if (Pkg.end() == true)
1422 {
1423 _error->Warning(_("Unable to locate package %s"),*I);
1424 continue;
1425 }
1426
1427 ++found;
1428
1429 // Find the proper version to use.
1430 if (_config->FindB("APT::Cache::AllVersions","true") == true)
1431 {
1432 pkgCache::VerIterator V;
1433 for (V = Pkg.VersionList(); V.end() == false; V++)
1434 {
1435 if (DisplayRecord(V) == false)
1436 return false;
1437 }
1438 }
1439 else
1440 {
1441 pkgCache::VerIterator V = Plcy.GetCandidateVer(Pkg);
1442 if (V.end() == true || V.FileList().end() == true)
1443 continue;
1444 if (DisplayRecord(V) == false)
1445 return false;
1446 }
1447 }
1448
1449 if (found > 0)
1450 return true;
1451 return _error->Error(_("No packages found"));
1452 }
1453 /*}}}*/
1454 // ShowPkgNames - Show package names /*{{{*/
1455 // ---------------------------------------------------------------------
1456 /* This does a prefix match on the first argument */
1457 bool ShowPkgNames(CommandLine &CmdL)
1458 {
1459 pkgCache &Cache = *GCache;
1460 pkgCache::PkgIterator I = Cache.PkgBegin();
1461 bool All = _config->FindB("APT::Cache::AllNames","false");
1462
1463 if (CmdL.FileList[1] != 0)
1464 {
1465 for (;I.end() != true; I++)
1466 {
1467 if (All == false && I->VersionList == 0)
1468 continue;
1469
1470 if (strncmp(I.Name(),CmdL.FileList[1],strlen(CmdL.FileList[1])) == 0)
1471 cout << I.Name() << endl;
1472 }
1473
1474 return true;
1475 }
1476
1477 // Show all pkgs
1478 for (;I.end() != true; I++)
1479 {
1480 if (All == false && I->VersionList == 0)
1481 continue;
1482 cout << I.Name() << endl;
1483 }
1484
1485 return true;
1486 }
1487 /*}}}*/
1488 // ShowSrcPackage - Show source package records /*{{{*/
1489 // ---------------------------------------------------------------------
1490 /* */
1491 bool ShowSrcPackage(CommandLine &CmdL)
1492 {
1493 pkgSourceList List;
1494 List.ReadMainList();
1495
1496 // Create the text record parsers
1497 pkgSrcRecords SrcRecs(List);
1498 if (_error->PendingError() == true)
1499 return false;
1500
1501 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1502 {
1503 SrcRecs.Restart();
1504
1505 pkgSrcRecords::Parser *Parse;
1506 while ((Parse = SrcRecs.Find(*I,false)) != 0)
1507 cout << Parse->AsStr() << endl;;
1508 }
1509 return true;
1510 }
1511 /*}}}*/
1512 // Policy - Show the results of the preferences file /*{{{*/
1513 // ---------------------------------------------------------------------
1514 /* */
1515 bool Policy(CommandLine &CmdL)
1516 {
1517 if (SrcList == 0)
1518 return _error->Error("Generate must be enabled for this function");
1519
1520 pkgCache &Cache = *GCache;
1521 pkgPolicy Plcy(&Cache);
1522 if (ReadPinFile(Plcy) == false)
1523 return false;
1524
1525 // Print out all of the package files
1526 if (CmdL.FileList[1] == 0)
1527 {
1528 cout << _("Package files:") << endl;
1529 for (pkgCache::PkgFileIterator F = Cache.FileBegin(); F.end() == false; F++)
1530 {
1531 // Locate the associated index files so we can derive a description
1532 pkgIndexFile *Indx;
1533 if (SrcList->FindIndex(F,Indx) == false &&
1534 _system->FindIndex(F,Indx) == false)
1535 return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
1536 printf(_("%4i %s\n"),
1537 Plcy.GetPriority(F),Indx->Describe(true).c_str());
1538
1539 // Print the reference information for the package
1540 string Str = F.RelStr();
1541 if (Str.empty() == false)
1542 printf(" release %s\n",F.RelStr().c_str());
1543 if (F.Site() != 0 && F.Site()[0] != 0)
1544 printf(" origin %s\n",F.Site());
1545 }
1546
1547 // Show any packages have explicit pins
1548 cout << _("Pinned packages:") << endl;
1549 pkgCache::PkgIterator I = Cache.PkgBegin();
1550 for (;I.end() != true; I++)
1551 {
1552 if (Plcy.GetPriority(I) == 0)
1553 continue;
1554
1555 // Print the package name and the version we are forcing to
1556 cout << " " << I.Name() << " -> ";
1557
1558 pkgCache::VerIterator V = Plcy.GetMatch(I);
1559 if (V.end() == true)
1560 cout << _("(not found)") << endl;
1561 else
1562 cout << V.VerStr() << endl;
1563 }
1564
1565 return true;
1566 }
1567
1568 // Print out detailed information for each package
1569 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1570 {
1571 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
1572 if (Pkg.end() == true)
1573 {
1574 _error->Warning(_("Unable to locate package %s"),*I);
1575 continue;
1576 }
1577
1578 cout << Pkg.Name() << ":" << endl;
1579
1580 // Installed version
1581 cout << _(" Installed: ");
1582 if (Pkg->CurrentVer == 0)
1583 cout << _("(none)") << endl;
1584 else
1585 cout << Pkg.CurrentVer().VerStr() << endl;
1586
1587 // Candidate Version
1588 cout << _(" Candidate: ");
1589 pkgCache::VerIterator V = Plcy.GetCandidateVer(Pkg);
1590 if (V.end() == true)
1591 cout << _("(none)") << endl;
1592 else
1593 cout << V.VerStr() << endl;
1594
1595 // Pinned version
1596 if (Plcy.GetPriority(Pkg) != 0)
1597 {
1598 cout << _(" Package pin: ");
1599 V = Plcy.GetMatch(Pkg);
1600 if (V.end() == true)
1601 cout << _("(not found)") << endl;
1602 else
1603 cout << V.VerStr() << endl;
1604 }
1605
1606 // Show the priority tables
1607 cout << _(" Version table:") << endl;
1608 for (V = Pkg.VersionList(); V.end() == false; V++)
1609 {
1610 if (Pkg.CurrentVer() == V)
1611 cout << " *** " << V.VerStr();
1612 else
1613 cout << " " << V.VerStr();
1614 cout << " " << Plcy.GetPriority(Pkg) << endl;
1615 for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; VF++)
1616 {
1617 // Locate the associated index files so we can derive a description
1618 pkgIndexFile *Indx;
1619 if (SrcList->FindIndex(VF.File(),Indx) == false &&
1620 _system->FindIndex(VF.File(),Indx) == false)
1621 return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
1622 printf(_(" %4i %s\n"),Plcy.GetPriority(VF.File()),
1623 Indx->Describe(true).c_str());
1624 }
1625 }
1626 }
1627
1628 return true;
1629 }
1630 /*}}}*/
1631 // Madison - Look a bit like katie's madison /*{{{*/
1632 // ---------------------------------------------------------------------
1633 /* */
1634 bool Madison(CommandLine &CmdL)
1635 {
1636 if (SrcList == 0)
1637 return _error->Error("Generate must be enabled for this function");
1638
1639 SrcList->ReadMainList();
1640
1641 pkgCache &Cache = *GCache;
1642
1643 // Create the src text record parsers and ignore errors about missing
1644 // deb-src lines that are generated from pkgSrcRecords::pkgSrcRecords
1645 pkgSrcRecords SrcRecs(*SrcList);
1646 if (_error->PendingError() == true)
1647 _error->Discard();
1648
1649 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1650 {
1651 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
1652
1653 if (Pkg.end() == false)
1654 {
1655 for (pkgCache::VerIterator V = Pkg.VersionList(); V.end() == false; V++)
1656 {
1657 for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; VF++)
1658 {
1659 // This might be nice, but wouldn't uniquely identify the source -mdz
1660 // if (VF.File().Archive() != 0)
1661 // {
1662 // cout << setw(10) << Pkg.Name() << " | " << setw(10) << V.VerStr() << " | "
1663 // << VF.File().Archive() << endl;
1664 // }
1665
1666 // Locate the associated index files so we can derive a description
1667 for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); S++)
1668 {
1669 vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
1670 for (vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
1671 IF != Indexes->end(); IF++)
1672 {
1673 if ((*IF)->FindInCache(*(VF.File().Cache())) == VF.File())
1674 {
1675 cout << setw(10) << Pkg.Name() << " | " << setw(10) << V.VerStr() << " | "
1676 << (*IF)->Describe(true) << endl;
1677 }
1678 }
1679 }
1680 }
1681 }
1682 }
1683
1684
1685 SrcRecs.Restart();
1686 pkgSrcRecords::Parser *SrcParser;
1687 while ((SrcParser = SrcRecs.Find(*I,false)) != 0)
1688 {
1689 // Maybe support Release info here too eventually
1690 cout << setw(10) << SrcParser->Package() << " | "
1691 << setw(10) << SrcParser->Version() << " | "
1692 << SrcParser->Index().Describe(true) << endl;
1693 }
1694 }
1695
1696 return true;
1697 }
1698
1699 /*}}}*/
1700 // GenCaches - Call the main cache generator /*{{{*/
1701 // ---------------------------------------------------------------------
1702 /* */
1703 bool GenCaches(CommandLine &Cmd)
1704 {
1705 OpTextProgress Progress(*_config);
1706
1707 pkgSourceList List;
1708 if (List.ReadMainList() == false)
1709 return false;
1710 return pkgMakeStatusCache(List,Progress);
1711 }
1712 /*}}}*/
1713 // ShowHelp - Show a help screen /*{{{*/
1714 // ---------------------------------------------------------------------
1715 /* */
1716 bool ShowHelp(CommandLine &Cmd)
1717 {
1718 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
1719 COMMON_ARCH,__DATE__,__TIME__);
1720
1721 if (_config->FindB("version") == true)
1722 return true;
1723
1724 cout <<
1725 _("Usage: apt-cache [options] command\n"
1726 " apt-cache [options] add file1 [file2 ...]\n"
1727 " apt-cache [options] showpkg pkg1 [pkg2 ...]\n"
1728 " apt-cache [options] showsrc pkg1 [pkg2 ...]\n"
1729 "\n"
1730 "apt-cache is a low-level tool used to manipulate APT's binary\n"
1731 "cache files, and query information from them\n"
1732 "\n"
1733 "Commands:\n"
1734 " add - Add a package file to the source cache\n"
1735 " gencaches - Build both the package and source cache\n"
1736 " showpkg - Show some general information for a single package\n"
1737 " showsrc - Show source records\n"
1738 " stats - Show some basic statistics\n"
1739 " dump - Show the entire file in a terse form\n"
1740 " dumpavail - Print an available file to stdout\n"
1741 " unmet - Show unmet dependencies\n"
1742 " search - Search the package list for a regex pattern\n"
1743 " show - Show a readable record for the package\n"
1744 " depends - Show raw dependency information for a package\n"
1745 " rdepends - Show reverse dependency information for a package\n"
1746 " pkgnames - List the names of all packages\n"
1747 " dotty - Generate package graphs for GraphVis\n"
1748 " xvcg - Generate package graphs for xvcg\n"
1749 " policy - Show policy settings\n"
1750 "\n"
1751 "Options:\n"
1752 " -h This help text.\n"
1753 " -p=? The package cache.\n"
1754 " -s=? The source cache.\n"
1755 " -q Disable progress indicator.\n"
1756 " -i Show only important deps for the unmet command.\n"
1757 " -c=? Read this configuration file\n"
1758 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
1759 "See the apt-cache(8) and apt.conf(5) manual pages for more information.\n");
1760 return true;
1761 }
1762 /*}}}*/
1763 // CacheInitialize - Initialize things for apt-cache /*{{{*/
1764 // ---------------------------------------------------------------------
1765 /* */
1766 void CacheInitialize()
1767 {
1768 _config->Set("quiet",0);
1769 _config->Set("help",false);
1770 }
1771 /*}}}*/
1772
1773 int main(int argc,const char *argv[])
1774 {
1775 CommandLine::Args Args[] = {
1776 {'h',"help","help",0},
1777 {'v',"version","version",0},
1778 {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg},
1779 {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg},
1780 {'q',"quiet","quiet",CommandLine::IntLevel},
1781 {'i',"important","APT::Cache::Important",0},
1782 {'f',"full","APT::Cache::ShowFull",0},
1783 {'g',"generate","APT::Cache::Generate",0},
1784 {'a',"all-versions","APT::Cache::AllVersions",0},
1785 {'n',"names-only","APT::Cache::NamesOnly",0},
1786 {0,"all-names","APT::Cache::AllNames",0},
1787 {0,"recurse","APT::Cache::RecurseDepends",0},
1788 {'c',"config-file",0,CommandLine::ConfigFile},
1789 {'o',"option",0,CommandLine::ArbItem},
1790 {0,"installed","APT::Cache::Installed",0},
1791 {0,0,0,0}};
1792 CommandLine::Dispatch CmdsA[] = {{"help",&ShowHelp},
1793 {"add",&DoAdd},
1794 {"gencaches",&GenCaches},
1795 {"showsrc",&ShowSrcPackage},
1796 {0,0}};
1797 CommandLine::Dispatch CmdsB[] = {{"showpkg",&DumpPackage},
1798 {"stats",&Stats},
1799 {"dump",&Dump},
1800 {"dumpavail",&DumpAvail},
1801 {"unmet",&UnMet},
1802 {"search",&Search},
1803 {"depends",&Depends},
1804 {"rdepends",&RDepends},
1805 {"dotty",&Dotty},
1806 {"xvcg",&XVcg},
1807 {"show",&ShowPackage},
1808 {"pkgnames",&ShowPkgNames},
1809 {"policy",&Policy},
1810 {"madison",&Madison},
1811 {0,0}};
1812
1813 CacheInitialize();
1814
1815 // Set up gettext support
1816 setlocale(LC_ALL,"");
1817 textdomain(PACKAGE);
1818
1819 // Parse the command line and initialize the package library
1820 CommandLine CmdL(Args,_config);
1821 if (pkgInitConfig(*_config) == false ||
1822 CmdL.Parse(argc,argv) == false ||
1823 pkgInitSystem(*_config,_system) == false)
1824 {
1825 _error->DumpErrors();
1826 return 100;
1827 }
1828
1829 // See if the help should be shown
1830 if (_config->FindB("help") == true ||
1831 CmdL.FileSize() == 0)
1832 {
1833 ShowHelp(CmdL);
1834 return 0;
1835 }
1836
1837 // Deal with stdout not being a tty
1838 if (isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
1839 _config->Set("quiet","1");
1840
1841 if (CmdL.DispatchArg(CmdsA,false) == false && _error->PendingError() == false)
1842 {
1843 MMap *Map = 0;
1844 if (_config->FindB("APT::Cache::Generate",true) == false)
1845 {
1846 Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"),
1847 FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
1848 }
1849 else
1850 {
1851 // Open the cache file
1852 SrcList = new pkgSourceList;
1853 SrcList->ReadMainList();
1854
1855 // Generate it and map it
1856 OpProgress Prog;
1857 pkgMakeStatusCache(*SrcList,Prog,&Map,true);
1858 }
1859
1860 if (_error->PendingError() == false)
1861 {
1862 pkgCache Cache(Map);
1863 GCache = &Cache;
1864 if (_error->PendingError() == false)
1865 CmdL.DispatchArg(CmdsB);
1866 }
1867 delete Map;
1868 }
1869
1870 // Print any errors or warnings found during parsing
1871 if (_error->empty() == false)
1872 {
1873 bool Errors = _error->PendingError();
1874 _error->DumpErrors();
1875 return Errors == true?100:0;
1876 }
1877
1878 return 0;
1879 }