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