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