apt-get.cc 70 KB

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