apt-get.cc 68 KB

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