apt-cache.cc 55 KB

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