apt-get.cc 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-get.cc,v 1.115 2002/03/20 05:51:52 jgg Exp $
  4. /* ######################################################################
  5. apt-get - Cover for dpkg
  6. This is an allout cover for dpkg implementing a safer front end. It is
  7. based largely on libapt-pkg.
  8. The syntax is different,
  9. apt-get [opt] command [things]
  10. Where command is:
  11. update - Resyncronize the package files from their sources
  12. upgrade - Smart-Download the newest versions of all packages
  13. dselect-upgrade - Follows dselect's changes to the Status: field
  14. and installes new and removes old packages
  15. dist-upgrade - Powerfull upgrader designed to handle the issues with
  16. a new distribution.
  17. install - Download and install a given package (by name, not by .deb)
  18. check - Update the package cache and check for broken packages
  19. clean - Erase the .debs downloaded to /var/cache/apt/archives and
  20. the partial dir too
  21. ##################################################################### */
  22. /*}}}*/
  23. // Include Files /*{{{*/
  24. #include <apt-pkg/error.h>
  25. #include <apt-pkg/cmndline.h>
  26. #include <apt-pkg/init.h>
  27. #include <apt-pkg/depcache.h>
  28. #include <apt-pkg/sourcelist.h>
  29. #include <apt-pkg/algorithms.h>
  30. #include <apt-pkg/acquire-item.h>
  31. #include <apt-pkg/strutl.h>
  32. #include <apt-pkg/clean.h>
  33. #include <apt-pkg/srcrecords.h>
  34. #include <apt-pkg/version.h>
  35. #include <apt-pkg/cachefile.h>
  36. #include <apt-pkg/sptr.h>
  37. #include <apt-pkg/versionmatch.h>
  38. #include <config.h>
  39. #include <apti18n.h>
  40. #include "acqprogress.h"
  41. #include <locale.h>
  42. #include <fstream.h>
  43. #include <termios.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/stat.h>
  46. #include <sys/statvfs.h>
  47. #include <signal.h>
  48. #include <unistd.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <regex.h>
  52. #include <sys/wait.h>
  53. /*}}}*/
  54. using namespace std;
  55. ostream c0out(0);
  56. ostream c1out(0);
  57. ostream c2out(0);
  58. ofstream devnull("/dev/null");
  59. unsigned int ScreenWidth = 80;
  60. // class CacheFile - Cover class for some dependency cache functions /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* */
  63. class CacheFile : public pkgCacheFile
  64. {
  65. static pkgCache *SortCache;
  66. static int NameComp(const void *a,const void *b);
  67. public:
  68. pkgCache::Package **List;
  69. void Sort();
  70. bool CheckDeps(bool AllowBroken = false);
  71. bool Open(bool WithLock = true)
  72. {
  73. OpTextProgress Prog(*_config);
  74. if (pkgCacheFile::Open(Prog,WithLock) == false)
  75. return false;
  76. Sort();
  77. return true;
  78. };
  79. bool OpenForInstall()
  80. {
  81. if (_config->FindB("APT::Get::Print-URIs") == true)
  82. return Open(false);
  83. else
  84. return Open(true);
  85. }
  86. CacheFile() : List(0) {};
  87. };
  88. /*}}}*/
  89. // YnPrompt - Yes No Prompt. /*{{{*/
  90. // ---------------------------------------------------------------------
  91. /* Returns true on a Yes.*/
  92. bool YnPrompt()
  93. {
  94. // This needs to be a capital
  95. const char *Yes = _("Y");
  96. if (_config->FindB("APT::Get::Assume-Yes",false) == true)
  97. {
  98. c1out << Yes << endl;
  99. return true;
  100. }
  101. char C = 0;
  102. char Jnk = 0;
  103. if (read(STDIN_FILENO,&C,1) != 1)
  104. return false;
  105. while (C != '\n' && Jnk != '\n')
  106. if (read(STDIN_FILENO,&Jnk,1) != 1)
  107. return false;
  108. if (!(toupper(C) == *Yes || C == '\n' || C == '\r'))
  109. return false;
  110. return true;
  111. }
  112. /*}}}*/
  113. // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
  114. // ---------------------------------------------------------------------
  115. /* Returns true on a Yes.*/
  116. bool AnalPrompt(const char *Text)
  117. {
  118. char Buf[1024];
  119. cin.getline(Buf,sizeof(Buf));
  120. if (strcmp(Buf,Text) == 0)
  121. return true;
  122. return false;
  123. }
  124. /*}}}*/
  125. // ShowList - Show a list /*{{{*/
  126. // ---------------------------------------------------------------------
  127. /* This prints out a string of space separated words with a title and
  128. a two space indent line wraped to the current screen width. */
  129. bool ShowList(ostream &out,string Title,string List)
  130. {
  131. if (List.empty() == true)
  132. return true;
  133. // Acount for the leading space
  134. int ScreenWidth = ::ScreenWidth - 3;
  135. out << Title << endl;
  136. string::size_type Start = 0;
  137. while (Start < List.size())
  138. {
  139. string::size_type End;
  140. if (Start + ScreenWidth >= List.size())
  141. End = List.size();
  142. else
  143. End = List.rfind(' ',Start+ScreenWidth);
  144. if (End == string::npos || End < Start)
  145. End = Start + ScreenWidth;
  146. out << " " << string(List,Start,End - Start) << endl;
  147. Start = End + 1;
  148. }
  149. return false;
  150. }
  151. /*}}}*/
  152. // ShowBroken - Debugging aide /*{{{*/
  153. // ---------------------------------------------------------------------
  154. /* This prints out the names of all the packages that are broken along
  155. with the name of each each broken dependency and a quite version
  156. description.
  157. The output looks like:
  158. Sorry, but the following packages have unmet dependencies:
  159. exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
  160. Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
  161. Depends: libsasl7 but it is not going to be installed
  162. */
  163. void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
  164. {
  165. out << _("Sorry, but the following packages have unmet dependencies:") << endl;
  166. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  167. {
  168. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  169. if (Now == true)
  170. {
  171. if (Cache[I].NowBroken() == false)
  172. continue;
  173. }
  174. else
  175. {
  176. if (Cache[I].InstBroken() == false)
  177. continue;
  178. }
  179. // Print out each package and the failed dependencies
  180. out <<" " << I.Name() << ":";
  181. unsigned Indent = strlen(I.Name()) + 3;
  182. bool First = true;
  183. pkgCache::VerIterator Ver;
  184. if (Now == true)
  185. Ver = I.CurrentVer();
  186. else
  187. Ver = Cache[I].InstVerIter(Cache);
  188. if (Ver.end() == true)
  189. {
  190. out << endl;
  191. continue;
  192. }
  193. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false;)
  194. {
  195. // Compute a single dependency element (glob or)
  196. pkgCache::DepIterator Start;
  197. pkgCache::DepIterator End;
  198. D.GlobOr(Start,End);
  199. if (Cache->IsImportantDep(End) == false)
  200. continue;
  201. if (Now == true)
  202. {
  203. if ((Cache[End] & pkgDepCache::DepGNow) == pkgDepCache::DepGNow)
  204. continue;
  205. }
  206. else
  207. {
  208. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  209. continue;
  210. }
  211. bool FirstOr = true;
  212. while (1)
  213. {
  214. if (First == false)
  215. for (unsigned J = 0; J != Indent; J++)
  216. out << ' ';
  217. First = false;
  218. if (FirstOr == false)
  219. {
  220. for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
  221. out << ' ';
  222. }
  223. else
  224. out << ' ' << End.DepType() << ": ";
  225. FirstOr = false;
  226. out << Start.TargetPkg().Name();
  227. // Show a quick summary of the version requirements
  228. if (Start.TargetVer() != 0)
  229. out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
  230. /* Show a summary of the target package if possible. In the case
  231. of virtual packages we show nothing */
  232. pkgCache::PkgIterator Targ = Start.TargetPkg();
  233. if (Targ->ProvidesList == 0)
  234. {
  235. out << ' ';
  236. pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
  237. if (Now == true)
  238. Ver = Targ.CurrentVer();
  239. if (Ver.end() == false)
  240. {
  241. if (Now == true)
  242. ioprintf(out,_("but %s is installed"),Ver.VerStr());
  243. else
  244. ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
  245. }
  246. else
  247. {
  248. if (Cache[Targ].CandidateVerIter(Cache).end() == true)
  249. {
  250. if (Targ->ProvidesList == 0)
  251. out << _("but it is not installable");
  252. else
  253. out << _("but it is a virtual package");
  254. }
  255. else
  256. out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
  257. }
  258. }
  259. if (Start != End)
  260. out << _(" or");
  261. out << endl;
  262. if (Start == End)
  263. break;
  264. Start++;
  265. }
  266. }
  267. }
  268. }
  269. /*}}}*/
  270. // ShowNew - Show packages to newly install /*{{{*/
  271. // ---------------------------------------------------------------------
  272. /* */
  273. void ShowNew(ostream &out,CacheFile &Cache)
  274. {
  275. /* Print out a list of packages that are going to be removed extra
  276. to what the user asked */
  277. string List;
  278. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  279. {
  280. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  281. if (Cache[I].NewInstall() == true)
  282. List += string(I.Name()) + " ";
  283. }
  284. ShowList(out,_("The following NEW packages will be installed:"),List);
  285. }
  286. /*}}}*/
  287. // ShowDel - Show packages to delete /*{{{*/
  288. // ---------------------------------------------------------------------
  289. /* */
  290. void ShowDel(ostream &out,CacheFile &Cache)
  291. {
  292. /* Print out a list of packages that are going to be removed extra
  293. to what the user asked */
  294. string List;
  295. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  296. {
  297. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  298. if (Cache[I].Delete() == true)
  299. {
  300. if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
  301. List += string(I.Name()) + "* ";
  302. else
  303. List += string(I.Name()) + " ";
  304. }
  305. }
  306. ShowList(out,_("The following packages will be REMOVED:"),List);
  307. }
  308. /*}}}*/
  309. // ShowKept - Show kept packages /*{{{*/
  310. // ---------------------------------------------------------------------
  311. /* */
  312. void ShowKept(ostream &out,CacheFile &Cache)
  313. {
  314. string List;
  315. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  316. {
  317. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  318. // Not interesting
  319. if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
  320. I->CurrentVer == 0 || Cache[I].Delete() == true)
  321. continue;
  322. List += string(I.Name()) + " ";
  323. }
  324. ShowList(out,_("The following packages have been kept back"),List);
  325. }
  326. /*}}}*/
  327. // ShowUpgraded - Show upgraded packages /*{{{*/
  328. // ---------------------------------------------------------------------
  329. /* */
  330. void ShowUpgraded(ostream &out,CacheFile &Cache)
  331. {
  332. string List;
  333. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  334. {
  335. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  336. // Not interesting
  337. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  338. continue;
  339. List += string(I.Name()) + " ";
  340. }
  341. ShowList(out,_("The following packages will be upgraded"),List);
  342. }
  343. /*}}}*/
  344. // ShowDowngraded - Show downgraded packages /*{{{*/
  345. // ---------------------------------------------------------------------
  346. /* */
  347. bool ShowDowngraded(ostream &out,CacheFile &Cache)
  348. {
  349. string List;
  350. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  351. {
  352. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  353. // Not interesting
  354. if (Cache[I].Downgrade() == false || Cache[I].NewInstall() == true)
  355. continue;
  356. List += string(I.Name()) + " ";
  357. }
  358. return ShowList(out,_("The following packages will be DOWNGRADED"),List);
  359. }
  360. /*}}}*/
  361. // ShowHold - Show held but changed packages /*{{{*/
  362. // ---------------------------------------------------------------------
  363. /* */
  364. bool ShowHold(ostream &out,CacheFile &Cache)
  365. {
  366. string List;
  367. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  368. {
  369. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  370. if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
  371. I->SelectedState == pkgCache::State::Hold)
  372. List += string(I.Name()) + " ";
  373. }
  374. return ShowList(out,_("The following held packages will be changed:"),List);
  375. }
  376. /*}}}*/
  377. // ShowEssential - Show an essential package warning /*{{{*/
  378. // ---------------------------------------------------------------------
  379. /* This prints out a warning message that is not to be ignored. It shows
  380. all essential packages and their dependents that are to be removed.
  381. It is insanely risky to remove the dependents of an essential package! */
  382. bool ShowEssential(ostream &out,CacheFile &Cache)
  383. {
  384. string List;
  385. bool *Added = new bool[Cache->Head().PackageCount];
  386. for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
  387. Added[I] = false;
  388. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  389. {
  390. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  391. if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
  392. (I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
  393. continue;
  394. // The essential package is being removed
  395. if (Cache[I].Delete() == true)
  396. {
  397. if (Added[I->ID] == false)
  398. {
  399. Added[I->ID] = true;
  400. List += string(I.Name()) + " ";
  401. }
  402. }
  403. if (I->CurrentVer == 0)
  404. continue;
  405. // Print out any essential package depenendents that are to be removed
  406. for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
  407. {
  408. // Skip everything but depends
  409. if (D->Type != pkgCache::Dep::PreDepends &&
  410. D->Type != pkgCache::Dep::Depends)
  411. continue;
  412. pkgCache::PkgIterator P = D.SmartTargetPkg();
  413. if (Cache[P].Delete() == true)
  414. {
  415. if (Added[P->ID] == true)
  416. continue;
  417. Added[P->ID] = true;
  418. char S[300];
  419. snprintf(S,sizeof(S),_("%s (due to %s) "),P.Name(),I.Name());
  420. List += S;
  421. }
  422. }
  423. }
  424. delete [] Added;
  425. return ShowList(out,_("WARNING: The following essential packages will be removed\n"
  426. "This should NOT be done unless you know exactly what you are doing!"),List);
  427. }
  428. /*}}}*/
  429. // Stats - Show some statistics /*{{{*/
  430. // ---------------------------------------------------------------------
  431. /* */
  432. void Stats(ostream &out,pkgDepCache &Dep)
  433. {
  434. unsigned long Upgrade = 0;
  435. unsigned long Downgrade = 0;
  436. unsigned long Install = 0;
  437. unsigned long ReInstall = 0;
  438. for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
  439. {
  440. if (Dep[I].NewInstall() == true)
  441. Install++;
  442. else
  443. {
  444. if (Dep[I].Upgrade() == true)
  445. Upgrade++;
  446. else
  447. if (Dep[I].Downgrade() == true)
  448. Downgrade++;
  449. }
  450. if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
  451. ReInstall++;
  452. }
  453. ioprintf(out,_("%lu packages upgraded, %lu newly installed, "),
  454. Upgrade,Install);
  455. if (ReInstall != 0)
  456. ioprintf(out,_("%lu reinstalled, "),ReInstall);
  457. if (Downgrade != 0)
  458. ioprintf(out,_("%lu downgraded, "),Downgrade);
  459. ioprintf(out,_("%lu to remove and %lu not upgraded.\n"),
  460. Dep.DelCount(),Dep.KeepCount());
  461. if (Dep.BadCount() != 0)
  462. ioprintf(out,_("%lu packages not fully installed or removed.\n"),
  463. Dep.BadCount());
  464. }
  465. /*}}}*/
  466. // CacheFile::NameComp - QSort compare by name /*{{{*/
  467. // ---------------------------------------------------------------------
  468. /* */
  469. pkgCache *CacheFile::SortCache = 0;
  470. int CacheFile::NameComp(const void *a,const void *b)
  471. {
  472. if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
  473. return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
  474. const pkgCache::Package &A = **(pkgCache::Package **)a;
  475. const pkgCache::Package &B = **(pkgCache::Package **)b;
  476. return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
  477. }
  478. /*}}}*/
  479. // CacheFile::Sort - Sort by name /*{{{*/
  480. // ---------------------------------------------------------------------
  481. /* */
  482. void CacheFile::Sort()
  483. {
  484. delete [] List;
  485. List = new pkgCache::Package *[Cache->Head().PackageCount];
  486. memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
  487. pkgCache::PkgIterator I = Cache->PkgBegin();
  488. for (;I.end() != true; I++)
  489. List[I->ID] = I;
  490. SortCache = *this;
  491. qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
  492. }
  493. /*}}}*/
  494. // CacheFile::CheckDeps - Open the cache file /*{{{*/
  495. // ---------------------------------------------------------------------
  496. /* This routine generates the caches and then opens the dependency cache
  497. and verifies that the system is OK. */
  498. bool CacheFile::CheckDeps(bool AllowBroken)
  499. {
  500. if (_error->PendingError() == true)
  501. return false;
  502. // Check that the system is OK
  503. if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
  504. return _error->Error("Internal Error, non-zero counts");
  505. // Apply corrections for half-installed packages
  506. if (pkgApplyStatus(*DCache) == false)
  507. return false;
  508. // Nothing is broken
  509. if (DCache->BrokenCount() == 0 || AllowBroken == true)
  510. return true;
  511. // Attempt to fix broken things
  512. if (_config->FindB("APT::Get::Fix-Broken",false) == true)
  513. {
  514. c1out << _("Correcting dependencies...") << flush;
  515. if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
  516. {
  517. c1out << _(" failed.") << endl;
  518. ShowBroken(c1out,*this,true);
  519. return _error->Error(_("Unable to correct dependencies"));
  520. }
  521. if (pkgMinimizeUpgrade(*DCache) == false)
  522. return _error->Error(_("Unable to minimize the upgrade set"));
  523. c1out << _(" Done") << endl;
  524. }
  525. else
  526. {
  527. c1out << _("You might want to run `apt-get -f install' to correct these.") << endl;
  528. ShowBroken(c1out,*this,true);
  529. return _error->Error(_("Unmet dependencies. Try using -f."));
  530. }
  531. return true;
  532. }
  533. /*}}}*/
  534. // InstallPackages - Actually download and install the packages /*{{{*/
  535. // ---------------------------------------------------------------------
  536. /* This displays the informative messages describing what is going to
  537. happen and then calls the download routines */
  538. bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
  539. bool Saftey = true)
  540. {
  541. if (_config->FindB("APT::Get::Purge",false) == true)
  542. {
  543. pkgCache::PkgIterator I = Cache->PkgBegin();
  544. for (; I.end() == false; I++)
  545. {
  546. if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
  547. Cache->MarkDelete(I,true);
  548. }
  549. }
  550. bool Fail = false;
  551. bool Essential = false;
  552. // Show all the various warning indicators
  553. ShowDel(c1out,Cache);
  554. ShowNew(c1out,Cache);
  555. if (ShwKept == true)
  556. ShowKept(c1out,Cache);
  557. Fail |= !ShowHold(c1out,Cache);
  558. if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
  559. ShowUpgraded(c1out,Cache);
  560. Fail |= !ShowDowngraded(c1out,Cache);
  561. Essential = !ShowEssential(c1out,Cache);
  562. Fail |= Essential;
  563. Stats(c1out,Cache);
  564. // Sanity check
  565. if (Cache->BrokenCount() != 0)
  566. {
  567. ShowBroken(c1out,Cache,false);
  568. return _error->Error("Internal Error, InstallPackages was called with broken packages!");
  569. }
  570. if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
  571. Cache->BadCount() == 0)
  572. return true;
  573. // No remove flag
  574. if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
  575. return _error->Error(_("Packages need to be removed but Remove is disabled."));
  576. // Run the simulator ..
  577. if (_config->FindB("APT::Get::Simulate") == true)
  578. {
  579. pkgSimulate PM(Cache);
  580. pkgPackageManager::OrderResult Res = PM.DoInstall();
  581. if (Res == pkgPackageManager::Failed)
  582. return false;
  583. if (Res != pkgPackageManager::Completed)
  584. return _error->Error("Internal Error, Ordering didn't finish");
  585. return true;
  586. }
  587. // Create the text record parser
  588. pkgRecords Recs(Cache);
  589. if (_error->PendingError() == true)
  590. return false;
  591. // Lock the archive directory
  592. FileFd Lock;
  593. if (_config->FindB("Debug::NoLocking",false) == false &&
  594. _config->FindB("APT::Get::Print-URIs") == false)
  595. {
  596. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  597. if (_error->PendingError() == true)
  598. return _error->Error(_("Unable to lock the download directory"));
  599. }
  600. // Create the download object
  601. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  602. pkgAcquire Fetcher(&Stat);
  603. // Read the source list
  604. pkgSourceList List;
  605. if (List.ReadMainList() == false)
  606. return _error->Error(_("The list of sources could not be read."));
  607. // Create the package manager and prepare to download
  608. SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
  609. if (PM->GetArchives(&Fetcher,&List,&Recs) == false ||
  610. _error->PendingError() == true)
  611. return false;
  612. // Display statistics
  613. double FetchBytes = Fetcher.FetchNeeded();
  614. double FetchPBytes = Fetcher.PartialPresent();
  615. double DebBytes = Fetcher.TotalNeeded();
  616. if (DebBytes != Cache->DebSize())
  617. {
  618. c0out << DebBytes << ',' << Cache->DebSize() << endl;
  619. c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl;
  620. }
  621. // Number of bytes
  622. if (DebBytes != FetchBytes)
  623. ioprintf(c1out,_("Need to get %sB/%sB of archives. "),
  624. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  625. else
  626. ioprintf(c1out,_("Need to get %sB of archives. "),
  627. SizeToStr(DebBytes).c_str());
  628. // Size delta
  629. if (Cache->UsrSize() >= 0)
  630. ioprintf(c1out,_("After unpacking %sB will be used.\n"),
  631. SizeToStr(Cache->UsrSize()).c_str());
  632. else
  633. ioprintf(c1out,_("After unpacking %sB will be freed.\n"),
  634. SizeToStr(-1*Cache->UsrSize()).c_str());
  635. if (_error->PendingError() == true)
  636. return false;
  637. /* Check for enough free space, but only if we are actually going to
  638. download */
  639. if (_config->FindB("APT::Get::Print-URIs") == false)
  640. {
  641. struct statvfs Buf;
  642. string OutputDir = _config->FindDir("Dir::Cache::Archives");
  643. if (statvfs(OutputDir.c_str(),&Buf) != 0)
  644. return _error->Errno("statvfs","Couldn't determine free space in %s",
  645. OutputDir.c_str());
  646. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  647. return _error->Error(_("Sorry, you don't have enough free space in %s to hold all the .debs."),
  648. OutputDir.c_str());
  649. }
  650. // Fail safe check
  651. if (_config->FindI("quiet",0) >= 2 ||
  652. _config->FindB("APT::Get::Assume-Yes",false) == true)
  653. {
  654. if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
  655. return _error->Error(_("There are problems and -y was used without --force-yes"));
  656. }
  657. if (Essential == true && Saftey == true)
  658. {
  659. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  660. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  661. const char *Prompt = _("Yes, do as I say!");
  662. ioprintf(c2out,
  663. _("You are about to do something potentially harmful\n"
  664. "To continue type in the phrase '%s'\n"
  665. " ?] "),Prompt);
  666. c2out << flush;
  667. if (AnalPrompt(Prompt) == false)
  668. {
  669. c2out << _("Abort.") << endl;
  670. exit(1);
  671. }
  672. }
  673. else
  674. {
  675. // Prompt to continue
  676. if (Ask == true || Fail == true)
  677. {
  678. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  679. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  680. if (_config->FindI("quiet",0) < 2 &&
  681. _config->FindB("APT::Get::Assume-Yes",false) == false)
  682. {
  683. c2out << _("Do you want to continue? [Y/n] ") << flush;
  684. if (YnPrompt() == false)
  685. {
  686. c2out << _("Abort.") << endl;
  687. exit(1);
  688. }
  689. }
  690. }
  691. }
  692. // Just print out the uris an exit if the --print-uris flag was used
  693. if (_config->FindB("APT::Get::Print-URIs") == true)
  694. {
  695. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  696. for (; I != Fetcher.UriEnd(); I++)
  697. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  698. I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
  699. return true;
  700. }
  701. /* Unlock the dpkg lock if we are not going to be doing an install
  702. after. */
  703. if (_config->FindB("APT::Get::Download-Only",false) == true)
  704. _system->UnLock();
  705. // Run it
  706. while (1)
  707. {
  708. bool Transient = false;
  709. if (_config->FindB("APT::Get::Download",true) == false)
  710. {
  711. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
  712. {
  713. if ((*I)->Local == true)
  714. {
  715. I++;
  716. continue;
  717. }
  718. // Close the item and check if it was found in cache
  719. (*I)->Finished();
  720. if ((*I)->Complete == false)
  721. Transient = true;
  722. // Clear it out of the fetch list
  723. delete *I;
  724. I = Fetcher.ItemsBegin();
  725. }
  726. }
  727. if (Fetcher.Run() == pkgAcquire::Failed)
  728. return false;
  729. // Print out errors
  730. bool Failed = false;
  731. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  732. {
  733. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  734. (*I)->Complete == true)
  735. continue;
  736. if ((*I)->Status == pkgAcquire::Item::StatIdle)
  737. {
  738. Transient = true;
  739. // Failed = true;
  740. continue;
  741. }
  742. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  743. (*I)->ErrorText.c_str());
  744. Failed = true;
  745. }
  746. /* If we are in no download mode and missing files and there were
  747. 'failures' then the user must specify -m. Furthermore, there
  748. is no such thing as a transient error in no-download mode! */
  749. if (Transient == true &&
  750. _config->FindB("APT::Get::Download",true) == false)
  751. {
  752. Transient = false;
  753. Failed = true;
  754. }
  755. if (_config->FindB("APT::Get::Download-Only",false) == true)
  756. {
  757. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  758. return _error->Error(_("Some files failed to download"));
  759. c1out << _("Download complete and in download only mode") << endl;
  760. return true;
  761. }
  762. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  763. {
  764. return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
  765. }
  766. if (Transient == true && Failed == true)
  767. return _error->Error(_("--fix-missing and media swapping is not currently supported"));
  768. // Try to deal with missing package files
  769. if (Failed == true && PM->FixMissing() == false)
  770. {
  771. cerr << _("Unable to correct missing packages.") << endl;
  772. return _error->Error(_("Aborting Install."));
  773. }
  774. _system->UnLock();
  775. pkgPackageManager::OrderResult Res = PM->DoInstall();
  776. if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
  777. return false;
  778. if (Res == pkgPackageManager::Completed)
  779. return true;
  780. // Reload the fetcher object and loop again for media swapping
  781. Fetcher.Shutdown();
  782. if (PM->GetArchives(&Fetcher,&List,&Recs) == false)
  783. return false;
  784. _system->Lock();
  785. }
  786. }
  787. /*}}}*/
  788. // TryToInstall - Try to install a single package /*{{{*/
  789. // ---------------------------------------------------------------------
  790. /* This used to be inlined in DoInstall, but with the advent of regex package
  791. name matching it was split out.. */
  792. bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
  793. pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
  794. unsigned int &ExpectedInst,bool AllowFail = true)
  795. {
  796. /* This is a pure virtual package and there is a single available
  797. provides */
  798. if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0 &&
  799. Pkg.ProvidesList()->NextProvides == 0)
  800. {
  801. pkgCache::PkgIterator Tmp = Pkg.ProvidesList().OwnerPkg();
  802. ioprintf(c1out,_("Note, selecting %s instead of %s\n"),
  803. Tmp.Name(),Pkg.Name());
  804. Pkg = Tmp;
  805. }
  806. // Handle the no-upgrade case
  807. if (_config->FindB("APT::Get::upgrade",true) == false &&
  808. Pkg->CurrentVer != 0)
  809. {
  810. if (AllowFail == true)
  811. ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
  812. Pkg.Name());
  813. return true;
  814. }
  815. // Check if there is something at all to install
  816. pkgDepCache::StateCache &State = Cache[Pkg];
  817. if (Remove == true && Pkg->CurrentVer == 0)
  818. {
  819. /* We want to continue searching for regex hits, so we return false here
  820. otherwise this is not really an error. */
  821. if (AllowFail == false)
  822. return false;
  823. ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.Name());
  824. return true;
  825. }
  826. if (State.CandidateVer == 0 && Remove == false)
  827. {
  828. if (AllowFail == false)
  829. return false;
  830. if (Pkg->ProvidesList != 0)
  831. {
  832. ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
  833. Pkg.Name());
  834. pkgCache::PrvIterator I = Pkg.ProvidesList();
  835. for (; I.end() == false; I++)
  836. {
  837. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  838. if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer())
  839. {
  840. if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
  841. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
  842. _(" [Installed]") << endl;
  843. else
  844. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
  845. }
  846. }
  847. c1out << _("You should explicitly select one to install.") << endl;
  848. }
  849. else
  850. {
  851. ioprintf(c1out,
  852. _("Package %s has no available version, but exists in the database.\n"
  853. "This typically means that the package was mentioned in a dependency and\n"
  854. "never uploaded, has been obsoleted or is not available with the contents\n"
  855. "of sources.list\n"),Pkg.Name());
  856. string List;
  857. SPtrArray<bool> Seen = new bool[Cache.Head().PackageCount];
  858. memset(Seen,0,Cache.Head().PackageCount*sizeof(*Seen));
  859. pkgCache::DepIterator Dep = Pkg.RevDependsList();
  860. for (; Dep.end() == false; Dep++)
  861. {
  862. if (Dep->Type != pkgCache::Dep::Replaces)
  863. continue;
  864. if (Seen[Dep.ParentPkg()->ID] == true)
  865. continue;
  866. Seen[Dep.ParentPkg()->ID] = true;
  867. List += string(Dep.ParentPkg().Name()) + " ";
  868. }
  869. ShowList(c1out,_("However the following packages replace it:"),List);
  870. }
  871. _error->Error(_("Package %s has no installation candidate"),Pkg.Name());
  872. return false;
  873. }
  874. Fix.Clear(Pkg);
  875. Fix.Protect(Pkg);
  876. if (Remove == true)
  877. {
  878. Fix.Remove(Pkg);
  879. Cache.MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
  880. return true;
  881. }
  882. // Install it
  883. Cache.MarkInstall(Pkg,false);
  884. if (State.Install() == false)
  885. {
  886. if (_config->FindB("APT::Get::ReInstall",false) == true)
  887. {
  888. if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
  889. ioprintf(c1out,_("Sorry, re-installation of %s is not possible, it cannot be downloaded.\n"),
  890. Pkg.Name());
  891. else
  892. Cache.SetReInstall(Pkg,true);
  893. }
  894. else
  895. {
  896. if (AllowFail == true)
  897. ioprintf(c1out,_("Sorry, %s is already the newest version.\n"),
  898. Pkg.Name());
  899. }
  900. }
  901. else
  902. ExpectedInst++;
  903. // Install it with autoinstalling enabled.
  904. if (State.InstBroken() == true && BrokenFix == false)
  905. Cache.MarkInstall(Pkg,true);
  906. return true;
  907. }
  908. /*}}}*/
  909. // TryToChangeVer - Try to change a candidate version /*{{{*/
  910. // ---------------------------------------------------------------------
  911. /* */
  912. bool TryToChangeVer(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
  913. const char *VerTag,bool IsRel)
  914. {
  915. pkgVersionMatch Match(VerTag,(IsRel == true?pkgVersionMatch::Release:pkgVersionMatch::Version));
  916. pkgCache::VerIterator Ver = Match.Find(Pkg);
  917. if (Ver.end() == true)
  918. {
  919. if (IsRel == true)
  920. return _error->Error(_("Release '%s' for '%s' was not found"),
  921. VerTag,Pkg.Name());
  922. return _error->Error(_("Version '%s' for '%s' was not found"),
  923. VerTag,Pkg.Name());
  924. }
  925. if (strcmp(VerTag,Ver.VerStr()) != 0)
  926. {
  927. ioprintf(c1out,_("Selected version %s (%s) for %s\n"),
  928. Ver.VerStr(),Ver.RelStr().c_str(),Pkg.Name());
  929. }
  930. Cache.SetCandidateVersion(Ver);
  931. return true;
  932. }
  933. /*}}}*/
  934. // FindSrc - Find a source record /*{{{*/
  935. // ---------------------------------------------------------------------
  936. /* */
  937. pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
  938. pkgSrcRecords &SrcRecs,string &Src,
  939. pkgDepCache &Cache)
  940. {
  941. // We want to pull the version off the package specification..
  942. string VerTag;
  943. string TmpSrc = Name;
  944. string::size_type Slash = TmpSrc.rfind('=');
  945. if (Slash != string::npos)
  946. {
  947. VerTag = string(TmpSrc.begin() + Slash + 1,TmpSrc.end());
  948. TmpSrc = string(TmpSrc.begin(),TmpSrc.begin() + Slash);
  949. }
  950. /* Lookup the version of the package we would install if we were to
  951. install a version and determine the source package name, then look
  952. in the archive for a source package of the same name. In theory
  953. we could stash the version string as well and match that too but
  954. today there aren't multi source versions in the archive. */
  955. if (_config->FindB("APT::Get::Only-Source") == false &&
  956. VerTag.empty() == true)
  957. {
  958. pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
  959. if (Pkg.end() == false)
  960. {
  961. pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
  962. if (Ver.end() == false)
  963. {
  964. pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
  965. Src = Parse.SourcePkg();
  966. }
  967. }
  968. }
  969. // No source package name..
  970. if (Src.empty() == true)
  971. Src = TmpSrc;
  972. // The best hit
  973. pkgSrcRecords::Parser *Last = 0;
  974. unsigned long Offset = 0;
  975. string Version;
  976. bool IsMatch = false;
  977. // If we are matching by version then we need exact matches to be happy
  978. if (VerTag.empty() == false)
  979. IsMatch = true;
  980. /* Iterate over all of the hits, which includes the resulting
  981. binary packages in the search */
  982. pkgSrcRecords::Parser *Parse;
  983. SrcRecs.Restart();
  984. while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
  985. {
  986. string Ver = Parse->Version();
  987. // Skip name mismatches
  988. if (IsMatch == true && Parse->Package() != Src)
  989. continue;
  990. if (VerTag.empty() == false)
  991. {
  992. /* Don't want to fall through because we are doing exact version
  993. matching. */
  994. if (Cache.VS().CmpVersion(VerTag,Ver) != 0)
  995. continue;
  996. Last = Parse;
  997. Offset = Parse->Offset();
  998. break;
  999. }
  1000. // Newer version or an exact match
  1001. if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0 ||
  1002. (Parse->Package() == Src && IsMatch == false))
  1003. {
  1004. IsMatch = Parse->Package() == Src;
  1005. Last = Parse;
  1006. Offset = Parse->Offset();
  1007. Version = Ver;
  1008. }
  1009. }
  1010. if (Last == 0)
  1011. return 0;
  1012. if (Last->Jump(Offset) == false)
  1013. return 0;
  1014. return Last;
  1015. }
  1016. /*}}}*/
  1017. // DoUpdate - Update the package lists /*{{{*/
  1018. // ---------------------------------------------------------------------
  1019. /* */
  1020. bool DoUpdate(CommandLine &CmdL)
  1021. {
  1022. if (CmdL.FileSize() != 1)
  1023. return _error->Error(_("The update command takes no arguments"));
  1024. // Get the source list
  1025. pkgSourceList List;
  1026. if (List.ReadMainList() == false)
  1027. return false;
  1028. // Lock the list directory
  1029. FileFd Lock;
  1030. if (_config->FindB("Debug::NoLocking",false) == false)
  1031. {
  1032. Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
  1033. if (_error->PendingError() == true)
  1034. return _error->Error(_("Unable to lock the list directory"));
  1035. }
  1036. // Create the download object
  1037. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1038. pkgAcquire Fetcher(&Stat);
  1039. // Populate it with the source selection
  1040. if (List.GetIndexes(&Fetcher) == false)
  1041. return false;
  1042. // Run it
  1043. if (Fetcher.Run() == pkgAcquire::Failed)
  1044. return false;
  1045. bool Failed = false;
  1046. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  1047. {
  1048. if ((*I)->Status == pkgAcquire::Item::StatDone)
  1049. continue;
  1050. (*I)->Finished();
  1051. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  1052. (*I)->ErrorText.c_str());
  1053. Failed = true;
  1054. }
  1055. // Clean out any old list files
  1056. if (_config->FindB("APT::Get::List-Cleanup",true) == true)
  1057. {
  1058. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  1059. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  1060. return false;
  1061. }
  1062. // Prepare the cache.
  1063. CacheFile Cache;
  1064. if (Cache.Open() == false)
  1065. return false;
  1066. if (Failed == true)
  1067. return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  1068. return true;
  1069. }
  1070. /*}}}*/
  1071. // DoUpgrade - Upgrade all packages /*{{{*/
  1072. // ---------------------------------------------------------------------
  1073. /* Upgrade all packages without installing new packages or erasing old
  1074. packages */
  1075. bool DoUpgrade(CommandLine &CmdL)
  1076. {
  1077. CacheFile Cache;
  1078. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1079. return false;
  1080. // Do the upgrade
  1081. if (pkgAllUpgrade(Cache) == false)
  1082. {
  1083. ShowBroken(c1out,Cache,false);
  1084. return _error->Error(_("Internal Error, AllUpgrade broke stuff"));
  1085. }
  1086. return InstallPackages(Cache,true);
  1087. }
  1088. /*}}}*/
  1089. // DoInstall - Install packages from the command line /*{{{*/
  1090. // ---------------------------------------------------------------------
  1091. /* Install named packages */
  1092. bool DoInstall(CommandLine &CmdL)
  1093. {
  1094. CacheFile Cache;
  1095. if (Cache.OpenForInstall() == false ||
  1096. Cache.CheckDeps(CmdL.FileSize() != 1) == false)
  1097. return false;
  1098. // Enter the special broken fixing mode if the user specified arguments
  1099. bool BrokenFix = false;
  1100. if (Cache->BrokenCount() != 0)
  1101. BrokenFix = true;
  1102. unsigned int ExpectedInst = 0;
  1103. unsigned int Packages = 0;
  1104. pkgProblemResolver Fix(Cache);
  1105. bool DefRemove = false;
  1106. if (strcasecmp(CmdL.FileList[0],"remove") == 0)
  1107. DefRemove = true;
  1108. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  1109. {
  1110. // Duplicate the string
  1111. unsigned int Length = strlen(*I);
  1112. char S[300];
  1113. if (Length >= sizeof(S))
  1114. continue;
  1115. strcpy(S,*I);
  1116. // See if we are removing and special indicators..
  1117. bool Remove = DefRemove;
  1118. char *VerTag = 0;
  1119. bool VerIsRel = false;
  1120. while (Cache->FindPkg(S).end() == true)
  1121. {
  1122. // Handle an optional end tag indicating what to do
  1123. if (S[Length - 1] == '-')
  1124. {
  1125. Remove = true;
  1126. S[--Length] = 0;
  1127. continue;
  1128. }
  1129. if (S[Length - 1] == '+')
  1130. {
  1131. Remove = false;
  1132. S[--Length] = 0;
  1133. continue;
  1134. }
  1135. char *Slash = strchr(S,'=');
  1136. if (Slash != 0)
  1137. {
  1138. VerIsRel = false;
  1139. *Slash = 0;
  1140. VerTag = Slash + 1;
  1141. }
  1142. Slash = strchr(S,'/');
  1143. if (Slash != 0)
  1144. {
  1145. VerIsRel = true;
  1146. *Slash = 0;
  1147. VerTag = Slash + 1;
  1148. }
  1149. break;
  1150. }
  1151. // Locate the package
  1152. pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
  1153. Packages++;
  1154. if (Pkg.end() == true)
  1155. {
  1156. // Check if the name is a regex
  1157. const char *I;
  1158. for (I = S; *I != 0; I++)
  1159. if (*I == '.' || *I == '?' || *I == '*' || *I == '|')
  1160. break;
  1161. if (*I == 0)
  1162. return _error->Error(_("Couldn't find package %s"),S);
  1163. // Regexs must always be confirmed
  1164. ExpectedInst += 1000;
  1165. // Compile the regex pattern
  1166. regex_t Pattern;
  1167. int Res;
  1168. if ((Res = regcomp(&Pattern,S,REG_EXTENDED | REG_ICASE |
  1169. REG_NOSUB)) != 0)
  1170. {
  1171. char Error[300];
  1172. regerror(Res,&Pattern,Error,sizeof(Error));
  1173. return _error->Error(_("Regex compilation error - %s"),Error);
  1174. }
  1175. // Run over the matches
  1176. bool Hit = false;
  1177. for (Pkg = Cache->PkgBegin(); Pkg.end() == false; Pkg++)
  1178. {
  1179. if (regexec(&Pattern,Pkg.Name(),0,0,0) != 0)
  1180. continue;
  1181. if (VerTag != 0)
  1182. if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
  1183. return false;
  1184. Hit |= TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,
  1185. ExpectedInst,false);
  1186. }
  1187. regfree(&Pattern);
  1188. if (Hit == false)
  1189. return _error->Error(_("Couldn't find package %s"),S);
  1190. }
  1191. else
  1192. {
  1193. if (VerTag != 0)
  1194. if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
  1195. return false;
  1196. if (TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,ExpectedInst) == false)
  1197. return false;
  1198. }
  1199. }
  1200. /* If we are in the Broken fixing mode we do not attempt to fix the
  1201. problems. This is if the user invoked install without -f and gave
  1202. packages */
  1203. if (BrokenFix == true && Cache->BrokenCount() != 0)
  1204. {
  1205. c1out << _("You might want to run `apt-get -f install' to correct these:") << endl;
  1206. ShowBroken(c1out,Cache,false);
  1207. return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
  1208. }
  1209. // Call the scored problem resolver
  1210. Fix.InstallProtect();
  1211. if (Fix.Resolve(true) == false)
  1212. _error->Discard();
  1213. // Now we check the state of the packages,
  1214. if (Cache->BrokenCount() != 0)
  1215. {
  1216. c1out <<
  1217. _("Some packages could not be installed. This may mean that you have\n"
  1218. "requested an impossible situation or if you are using the unstable\n"
  1219. "distribution that some required packages have not yet been created\n"
  1220. "or been moved out of Incoming.") << endl;
  1221. if (Packages == 1)
  1222. {
  1223. c1out << endl;
  1224. c1out <<
  1225. _("Since you only requested a single operation it is extremely likely that\n"
  1226. "the package is simply not installable and a bug report against\n"
  1227. "that package should be filed.") << endl;
  1228. }
  1229. c1out << _("The following information may help to resolve the situation:") << endl;
  1230. c1out << endl;
  1231. ShowBroken(c1out,Cache,false);
  1232. return _error->Error(_("Sorry, broken packages"));
  1233. }
  1234. /* Print out a list of packages that are going to be installed extra
  1235. to what the user asked */
  1236. if (Cache->InstCount() != ExpectedInst)
  1237. {
  1238. string List;
  1239. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  1240. {
  1241. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  1242. if ((*Cache)[I].Install() == false)
  1243. continue;
  1244. const char **J;
  1245. for (J = CmdL.FileList + 1; *J != 0; J++)
  1246. if (strcmp(*J,I.Name()) == 0)
  1247. break;
  1248. if (*J == 0)
  1249. List += string(I.Name()) + " ";
  1250. }
  1251. ShowList(c1out,_("The following extra packages will be installed:"),List);
  1252. }
  1253. // See if we need to prompt
  1254. if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
  1255. return InstallPackages(Cache,false,false);
  1256. return InstallPackages(Cache,false);
  1257. }
  1258. /*}}}*/
  1259. // DoDistUpgrade - Automatic smart upgrader /*{{{*/
  1260. // ---------------------------------------------------------------------
  1261. /* Intelligent upgrader that will install and remove packages at will */
  1262. bool DoDistUpgrade(CommandLine &CmdL)
  1263. {
  1264. CacheFile Cache;
  1265. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1266. return false;
  1267. c0out << _("Calculating Upgrade... ") << flush;
  1268. if (pkgDistUpgrade(*Cache) == false)
  1269. {
  1270. c0out << _("Failed") << endl;
  1271. ShowBroken(c1out,Cache,false);
  1272. return false;
  1273. }
  1274. c0out << _("Done") << endl;
  1275. return InstallPackages(Cache,true);
  1276. }
  1277. /*}}}*/
  1278. // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
  1279. // ---------------------------------------------------------------------
  1280. /* Follows dselect's selections */
  1281. bool DoDSelectUpgrade(CommandLine &CmdL)
  1282. {
  1283. CacheFile Cache;
  1284. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1285. return false;
  1286. // Install everything with the install flag set
  1287. pkgCache::PkgIterator I = Cache->PkgBegin();
  1288. for (;I.end() != true; I++)
  1289. {
  1290. /* Install the package only if it is a new install, the autoupgrader
  1291. will deal with the rest */
  1292. if (I->SelectedState == pkgCache::State::Install)
  1293. Cache->MarkInstall(I,false);
  1294. }
  1295. /* Now install their deps too, if we do this above then order of
  1296. the status file is significant for | groups */
  1297. for (I = Cache->PkgBegin();I.end() != true; I++)
  1298. {
  1299. /* Install the package only if it is a new install, the autoupgrader
  1300. will deal with the rest */
  1301. if (I->SelectedState == pkgCache::State::Install)
  1302. Cache->MarkInstall(I,true);
  1303. }
  1304. // Apply erasures now, they override everything else.
  1305. for (I = Cache->PkgBegin();I.end() != true; I++)
  1306. {
  1307. // Remove packages
  1308. if (I->SelectedState == pkgCache::State::DeInstall ||
  1309. I->SelectedState == pkgCache::State::Purge)
  1310. Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
  1311. }
  1312. /* Resolve any problems that dselect created, allupgrade cannot handle
  1313. such things. We do so quite agressively too.. */
  1314. if (Cache->BrokenCount() != 0)
  1315. {
  1316. pkgProblemResolver Fix(Cache);
  1317. // Hold back held packages.
  1318. if (_config->FindB("APT::Ignore-Hold",false) == false)
  1319. {
  1320. for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
  1321. {
  1322. if (I->SelectedState == pkgCache::State::Hold)
  1323. {
  1324. Fix.Protect(I);
  1325. Cache->MarkKeep(I);
  1326. }
  1327. }
  1328. }
  1329. if (Fix.Resolve() == false)
  1330. {
  1331. ShowBroken(c1out,Cache,false);
  1332. return _error->Error("Internal Error, problem resolver broke stuff");
  1333. }
  1334. }
  1335. // Now upgrade everything
  1336. if (pkgAllUpgrade(Cache) == false)
  1337. {
  1338. ShowBroken(c1out,Cache,false);
  1339. return _error->Error("Internal Error, problem resolver broke stuff");
  1340. }
  1341. return InstallPackages(Cache,false);
  1342. }
  1343. /*}}}*/
  1344. // DoClean - Remove download archives /*{{{*/
  1345. // ---------------------------------------------------------------------
  1346. /* */
  1347. bool DoClean(CommandLine &CmdL)
  1348. {
  1349. if (_config->FindB("APT::Get::Simulate") == true)
  1350. {
  1351. cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
  1352. _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
  1353. return true;
  1354. }
  1355. // Lock the archive directory
  1356. FileFd Lock;
  1357. if (_config->FindB("Debug::NoLocking",false) == false)
  1358. {
  1359. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1360. if (_error->PendingError() == true)
  1361. return _error->Error(_("Unable to lock the download directory"));
  1362. }
  1363. pkgAcquire Fetcher;
  1364. Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
  1365. Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
  1366. return true;
  1367. }
  1368. /*}}}*/
  1369. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  1370. // ---------------------------------------------------------------------
  1371. /* This is similar to clean but it only purges things that cannot be
  1372. downloaded, that is old versions of cached packages. */
  1373. class LogCleaner : public pkgArchiveCleaner
  1374. {
  1375. protected:
  1376. virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
  1377. {
  1378. c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
  1379. if (_config->FindB("APT::Get::Simulate") == false)
  1380. unlink(File);
  1381. };
  1382. };
  1383. bool DoAutoClean(CommandLine &CmdL)
  1384. {
  1385. // Lock the archive directory
  1386. FileFd Lock;
  1387. if (_config->FindB("Debug::NoLocking",false) == false)
  1388. {
  1389. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1390. if (_error->PendingError() == true)
  1391. return _error->Error(_("Unable to lock the download directory"));
  1392. }
  1393. CacheFile Cache;
  1394. if (Cache.Open() == false)
  1395. return false;
  1396. LogCleaner Cleaner;
  1397. return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
  1398. Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
  1399. }
  1400. /*}}}*/
  1401. // DoCheck - Perform the check operation /*{{{*/
  1402. // ---------------------------------------------------------------------
  1403. /* Opening automatically checks the system, this command is mostly used
  1404. for debugging */
  1405. bool DoCheck(CommandLine &CmdL)
  1406. {
  1407. CacheFile Cache;
  1408. Cache.Open();
  1409. Cache.CheckDeps();
  1410. return true;
  1411. }
  1412. /*}}}*/
  1413. // DoSource - Fetch a source archive /*{{{*/
  1414. // ---------------------------------------------------------------------
  1415. /* Fetch souce packages */
  1416. struct DscFile
  1417. {
  1418. string Package;
  1419. string Version;
  1420. string Dsc;
  1421. };
  1422. bool DoSource(CommandLine &CmdL)
  1423. {
  1424. CacheFile Cache;
  1425. if (Cache.Open(false) == false)
  1426. return false;
  1427. if (CmdL.FileSize() <= 1)
  1428. return _error->Error(_("Must specify at least one package to fetch source for"));
  1429. // Read the source list
  1430. pkgSourceList List;
  1431. if (List.ReadMainList() == false)
  1432. return _error->Error(_("The list of sources could not be read."));
  1433. // Create the text record parsers
  1434. pkgRecords Recs(Cache);
  1435. pkgSrcRecords SrcRecs(List);
  1436. if (_error->PendingError() == true)
  1437. return false;
  1438. // Create the download object
  1439. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1440. pkgAcquire Fetcher(&Stat);
  1441. DscFile *Dsc = new DscFile[CmdL.FileSize()];
  1442. // Load the requestd sources into the fetcher
  1443. unsigned J = 0;
  1444. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  1445. {
  1446. string Src;
  1447. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  1448. if (Last == 0)
  1449. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  1450. // Back track
  1451. vector<pkgSrcRecords::File> Lst;
  1452. if (Last->Files(Lst) == false)
  1453. return false;
  1454. // Load them into the fetcher
  1455. for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
  1456. I != Lst.end(); I++)
  1457. {
  1458. // Try to guess what sort of file it is we are getting.
  1459. if (I->Type == "dsc")
  1460. {
  1461. Dsc[J].Package = Last->Package();
  1462. Dsc[J].Version = Last->Version();
  1463. Dsc[J].Dsc = flNotDir(I->Path);
  1464. }
  1465. // Diff only mode only fetches .diff files
  1466. if (_config->FindB("APT::Get::Diff-Only",false) == true &&
  1467. I->Type != "diff")
  1468. continue;
  1469. // Tar only mode only fetches .tar files
  1470. if (_config->FindB("APT::Get::Tar-Only",false) == true &&
  1471. I->Type != "tar")
  1472. continue;
  1473. new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
  1474. I->MD5Hash,I->Size,
  1475. Last->Index().SourceInfo(*Last,*I),Src);
  1476. }
  1477. }
  1478. // Display statistics
  1479. double FetchBytes = Fetcher.FetchNeeded();
  1480. double FetchPBytes = Fetcher.PartialPresent();
  1481. double DebBytes = Fetcher.TotalNeeded();
  1482. // Check for enough free space
  1483. struct statvfs Buf;
  1484. string OutputDir = ".";
  1485. if (statvfs(OutputDir.c_str(),&Buf) != 0)
  1486. return _error->Errno("statvfs","Couldn't determine free space in %s",
  1487. OutputDir.c_str());
  1488. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  1489. return _error->Error(_("Sorry, you don't have enough free space in %s"),
  1490. OutputDir.c_str());
  1491. // Number of bytes
  1492. if (DebBytes != FetchBytes)
  1493. ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
  1494. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  1495. else
  1496. ioprintf(c1out,_("Need to get %sB of source archives.\n"),
  1497. SizeToStr(DebBytes).c_str());
  1498. if (_config->FindB("APT::Get::Simulate",false) == true)
  1499. {
  1500. for (unsigned I = 0; I != J; I++)
  1501. ioprintf(cout,_("Fetch Source %s\n"),Dsc[I].Package.c_str());
  1502. return true;
  1503. }
  1504. // Just print out the uris an exit if the --print-uris flag was used
  1505. if (_config->FindB("APT::Get::Print-URIs") == true)
  1506. {
  1507. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  1508. for (; I != Fetcher.UriEnd(); I++)
  1509. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  1510. I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
  1511. return true;
  1512. }
  1513. // Run it
  1514. if (Fetcher.Run() == pkgAcquire::Failed)
  1515. return false;
  1516. // Print error messages
  1517. bool Failed = false;
  1518. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  1519. {
  1520. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  1521. (*I)->Complete == true)
  1522. continue;
  1523. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  1524. (*I)->ErrorText.c_str());
  1525. Failed = true;
  1526. }
  1527. if (Failed == true)
  1528. return _error->Error(_("Failed to fetch some archives."));
  1529. if (_config->FindB("APT::Get::Download-only",false) == true)
  1530. {
  1531. c1out << _("Download complete and in download only mode") << endl;
  1532. return true;
  1533. }
  1534. // Unpack the sources
  1535. pid_t Process = ExecFork();
  1536. if (Process == 0)
  1537. {
  1538. for (unsigned I = 0; I != J; I++)
  1539. {
  1540. string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
  1541. // Diff only mode only fetches .diff files
  1542. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  1543. _config->FindB("APT::Get::Tar-Only",false) == true ||
  1544. Dsc[I].Dsc.empty() == true)
  1545. continue;
  1546. // See if the package is already unpacked
  1547. struct stat Stat;
  1548. if (stat(Dir.c_str(),&Stat) == 0 &&
  1549. S_ISDIR(Stat.st_mode) != 0)
  1550. {
  1551. ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
  1552. Dir.c_str());
  1553. }
  1554. else
  1555. {
  1556. // Call dpkg-source
  1557. char S[500];
  1558. snprintf(S,sizeof(S),"%s -x %s",
  1559. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  1560. Dsc[I].Dsc.c_str());
  1561. if (system(S) != 0)
  1562. {
  1563. fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
  1564. _exit(1);
  1565. }
  1566. }
  1567. // Try to compile it with dpkg-buildpackage
  1568. if (_config->FindB("APT::Get::Compile",false) == true)
  1569. {
  1570. // Call dpkg-buildpackage
  1571. char S[500];
  1572. snprintf(S,sizeof(S),"cd %s && %s %s",
  1573. Dir.c_str(),
  1574. _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
  1575. _config->Find("DPkg::Build-Options","-b -uc").c_str());
  1576. if (system(S) != 0)
  1577. {
  1578. fprintf(stderr,_("Build command '%s' failed.\n"),S);
  1579. _exit(1);
  1580. }
  1581. }
  1582. }
  1583. _exit(0);
  1584. }
  1585. // Wait for the subprocess
  1586. int Status = 0;
  1587. while (waitpid(Process,&Status,0) != Process)
  1588. {
  1589. if (errno == EINTR)
  1590. continue;
  1591. return _error->Errno("waitpid","Couldn't wait for subprocess");
  1592. }
  1593. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  1594. return _error->Error(_("Child process failed"));
  1595. return true;
  1596. }
  1597. /*}}}*/
  1598. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  1599. // ---------------------------------------------------------------------
  1600. /* This function will look at the build depends list of the given source
  1601. package and install the necessary packages to make it true, or fail. */
  1602. bool DoBuildDep(CommandLine &CmdL)
  1603. {
  1604. CacheFile Cache;
  1605. if (Cache.Open(true) == false)
  1606. return false;
  1607. if (CmdL.FileSize() <= 1)
  1608. return _error->Error(_("Must specify at least one package to check builddeps for"));
  1609. // Read the source list
  1610. pkgSourceList List;
  1611. if (List.ReadMainList() == false)
  1612. return _error->Error(_("The list of sources could not be read."));
  1613. // Create the text record parsers
  1614. pkgRecords Recs(Cache);
  1615. pkgSrcRecords SrcRecs(List);
  1616. if (_error->PendingError() == true)
  1617. return false;
  1618. // Create the download object
  1619. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1620. pkgAcquire Fetcher(&Stat);
  1621. unsigned J = 0;
  1622. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  1623. {
  1624. string Src;
  1625. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  1626. if (Last == 0)
  1627. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  1628. // Process the build-dependencies
  1629. vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  1630. if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only",false)) == false)
  1631. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  1632. // Also ensure that build-essential packages are present
  1633. Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
  1634. if (Opts)
  1635. Opts = Opts->Child;
  1636. for (; Opts; Opts = Opts->Next)
  1637. {
  1638. if (Opts->Value.empty() == true)
  1639. continue;
  1640. pkgSrcRecords::Parser::BuildDepRec rec;
  1641. rec.Package = Opts->Value;
  1642. rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
  1643. rec.Op = 0;
  1644. BuildDeps.insert(BuildDeps.begin(), rec);
  1645. }
  1646. if (BuildDeps.size() == 0)
  1647. {
  1648. ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
  1649. continue;
  1650. }
  1651. // Install the requested packages
  1652. unsigned int ExpectedInst = 0;
  1653. vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
  1654. pkgProblemResolver Fix(Cache);
  1655. for (D = BuildDeps.begin(); D != BuildDeps.end(); D++)
  1656. {
  1657. pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
  1658. if (Pkg.end() == true)
  1659. {
  1660. /* for a build-conflict; ignore unknown packages */
  1661. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  1662. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  1663. continue;
  1664. return _error->Error(_("%s dependency on %s cannot be satisfied because the package %s cannot be found"),
  1665. Last->BuildDepType((*D).Type),Src.c_str(),(*D).Package.c_str());
  1666. }
  1667. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  1668. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  1669. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  1670. {
  1671. /*
  1672. * conflict; need to remove if we have an installed version
  1673. * that satisfies the version criterial
  1674. */
  1675. if (IV.end() == false &&
  1676. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  1677. TryToInstall(Pkg,Cache,Fix,true,false,ExpectedInst);
  1678. }
  1679. else
  1680. {
  1681. /*
  1682. * If this is a virtual package, we need to check the list of
  1683. * packages that provide it and see if any of those are
  1684. * installed
  1685. */
  1686. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  1687. for (; Prv.end() != true; Prv++)
  1688. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  1689. break;
  1690. if (Prv.end() == true)
  1691. {
  1692. /*
  1693. * depends; need to install or upgrade if we don't have the
  1694. * package installed or if the version does not satisfy the
  1695. * build dep. This is complicated by the fact that if we
  1696. * depend on a version lower than what we already have
  1697. * installed it is not clear what should be done; in practice
  1698. * this case should be rare though and right now nothing
  1699. * is done about it :-(
  1700. */
  1701. if (IV.end() == true ||
  1702. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
  1703. TryToInstall(Pkg,Cache,Fix,false,false,ExpectedInst);
  1704. }
  1705. }
  1706. }
  1707. Fix.InstallProtect();
  1708. if (Fix.Resolve(true) == false)
  1709. _error->Discard();
  1710. // Now we check the state of the packages,
  1711. if (Cache->BrokenCount() != 0)
  1712. return _error->Error(_("Some broken packages were found while trying to process build-dependencies.\n"
  1713. "You might want to run `apt-get -f install' to correct these."));
  1714. }
  1715. if (InstallPackages(Cache, false, true) == false)
  1716. return _error->Error(_("Failed to process build dependencies"));
  1717. return true;
  1718. }
  1719. /*}}}*/
  1720. // DoMoo - Never Ask, Never Tell /*{{{*/
  1721. // ---------------------------------------------------------------------
  1722. /* */
  1723. bool DoMoo(CommandLine &CmdL)
  1724. {
  1725. cout <<
  1726. " (__) \n"
  1727. " (oo) \n"
  1728. " /------\\/ \n"
  1729. " / | || \n"
  1730. " * /\\---/\\ \n"
  1731. " ~~ ~~ \n"
  1732. "....\"Have you mooed today?\"...\n";
  1733. return true;
  1734. }
  1735. /*}}}*/
  1736. // ShowHelp - Show a help screen /*{{{*/
  1737. // ---------------------------------------------------------------------
  1738. /* */
  1739. bool ShowHelp(CommandLine &CmdL)
  1740. {
  1741. ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
  1742. COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
  1743. if (_config->FindB("version") == true)
  1744. {
  1745. cout << _("Supported Modules:") << endl;
  1746. for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
  1747. {
  1748. pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
  1749. if (_system != 0 && _system->VS == VS)
  1750. cout << '*';
  1751. else
  1752. cout << ' ';
  1753. cout << "Ver: " << VS->Label << endl;
  1754. /* Print out all the packaging systems that will work with
  1755. this VS */
  1756. for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
  1757. {
  1758. pkgSystem *Sys = pkgSystem::GlobalList[J];
  1759. if (_system == Sys)
  1760. cout << '*';
  1761. else
  1762. cout << ' ';
  1763. if (Sys->VS->TestCompatibility(*VS) == true)
  1764. cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
  1765. }
  1766. }
  1767. for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
  1768. {
  1769. pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
  1770. cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
  1771. }
  1772. for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
  1773. {
  1774. pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
  1775. cout << " Idx: " << Type->Label << endl;
  1776. }
  1777. return true;
  1778. }
  1779. cout <<
  1780. _("Usage: apt-get [options] command\n"
  1781. " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
  1782. " apt-get [options] source pkg1 [pkg2 ...]\n"
  1783. "\n"
  1784. "apt-get is a simple command line interface for downloading and\n"
  1785. "installing packages. The most frequently used commands are update\n"
  1786. "and install.\n"
  1787. "\n"
  1788. "Commands:\n"
  1789. " update - Retrieve new lists of packages\n"
  1790. " upgrade - Perform an upgrade\n"
  1791. " install - Install new packages (pkg is libc6 not libc6.deb)\n"
  1792. " remove - Remove packages\n"
  1793. " source - Download source archives\n"
  1794. " build-dep - Configure build-dependencies for source packages\n"
  1795. " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
  1796. " dselect-upgrade - Follow dselect selections\n"
  1797. " clean - Erase downloaded archive files\n"
  1798. " autoclean - Erase old downloaded archive files\n"
  1799. " check - Verify that there are no broken dependencies\n"
  1800. "\n"
  1801. "Options:\n"
  1802. " -h This help text.\n"
  1803. " -q Loggable output - no progress indicator\n"
  1804. " -qq No output except for errors\n"
  1805. " -d Download only - do NOT install or unpack archives\n"
  1806. " -s No-act. Perform ordering simulation\n"
  1807. " -y Assume Yes to all queries and do not prompt\n"
  1808. " -f Attempt to continue if the integrity check fails\n"
  1809. " -m Attempt to continue if archives are unlocatable\n"
  1810. " -u Show a list of upgraded packages as well\n"
  1811. " -b Build the source package after fetching it\n"
  1812. " -c=? Read this configuration file\n"
  1813. " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n"
  1814. "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
  1815. "pages for more information and options.\n"
  1816. " This APT has Super Cow Powers.\n");
  1817. return true;
  1818. }
  1819. /*}}}*/
  1820. // GetInitialize - Initialize things for apt-get /*{{{*/
  1821. // ---------------------------------------------------------------------
  1822. /* */
  1823. void GetInitialize()
  1824. {
  1825. _config->Set("quiet",0);
  1826. _config->Set("help",false);
  1827. _config->Set("APT::Get::Download-Only",false);
  1828. _config->Set("APT::Get::Simulate",false);
  1829. _config->Set("APT::Get::Assume-Yes",false);
  1830. _config->Set("APT::Get::Fix-Broken",false);
  1831. _config->Set("APT::Get::Force-Yes",false);
  1832. _config->Set("APT::Get::APT::Get::No-List-Cleanup",true);
  1833. }
  1834. /*}}}*/
  1835. // SigWinch - Window size change signal handler /*{{{*/
  1836. // ---------------------------------------------------------------------
  1837. /* */
  1838. void SigWinch(int)
  1839. {
  1840. // Riped from GNU ls
  1841. #ifdef TIOCGWINSZ
  1842. struct winsize ws;
  1843. if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
  1844. ScreenWidth = ws.ws_col - 1;
  1845. #endif
  1846. }
  1847. /*}}}*/
  1848. int main(int argc,const char *argv[])
  1849. {
  1850. CommandLine::Args Args[] = {
  1851. {'h',"help","help",0},
  1852. {'v',"version","version",0},
  1853. {'q',"quiet","quiet",CommandLine::IntLevel},
  1854. {'q',"silent","quiet",CommandLine::IntLevel},
  1855. {'d',"download-only","APT::Get::Download-Only",0},
  1856. {'b',"compile","APT::Get::Compile",0},
  1857. {'b',"build","APT::Get::Compile",0},
  1858. {'s',"simulate","APT::Get::Simulate",0},
  1859. {'s',"just-print","APT::Get::Simulate",0},
  1860. {'s',"recon","APT::Get::Simulate",0},
  1861. {'s',"dry-run","APT::Get::Simulate",0},
  1862. {'s',"no-act","APT::Get::Simulate",0},
  1863. {'y',"yes","APT::Get::Assume-Yes",0},
  1864. {'y',"assume-yes","APT::Get::Assume-Yes",0},
  1865. {'f',"fix-broken","APT::Get::Fix-Broken",0},
  1866. {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
  1867. {'m',"ignore-missing","APT::Get::Fix-Missing",0},
  1868. {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
  1869. {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
  1870. {0,"download","APT::Get::Download",0},
  1871. {0,"fix-missing","APT::Get::Fix-Missing",0},
  1872. {0,"ignore-hold","APT::Ignore-Hold",0},
  1873. {0,"upgrade","APT::Get::upgrade",0},
  1874. {0,"force-yes","APT::Get::force-yes",0},
  1875. {0,"print-uris","APT::Get::Print-URIs",0},
  1876. {0,"diff-only","APT::Get::Diff-Only",0},
  1877. {0,"tar-only","APT::Get::tar-Only",0},
  1878. {0,"purge","APT::Get::Purge",0},
  1879. {0,"list-cleanup","APT::Get::List-Cleanup",0},
  1880. {0,"reinstall","APT::Get::ReInstall",0},
  1881. {0,"trivial-only","APT::Get::Trivial-Only",0},
  1882. {0,"remove","APT::Get::Remove",0},
  1883. {0,"only-source","APT::Get::Only-Source",0},
  1884. {0,"arch-only","APT::Get::Arch-Only",0},
  1885. {'c',"config-file",0,CommandLine::ConfigFile},
  1886. {'o',"option",0,CommandLine::ArbItem},
  1887. {0,0,0,0}};
  1888. CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
  1889. {"upgrade",&DoUpgrade},
  1890. {"install",&DoInstall},
  1891. {"remove",&DoInstall},
  1892. {"dist-upgrade",&DoDistUpgrade},
  1893. {"dselect-upgrade",&DoDSelectUpgrade},
  1894. {"build-dep",&DoBuildDep},
  1895. {"clean",&DoClean},
  1896. {"autoclean",&DoAutoClean},
  1897. {"check",&DoCheck},
  1898. {"source",&DoSource},
  1899. {"moo",&DoMoo},
  1900. {"help",&ShowHelp},
  1901. {0,0}};
  1902. // Set up gettext support
  1903. setlocale(LC_ALL,"");
  1904. textdomain(PACKAGE);
  1905. // Parse the command line and initialize the package library
  1906. CommandLine CmdL(Args,_config);
  1907. if (pkgInitConfig(*_config) == false ||
  1908. CmdL.Parse(argc,argv) == false ||
  1909. pkgInitSystem(*_config,_system) == false)
  1910. {
  1911. if (_config->FindB("version") == true)
  1912. ShowHelp(CmdL);
  1913. _error->DumpErrors();
  1914. return 100;
  1915. }
  1916. // See if the help should be shown
  1917. if (_config->FindB("help") == true ||
  1918. _config->FindB("version") == true ||
  1919. CmdL.FileSize() == 0)
  1920. {
  1921. ShowHelp(CmdL);
  1922. return 0;
  1923. }
  1924. // Deal with stdout not being a tty
  1925. if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
  1926. _config->Set("quiet","1");
  1927. // Setup the output streams
  1928. c0out.rdbuf(cout.rdbuf());
  1929. c1out.rdbuf(cout.rdbuf());
  1930. c2out.rdbuf(cout.rdbuf());
  1931. if (_config->FindI("quiet",0) > 0)
  1932. c0out.rdbuf(devnull.rdbuf());
  1933. if (_config->FindI("quiet",0) > 1)
  1934. c1out.rdbuf(devnull.rdbuf());
  1935. // Setup the signals
  1936. signal(SIGPIPE,SIG_IGN);
  1937. signal(SIGWINCH,SigWinch);
  1938. SigWinch(0);
  1939. // Match the operation
  1940. CmdL.DispatchArg(Cmds);
  1941. // Print any errors or warnings found during parsing
  1942. if (_error->empty() == false)
  1943. {
  1944. bool Errors = _error->PendingError();
  1945. _error->DumpErrors();
  1946. return Errors == true?100:0;
  1947. }
  1948. return 0;
  1949. }