apt-get.cc 83 KB

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