apt-get.cc 65 KB

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