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