apt-get.cc 65 KB

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