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