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