apt-get.cc 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510
  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 <config.h>
  25. #include <apt-pkg/aptconfiguration.h>
  26. #include <apt-pkg/error.h>
  27. #include <apt-pkg/cmndline.h>
  28. #include <apt-pkg/init.h>
  29. #include <apt-pkg/depcache.h>
  30. #include <apt-pkg/sourcelist.h>
  31. #include <apt-pkg/algorithms.h>
  32. #include <apt-pkg/acquire-item.h>
  33. #include <apt-pkg/strutl.h>
  34. #include <apt-pkg/fileutl.h>
  35. #include <apt-pkg/clean.h>
  36. #include <apt-pkg/srcrecords.h>
  37. #include <apt-pkg/version.h>
  38. #include <apt-pkg/cachefile.h>
  39. #include <apt-pkg/cacheset.h>
  40. #include <apt-pkg/sptr.h>
  41. #include <apt-pkg/md5.h>
  42. #include <apt-pkg/versionmatch.h>
  43. #include <apt-pkg/progress.h>
  44. #include <apt-pkg/pkgsystem.h>
  45. #include <apt-pkg/pkgrecords.h>
  46. #include <apt-pkg/indexfile.h>
  47. #include "acqprogress.h"
  48. #include <set>
  49. #include <locale.h>
  50. #include <langinfo.h>
  51. #include <fstream>
  52. #include <termios.h>
  53. #include <sys/ioctl.h>
  54. #include <sys/stat.h>
  55. #include <sys/statfs.h>
  56. #include <sys/statvfs.h>
  57. #include <signal.h>
  58. #include <unistd.h>
  59. #include <stdio.h>
  60. #include <errno.h>
  61. #include <regex.h>
  62. #include <sys/wait.h>
  63. #include <sstream>
  64. #include <apti18n.h>
  65. /*}}}*/
  66. #define RAMFS_MAGIC 0x858458f6
  67. using namespace std;
  68. ostream c0out(0);
  69. ostream c1out(0);
  70. ostream c2out(0);
  71. ofstream devnull("/dev/null");
  72. unsigned int ScreenWidth = 80 - 1; /* - 1 for the cursor */
  73. // class CacheFile - Cover class for some dependency cache functions /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* */
  76. class CacheFile : public pkgCacheFile
  77. {
  78. static pkgCache *SortCache;
  79. static int NameComp(const void *a,const void *b);
  80. public:
  81. pkgCache::Package **List;
  82. void Sort();
  83. bool CheckDeps(bool AllowBroken = false);
  84. bool BuildCaches(bool WithLock = true)
  85. {
  86. OpTextProgress Prog(*_config);
  87. if (pkgCacheFile::BuildCaches(&Prog,WithLock) == false)
  88. return false;
  89. return true;
  90. }
  91. bool Open(bool WithLock = true)
  92. {
  93. OpTextProgress Prog(*_config);
  94. if (pkgCacheFile::Open(&Prog,WithLock) == false)
  95. return false;
  96. Sort();
  97. return true;
  98. };
  99. bool OpenForInstall()
  100. {
  101. if (_config->FindB("APT::Get::Print-URIs") == true)
  102. return Open(false);
  103. else
  104. return Open(true);
  105. }
  106. CacheFile() : List(0) {};
  107. ~CacheFile() {
  108. delete[] List;
  109. }
  110. };
  111. /*}}}*/
  112. // YnPrompt - Yes No Prompt. /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* Returns true on a Yes.*/
  115. bool YnPrompt(bool Default=true)
  116. {
  117. if (_config->FindB("APT::Get::Assume-Yes",false) == true)
  118. {
  119. c1out << _("Y") << endl;
  120. return true;
  121. }
  122. else if (_config->FindB("APT::Get::Assume-No",false) == true)
  123. {
  124. c1out << _("N") << endl;
  125. return false;
  126. }
  127. char response[1024] = "";
  128. cin.getline(response, sizeof(response));
  129. if (!cin)
  130. return false;
  131. if (strlen(response) == 0)
  132. return Default;
  133. regex_t Pattern;
  134. int Res;
  135. Res = regcomp(&Pattern, nl_langinfo(YESEXPR),
  136. REG_EXTENDED|REG_ICASE|REG_NOSUB);
  137. if (Res != 0) {
  138. char Error[300];
  139. regerror(Res,&Pattern,Error,sizeof(Error));
  140. return _error->Error(_("Regex compilation error - %s"),Error);
  141. }
  142. Res = regexec(&Pattern, response, 0, NULL, 0);
  143. if (Res == 0)
  144. return true;
  145. return false;
  146. }
  147. /*}}}*/
  148. // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
  149. // ---------------------------------------------------------------------
  150. /* Returns true on a Yes.*/
  151. bool AnalPrompt(const char *Text)
  152. {
  153. char Buf[1024];
  154. cin.getline(Buf,sizeof(Buf));
  155. if (strcmp(Buf,Text) == 0)
  156. return true;
  157. return false;
  158. }
  159. /*}}}*/
  160. // ShowList - Show a list /*{{{*/
  161. // ---------------------------------------------------------------------
  162. /* This prints out a string of space separated words with a title and
  163. a two space indent line wraped to the current screen width. */
  164. bool ShowList(ostream &out,string Title,string List,string VersionsList)
  165. {
  166. if (List.empty() == true)
  167. return true;
  168. // trim trailing space
  169. int NonSpace = List.find_last_not_of(' ');
  170. if (NonSpace != -1)
  171. {
  172. List = List.erase(NonSpace + 1);
  173. if (List.empty() == true)
  174. return true;
  175. }
  176. // Acount for the leading space
  177. int ScreenWidth = ::ScreenWidth - 3;
  178. out << Title << endl;
  179. string::size_type Start = 0;
  180. string::size_type VersionsStart = 0;
  181. while (Start < List.size())
  182. {
  183. if(_config->FindB("APT::Get::Show-Versions",false) == true &&
  184. VersionsList.size() > 0) {
  185. string::size_type End;
  186. string::size_type VersionsEnd;
  187. End = List.find(' ',Start);
  188. VersionsEnd = VersionsList.find('\n', VersionsStart);
  189. out << " " << string(List,Start,End - Start) << " (" <<
  190. string(VersionsList,VersionsStart,VersionsEnd - VersionsStart) <<
  191. ")" << endl;
  192. if (End == string::npos || End < Start)
  193. End = Start + ScreenWidth;
  194. Start = End + 1;
  195. VersionsStart = VersionsEnd + 1;
  196. } else {
  197. string::size_type End;
  198. if (Start + ScreenWidth >= List.size())
  199. End = List.size();
  200. else
  201. End = List.rfind(' ',Start+ScreenWidth);
  202. if (End == string::npos || End < Start)
  203. End = Start + ScreenWidth;
  204. out << " " << string(List,Start,End - Start) << endl;
  205. Start = End + 1;
  206. }
  207. }
  208. return false;
  209. }
  210. /*}}}*/
  211. // ShowBroken - Debugging aide /*{{{*/
  212. // ---------------------------------------------------------------------
  213. /* This prints out the names of all the packages that are broken along
  214. with the name of each each broken dependency and a quite version
  215. description.
  216. The output looks like:
  217. The following packages have unmet dependencies:
  218. exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
  219. Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
  220. Depends: libsasl7 but it is not going to be installed
  221. */
  222. void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
  223. {
  224. out << _("The following packages have unmet dependencies:") << endl;
  225. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  226. {
  227. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  228. if (Now == true)
  229. {
  230. if (Cache[I].NowBroken() == false)
  231. continue;
  232. }
  233. else
  234. {
  235. if (Cache[I].InstBroken() == false)
  236. continue;
  237. }
  238. // Print out each package and the failed dependencies
  239. out << " " << I.FullName(true) << " :";
  240. unsigned const Indent = I.FullName(true).size() + 3;
  241. bool First = true;
  242. pkgCache::VerIterator Ver;
  243. if (Now == true)
  244. Ver = I.CurrentVer();
  245. else
  246. Ver = Cache[I].InstVerIter(Cache);
  247. if (Ver.end() == true)
  248. {
  249. out << endl;
  250. continue;
  251. }
  252. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false;)
  253. {
  254. // Compute a single dependency element (glob or)
  255. pkgCache::DepIterator Start;
  256. pkgCache::DepIterator End;
  257. D.GlobOr(Start,End); // advances D
  258. if (Cache->IsImportantDep(End) == false)
  259. continue;
  260. if (Now == true)
  261. {
  262. if ((Cache[End] & pkgDepCache::DepGNow) == pkgDepCache::DepGNow)
  263. continue;
  264. }
  265. else
  266. {
  267. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  268. continue;
  269. }
  270. bool FirstOr = true;
  271. while (1)
  272. {
  273. if (First == false)
  274. for (unsigned J = 0; J != Indent; J++)
  275. out << ' ';
  276. First = false;
  277. if (FirstOr == false)
  278. {
  279. for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
  280. out << ' ';
  281. }
  282. else
  283. out << ' ' << End.DepType() << ": ";
  284. FirstOr = false;
  285. out << Start.TargetPkg().FullName(true);
  286. // Show a quick summary of the version requirements
  287. if (Start.TargetVer() != 0)
  288. out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
  289. /* Show a summary of the target package if possible. In the case
  290. of virtual packages we show nothing */
  291. pkgCache::PkgIterator Targ = Start.TargetPkg();
  292. if (Targ->ProvidesList == 0)
  293. {
  294. out << ' ';
  295. pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
  296. if (Now == true)
  297. Ver = Targ.CurrentVer();
  298. if (Ver.end() == false)
  299. {
  300. if (Now == true)
  301. ioprintf(out,_("but %s is installed"),Ver.VerStr());
  302. else
  303. ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
  304. }
  305. else
  306. {
  307. if (Cache[Targ].CandidateVerIter(Cache).end() == true)
  308. {
  309. if (Targ->ProvidesList == 0)
  310. out << _("but it is not installable");
  311. else
  312. out << _("but it is a virtual package");
  313. }
  314. else
  315. out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
  316. }
  317. }
  318. if (Start != End)
  319. out << _(" or");
  320. out << endl;
  321. if (Start == End)
  322. break;
  323. ++Start;
  324. }
  325. }
  326. }
  327. }
  328. /*}}}*/
  329. // ShowNew - Show packages to newly install /*{{{*/
  330. // ---------------------------------------------------------------------
  331. /* */
  332. void ShowNew(ostream &out,CacheFile &Cache)
  333. {
  334. /* Print out a list of packages that are going to be installed extra
  335. to what the user asked */
  336. string List;
  337. string VersionsList;
  338. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  339. {
  340. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  341. if (Cache[I].NewInstall() == true) {
  342. List += I.FullName(true) + " ";
  343. VersionsList += string(Cache[I].CandVersion) + "\n";
  344. }
  345. }
  346. ShowList(out,_("The following NEW packages will be installed:"),List,VersionsList);
  347. }
  348. /*}}}*/
  349. // ShowDel - Show packages to delete /*{{{*/
  350. // ---------------------------------------------------------------------
  351. /* */
  352. void ShowDel(ostream &out,CacheFile &Cache)
  353. {
  354. /* Print out a list of packages that are going to be removed extra
  355. to what the user asked */
  356. string List;
  357. string VersionsList;
  358. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  359. {
  360. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  361. if (Cache[I].Delete() == true)
  362. {
  363. if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
  364. List += I.FullName(true) + "* ";
  365. else
  366. List += I.FullName(true) + " ";
  367. VersionsList += string(Cache[I].CandVersion)+ "\n";
  368. }
  369. }
  370. ShowList(out,_("The following packages will be REMOVED:"),List,VersionsList);
  371. }
  372. /*}}}*/
  373. // ShowKept - Show kept packages /*{{{*/
  374. // ---------------------------------------------------------------------
  375. /* */
  376. void ShowKept(ostream &out,CacheFile &Cache)
  377. {
  378. string List;
  379. string VersionsList;
  380. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  381. {
  382. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  383. // Not interesting
  384. if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
  385. I->CurrentVer == 0 || Cache[I].Delete() == true)
  386. continue;
  387. List += I.FullName(true) + " ";
  388. VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
  389. }
  390. ShowList(out,_("The following packages have been kept back:"),List,VersionsList);
  391. }
  392. /*}}}*/
  393. // ShowUpgraded - Show upgraded packages /*{{{*/
  394. // ---------------------------------------------------------------------
  395. /* */
  396. void ShowUpgraded(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].Upgrade() == false || Cache[I].NewInstall() == true)
  405. continue;
  406. List += I.FullName(true) + " ";
  407. VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
  408. }
  409. ShowList(out,_("The following packages will be upgraded:"),List,VersionsList);
  410. }
  411. /*}}}*/
  412. // ShowDowngraded - Show downgraded packages /*{{{*/
  413. // ---------------------------------------------------------------------
  414. /* */
  415. bool ShowDowngraded(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. // Not interesting
  423. if (Cache[I].Downgrade() == false || Cache[I].NewInstall() == true)
  424. continue;
  425. List += I.FullName(true) + " ";
  426. VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
  427. }
  428. return ShowList(out,_("The following packages will be DOWNGRADED:"),List,VersionsList);
  429. }
  430. /*}}}*/
  431. // ShowHold - Show held but changed packages /*{{{*/
  432. // ---------------------------------------------------------------------
  433. /* */
  434. bool ShowHold(ostream &out,CacheFile &Cache)
  435. {
  436. string List;
  437. string VersionsList;
  438. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  439. {
  440. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  441. if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
  442. I->SelectedState == pkgCache::State::Hold) {
  443. List += I.FullName(true) + " ";
  444. VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
  445. }
  446. }
  447. return ShowList(out,_("The following held packages will be changed:"),List,VersionsList);
  448. }
  449. /*}}}*/
  450. // ShowEssential - Show an essential package warning /*{{{*/
  451. // ---------------------------------------------------------------------
  452. /* This prints out a warning message that is not to be ignored. It shows
  453. all essential packages and their dependents that are to be removed.
  454. It is insanely risky to remove the dependents of an essential package! */
  455. bool ShowEssential(ostream &out,CacheFile &Cache)
  456. {
  457. string List;
  458. string VersionsList;
  459. bool *Added = new bool[Cache->Head().PackageCount];
  460. for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
  461. Added[I] = false;
  462. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  463. {
  464. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  465. if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
  466. (I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
  467. continue;
  468. // The essential package is being removed
  469. if (Cache[I].Delete() == true)
  470. {
  471. if (Added[I->ID] == false)
  472. {
  473. Added[I->ID] = true;
  474. List += I.FullName(true) + " ";
  475. //VersionsList += string(Cache[I].CurVersion) + "\n"; ???
  476. }
  477. }
  478. else
  479. continue;
  480. if (I->CurrentVer == 0)
  481. continue;
  482. // Print out any essential package depenendents that are to be removed
  483. for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; ++D)
  484. {
  485. // Skip everything but depends
  486. if (D->Type != pkgCache::Dep::PreDepends &&
  487. D->Type != pkgCache::Dep::Depends)
  488. continue;
  489. pkgCache::PkgIterator P = D.SmartTargetPkg();
  490. if (Cache[P].Delete() == true)
  491. {
  492. if (Added[P->ID] == true)
  493. continue;
  494. Added[P->ID] = true;
  495. char S[300];
  496. snprintf(S,sizeof(S),_("%s (due to %s) "),P.FullName(true).c_str(),I.FullName(true).c_str());
  497. List += S;
  498. //VersionsList += "\n"; ???
  499. }
  500. }
  501. }
  502. delete [] Added;
  503. return ShowList(out,_("WARNING: The following essential packages will be removed.\n"
  504. "This should NOT be done unless you know exactly what you are doing!"),List,VersionsList);
  505. }
  506. /*}}}*/
  507. // Stats - Show some statistics /*{{{*/
  508. // ---------------------------------------------------------------------
  509. /* */
  510. void Stats(ostream &out,pkgDepCache &Dep)
  511. {
  512. unsigned long Upgrade = 0;
  513. unsigned long Downgrade = 0;
  514. unsigned long Install = 0;
  515. unsigned long ReInstall = 0;
  516. for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; ++I)
  517. {
  518. if (Dep[I].NewInstall() == true)
  519. Install++;
  520. else
  521. {
  522. if (Dep[I].Upgrade() == true)
  523. Upgrade++;
  524. else
  525. if (Dep[I].Downgrade() == true)
  526. Downgrade++;
  527. }
  528. if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
  529. ReInstall++;
  530. }
  531. ioprintf(out,_("%lu upgraded, %lu newly installed, "),
  532. Upgrade,Install);
  533. if (ReInstall != 0)
  534. ioprintf(out,_("%lu reinstalled, "),ReInstall);
  535. if (Downgrade != 0)
  536. ioprintf(out,_("%lu downgraded, "),Downgrade);
  537. ioprintf(out,_("%lu to remove and %lu not upgraded.\n"),
  538. Dep.DelCount(),Dep.KeepCount());
  539. if (Dep.BadCount() != 0)
  540. ioprintf(out,_("%lu not fully installed or removed.\n"),
  541. Dep.BadCount());
  542. }
  543. /*}}}*/
  544. // CacheSetHelperAPTGet - responsible for message telling from the CacheSets/*{{{*/
  545. class CacheSetHelperAPTGet : public APT::CacheSetHelper {
  546. /** \brief stream message should be printed to */
  547. std::ostream &out;
  548. /** \brief were things like Task or RegEx used to select packages? */
  549. bool explicitlyNamed;
  550. APT::PackageSet virtualPkgs;
  551. public:
  552. std::list<std::pair<pkgCache::VerIterator, std::string> > selectedByRelease;
  553. CacheSetHelperAPTGet(std::ostream &out) : APT::CacheSetHelper(true), out(out) {
  554. explicitlyNamed = true;
  555. }
  556. virtual void showTaskSelection(pkgCache::PkgIterator const &Pkg, string const &pattern) {
  557. ioprintf(out, _("Note, selecting '%s' for task '%s'\n"),
  558. Pkg.FullName(true).c_str(), pattern.c_str());
  559. explicitlyNamed = false;
  560. }
  561. virtual void showRegExSelection(pkgCache::PkgIterator const &Pkg, string const &pattern) {
  562. ioprintf(out, _("Note, selecting '%s' for regex '%s'\n"),
  563. Pkg.FullName(true).c_str(), pattern.c_str());
  564. explicitlyNamed = false;
  565. }
  566. virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
  567. string const &ver, bool const verIsRel) {
  568. if (ver == Ver.VerStr())
  569. return;
  570. selectedByRelease.push_back(make_pair(Ver, ver));
  571. }
  572. bool showVirtualPackageErrors(pkgCacheFile &Cache) {
  573. if (virtualPkgs.empty() == true)
  574. return true;
  575. for (APT::PackageSet::const_iterator Pkg = virtualPkgs.begin();
  576. Pkg != virtualPkgs.end(); ++Pkg) {
  577. if (Pkg->ProvidesList != 0) {
  578. ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
  579. Pkg.FullName(true).c_str());
  580. pkgCache::PrvIterator I = Pkg.ProvidesList();
  581. unsigned short provider = 0;
  582. for (; I.end() == false; ++I) {
  583. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  584. if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer()) {
  585. c1out << " " << Pkg.FullName(true) << " " << I.OwnerVer().VerStr();
  586. if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
  587. c1out << _(" [Installed]");
  588. c1out << endl;
  589. ++provider;
  590. }
  591. }
  592. // if we found no candidate which provide this package, show non-candidates
  593. if (provider == 0)
  594. for (I = Pkg.ProvidesList(); I.end() == false; ++I)
  595. c1out << " " << I.OwnerPkg().FullName(true) << " " << I.OwnerVer().VerStr()
  596. << _(" [Not candidate version]") << endl;
  597. else
  598. out << _("You should explicitly select one to install.") << endl;
  599. } else {
  600. ioprintf(c1out,
  601. _("Package %s is not available, but is referred to by another package.\n"
  602. "This may mean that the package is missing, has been obsoleted, or\n"
  603. "is only available from another source\n"),Pkg.FullName(true).c_str());
  604. string List;
  605. string VersionsList;
  606. SPtrArray<bool> Seen = new bool[Cache.GetPkgCache()->Head().PackageCount];
  607. memset(Seen,0,Cache.GetPkgCache()->Head().PackageCount*sizeof(*Seen));
  608. for (pkgCache::DepIterator Dep = Pkg.RevDependsList();
  609. Dep.end() == false; ++Dep) {
  610. if (Dep->Type != pkgCache::Dep::Replaces)
  611. continue;
  612. if (Seen[Dep.ParentPkg()->ID] == true)
  613. continue;
  614. Seen[Dep.ParentPkg()->ID] = true;
  615. List += Dep.ParentPkg().FullName(true) + " ";
  616. //VersionsList += string(Dep.ParentPkg().CurVersion) + "\n"; ???
  617. }
  618. ShowList(c1out,_("However the following packages replace it:"),List,VersionsList);
  619. }
  620. c1out << std::endl;
  621. }
  622. return false;
  623. }
  624. virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
  625. APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::CANDIDATE);
  626. if (verset.empty() == false)
  627. return *(verset.begin());
  628. else if (ShowError == true) {
  629. _error->Error(_("Package '%s' has no installation candidate"),Pkg.FullName(true).c_str());
  630. virtualPkgs.insert(Pkg);
  631. }
  632. return pkgCache::VerIterator(Cache, 0);
  633. }
  634. virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
  635. APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::NEWEST);
  636. if (verset.empty() == false)
  637. return *(verset.begin());
  638. if (ShowError == true)
  639. ioprintf(out, _("Virtual packages like '%s' can't be removed\n"), Pkg.FullName(true).c_str());
  640. return pkgCache::VerIterator(Cache, 0);
  641. }
  642. APT::VersionSet tryVirtualPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg,
  643. APT::VersionSet::Version const &select) {
  644. /* This is a pure virtual package and there is a single available
  645. candidate providing it. */
  646. if (unlikely(Cache[Pkg].CandidateVer != 0) || Pkg->ProvidesList == 0)
  647. return APT::VersionSet();
  648. pkgCache::PkgIterator Prov;
  649. bool found_one = false;
  650. for (pkgCache::PrvIterator P = Pkg.ProvidesList(); P; ++P) {
  651. pkgCache::VerIterator const PVer = P.OwnerVer();
  652. pkgCache::PkgIterator const PPkg = PVer.ParentPkg();
  653. /* Ignore versions that are not a candidate. */
  654. if (Cache[PPkg].CandidateVer != PVer)
  655. continue;
  656. if (found_one == false) {
  657. Prov = PPkg;
  658. found_one = true;
  659. } else if (PPkg != Prov) {
  660. // same group, so it's a foreign package
  661. if (PPkg->Group == Prov->Group) {
  662. // do we already have the requested arch?
  663. if (strcmp(Pkg.Arch(), Prov.Arch()) == 0 ||
  664. strcmp(Prov.Arch(), "all") == 0 ||
  665. unlikely(strcmp(PPkg.Arch(), Prov.Arch()) == 0)) // packages have only on candidate, but just to be sure
  666. continue;
  667. // see which architecture we prefer more and switch to it
  668. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  669. if (std::find(archs.begin(), archs.end(), PPkg.Arch()) < std::find(archs.begin(), archs.end(), Prov.Arch()))
  670. Prov = PPkg;
  671. continue;
  672. }
  673. found_one = false; // we found at least two
  674. break;
  675. }
  676. }
  677. if (found_one == true) {
  678. ioprintf(out, _("Note, selecting '%s' instead of '%s'\n"),
  679. Prov.FullName(true).c_str(), Pkg.FullName(true).c_str());
  680. return APT::VersionSet::FromPackage(Cache, Prov, select, *this);
  681. }
  682. return APT::VersionSet();
  683. }
  684. inline bool allPkgNamedExplicitly() const { return explicitlyNamed; }
  685. };
  686. /*}}}*/
  687. // TryToInstall - Mark a package for installation /*{{{*/
  688. struct TryToInstall {
  689. pkgCacheFile* Cache;
  690. pkgProblemResolver* Fix;
  691. bool FixBroken;
  692. unsigned long AutoMarkChanged;
  693. APT::PackageSet doAutoInstallLater;
  694. TryToInstall(pkgCacheFile &Cache, pkgProblemResolver *PM, bool const FixBroken) : Cache(&Cache), Fix(PM),
  695. FixBroken(FixBroken), AutoMarkChanged(0) {};
  696. void operator() (pkgCache::VerIterator const &Ver) {
  697. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  698. Cache->GetDepCache()->SetCandidateVersion(Ver);
  699. pkgDepCache::StateCache &State = (*Cache)[Pkg];
  700. // Handle the no-upgrade case
  701. if (_config->FindB("APT::Get::upgrade",true) == false && Pkg->CurrentVer != 0)
  702. ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
  703. Pkg.FullName(true).c_str());
  704. // Ignore request for install if package would be new
  705. else if (_config->FindB("APT::Get::Only-Upgrade", false) == true && Pkg->CurrentVer == 0)
  706. ioprintf(c1out,_("Skipping %s, it is not installed and only upgrades are requested.\n"),
  707. Pkg.FullName(true).c_str());
  708. else {
  709. if (Fix != NULL) {
  710. Fix->Clear(Pkg);
  711. Fix->Protect(Pkg);
  712. }
  713. Cache->GetDepCache()->MarkInstall(Pkg,false);
  714. if (State.Install() == false) {
  715. if (_config->FindB("APT::Get::ReInstall",false) == true) {
  716. if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
  717. ioprintf(c1out,_("Reinstallation of %s is not possible, it cannot be downloaded.\n"),
  718. Pkg.FullName(true).c_str());
  719. else
  720. Cache->GetDepCache()->SetReInstall(Pkg, true);
  721. } else
  722. ioprintf(c1out,_("%s is already the newest version.\n"),
  723. Pkg.FullName(true).c_str());
  724. }
  725. // Install it with autoinstalling enabled (if we not respect the minial
  726. // required deps or the policy)
  727. if (FixBroken == false)
  728. doAutoInstallLater.insert(Pkg);
  729. }
  730. // see if we need to fix the auto-mark flag
  731. // e.g. apt-get install foo
  732. // where foo is marked automatic
  733. if (State.Install() == false &&
  734. (State.Flags & pkgCache::Flag::Auto) &&
  735. _config->FindB("APT::Get::ReInstall",false) == false &&
  736. _config->FindB("APT::Get::Only-Upgrade",false) == false &&
  737. _config->FindB("APT::Get::Download-Only",false) == false)
  738. {
  739. ioprintf(c1out,_("%s set to manually installed.\n"),
  740. Pkg.FullName(true).c_str());
  741. Cache->GetDepCache()->MarkAuto(Pkg,false);
  742. AutoMarkChanged++;
  743. }
  744. }
  745. bool propergateReleaseCandiateSwitching(std::list<std::pair<pkgCache::VerIterator, std::string> > start, std::ostream &out)
  746. {
  747. for (std::list<std::pair<pkgCache::VerIterator, std::string> >::const_iterator s = start.begin();
  748. s != start.end(); ++s)
  749. Cache->GetDepCache()->SetCandidateVersion(s->first);
  750. bool Success = true;
  751. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
  752. for (std::list<std::pair<pkgCache::VerIterator, std::string> >::const_iterator s = start.begin();
  753. s != start.end(); ++s)
  754. {
  755. Changed.push_back(std::make_pair(s->first, pkgCache::VerIterator(*Cache)));
  756. // We continue here even if it failed to enhance the ShowBroken output
  757. Success &= Cache->GetDepCache()->SetCandidateRelease(s->first, s->second, Changed);
  758. }
  759. for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
  760. c != Changed.end(); ++c)
  761. {
  762. if (c->second.end() == true)
  763. ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
  764. c->first.VerStr(), c->first.RelStr().c_str(), c->first.ParentPkg().FullName(true).c_str());
  765. else if (c->first.ParentPkg()->Group != c->second.ParentPkg()->Group)
  766. {
  767. pkgCache::VerIterator V = (*Cache)[c->first.ParentPkg()].CandidateVerIter(*Cache);
  768. ioprintf(out, _("Selected version '%s' (%s) for '%s' because of '%s'\n"), V.VerStr(),
  769. V.RelStr().c_str(), V.ParentPkg().FullName(true).c_str(), c->second.ParentPkg().FullName(true).c_str());
  770. }
  771. }
  772. return Success;
  773. }
  774. void doAutoInstall() {
  775. for (APT::PackageSet::const_iterator P = doAutoInstallLater.begin();
  776. P != doAutoInstallLater.end(); ++P) {
  777. pkgDepCache::StateCache &State = (*Cache)[P];
  778. if (State.InstBroken() == false && State.InstPolicyBroken() == false)
  779. continue;
  780. Cache->GetDepCache()->MarkInstall(P, true);
  781. }
  782. doAutoInstallLater.clear();
  783. }
  784. };
  785. /*}}}*/
  786. // TryToRemove - Mark a package for removal /*{{{*/
  787. struct TryToRemove {
  788. pkgCacheFile* Cache;
  789. pkgProblemResolver* Fix;
  790. bool PurgePkgs;
  791. TryToRemove(pkgCacheFile &Cache, pkgProblemResolver *PM) : Cache(&Cache), Fix(PM),
  792. PurgePkgs(_config->FindB("APT::Get::Purge", false)) {};
  793. void operator() (pkgCache::VerIterator const &Ver)
  794. {
  795. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  796. if (Fix != NULL)
  797. {
  798. Fix->Clear(Pkg);
  799. Fix->Protect(Pkg);
  800. Fix->Remove(Pkg);
  801. }
  802. if ((Pkg->CurrentVer == 0 && PurgePkgs == false) ||
  803. (PurgePkgs == true && Pkg->CurrentState == pkgCache::State::NotInstalled))
  804. {
  805. ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.FullName(true).c_str());
  806. // MarkInstall refuses to install packages on hold
  807. Pkg->SelectedState = pkgCache::State::Hold;
  808. }
  809. else
  810. Cache->GetDepCache()->MarkDelete(Pkg, PurgePkgs);
  811. }
  812. };
  813. /*}}}*/
  814. // CacheFile::NameComp - QSort compare by name /*{{{*/
  815. // ---------------------------------------------------------------------
  816. /* */
  817. pkgCache *CacheFile::SortCache = 0;
  818. int CacheFile::NameComp(const void *a,const void *b)
  819. {
  820. if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
  821. return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
  822. const pkgCache::Package &A = **(pkgCache::Package **)a;
  823. const pkgCache::Package &B = **(pkgCache::Package **)b;
  824. return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
  825. }
  826. /*}}}*/
  827. // CacheFile::Sort - Sort by name /*{{{*/
  828. // ---------------------------------------------------------------------
  829. /* */
  830. void CacheFile::Sort()
  831. {
  832. delete [] List;
  833. List = new pkgCache::Package *[Cache->Head().PackageCount];
  834. memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
  835. pkgCache::PkgIterator I = Cache->PkgBegin();
  836. for (;I.end() != true; ++I)
  837. List[I->ID] = I;
  838. SortCache = *this;
  839. qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
  840. }
  841. /*}}}*/
  842. // CacheFile::CheckDeps - Open the cache file /*{{{*/
  843. // ---------------------------------------------------------------------
  844. /* This routine generates the caches and then opens the dependency cache
  845. and verifies that the system is OK. */
  846. bool CacheFile::CheckDeps(bool AllowBroken)
  847. {
  848. bool FixBroken = _config->FindB("APT::Get::Fix-Broken",false);
  849. if (_error->PendingError() == true)
  850. return false;
  851. // Check that the system is OK
  852. if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
  853. return _error->Error("Internal error, non-zero counts");
  854. // Apply corrections for half-installed packages
  855. if (pkgApplyStatus(*DCache) == false)
  856. return false;
  857. if (_config->FindB("APT::Get::Fix-Policy-Broken",false) == true)
  858. {
  859. FixBroken = true;
  860. if ((DCache->PolicyBrokenCount() > 0))
  861. {
  862. // upgrade all policy-broken packages with ForceImportantDeps=True
  863. for (pkgCache::PkgIterator I = Cache->PkgBegin(); !I.end(); ++I)
  864. if ((*DCache)[I].NowPolicyBroken() == true)
  865. DCache->MarkInstall(I,true,0, false, true);
  866. }
  867. }
  868. // Nothing is broken
  869. if (DCache->BrokenCount() == 0 || AllowBroken == true)
  870. return true;
  871. // Attempt to fix broken things
  872. if (FixBroken == true)
  873. {
  874. c1out << _("Correcting dependencies...") << flush;
  875. if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
  876. {
  877. c1out << _(" failed.") << endl;
  878. ShowBroken(c1out,*this,true);
  879. return _error->Error(_("Unable to correct dependencies"));
  880. }
  881. if (pkgMinimizeUpgrade(*DCache) == false)
  882. return _error->Error(_("Unable to minimize the upgrade set"));
  883. c1out << _(" Done") << endl;
  884. }
  885. else
  886. {
  887. c1out << _("You might want to run 'apt-get -f install' to correct these.") << endl;
  888. ShowBroken(c1out,*this,true);
  889. return _error->Error(_("Unmet dependencies. Try using -f."));
  890. }
  891. return true;
  892. }
  893. /*}}}*/
  894. // CheckAuth - check if each download comes form a trusted source /*{{{*/
  895. // ---------------------------------------------------------------------
  896. /* */
  897. static bool CheckAuth(pkgAcquire& Fetcher)
  898. {
  899. string UntrustedList;
  900. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
  901. {
  902. if (!(*I)->IsTrusted())
  903. {
  904. UntrustedList += string((*I)->ShortDesc()) + " ";
  905. }
  906. }
  907. if (UntrustedList == "")
  908. {
  909. return true;
  910. }
  911. ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"),UntrustedList,"");
  912. if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
  913. {
  914. c2out << _("Authentication warning overridden.\n");
  915. return true;
  916. }
  917. if (_config->FindI("quiet",0) < 2
  918. && _config->FindB("APT::Get::Assume-Yes",false) == false)
  919. {
  920. c2out << _("Install these packages without verification [y/N]? ") << flush;
  921. if (!YnPrompt(false))
  922. return _error->Error(_("Some packages could not be authenticated"));
  923. return true;
  924. }
  925. else if (_config->FindB("APT::Get::Force-Yes",false) == true)
  926. {
  927. return true;
  928. }
  929. return _error->Error(_("There are problems and -y was used without --force-yes"));
  930. }
  931. /*}}}*/
  932. // InstallPackages - Actually download and install the packages /*{{{*/
  933. // ---------------------------------------------------------------------
  934. /* This displays the informative messages describing what is going to
  935. happen and then calls the download routines */
  936. bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
  937. bool Safety = true)
  938. {
  939. if (_config->FindB("APT::Get::Purge",false) == true)
  940. {
  941. pkgCache::PkgIterator I = Cache->PkgBegin();
  942. for (; I.end() == false; ++I)
  943. {
  944. if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
  945. Cache->MarkDelete(I,true);
  946. }
  947. }
  948. bool Fail = false;
  949. bool Essential = false;
  950. // Show all the various warning indicators
  951. ShowDel(c1out,Cache);
  952. ShowNew(c1out,Cache);
  953. if (ShwKept == true)
  954. ShowKept(c1out,Cache);
  955. Fail |= !ShowHold(c1out,Cache);
  956. if (_config->FindB("APT::Get::Show-Upgraded",true) == true)
  957. ShowUpgraded(c1out,Cache);
  958. Fail |= !ShowDowngraded(c1out,Cache);
  959. if (_config->FindB("APT::Get::Download-Only",false) == false)
  960. Essential = !ShowEssential(c1out,Cache);
  961. Fail |= Essential;
  962. Stats(c1out,Cache);
  963. // Sanity check
  964. if (Cache->BrokenCount() != 0)
  965. {
  966. ShowBroken(c1out,Cache,false);
  967. return _error->Error(_("Internal error, InstallPackages was called with broken packages!"));
  968. }
  969. if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
  970. Cache->BadCount() == 0)
  971. return true;
  972. // No remove flag
  973. if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
  974. return _error->Error(_("Packages need to be removed but remove is disabled."));
  975. // Run the simulator ..
  976. if (_config->FindB("APT::Get::Simulate") == true)
  977. {
  978. pkgSimulate PM(Cache);
  979. int status_fd = _config->FindI("APT::Status-Fd",-1);
  980. pkgPackageManager::OrderResult Res = PM.DoInstall(status_fd);
  981. if (Res == pkgPackageManager::Failed)
  982. return false;
  983. if (Res != pkgPackageManager::Completed)
  984. return _error->Error(_("Internal error, Ordering didn't finish"));
  985. return true;
  986. }
  987. // Create the text record parser
  988. pkgRecords Recs(Cache);
  989. if (_error->PendingError() == true)
  990. return false;
  991. // Create the download object
  992. pkgAcquire Fetcher;
  993. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  994. if (_config->FindB("APT::Get::Print-URIs", false) == true)
  995. {
  996. // force a hashsum for compatibility reasons
  997. _config->CndSet("Acquire::ForceHash", "md5sum");
  998. }
  999. else if (Fetcher.Setup(&Stat, _config->FindDir("Dir::Cache::Archives")) == false)
  1000. return false;
  1001. // Read the source list
  1002. if (Cache.BuildSourceList() == false)
  1003. return false;
  1004. pkgSourceList *List = Cache.GetSourceList();
  1005. // Create the package manager and prepare to download
  1006. SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
  1007. if (PM->GetArchives(&Fetcher,List,&Recs) == false ||
  1008. _error->PendingError() == true)
  1009. return false;
  1010. // Display statistics
  1011. unsigned long long FetchBytes = Fetcher.FetchNeeded();
  1012. unsigned long long FetchPBytes = Fetcher.PartialPresent();
  1013. unsigned long long DebBytes = Fetcher.TotalNeeded();
  1014. if (DebBytes != Cache->DebSize())
  1015. {
  1016. c0out << DebBytes << ',' << Cache->DebSize() << endl;
  1017. c0out << _("How odd.. The sizes didn't match, email apt@packages.debian.org") << endl;
  1018. }
  1019. // Number of bytes
  1020. if (DebBytes != FetchBytes)
  1021. //TRANSLATOR: The required space between number and unit is already included
  1022. // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
  1023. ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"),
  1024. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  1025. else if (DebBytes != 0)
  1026. //TRANSLATOR: The required space between number and unit is already included
  1027. // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
  1028. ioprintf(c1out,_("Need to get %sB of archives.\n"),
  1029. SizeToStr(DebBytes).c_str());
  1030. // Size delta
  1031. if (Cache->UsrSize() >= 0)
  1032. //TRANSLATOR: The required space between number and unit is already included
  1033. // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
  1034. ioprintf(c1out,_("After this operation, %sB of additional disk space will be used.\n"),
  1035. SizeToStr(Cache->UsrSize()).c_str());
  1036. else
  1037. //TRANSLATOR: The required space between number and unit is already included
  1038. // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
  1039. ioprintf(c1out,_("After this operation, %sB disk space will be freed.\n"),
  1040. SizeToStr(-1*Cache->UsrSize()).c_str());
  1041. if (_error->PendingError() == true)
  1042. return false;
  1043. /* Check for enough free space, but only if we are actually going to
  1044. download */
  1045. if (_config->FindB("APT::Get::Print-URIs") == false &&
  1046. _config->FindB("APT::Get::Download",true) == true)
  1047. {
  1048. struct statvfs Buf;
  1049. string OutputDir = _config->FindDir("Dir::Cache::Archives");
  1050. if (statvfs(OutputDir.c_str(),&Buf) != 0) {
  1051. if (errno == EOVERFLOW)
  1052. return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
  1053. OutputDir.c_str());
  1054. else
  1055. return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
  1056. OutputDir.c_str());
  1057. } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  1058. {
  1059. struct statfs Stat;
  1060. if (statfs(OutputDir.c_str(),&Stat) != 0
  1061. #if HAVE_STRUCT_STATFS_F_TYPE
  1062. || unsigned(Stat.f_type) != RAMFS_MAGIC
  1063. #endif
  1064. )
  1065. return _error->Error(_("You don't have enough free space in %s."),
  1066. OutputDir.c_str());
  1067. }
  1068. }
  1069. // Fail safe check
  1070. if (_config->FindI("quiet",0) >= 2 ||
  1071. _config->FindB("APT::Get::Assume-Yes",false) == true)
  1072. {
  1073. if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
  1074. return _error->Error(_("There are problems and -y was used without --force-yes"));
  1075. }
  1076. if (Essential == true && Safety == true)
  1077. {
  1078. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  1079. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  1080. const char *Prompt = _("Yes, do as I say!");
  1081. ioprintf(c2out,
  1082. _("You are about to do something potentially harmful.\n"
  1083. "To continue type in the phrase '%s'\n"
  1084. " ?] "),Prompt);
  1085. c2out << flush;
  1086. if (AnalPrompt(Prompt) == false)
  1087. {
  1088. c2out << _("Abort.") << endl;
  1089. exit(1);
  1090. }
  1091. }
  1092. else
  1093. {
  1094. // Prompt to continue
  1095. if (Ask == true || Fail == true)
  1096. {
  1097. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  1098. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  1099. if (_config->FindI("quiet",0) < 2 &&
  1100. _config->FindB("APT::Get::Assume-Yes",false) == false)
  1101. {
  1102. c2out << _("Do you want to continue [Y/n]? ") << flush;
  1103. if (YnPrompt() == false)
  1104. {
  1105. c2out << _("Abort.") << endl;
  1106. exit(1);
  1107. }
  1108. }
  1109. }
  1110. }
  1111. // Just print out the uris an exit if the --print-uris flag was used
  1112. if (_config->FindB("APT::Get::Print-URIs") == true)
  1113. {
  1114. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  1115. for (; I != Fetcher.UriEnd(); ++I)
  1116. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  1117. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  1118. return true;
  1119. }
  1120. if (!CheckAuth(Fetcher))
  1121. return false;
  1122. /* Unlock the dpkg lock if we are not going to be doing an install
  1123. after. */
  1124. if (_config->FindB("APT::Get::Download-Only",false) == true)
  1125. _system->UnLock();
  1126. // Run it
  1127. while (1)
  1128. {
  1129. bool Transient = false;
  1130. if (_config->FindB("APT::Get::Download",true) == false)
  1131. {
  1132. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
  1133. {
  1134. if ((*I)->Local == true)
  1135. {
  1136. ++I;
  1137. continue;
  1138. }
  1139. // Close the item and check if it was found in cache
  1140. (*I)->Finished();
  1141. if ((*I)->Complete == false)
  1142. Transient = true;
  1143. // Clear it out of the fetch list
  1144. delete *I;
  1145. I = Fetcher.ItemsBegin();
  1146. }
  1147. }
  1148. if (Fetcher.Run() == pkgAcquire::Failed)
  1149. return false;
  1150. // Print out errors
  1151. bool Failed = false;
  1152. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
  1153. {
  1154. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  1155. (*I)->Complete == true)
  1156. continue;
  1157. if ((*I)->Status == pkgAcquire::Item::StatIdle)
  1158. {
  1159. Transient = true;
  1160. // Failed = true;
  1161. continue;
  1162. }
  1163. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  1164. (*I)->ErrorText.c_str());
  1165. Failed = true;
  1166. }
  1167. /* If we are in no download mode and missing files and there were
  1168. 'failures' then the user must specify -m. Furthermore, there
  1169. is no such thing as a transient error in no-download mode! */
  1170. if (Transient == true &&
  1171. _config->FindB("APT::Get::Download",true) == false)
  1172. {
  1173. Transient = false;
  1174. Failed = true;
  1175. }
  1176. if (_config->FindB("APT::Get::Download-Only",false) == true)
  1177. {
  1178. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  1179. return _error->Error(_("Some files failed to download"));
  1180. c1out << _("Download complete and in download only mode") << endl;
  1181. return true;
  1182. }
  1183. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  1184. {
  1185. return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
  1186. }
  1187. if (Transient == true && Failed == true)
  1188. return _error->Error(_("--fix-missing and media swapping is not currently supported"));
  1189. // Try to deal with missing package files
  1190. if (Failed == true && PM->FixMissing() == false)
  1191. {
  1192. cerr << _("Unable to correct missing packages.") << endl;
  1193. return _error->Error(_("Aborting install."));
  1194. }
  1195. _system->UnLock();
  1196. int status_fd = _config->FindI("APT::Status-Fd",-1);
  1197. pkgPackageManager::OrderResult Res = PM->DoInstall(status_fd);
  1198. if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
  1199. return false;
  1200. if (Res == pkgPackageManager::Completed)
  1201. break;
  1202. // Reload the fetcher object and loop again for media swapping
  1203. Fetcher.Shutdown();
  1204. if (PM->GetArchives(&Fetcher,List,&Recs) == false)
  1205. return false;
  1206. _system->Lock();
  1207. }
  1208. std::set<std::string> const disappearedPkgs = PM->GetDisappearedPackages();
  1209. if (disappearedPkgs.empty() == true)
  1210. return true;
  1211. string disappear;
  1212. for (std::set<std::string>::const_iterator d = disappearedPkgs.begin();
  1213. d != disappearedPkgs.end(); ++d)
  1214. disappear.append(*d).append(" ");
  1215. ShowList(c1out, P_("The following package disappeared from your system as\n"
  1216. "all files have been overwritten by other packages:",
  1217. "The following packages disappeared from your system as\n"
  1218. "all files have been overwritten by other packages:", disappearedPkgs.size()), disappear, "");
  1219. c0out << _("Note: This is done automatic and on purpose by dpkg.") << std::endl;
  1220. return true;
  1221. }
  1222. /*}}}*/
  1223. // TryToInstallBuildDep - Try to install a single package /*{{{*/
  1224. // ---------------------------------------------------------------------
  1225. /* This used to be inlined in DoInstall, but with the advent of regex package
  1226. name matching it was split out.. */
  1227. bool TryToInstallBuildDep(pkgCache::PkgIterator Pkg,pkgCacheFile &Cache,
  1228. pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
  1229. bool AllowFail = true)
  1230. {
  1231. if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0)
  1232. {
  1233. CacheSetHelperAPTGet helper(c1out);
  1234. helper.showErrors(AllowFail == false);
  1235. pkgCache::VerIterator Ver = helper.canNotFindNewestVer(Cache, Pkg);
  1236. if (Ver.end() == false)
  1237. Pkg = Ver.ParentPkg();
  1238. else if (helper.showVirtualPackageErrors(Cache) == false)
  1239. return AllowFail;
  1240. }
  1241. if (_config->FindB("Debug::BuildDeps",false) == true)
  1242. {
  1243. if (Remove == true)
  1244. cout << " Trying to remove " << Pkg << endl;
  1245. else
  1246. cout << " Trying to install " << Pkg << endl;
  1247. }
  1248. if (Remove == true)
  1249. {
  1250. TryToRemove RemoveAction(Cache, &Fix);
  1251. RemoveAction(Pkg.VersionList());
  1252. } else if (Cache[Pkg].CandidateVer != 0) {
  1253. TryToInstall InstallAction(Cache, &Fix, BrokenFix);
  1254. InstallAction(Cache[Pkg].CandidateVerIter(Cache));
  1255. InstallAction.doAutoInstall();
  1256. } else
  1257. return AllowFail;
  1258. return true;
  1259. }
  1260. /*}}}*/
  1261. // FindSrc - Find a source record /*{{{*/
  1262. // ---------------------------------------------------------------------
  1263. /* */
  1264. pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
  1265. pkgSrcRecords &SrcRecs,string &Src,
  1266. pkgDepCache &Cache)
  1267. {
  1268. string VerTag;
  1269. string DefRel = _config->Find("APT::Default-Release");
  1270. string TmpSrc = Name;
  1271. // extract the version/release from the pkgname
  1272. const size_t found = TmpSrc.find_last_of("/=");
  1273. if (found != string::npos) {
  1274. if (TmpSrc[found] == '/')
  1275. DefRel = TmpSrc.substr(found+1);
  1276. else
  1277. VerTag = TmpSrc.substr(found+1);
  1278. TmpSrc = TmpSrc.substr(0,found);
  1279. }
  1280. /* Lookup the version of the package we would install if we were to
  1281. install a version and determine the source package name, then look
  1282. in the archive for a source package of the same name. */
  1283. bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
  1284. const pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
  1285. if (MatchSrcOnly == false && Pkg.end() == false)
  1286. {
  1287. if(VerTag.empty() == false || DefRel.empty() == false)
  1288. {
  1289. bool fuzzy = false;
  1290. // we have a default release, try to locate the pkg. we do it like
  1291. // this because GetCandidateVer() will not "downgrade", that means
  1292. // "apt-get source -t stable apt" won't work on a unstable system
  1293. for (pkgCache::VerIterator Ver = Pkg.VersionList();; ++Ver)
  1294. {
  1295. // try first only exact matches, later fuzzy matches
  1296. if (Ver.end() == true)
  1297. {
  1298. if (fuzzy == true)
  1299. break;
  1300. fuzzy = true;
  1301. Ver = Pkg.VersionList();
  1302. // exit right away from the Pkg.VersionList() loop if we
  1303. // don't have any versions
  1304. if (Ver.end() == true)
  1305. break;
  1306. }
  1307. // We match against a concrete version (or a part of this version)
  1308. if (VerTag.empty() == false &&
  1309. (fuzzy == true || Cache.VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
  1310. (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
  1311. continue;
  1312. for (pkgCache::VerFileIterator VF = Ver.FileList();
  1313. VF.end() == false; ++VF)
  1314. {
  1315. /* If this is the status file, and the current version is not the
  1316. version in the status file (ie it is not installed, or somesuch)
  1317. then it is not a candidate for installation, ever. This weeds
  1318. out bogus entries that may be due to config-file states, or
  1319. other. */
  1320. if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
  1321. pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
  1322. continue;
  1323. // or we match against a release
  1324. if(VerTag.empty() == false ||
  1325. (VF.File().Archive() != 0 && VF.File().Archive() == DefRel) ||
  1326. (VF.File().Codename() != 0 && VF.File().Codename() == DefRel))
  1327. {
  1328. pkgRecords::Parser &Parse = Recs.Lookup(VF);
  1329. Src = Parse.SourcePkg();
  1330. // no SourcePkg name, so it is the "binary" name
  1331. if (Src.empty() == true)
  1332. Src = TmpSrc;
  1333. // the Version we have is possibly fuzzy or includes binUploads,
  1334. // so we use the Version of the SourcePkg (empty if same as package)
  1335. VerTag = Parse.SourceVer();
  1336. if (VerTag.empty() == true)
  1337. VerTag = Ver.VerStr();
  1338. break;
  1339. }
  1340. }
  1341. if (Src.empty() == false)
  1342. break;
  1343. }
  1344. if (Src.empty() == true)
  1345. {
  1346. // Sources files have no codename information
  1347. if (VerTag.empty() == true && DefRel.empty() == false)
  1348. {
  1349. _error->Error(_("Ignore unavailable target release '%s' of package '%s'"), DefRel.c_str(), TmpSrc.c_str());
  1350. return 0;
  1351. }
  1352. }
  1353. }
  1354. if (Src.empty() == true)
  1355. {
  1356. // if we don't have found a fitting package yet so we will
  1357. // choose a good candidate and proceed with that.
  1358. // Maybe we will find a source later on with the right VerTag
  1359. pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
  1360. if (Ver.end() == false)
  1361. {
  1362. pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
  1363. Src = Parse.SourcePkg();
  1364. if (VerTag.empty() == true)
  1365. VerTag = Parse.SourceVer();
  1366. }
  1367. }
  1368. }
  1369. if (Src.empty() == true)
  1370. Src = TmpSrc;
  1371. else
  1372. {
  1373. /* if we have a source pkg name, make sure to only search
  1374. for srcpkg names, otherwise apt gets confused if there
  1375. is a binary package "pkg1" and a source package "pkg1"
  1376. with the same name but that comes from different packages */
  1377. MatchSrcOnly = true;
  1378. if (Src != TmpSrc)
  1379. {
  1380. ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
  1381. }
  1382. }
  1383. // The best hit
  1384. pkgSrcRecords::Parser *Last = 0;
  1385. unsigned long Offset = 0;
  1386. string Version;
  1387. /* Iterate over all of the hits, which includes the resulting
  1388. binary packages in the search */
  1389. pkgSrcRecords::Parser *Parse;
  1390. while (true)
  1391. {
  1392. SrcRecs.Restart();
  1393. while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
  1394. {
  1395. const string Ver = Parse->Version();
  1396. // Ignore all versions which doesn't fit
  1397. if (VerTag.empty() == false &&
  1398. Cache.VS().CmpVersion(VerTag, Ver) != 0) // exact match
  1399. continue;
  1400. // Newer version or an exact match? Save the hit
  1401. if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0) {
  1402. Last = Parse;
  1403. Offset = Parse->Offset();
  1404. Version = Ver;
  1405. }
  1406. // was the version check above an exact match? If so, we don't need to look further
  1407. if (VerTag.empty() == false && VerTag.size() == Ver.size())
  1408. break;
  1409. }
  1410. if (Last != 0 || VerTag.empty() == true)
  1411. break;
  1412. //if (VerTag.empty() == false && Last == 0)
  1413. _error->Error(_("Ignore unavailable version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
  1414. return 0;
  1415. }
  1416. if (Last == 0 || Last->Jump(Offset) == false)
  1417. return 0;
  1418. return Last;
  1419. }
  1420. /*}}}*/
  1421. // DoUpdate - Update the package lists /*{{{*/
  1422. // ---------------------------------------------------------------------
  1423. /* */
  1424. bool DoUpdate(CommandLine &CmdL)
  1425. {
  1426. if (CmdL.FileSize() != 1)
  1427. return _error->Error(_("The update command takes no arguments"));
  1428. CacheFile Cache;
  1429. // Get the source list
  1430. if (Cache.BuildSourceList() == false)
  1431. return false;
  1432. pkgSourceList *List = Cache.GetSourceList();
  1433. // Create the progress
  1434. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1435. // Just print out the uris an exit if the --print-uris flag was used
  1436. if (_config->FindB("APT::Get::Print-URIs") == true)
  1437. {
  1438. // force a hashsum for compatibility reasons
  1439. _config->CndSet("Acquire::ForceHash", "md5sum");
  1440. // get a fetcher
  1441. pkgAcquire Fetcher;
  1442. if (Fetcher.Setup(&Stat) == false)
  1443. return false;
  1444. // Populate it with the source selection and get all Indexes
  1445. // (GetAll=true)
  1446. if (List->GetIndexes(&Fetcher,true) == false)
  1447. return false;
  1448. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  1449. for (; I != Fetcher.UriEnd(); ++I)
  1450. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  1451. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  1452. return true;
  1453. }
  1454. // do the work
  1455. if (_config->FindB("APT::Get::Download",true) == true)
  1456. ListUpdate(Stat, *List);
  1457. // Rebuild the cache.
  1458. pkgCacheFile::RemoveCaches();
  1459. if (Cache.BuildCaches() == false)
  1460. return false;
  1461. return true;
  1462. }
  1463. /*}}}*/
  1464. // DoAutomaticRemove - Remove all automatic unused packages /*{{{*/
  1465. // ---------------------------------------------------------------------
  1466. /* Remove unused automatic packages */
  1467. bool DoAutomaticRemove(CacheFile &Cache)
  1468. {
  1469. bool Debug = _config->FindI("Debug::pkgAutoRemove",false);
  1470. bool doAutoRemove = _config->FindB("APT::Get::AutomaticRemove", false);
  1471. bool hideAutoRemove = _config->FindB("APT::Get::HideAutoRemove");
  1472. pkgDepCache::ActionGroup group(*Cache);
  1473. if(Debug)
  1474. std::cout << "DoAutomaticRemove()" << std::endl;
  1475. if (doAutoRemove == true &&
  1476. _config->FindB("APT::Get::Remove",true) == false)
  1477. {
  1478. c1out << _("We are not supposed to delete stuff, can't start "
  1479. "AutoRemover") << std::endl;
  1480. return false;
  1481. }
  1482. bool purgePkgs = _config->FindB("APT::Get::Purge", false);
  1483. bool smallList = (hideAutoRemove == false &&
  1484. strcasecmp(_config->Find("APT::Get::HideAutoRemove","").c_str(),"small") == 0);
  1485. string autoremovelist, autoremoveversions;
  1486. unsigned long autoRemoveCount = 0;
  1487. APT::PackageSet tooMuch;
  1488. // look over the cache to see what can be removed
  1489. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); ! Pkg.end(); ++Pkg)
  1490. {
  1491. if (Cache[Pkg].Garbage)
  1492. {
  1493. if(Pkg.CurrentVer() != 0 || Cache[Pkg].Install())
  1494. if(Debug)
  1495. std::cout << "We could delete %s" << Pkg.FullName(true).c_str() << std::endl;
  1496. if (doAutoRemove)
  1497. {
  1498. if(Pkg.CurrentVer() != 0 &&
  1499. Pkg->CurrentState != pkgCache::State::ConfigFiles)
  1500. Cache->MarkDelete(Pkg, purgePkgs);
  1501. else
  1502. Cache->MarkKeep(Pkg, false, false);
  1503. }
  1504. else
  1505. {
  1506. // if the package is a new install and already garbage we don't need to
  1507. // install it in the first place, so nuke it instead of show it
  1508. if (Cache[Pkg].Install() == true && Pkg.CurrentVer() == 0)
  1509. {
  1510. if (Pkg.CandVersion() != 0)
  1511. tooMuch.insert(Pkg);
  1512. Cache->MarkDelete(Pkg, false);
  1513. }
  1514. // only show stuff in the list that is not yet marked for removal
  1515. else if(hideAutoRemove == false && Cache[Pkg].Delete() == false)
  1516. {
  1517. ++autoRemoveCount;
  1518. // we don't need to fill the strings if we don't need them
  1519. if (smallList == false)
  1520. {
  1521. autoremovelist += Pkg.FullName(true) + " ";
  1522. autoremoveversions += string(Cache[Pkg].CandVersion) + "\n";
  1523. }
  1524. }
  1525. }
  1526. }
  1527. }
  1528. // we could have removed a new dependency of a garbage package,
  1529. // so check if a reverse depends is broken and if so install it again.
  1530. if (tooMuch.empty() == false && (Cache->BrokenCount() != 0 || Cache->PolicyBrokenCount() != 0))
  1531. {
  1532. bool Changed;
  1533. do {
  1534. Changed = false;
  1535. for (APT::PackageSet::const_iterator Pkg = tooMuch.begin();
  1536. Pkg != tooMuch.end() && Changed == false; ++Pkg)
  1537. {
  1538. APT::PackageSet too;
  1539. too.insert(*Pkg);
  1540. for (pkgCache::PrvIterator Prv = Cache[Pkg].CandidateVerIter(Cache).ProvidesList();
  1541. Prv.end() == false; ++Prv)
  1542. too.insert(Prv.ParentPkg());
  1543. for (APT::PackageSet::const_iterator P = too.begin();
  1544. P != too.end() && Changed == false; ++P) {
  1545. for (pkgCache::DepIterator R = P.RevDependsList();
  1546. R.end() == false; ++R)
  1547. {
  1548. if (R.IsNegative() == true ||
  1549. Cache->IsImportantDep(R) == false)
  1550. continue;
  1551. pkgCache::PkgIterator N = R.ParentPkg();
  1552. if (N.end() == true || (N->CurrentVer == 0 && (*Cache)[N].Install() == false))
  1553. continue;
  1554. if (Debug == true)
  1555. std::clog << "Save " << Pkg << " as another installed garbage package depends on it" << std::endl;
  1556. Cache->MarkInstall(Pkg, false);
  1557. if (hideAutoRemove == false)
  1558. {
  1559. ++autoRemoveCount;
  1560. if (smallList == false)
  1561. {
  1562. autoremovelist += Pkg.FullName(true) + " ";
  1563. autoremoveversions += string(Cache[Pkg].CandVersion) + "\n";
  1564. }
  1565. }
  1566. tooMuch.erase(Pkg);
  1567. Changed = true;
  1568. break;
  1569. }
  1570. }
  1571. }
  1572. } while (Changed == true);
  1573. }
  1574. // Now see if we had destroyed anything (if we had done anything)
  1575. if (Cache->BrokenCount() != 0)
  1576. {
  1577. c1out << _("Hmm, seems like the AutoRemover destroyed something which really\n"
  1578. "shouldn't happen. Please file a bug report against apt.") << endl;
  1579. c1out << endl;
  1580. c1out << _("The following information may help to resolve the situation:") << endl;
  1581. c1out << endl;
  1582. ShowBroken(c1out,Cache,false);
  1583. return _error->Error(_("Internal Error, AutoRemover broke stuff"));
  1584. }
  1585. // if we don't remove them, we should show them!
  1586. if (doAutoRemove == false && (autoremovelist.empty() == false || autoRemoveCount != 0))
  1587. {
  1588. if (smallList == false)
  1589. ShowList(c1out, P_("The following package was automatically installed and is no longer required:",
  1590. "The following packages were automatically installed and are no longer required:",
  1591. autoRemoveCount), autoremovelist, autoremoveversions);
  1592. else
  1593. ioprintf(c1out, P_("%lu package was automatically installed and is no longer required.\n",
  1594. "%lu packages were automatically installed and are no longer required.\n", autoRemoveCount), autoRemoveCount);
  1595. c1out << _("Use 'apt-get autoremove' to remove them.") << std::endl;
  1596. }
  1597. return true;
  1598. }
  1599. /*}}}*/
  1600. // DoUpgrade - Upgrade all packages /*{{{*/
  1601. // ---------------------------------------------------------------------
  1602. /* Upgrade all packages without installing new packages or erasing old
  1603. packages */
  1604. bool DoUpgrade(CommandLine &CmdL)
  1605. {
  1606. CacheFile Cache;
  1607. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1608. return false;
  1609. // Do the upgrade
  1610. if (pkgAllUpgrade(Cache) == false)
  1611. {
  1612. ShowBroken(c1out,Cache,false);
  1613. return _error->Error(_("Internal error, AllUpgrade broke stuff"));
  1614. }
  1615. return InstallPackages(Cache,true);
  1616. }
  1617. /*}}}*/
  1618. // DoInstall - Install packages from the command line /*{{{*/
  1619. // ---------------------------------------------------------------------
  1620. /* Install named packages */
  1621. bool DoInstall(CommandLine &CmdL)
  1622. {
  1623. CacheFile Cache;
  1624. if (Cache.OpenForInstall() == false ||
  1625. Cache.CheckDeps(CmdL.FileSize() != 1) == false)
  1626. return false;
  1627. // Enter the special broken fixing mode if the user specified arguments
  1628. bool BrokenFix = false;
  1629. if (Cache->BrokenCount() != 0)
  1630. BrokenFix = true;
  1631. pkgProblemResolver* Fix = NULL;
  1632. if (_config->FindB("APT::Get::CallResolver", true) == true)
  1633. Fix = new pkgProblemResolver(Cache);
  1634. static const unsigned short MOD_REMOVE = 1;
  1635. static const unsigned short MOD_INSTALL = 2;
  1636. unsigned short fallback = MOD_INSTALL;
  1637. if (strcasecmp(CmdL.FileList[0],"remove") == 0)
  1638. fallback = MOD_REMOVE;
  1639. else if (strcasecmp(CmdL.FileList[0], "purge") == 0)
  1640. {
  1641. _config->Set("APT::Get::Purge", true);
  1642. fallback = MOD_REMOVE;
  1643. }
  1644. else if (strcasecmp(CmdL.FileList[0], "autoremove") == 0)
  1645. {
  1646. _config->Set("APT::Get::AutomaticRemove", "true");
  1647. fallback = MOD_REMOVE;
  1648. }
  1649. std::list<APT::VersionSet::Modifier> mods;
  1650. mods.push_back(APT::VersionSet::Modifier(MOD_INSTALL, "+",
  1651. APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::CANDIDATE));
  1652. mods.push_back(APT::VersionSet::Modifier(MOD_REMOVE, "-",
  1653. APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::NEWEST));
  1654. CacheSetHelperAPTGet helper(c0out);
  1655. std::map<unsigned short, APT::VersionSet> verset = APT::VersionSet::GroupedFromCommandLine(Cache,
  1656. CmdL.FileList + 1, mods, fallback, helper);
  1657. if (_error->PendingError() == true)
  1658. {
  1659. helper.showVirtualPackageErrors(Cache);
  1660. if (Fix != NULL)
  1661. delete Fix;
  1662. return false;
  1663. }
  1664. unsigned short const order[] = { MOD_REMOVE, MOD_INSTALL, 0 };
  1665. TryToInstall InstallAction(Cache, Fix, BrokenFix);
  1666. TryToRemove RemoveAction(Cache, Fix);
  1667. // new scope for the ActionGroup
  1668. {
  1669. pkgDepCache::ActionGroup group(Cache);
  1670. for (unsigned short i = 0; order[i] != 0; ++i)
  1671. {
  1672. if (order[i] == MOD_INSTALL)
  1673. InstallAction = std::for_each(verset[MOD_INSTALL].begin(), verset[MOD_INSTALL].end(), InstallAction);
  1674. else if (order[i] == MOD_REMOVE)
  1675. RemoveAction = std::for_each(verset[MOD_REMOVE].begin(), verset[MOD_REMOVE].end(), RemoveAction);
  1676. }
  1677. if (Fix != NULL && _config->FindB("APT::Get::AutoSolving", true) == true)
  1678. {
  1679. for (unsigned short i = 0; order[i] != 0; ++i)
  1680. {
  1681. if (order[i] != MOD_INSTALL)
  1682. continue;
  1683. InstallAction.propergateReleaseCandiateSwitching(helper.selectedByRelease, c0out);
  1684. InstallAction.doAutoInstall();
  1685. }
  1686. }
  1687. if (_error->PendingError() == true)
  1688. {
  1689. if (Fix != NULL)
  1690. delete Fix;
  1691. return false;
  1692. }
  1693. /* If we are in the Broken fixing mode we do not attempt to fix the
  1694. problems. This is if the user invoked install without -f and gave
  1695. packages */
  1696. if (BrokenFix == true && Cache->BrokenCount() != 0)
  1697. {
  1698. c1out << _("You might want to run 'apt-get -f install' to correct these:") << endl;
  1699. ShowBroken(c1out,Cache,false);
  1700. if (Fix != NULL)
  1701. delete Fix;
  1702. return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
  1703. }
  1704. if (Fix != NULL)
  1705. {
  1706. // Call the scored problem resolver
  1707. Fix->InstallProtect();
  1708. Fix->Resolve(true);
  1709. delete Fix;
  1710. }
  1711. // Now we check the state of the packages,
  1712. if (Cache->BrokenCount() != 0)
  1713. {
  1714. c1out <<
  1715. _("Some packages could not be installed. This may mean that you have\n"
  1716. "requested an impossible situation or if you are using the unstable\n"
  1717. "distribution that some required packages have not yet been created\n"
  1718. "or been moved out of Incoming.") << endl;
  1719. /*
  1720. if (Packages == 1)
  1721. {
  1722. c1out << endl;
  1723. c1out <<
  1724. _("Since you only requested a single operation it is extremely likely that\n"
  1725. "the package is simply not installable and a bug report against\n"
  1726. "that package should be filed.") << endl;
  1727. }
  1728. */
  1729. c1out << _("The following information may help to resolve the situation:") << endl;
  1730. c1out << endl;
  1731. ShowBroken(c1out,Cache,false);
  1732. if (_error->PendingError() == true)
  1733. return false;
  1734. else
  1735. return _error->Error(_("Broken packages"));
  1736. }
  1737. }
  1738. if (!DoAutomaticRemove(Cache))
  1739. return false;
  1740. /* Print out a list of packages that are going to be installed extra
  1741. to what the user asked */
  1742. if (Cache->InstCount() != verset[MOD_INSTALL].size())
  1743. {
  1744. string List;
  1745. string VersionsList;
  1746. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  1747. {
  1748. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  1749. if ((*Cache)[I].Install() == false)
  1750. continue;
  1751. pkgCache::VerIterator Cand = Cache[I].CandidateVerIter(Cache);
  1752. if (verset[MOD_INSTALL].find(Cand) != verset[MOD_INSTALL].end())
  1753. continue;
  1754. List += I.FullName(true) + " ";
  1755. VersionsList += string(Cache[I].CandVersion) + "\n";
  1756. }
  1757. ShowList(c1out,_("The following extra packages will be installed:"),List,VersionsList);
  1758. }
  1759. /* Print out a list of suggested and recommended packages */
  1760. {
  1761. string SuggestsList, RecommendsList, List;
  1762. string SuggestsVersions, RecommendsVersions;
  1763. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  1764. {
  1765. pkgCache::PkgIterator Pkg(Cache,Cache.List[J]);
  1766. /* Just look at the ones we want to install */
  1767. if ((*Cache)[Pkg].Install() == false)
  1768. continue;
  1769. // get the recommends/suggests for the candidate ver
  1770. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  1771. for (pkgCache::DepIterator D = CV.DependsList(); D.end() == false; )
  1772. {
  1773. pkgCache::DepIterator Start;
  1774. pkgCache::DepIterator End;
  1775. D.GlobOr(Start,End); // advances D
  1776. // FIXME: we really should display a or-group as a or-group to the user
  1777. // the problem is that ShowList is incapable of doing this
  1778. string RecommendsOrList,RecommendsOrVersions;
  1779. string SuggestsOrList,SuggestsOrVersions;
  1780. bool foundInstalledInOrGroup = false;
  1781. for(;;)
  1782. {
  1783. /* Skip if package is installed already, or is about to be */
  1784. string target = Start.TargetPkg().FullName(true) + " ";
  1785. pkgCache::PkgIterator const TarPkg = Start.TargetPkg();
  1786. if (TarPkg->SelectedState == pkgCache::State::Install ||
  1787. TarPkg->SelectedState == pkgCache::State::Hold ||
  1788. Cache[Start.TargetPkg()].Install())
  1789. {
  1790. foundInstalledInOrGroup=true;
  1791. break;
  1792. }
  1793. /* Skip if we already saw it */
  1794. if (int(SuggestsList.find(target)) != -1 || int(RecommendsList.find(target)) != -1)
  1795. {
  1796. foundInstalledInOrGroup=true;
  1797. break;
  1798. }
  1799. // this is a dep on a virtual pkg, check if any package that provides it
  1800. // should be installed
  1801. if(Start.TargetPkg().ProvidesList() != 0)
  1802. {
  1803. pkgCache::PrvIterator I = Start.TargetPkg().ProvidesList();
  1804. for (; I.end() == false; ++I)
  1805. {
  1806. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  1807. if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer() &&
  1808. Pkg.CurrentVer() != 0)
  1809. foundInstalledInOrGroup=true;
  1810. }
  1811. }
  1812. if (Start->Type == pkgCache::Dep::Suggests)
  1813. {
  1814. SuggestsOrList += target;
  1815. SuggestsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
  1816. }
  1817. if (Start->Type == pkgCache::Dep::Recommends)
  1818. {
  1819. RecommendsOrList += target;
  1820. RecommendsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
  1821. }
  1822. if (Start >= End)
  1823. break;
  1824. ++Start;
  1825. }
  1826. if(foundInstalledInOrGroup == false)
  1827. {
  1828. RecommendsList += RecommendsOrList;
  1829. RecommendsVersions += RecommendsOrVersions;
  1830. SuggestsList += SuggestsOrList;
  1831. SuggestsVersions += SuggestsOrVersions;
  1832. }
  1833. }
  1834. }
  1835. ShowList(c1out,_("Suggested packages:"),SuggestsList,SuggestsVersions);
  1836. ShowList(c1out,_("Recommended packages:"),RecommendsList,RecommendsVersions);
  1837. }
  1838. // if nothing changed in the cache, but only the automark information
  1839. // we write the StateFile here, otherwise it will be written in
  1840. // cache.commit()
  1841. if (InstallAction.AutoMarkChanged > 0 &&
  1842. Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
  1843. Cache->BadCount() == 0 &&
  1844. _config->FindB("APT::Get::Simulate",false) == false)
  1845. Cache->writeStateFile(NULL);
  1846. // See if we need to prompt
  1847. // FIXME: check if really the packages in the set are going to be installed
  1848. if (Cache->InstCount() == verset[MOD_INSTALL].size() && Cache->DelCount() == 0)
  1849. return InstallPackages(Cache,false,false);
  1850. return InstallPackages(Cache,false);
  1851. }
  1852. /*}}}*/
  1853. /* mark packages as automatically/manually installed. {{{*/
  1854. bool DoMarkAuto(CommandLine &CmdL)
  1855. {
  1856. bool Action = true;
  1857. int AutoMarkChanged = 0;
  1858. OpTextProgress progress;
  1859. CacheFile Cache;
  1860. if (Cache.Open() == false)
  1861. return false;
  1862. if (strcasecmp(CmdL.FileList[0],"markauto") == 0)
  1863. Action = true;
  1864. else if (strcasecmp(CmdL.FileList[0],"unmarkauto") == 0)
  1865. Action = false;
  1866. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  1867. {
  1868. const char *S = *I;
  1869. // Locate the package
  1870. pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
  1871. if (Pkg.end() == true) {
  1872. return _error->Error(_("Couldn't find package %s"),S);
  1873. }
  1874. else
  1875. {
  1876. if (!Action)
  1877. ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.Name());
  1878. else
  1879. ioprintf(c1out,_("%s set to automatically installed.\n"),
  1880. Pkg.Name());
  1881. Cache->MarkAuto(Pkg,Action);
  1882. AutoMarkChanged++;
  1883. }
  1884. }
  1885. _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
  1886. if (AutoMarkChanged && ! _config->FindB("APT::Get::Simulate",false))
  1887. return Cache->writeStateFile(NULL);
  1888. return false;
  1889. }
  1890. /*}}}*/
  1891. // DoDistUpgrade - Automatic smart upgrader /*{{{*/
  1892. // ---------------------------------------------------------------------
  1893. /* Intelligent upgrader that will install and remove packages at will */
  1894. bool DoDistUpgrade(CommandLine &CmdL)
  1895. {
  1896. CacheFile Cache;
  1897. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1898. return false;
  1899. c0out << _("Calculating upgrade... ") << flush;
  1900. if (pkgDistUpgrade(*Cache) == false)
  1901. {
  1902. c0out << _("Failed") << endl;
  1903. ShowBroken(c1out,Cache,false);
  1904. return false;
  1905. }
  1906. c0out << _("Done") << endl;
  1907. return InstallPackages(Cache,true);
  1908. }
  1909. /*}}}*/
  1910. // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
  1911. // ---------------------------------------------------------------------
  1912. /* Follows dselect's selections */
  1913. bool DoDSelectUpgrade(CommandLine &CmdL)
  1914. {
  1915. CacheFile Cache;
  1916. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1917. return false;
  1918. pkgDepCache::ActionGroup group(Cache);
  1919. // Install everything with the install flag set
  1920. pkgCache::PkgIterator I = Cache->PkgBegin();
  1921. for (;I.end() != true; ++I)
  1922. {
  1923. /* Install the package only if it is a new install, the autoupgrader
  1924. will deal with the rest */
  1925. if (I->SelectedState == pkgCache::State::Install)
  1926. Cache->MarkInstall(I,false);
  1927. }
  1928. /* Now install their deps too, if we do this above then order of
  1929. the status file is significant for | groups */
  1930. for (I = Cache->PkgBegin();I.end() != true; ++I)
  1931. {
  1932. /* Install the package only if it is a new install, the autoupgrader
  1933. will deal with the rest */
  1934. if (I->SelectedState == pkgCache::State::Install)
  1935. Cache->MarkInstall(I,true);
  1936. }
  1937. // Apply erasures now, they override everything else.
  1938. for (I = Cache->PkgBegin();I.end() != true; ++I)
  1939. {
  1940. // Remove packages
  1941. if (I->SelectedState == pkgCache::State::DeInstall ||
  1942. I->SelectedState == pkgCache::State::Purge)
  1943. Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
  1944. }
  1945. /* Resolve any problems that dselect created, allupgrade cannot handle
  1946. such things. We do so quite agressively too.. */
  1947. if (Cache->BrokenCount() != 0)
  1948. {
  1949. pkgProblemResolver Fix(Cache);
  1950. // Hold back held packages.
  1951. if (_config->FindB("APT::Ignore-Hold",false) == false)
  1952. {
  1953. for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; ++I)
  1954. {
  1955. if (I->SelectedState == pkgCache::State::Hold)
  1956. {
  1957. Fix.Protect(I);
  1958. Cache->MarkKeep(I);
  1959. }
  1960. }
  1961. }
  1962. if (Fix.Resolve() == false)
  1963. {
  1964. ShowBroken(c1out,Cache,false);
  1965. return _error->Error(_("Internal error, problem resolver broke stuff"));
  1966. }
  1967. }
  1968. // Now upgrade everything
  1969. if (pkgAllUpgrade(Cache) == false)
  1970. {
  1971. ShowBroken(c1out,Cache,false);
  1972. return _error->Error(_("Internal error, problem resolver broke stuff"));
  1973. }
  1974. return InstallPackages(Cache,false);
  1975. }
  1976. /*}}}*/
  1977. // DoClean - Remove download archives /*{{{*/
  1978. // ---------------------------------------------------------------------
  1979. /* */
  1980. bool DoClean(CommandLine &CmdL)
  1981. {
  1982. std::string const archivedir = _config->FindDir("Dir::Cache::archives");
  1983. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  1984. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  1985. if (_config->FindB("APT::Get::Simulate") == true)
  1986. {
  1987. cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< endl
  1988. << "Del " << pkgcache << " " << srcpkgcache << endl;
  1989. return true;
  1990. }
  1991. // Lock the archive directory
  1992. FileFd Lock;
  1993. if (_config->FindB("Debug::NoLocking",false) == false)
  1994. {
  1995. Lock.Fd(GetLock(archivedir + "lock"));
  1996. if (_error->PendingError() == true)
  1997. return _error->Error(_("Unable to lock the download directory"));
  1998. }
  1999. pkgAcquire Fetcher;
  2000. Fetcher.Clean(archivedir);
  2001. Fetcher.Clean(archivedir + "partial/");
  2002. pkgCacheFile::RemoveCaches();
  2003. return true;
  2004. }
  2005. /*}}}*/
  2006. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  2007. // ---------------------------------------------------------------------
  2008. /* This is similar to clean but it only purges things that cannot be
  2009. downloaded, that is old versions of cached packages. */
  2010. class LogCleaner : public pkgArchiveCleaner
  2011. {
  2012. protected:
  2013. virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
  2014. {
  2015. c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
  2016. if (_config->FindB("APT::Get::Simulate") == false)
  2017. unlink(File);
  2018. };
  2019. };
  2020. bool DoAutoClean(CommandLine &CmdL)
  2021. {
  2022. // Lock the archive directory
  2023. FileFd Lock;
  2024. if (_config->FindB("Debug::NoLocking",false) == false)
  2025. {
  2026. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  2027. if (_error->PendingError() == true)
  2028. return _error->Error(_("Unable to lock the download directory"));
  2029. }
  2030. CacheFile Cache;
  2031. if (Cache.Open() == false)
  2032. return false;
  2033. LogCleaner Cleaner;
  2034. return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
  2035. Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
  2036. }
  2037. /*}}}*/
  2038. // DoDownload - download a binary /*{{{*/
  2039. // ---------------------------------------------------------------------
  2040. bool DoDownload(CommandLine &CmdL)
  2041. {
  2042. CacheFile Cache;
  2043. if (Cache.ReadOnlyOpen() == false)
  2044. return false;
  2045. APT::CacheSetHelper helper(c0out);
  2046. APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
  2047. CmdL.FileList + 1, APT::VersionList::CANDIDATE, helper);
  2048. if (verset.empty() == true)
  2049. return false;
  2050. pkgAcquire Fetcher;
  2051. AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
  2052. if (_config->FindB("APT::Get::Print-URIs") == false)
  2053. Fetcher.Setup(&Stat);
  2054. pkgRecords Recs(Cache);
  2055. pkgSourceList *SrcList = Cache.GetSourceList();
  2056. for (APT::VersionList::const_iterator Ver = verset.begin();
  2057. Ver != verset.end();
  2058. ++Ver)
  2059. {
  2060. string descr;
  2061. // get the right version
  2062. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  2063. pkgRecords::Parser &rec=Recs.Lookup(Ver.FileList());
  2064. pkgCache::VerFileIterator Vf = Ver.FileList();
  2065. if (Vf.end() == true)
  2066. return _error->Error("Can not find VerFile");
  2067. pkgCache::PkgFileIterator F = Vf.File();
  2068. pkgIndexFile *index;
  2069. if(SrcList->FindIndex(F, index) == false)
  2070. return _error->Error("FindIndex failed");
  2071. string uri = index->ArchiveURI(rec.FileName());
  2072. strprintf(descr, _("Downloading %s %s"), Pkg.Name(), Ver.VerStr());
  2073. // get the most appropriate hash
  2074. HashString hash;
  2075. if (rec.SHA512Hash() != "")
  2076. hash = HashString("sha512", rec.SHA512Hash());
  2077. if (rec.SHA256Hash() != "")
  2078. hash = HashString("sha256", rec.SHA256Hash());
  2079. else if (rec.SHA1Hash() != "")
  2080. hash = HashString("sha1", rec.SHA1Hash());
  2081. else if (rec.MD5Hash() != "")
  2082. hash = HashString("md5", rec.MD5Hash());
  2083. // get the file
  2084. new pkgAcqFile(&Fetcher, uri, hash.toStr(), (*Ver)->Size, descr, Pkg.Name(), ".");
  2085. }
  2086. // Just print out the uris and exit if the --print-uris flag was used
  2087. if (_config->FindB("APT::Get::Print-URIs") == true)
  2088. {
  2089. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  2090. for (; I != Fetcher.UriEnd(); ++I)
  2091. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  2092. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  2093. return true;
  2094. }
  2095. return (Fetcher.Run() == pkgAcquire::Continue);
  2096. }
  2097. /*}}}*/
  2098. // DoCheck - Perform the check operation /*{{{*/
  2099. // ---------------------------------------------------------------------
  2100. /* Opening automatically checks the system, this command is mostly used
  2101. for debugging */
  2102. bool DoCheck(CommandLine &CmdL)
  2103. {
  2104. CacheFile Cache;
  2105. Cache.Open();
  2106. Cache.CheckDeps();
  2107. return true;
  2108. }
  2109. /*}}}*/
  2110. // DoSource - Fetch a source archive /*{{{*/
  2111. // ---------------------------------------------------------------------
  2112. /* Fetch souce packages */
  2113. struct DscFile
  2114. {
  2115. string Package;
  2116. string Version;
  2117. string Dsc;
  2118. };
  2119. bool DoSource(CommandLine &CmdL)
  2120. {
  2121. CacheFile Cache;
  2122. if (Cache.Open(false) == false)
  2123. return false;
  2124. if (CmdL.FileSize() <= 1)
  2125. return _error->Error(_("Must specify at least one package to fetch source for"));
  2126. // Read the source list
  2127. if (Cache.BuildSourceList() == false)
  2128. return false;
  2129. pkgSourceList *List = Cache.GetSourceList();
  2130. // Create the text record parsers
  2131. pkgRecords Recs(Cache);
  2132. pkgSrcRecords SrcRecs(*List);
  2133. if (_error->PendingError() == true)
  2134. return false;
  2135. // Create the download object
  2136. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  2137. pkgAcquire Fetcher;
  2138. Fetcher.SetLog(&Stat);
  2139. DscFile *Dsc = new DscFile[CmdL.FileSize()];
  2140. // insert all downloaded uris into this set to avoid downloading them
  2141. // twice
  2142. set<string> queued;
  2143. // Diff only mode only fetches .diff files
  2144. bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
  2145. // Tar only mode only fetches .tar files
  2146. bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
  2147. // Dsc only mode only fetches .dsc files
  2148. bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
  2149. // Load the requestd sources into the fetcher
  2150. unsigned J = 0;
  2151. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  2152. {
  2153. string Src;
  2154. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  2155. if (Last == 0) {
  2156. delete[] Dsc;
  2157. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  2158. }
  2159. string srec = Last->AsStr();
  2160. string::size_type pos = srec.find("\nVcs-");
  2161. while (pos != string::npos)
  2162. {
  2163. pos += strlen("\nVcs-");
  2164. string vcs = srec.substr(pos,srec.find(":",pos)-pos);
  2165. if(vcs == "Browser")
  2166. {
  2167. pos = srec.find("\nVcs-", pos);
  2168. continue;
  2169. }
  2170. pos += vcs.length()+2;
  2171. string::size_type epos = srec.find("\n", pos);
  2172. string uri = srec.substr(pos,epos-pos).c_str();
  2173. ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
  2174. "the '%s' version control system at:\n"
  2175. "%s\n"),
  2176. Src.c_str(), vcs.c_str(), uri.c_str());
  2177. if(vcs == "Bzr")
  2178. ioprintf(c1out,_("Please use:\n"
  2179. "bzr get %s\n"
  2180. "to retrieve the latest (possibly unreleased) "
  2181. "updates to the package.\n"),
  2182. uri.c_str());
  2183. break;
  2184. }
  2185. // Back track
  2186. vector<pkgSrcRecords::File> Lst;
  2187. if (Last->Files(Lst) == false) {
  2188. delete[] Dsc;
  2189. return false;
  2190. }
  2191. // Load them into the fetcher
  2192. for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
  2193. I != Lst.end(); ++I)
  2194. {
  2195. // Try to guess what sort of file it is we are getting.
  2196. if (I->Type == "dsc")
  2197. {
  2198. Dsc[J].Package = Last->Package();
  2199. Dsc[J].Version = Last->Version();
  2200. Dsc[J].Dsc = flNotDir(I->Path);
  2201. }
  2202. // Handle the only options so that multiple can be used at once
  2203. if (diffOnly == true || tarOnly == true || dscOnly == true)
  2204. {
  2205. if ((diffOnly == true && I->Type == "diff") ||
  2206. (tarOnly == true && I->Type == "tar") ||
  2207. (dscOnly == true && I->Type == "dsc"))
  2208. ; // Fine, we want this file downloaded
  2209. else
  2210. continue;
  2211. }
  2212. // don't download the same uri twice (should this be moved to
  2213. // the fetcher interface itself?)
  2214. if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
  2215. continue;
  2216. queued.insert(Last->Index().ArchiveURI(I->Path));
  2217. // check if we have a file with that md5 sum already localy
  2218. if(!I->MD5Hash.empty() && FileExists(flNotDir(I->Path)))
  2219. {
  2220. FileFd Fd(flNotDir(I->Path), FileFd::ReadOnly);
  2221. MD5Summation sum;
  2222. sum.AddFD(Fd.Fd(), Fd.Size());
  2223. Fd.Close();
  2224. if((string)sum.Result() == I->MD5Hash)
  2225. {
  2226. ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
  2227. flNotDir(I->Path).c_str());
  2228. continue;
  2229. }
  2230. }
  2231. new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
  2232. I->MD5Hash,I->Size,
  2233. Last->Index().SourceInfo(*Last,*I),Src);
  2234. }
  2235. }
  2236. // Display statistics
  2237. unsigned long long FetchBytes = Fetcher.FetchNeeded();
  2238. unsigned long long FetchPBytes = Fetcher.PartialPresent();
  2239. unsigned long long DebBytes = Fetcher.TotalNeeded();
  2240. // Check for enough free space
  2241. struct statvfs Buf;
  2242. string OutputDir = ".";
  2243. if (statvfs(OutputDir.c_str(),&Buf) != 0) {
  2244. delete[] Dsc;
  2245. if (errno == EOVERFLOW)
  2246. return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
  2247. OutputDir.c_str());
  2248. else
  2249. return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
  2250. OutputDir.c_str());
  2251. } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  2252. {
  2253. struct statfs Stat;
  2254. if (statfs(OutputDir.c_str(),&Stat) != 0
  2255. #if HAVE_STRUCT_STATFS_F_TYPE
  2256. || unsigned(Stat.f_type) != RAMFS_MAGIC
  2257. #endif
  2258. ) {
  2259. delete[] Dsc;
  2260. return _error->Error(_("You don't have enough free space in %s"),
  2261. OutputDir.c_str());
  2262. }
  2263. }
  2264. // Number of bytes
  2265. if (DebBytes != FetchBytes)
  2266. //TRANSLATOR: The required space between number and unit is already included
  2267. // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
  2268. ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
  2269. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  2270. else
  2271. //TRANSLATOR: The required space between number and unit is already included
  2272. // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
  2273. ioprintf(c1out,_("Need to get %sB of source archives.\n"),
  2274. SizeToStr(DebBytes).c_str());
  2275. if (_config->FindB("APT::Get::Simulate",false) == true)
  2276. {
  2277. for (unsigned I = 0; I != J; I++)
  2278. ioprintf(cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
  2279. delete[] Dsc;
  2280. return true;
  2281. }
  2282. // Just print out the uris an exit if the --print-uris flag was used
  2283. if (_config->FindB("APT::Get::Print-URIs") == true)
  2284. {
  2285. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  2286. for (; I != Fetcher.UriEnd(); ++I)
  2287. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  2288. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  2289. delete[] Dsc;
  2290. return true;
  2291. }
  2292. // Run it
  2293. if (Fetcher.Run() == pkgAcquire::Failed)
  2294. {
  2295. delete[] Dsc;
  2296. return false;
  2297. }
  2298. // Print error messages
  2299. bool Failed = false;
  2300. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
  2301. {
  2302. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  2303. (*I)->Complete == true)
  2304. continue;
  2305. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  2306. (*I)->ErrorText.c_str());
  2307. Failed = true;
  2308. }
  2309. if (Failed == true)
  2310. {
  2311. delete[] Dsc;
  2312. return _error->Error(_("Failed to fetch some archives."));
  2313. }
  2314. if (_config->FindB("APT::Get::Download-only",false) == true)
  2315. {
  2316. c1out << _("Download complete and in download only mode") << endl;
  2317. delete[] Dsc;
  2318. return true;
  2319. }
  2320. // Unpack the sources
  2321. pid_t Process = ExecFork();
  2322. if (Process == 0)
  2323. {
  2324. bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
  2325. for (unsigned I = 0; I != J; ++I)
  2326. {
  2327. string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
  2328. // Diff only mode only fetches .diff files
  2329. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  2330. _config->FindB("APT::Get::Tar-Only",false) == true ||
  2331. Dsc[I].Dsc.empty() == true)
  2332. continue;
  2333. // See if the package is already unpacked
  2334. struct stat Stat;
  2335. if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
  2336. S_ISDIR(Stat.st_mode) != 0)
  2337. {
  2338. ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
  2339. Dir.c_str());
  2340. }
  2341. else
  2342. {
  2343. // Call dpkg-source
  2344. char S[500];
  2345. snprintf(S,sizeof(S),"%s -x %s",
  2346. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  2347. Dsc[I].Dsc.c_str());
  2348. if (system(S) != 0)
  2349. {
  2350. fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
  2351. fprintf(stderr,_("Check if the 'dpkg-dev' package is installed.\n"));
  2352. _exit(1);
  2353. }
  2354. }
  2355. // Try to compile it with dpkg-buildpackage
  2356. if (_config->FindB("APT::Get::Compile",false) == true)
  2357. {
  2358. string buildopts = _config->Find("APT::Get::Host-Architecture");
  2359. if (buildopts.empty() == false)
  2360. buildopts = "-a " + buildopts + " ";
  2361. buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
  2362. // Call dpkg-buildpackage
  2363. char S[500];
  2364. snprintf(S,sizeof(S),"cd %s && %s %s",
  2365. Dir.c_str(),
  2366. _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
  2367. buildopts.c_str());
  2368. if (system(S) != 0)
  2369. {
  2370. fprintf(stderr,_("Build command '%s' failed.\n"),S);
  2371. _exit(1);
  2372. }
  2373. }
  2374. }
  2375. _exit(0);
  2376. }
  2377. delete[] Dsc;
  2378. // Wait for the subprocess
  2379. int Status = 0;
  2380. while (waitpid(Process,&Status,0) != Process)
  2381. {
  2382. if (errno == EINTR)
  2383. continue;
  2384. return _error->Errno("waitpid","Couldn't wait for subprocess");
  2385. }
  2386. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  2387. return _error->Error(_("Child process failed"));
  2388. return true;
  2389. }
  2390. /*}}}*/
  2391. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  2392. // ---------------------------------------------------------------------
  2393. /* This function will look at the build depends list of the given source
  2394. package and install the necessary packages to make it true, or fail. */
  2395. bool DoBuildDep(CommandLine &CmdL)
  2396. {
  2397. CacheFile Cache;
  2398. _config->Set("APT::Install-Recommends", false);
  2399. if (Cache.Open(true) == false)
  2400. return false;
  2401. if (CmdL.FileSize() <= 1)
  2402. return _error->Error(_("Must specify at least one package to check builddeps for"));
  2403. // Read the source list
  2404. if (Cache.BuildSourceList() == false)
  2405. return false;
  2406. pkgSourceList *List = Cache.GetSourceList();
  2407. // Create the text record parsers
  2408. pkgRecords Recs(Cache);
  2409. pkgSrcRecords SrcRecs(*List);
  2410. if (_error->PendingError() == true)
  2411. return false;
  2412. // Create the download object
  2413. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  2414. pkgAcquire Fetcher;
  2415. if (Fetcher.Setup(&Stat) == false)
  2416. return false;
  2417. bool StripMultiArch;
  2418. string hostArch = _config->Find("APT::Get::Host-Architecture");
  2419. if (hostArch.empty() == false)
  2420. {
  2421. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  2422. if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
  2423. return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
  2424. StripMultiArch = false;
  2425. }
  2426. else
  2427. StripMultiArch = true;
  2428. unsigned J = 0;
  2429. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  2430. {
  2431. string Src;
  2432. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  2433. if (Last == 0)
  2434. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  2435. // Process the build-dependencies
  2436. vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  2437. if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
  2438. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  2439. // Also ensure that build-essential packages are present
  2440. Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
  2441. if (Opts)
  2442. Opts = Opts->Child;
  2443. for (; Opts; Opts = Opts->Next)
  2444. {
  2445. if (Opts->Value.empty() == true)
  2446. continue;
  2447. pkgSrcRecords::Parser::BuildDepRec rec;
  2448. rec.Package = Opts->Value;
  2449. rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
  2450. rec.Op = 0;
  2451. BuildDeps.push_back(rec);
  2452. }
  2453. if (BuildDeps.empty() == true)
  2454. {
  2455. ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
  2456. continue;
  2457. }
  2458. // Install the requested packages
  2459. vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
  2460. pkgProblemResolver Fix(Cache);
  2461. bool skipAlternatives = false; // skip remaining alternatives in an or group
  2462. for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
  2463. {
  2464. bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
  2465. if (skipAlternatives == true)
  2466. {
  2467. /*
  2468. * if there are alternatives, we've already picked one, so skip
  2469. * the rest
  2470. *
  2471. * TODO: this means that if there's a build-dep on A|B and B is
  2472. * installed, we'll still try to install A; more importantly,
  2473. * if A is currently broken, we cannot go back and try B. To fix
  2474. * this would require we do a Resolve cycle for each package we
  2475. * add to the install list. Ugh
  2476. */
  2477. if (!hasAlternatives)
  2478. skipAlternatives = false; // end of or group
  2479. continue;
  2480. }
  2481. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  2482. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  2483. {
  2484. pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
  2485. // Build-conflicts on unknown packages are silently ignored
  2486. if (Grp.end() == true)
  2487. continue;
  2488. for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  2489. {
  2490. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  2491. /*
  2492. * Remove if we have an installed version that satisfies the
  2493. * version criteria
  2494. */
  2495. if (IV.end() == false &&
  2496. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  2497. TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
  2498. }
  2499. }
  2500. else // BuildDep || BuildDepIndep
  2501. {
  2502. if (_config->FindB("Debug::BuildDeps",false) == true)
  2503. cout << "Looking for " << (*D).Package << "...\n";
  2504. pkgCache::PkgIterator Pkg;
  2505. // Cross-Building?
  2506. if (StripMultiArch == false)
  2507. {
  2508. size_t const colon = D->Package.find(":");
  2509. if (colon != string::npos &&
  2510. (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0))
  2511. Pkg = Cache->FindPkg(D->Package.substr(0,colon));
  2512. else
  2513. Pkg = Cache->FindPkg(D->Package);
  2514. // We need to decide if host or build arch, so find a version we can look at
  2515. pkgCache::VerIterator Ver;
  2516. // a bad version either is invalid or doesn't satify dependency
  2517. #define BADVER(Ver) Ver.end() == true || \
  2518. (Ver.end() == false && D->Version.empty() == false && \
  2519. Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false)
  2520. if (Pkg.end() == false)
  2521. {
  2522. Ver = (*Cache)[Pkg].InstVerIter(*Cache);
  2523. if (BADVER(Ver))
  2524. Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
  2525. }
  2526. if (BADVER(Ver))
  2527. {
  2528. pkgCache::PkgIterator HostPkg = Cache->FindPkg(D->Package, hostArch);
  2529. if (HostPkg.end() == false)
  2530. {
  2531. Ver = (*Cache)[HostPkg].InstVerIter(*Cache);
  2532. if (BADVER(Ver))
  2533. Ver = (*Cache)[HostPkg].CandidateVerIter(*Cache);
  2534. }
  2535. }
  2536. if ((BADVER(Ver)) == false)
  2537. {
  2538. string forbidden;
  2539. if (Ver->MultiArch == pkgCache::Version::None || Ver->MultiArch == pkgCache::Version::All)
  2540. {
  2541. if (colon == string::npos)
  2542. {
  2543. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  2544. }
  2545. }
  2546. else if (Ver->MultiArch == pkgCache::Version::Same)
  2547. {
  2548. if (colon == string::npos)
  2549. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  2550. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  2551. forbidden = "Multi-Arch: same";
  2552. // :native gets the buildArch
  2553. }
  2554. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  2555. {
  2556. if (colon != string::npos)
  2557. forbidden = "Multi-Arch: foreign";
  2558. }
  2559. else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  2560. {
  2561. if (colon == string::npos)
  2562. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  2563. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  2564. {
  2565. // prefer any installed over preferred non-installed architectures
  2566. pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
  2567. // we don't check for version here as we are better of with upgrading than remove and install
  2568. for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  2569. if (Pkg.CurrentVer().end() == false)
  2570. break;
  2571. if (Pkg.end() == true)
  2572. Pkg = Grp.FindPreferredPkg(true);
  2573. }
  2574. // native gets buildArch
  2575. }
  2576. if (forbidden.empty() == false)
  2577. {
  2578. if (_config->FindB("Debug::BuildDeps",false) == true)
  2579. cout << " :any is not allowed from M-A: same package " << (*D).Package << endl;
  2580. if (hasAlternatives)
  2581. continue;
  2582. return _error->Error(_("%s dependency for %s can't be satisfied "
  2583. "because %s is not allowed on '%s' packages"),
  2584. Last->BuildDepType(D->Type), Src.c_str(),
  2585. D->Package.c_str(), "Multi-Arch: same");
  2586. }
  2587. }
  2588. else if (_config->FindB("Debug::BuildDeps",false) == true)
  2589. cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << endl;
  2590. #undef BADVER
  2591. }
  2592. else
  2593. Pkg = Cache->FindPkg(D->Package);
  2594. if (Pkg.end() == true)
  2595. {
  2596. if (_config->FindB("Debug::BuildDeps",false) == true)
  2597. cout << " (not found)" << (*D).Package << endl;
  2598. if (hasAlternatives)
  2599. continue;
  2600. return _error->Error(_("%s dependency for %s cannot be satisfied "
  2601. "because the package %s cannot be found"),
  2602. Last->BuildDepType((*D).Type),Src.c_str(),
  2603. (*D).Package.c_str());
  2604. }
  2605. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  2606. if (IV.end() == false)
  2607. {
  2608. if (_config->FindB("Debug::BuildDeps",false) == true)
  2609. cout << " Is installed\n";
  2610. if (D->Version.empty() == true ||
  2611. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  2612. {
  2613. skipAlternatives = hasAlternatives;
  2614. continue;
  2615. }
  2616. if (_config->FindB("Debug::BuildDeps",false) == true)
  2617. cout << " ...but the installed version doesn't meet the version requirement\n";
  2618. if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
  2619. return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
  2620. Last->BuildDepType((*D).Type), Src.c_str(), Pkg.FullName(true).c_str());
  2621. }
  2622. // Only consider virtual packages if there is no versioned dependency
  2623. if ((*D).Version.empty() == true)
  2624. {
  2625. /*
  2626. * If this is a virtual package, we need to check the list of
  2627. * packages that provide it and see if any of those are
  2628. * installed
  2629. */
  2630. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  2631. for (; Prv.end() != true; ++Prv)
  2632. {
  2633. if (_config->FindB("Debug::BuildDeps",false) == true)
  2634. cout << " Checking provider " << Prv.OwnerPkg().FullName() << endl;
  2635. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  2636. break;
  2637. }
  2638. if (Prv.end() == false)
  2639. {
  2640. if (_config->FindB("Debug::BuildDeps",false) == true)
  2641. cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << endl;
  2642. skipAlternatives = hasAlternatives;
  2643. continue;
  2644. }
  2645. }
  2646. else // versioned dependency
  2647. {
  2648. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  2649. if (CV.end() == true ||
  2650. Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
  2651. {
  2652. if (hasAlternatives)
  2653. continue;
  2654. else if (CV.end() == false)
  2655. return _error->Error(_("%s dependency for %s cannot be satisfied "
  2656. "because candidate version of package %s "
  2657. "can't satisfy version requirements"),
  2658. Last->BuildDepType(D->Type), Src.c_str(),
  2659. D->Package.c_str());
  2660. else
  2661. return _error->Error(_("%s dependency for %s cannot be satisfied "
  2662. "because package %s has no candidate version"),
  2663. Last->BuildDepType(D->Type), Src.c_str(),
  2664. D->Package.c_str());
  2665. }
  2666. }
  2667. if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false) == true)
  2668. {
  2669. // We successfully installed something; skip remaining alternatives
  2670. skipAlternatives = hasAlternatives;
  2671. if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
  2672. Cache->MarkAuto(Pkg, true);
  2673. continue;
  2674. }
  2675. else if (hasAlternatives)
  2676. {
  2677. if (_config->FindB("Debug::BuildDeps",false) == true)
  2678. cout << " Unsatisfiable, trying alternatives\n";
  2679. continue;
  2680. }
  2681. else
  2682. {
  2683. return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
  2684. Last->BuildDepType((*D).Type),
  2685. Src.c_str(),
  2686. (*D).Package.c_str());
  2687. }
  2688. }
  2689. }
  2690. Fix.InstallProtect();
  2691. if (Fix.Resolve(true) == false)
  2692. _error->Discard();
  2693. // Now we check the state of the packages,
  2694. if (Cache->BrokenCount() != 0)
  2695. {
  2696. ShowBroken(cout, Cache, false);
  2697. return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
  2698. }
  2699. }
  2700. if (InstallPackages(Cache, false, true) == false)
  2701. return _error->Error(_("Failed to process build dependencies"));
  2702. return true;
  2703. }
  2704. /*}}}*/
  2705. // GetChangelogPath - return a path pointing to a changelog file or dir /*{{{*/
  2706. // ---------------------------------------------------------------------
  2707. /* This returns a "path" string for the changelog url construction.
  2708. * Please note that its not complete, it either needs a "/changelog"
  2709. * appended (for the packages.debian.org/changelogs site) or a
  2710. * ".changelog" (for third party sites that store the changelog in the
  2711. * pool/ next to the deb itself)
  2712. * Example return: "pool/main/a/apt/apt_0.8.8ubuntu3"
  2713. */
  2714. string GetChangelogPath(CacheFile &Cache,
  2715. pkgCache::PkgIterator Pkg,
  2716. pkgCache::VerIterator Ver)
  2717. {
  2718. string path;
  2719. pkgRecords Recs(Cache);
  2720. pkgRecords::Parser &rec=Recs.Lookup(Ver.FileList());
  2721. string srcpkg = rec.SourcePkg().empty() ? Pkg.Name() : rec.SourcePkg();
  2722. string ver = Ver.VerStr();
  2723. // if there is a source version it always wins
  2724. if (rec.SourceVer() != "")
  2725. ver = rec.SourceVer();
  2726. path = flNotFile(rec.FileName());
  2727. path += srcpkg + "_" + StripEpoch(ver);
  2728. return path;
  2729. }
  2730. /*}}}*/
  2731. // GuessThirdPartyChangelogUri - return url /*{{{*/
  2732. // ---------------------------------------------------------------------
  2733. /* Contruct a changelog file path for third party sites that do not use
  2734. * packages.debian.org/changelogs
  2735. * This simply uses the ArchiveURI() of the source pkg and looks for
  2736. * a .changelog file there, Example for "mediabuntu":
  2737. * apt-get changelog mplayer-doc:
  2738. * http://packages.medibuntu.org/pool/non-free/m/mplayer/mplayer_1.0~rc4~try1.dsfg1-1ubuntu1+medibuntu1.changelog
  2739. */
  2740. bool GuessThirdPartyChangelogUri(CacheFile &Cache,
  2741. pkgCache::PkgIterator Pkg,
  2742. pkgCache::VerIterator Ver,
  2743. string &out_uri)
  2744. {
  2745. // get the binary deb server path
  2746. pkgCache::VerFileIterator Vf = Ver.FileList();
  2747. if (Vf.end() == true)
  2748. return false;
  2749. pkgCache::PkgFileIterator F = Vf.File();
  2750. pkgIndexFile *index;
  2751. pkgSourceList *SrcList = Cache.GetSourceList();
  2752. if(SrcList->FindIndex(F, index) == false)
  2753. return false;
  2754. // get archive uri for the binary deb
  2755. string path_without_dot_changelog = GetChangelogPath(Cache, Pkg, Ver);
  2756. out_uri = index->ArchiveURI(path_without_dot_changelog + ".changelog");
  2757. // now strip away the filename and add srcpkg_srcver.changelog
  2758. return true;
  2759. }
  2760. /*}}}*/
  2761. // DownloadChangelog - Download the changelog /*{{{*/
  2762. // ---------------------------------------------------------------------
  2763. bool DownloadChangelog(CacheFile &CacheFile, pkgAcquire &Fetcher,
  2764. pkgCache::VerIterator Ver, string targetfile)
  2765. /* Download a changelog file for the given package version to
  2766. * targetfile. This will first try the server from Apt::Changelogs::Server
  2767. * (http://packages.debian.org/changelogs by default) and if that gives
  2768. * a 404 tries to get it from the archive directly (see
  2769. * GuessThirdPartyChangelogUri for details how)
  2770. */
  2771. {
  2772. string path;
  2773. string descr;
  2774. string server;
  2775. string changelog_uri;
  2776. // data structures we need
  2777. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  2778. // make the server root configurable
  2779. server = _config->Find("Apt::Changelogs::Server",
  2780. "http://packages.debian.org/changelogs");
  2781. path = GetChangelogPath(CacheFile, Pkg, Ver);
  2782. strprintf(changelog_uri, "%s/%s/changelog", server.c_str(), path.c_str());
  2783. if (_config->FindB("APT::Get::Print-URIs", false) == true)
  2784. {
  2785. std::cout << '\'' << changelog_uri << '\'' << std::endl;
  2786. return true;
  2787. }
  2788. strprintf(descr, _("Changelog for %s (%s)"), Pkg.Name(), changelog_uri.c_str());
  2789. // queue it
  2790. new pkgAcqFile(&Fetcher, changelog_uri, "", 0, descr, Pkg.Name(), "ignored", targetfile);
  2791. // try downloading it, if that fails, try third-party-changelogs location
  2792. // FIXME: Fetcher.Run() is "Continue" even if I get a 404?!?
  2793. Fetcher.Run();
  2794. if (!FileExists(targetfile))
  2795. {
  2796. string third_party_uri;
  2797. if (GuessThirdPartyChangelogUri(CacheFile, Pkg, Ver, third_party_uri))
  2798. {
  2799. strprintf(descr, _("Changelog for %s (%s)"), Pkg.Name(), third_party_uri.c_str());
  2800. new pkgAcqFile(&Fetcher, third_party_uri, "", 0, descr, Pkg.Name(), "ignored", targetfile);
  2801. Fetcher.Run();
  2802. }
  2803. }
  2804. if (FileExists(targetfile))
  2805. return true;
  2806. // error
  2807. return _error->Error("changelog download failed");
  2808. }
  2809. /*}}}*/
  2810. // DisplayFileInPager - Display File with pager /*{{{*/
  2811. void DisplayFileInPager(string filename)
  2812. {
  2813. pid_t Process = ExecFork();
  2814. if (Process == 0)
  2815. {
  2816. const char *Args[3];
  2817. Args[0] = "/usr/bin/sensible-pager";
  2818. Args[1] = filename.c_str();
  2819. Args[2] = 0;
  2820. execvp(Args[0],(char **)Args);
  2821. exit(100);
  2822. }
  2823. // Wait for the subprocess
  2824. ExecWait(Process, "sensible-pager", false);
  2825. }
  2826. /*}}}*/
  2827. // DoChangelog - Get changelog from the command line /*{{{*/
  2828. // ---------------------------------------------------------------------
  2829. bool DoChangelog(CommandLine &CmdL)
  2830. {
  2831. CacheFile Cache;
  2832. if (Cache.ReadOnlyOpen() == false)
  2833. return false;
  2834. APT::CacheSetHelper helper(c0out);
  2835. APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
  2836. CmdL.FileList + 1, APT::VersionList::CANDIDATE, helper);
  2837. if (verset.empty() == true)
  2838. return false;
  2839. pkgAcquire Fetcher;
  2840. if (_config->FindB("APT::Get::Print-URIs", false) == true)
  2841. for (APT::VersionList::const_iterator Ver = verset.begin();
  2842. Ver != verset.end(); ++Ver)
  2843. return DownloadChangelog(Cache, Fetcher, Ver, "");
  2844. AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
  2845. Fetcher.Setup(&Stat);
  2846. bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
  2847. char tmpname[100];
  2848. char* tmpdir = NULL;
  2849. if (downOnly == false)
  2850. {
  2851. const char* const tmpDir = getenv("TMPDIR");
  2852. if (tmpDir != NULL && *tmpDir != '\0')
  2853. snprintf(tmpname, sizeof(tmpname), "%s/apt-changelog-XXXXXX", tmpDir);
  2854. else
  2855. strncpy(tmpname, "/tmp/apt-changelog-XXXXXX", sizeof(tmpname));
  2856. tmpdir = mkdtemp(tmpname);
  2857. if (tmpdir == NULL)
  2858. return _error->Errno("mkdtemp", "mkdtemp failed");
  2859. }
  2860. for (APT::VersionList::const_iterator Ver = verset.begin();
  2861. Ver != verset.end();
  2862. ++Ver)
  2863. {
  2864. string changelogfile;
  2865. if (downOnly == false)
  2866. changelogfile.append(tmpname).append("changelog");
  2867. else
  2868. changelogfile.append(Ver.ParentPkg().Name()).append(".changelog");
  2869. if (DownloadChangelog(Cache, Fetcher, Ver, changelogfile) && downOnly == false)
  2870. {
  2871. DisplayFileInPager(changelogfile);
  2872. // cleanup temp file
  2873. unlink(changelogfile.c_str());
  2874. }
  2875. }
  2876. // clenaup tmp dir
  2877. if (tmpdir != NULL)
  2878. rmdir(tmpdir);
  2879. return true;
  2880. }
  2881. /*}}}*/
  2882. // DoMoo - Never Ask, Never Tell /*{{{*/
  2883. // ---------------------------------------------------------------------
  2884. /* */
  2885. bool DoMoo(CommandLine &CmdL)
  2886. {
  2887. cout <<
  2888. " (__) \n"
  2889. " (oo) \n"
  2890. " /------\\/ \n"
  2891. " / | || \n"
  2892. " * /\\---/\\ \n"
  2893. " ~~ ~~ \n"
  2894. "....\"Have you mooed today?\"...\n";
  2895. return true;
  2896. }
  2897. /*}}}*/
  2898. // ShowHelp - Show a help screen /*{{{*/
  2899. // ---------------------------------------------------------------------
  2900. /* */
  2901. bool ShowHelp(CommandLine &CmdL)
  2902. {
  2903. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
  2904. COMMON_ARCH,__DATE__,__TIME__);
  2905. if (_config->FindB("version") == true)
  2906. {
  2907. cout << _("Supported modules:") << endl;
  2908. for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
  2909. {
  2910. pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
  2911. if (_system != 0 && _system->VS == VS)
  2912. cout << '*';
  2913. else
  2914. cout << ' ';
  2915. cout << "Ver: " << VS->Label << endl;
  2916. /* Print out all the packaging systems that will work with
  2917. this VS */
  2918. for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
  2919. {
  2920. pkgSystem *Sys = pkgSystem::GlobalList[J];
  2921. if (_system == Sys)
  2922. cout << '*';
  2923. else
  2924. cout << ' ';
  2925. if (Sys->VS->TestCompatibility(*VS) == true)
  2926. cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
  2927. }
  2928. }
  2929. for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
  2930. {
  2931. pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
  2932. cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
  2933. }
  2934. for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
  2935. {
  2936. pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
  2937. cout << " Idx: " << Type->Label << endl;
  2938. }
  2939. return true;
  2940. }
  2941. cout <<
  2942. _("Usage: apt-get [options] command\n"
  2943. " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
  2944. " apt-get [options] source pkg1 [pkg2 ...]\n"
  2945. "\n"
  2946. "apt-get is a simple command line interface for downloading and\n"
  2947. "installing packages. The most frequently used commands are update\n"
  2948. "and install.\n"
  2949. "\n"
  2950. "Commands:\n"
  2951. " update - Retrieve new lists of packages\n"
  2952. " upgrade - Perform an upgrade\n"
  2953. " install - Install new packages (pkg is libc6 not libc6.deb)\n"
  2954. " remove - Remove packages\n"
  2955. " autoremove - Remove automatically all unused packages\n"
  2956. " purge - Remove packages and config files\n"
  2957. " source - Download source archives\n"
  2958. " build-dep - Configure build-dependencies for source packages\n"
  2959. " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
  2960. " dselect-upgrade - Follow dselect selections\n"
  2961. " clean - Erase downloaded archive files\n"
  2962. " autoclean - Erase old downloaded archive files\n"
  2963. " check - Verify that there are no broken dependencies\n"
  2964. " changelog - Download and display the changelog for the given package\n"
  2965. " download - Download the binary package into the current directory\n"
  2966. "\n"
  2967. "Options:\n"
  2968. " -h This help text.\n"
  2969. " -q Loggable output - no progress indicator\n"
  2970. " -qq No output except for errors\n"
  2971. " -d Download only - do NOT install or unpack archives\n"
  2972. " -s No-act. Perform ordering simulation\n"
  2973. " -y Assume Yes to all queries and do not prompt\n"
  2974. " -f Attempt to correct a system with broken dependencies in place\n"
  2975. " -m Attempt to continue if archives are unlocatable\n"
  2976. " -u Show a list of upgraded packages as well\n"
  2977. " -b Build the source package after fetching it\n"
  2978. " -V Show verbose version numbers\n"
  2979. " -c=? Read this configuration file\n"
  2980. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  2981. "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
  2982. "pages for more information and options.\n"
  2983. " This APT has Super Cow Powers.\n");
  2984. return true;
  2985. }
  2986. /*}}}*/
  2987. // SigWinch - Window size change signal handler /*{{{*/
  2988. // ---------------------------------------------------------------------
  2989. /* */
  2990. void SigWinch(int)
  2991. {
  2992. // Riped from GNU ls
  2993. #ifdef TIOCGWINSZ
  2994. struct winsize ws;
  2995. if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
  2996. ScreenWidth = ws.ws_col - 1;
  2997. #endif
  2998. }
  2999. /*}}}*/
  3000. int main(int argc,const char *argv[]) /*{{{*/
  3001. {
  3002. CommandLine::Args Args[] = {
  3003. {'h',"help","help",0},
  3004. {'v',"version","version",0},
  3005. {'V',"verbose-versions","APT::Get::Show-Versions",0},
  3006. {'q',"quiet","quiet",CommandLine::IntLevel},
  3007. {'q',"silent","quiet",CommandLine::IntLevel},
  3008. {'d',"download-only","APT::Get::Download-Only",0},
  3009. {'b',"compile","APT::Get::Compile",0},
  3010. {'b',"build","APT::Get::Compile",0},
  3011. {'s',"simulate","APT::Get::Simulate",0},
  3012. {'s',"just-print","APT::Get::Simulate",0},
  3013. {'s',"recon","APT::Get::Simulate",0},
  3014. {'s',"dry-run","APT::Get::Simulate",0},
  3015. {'s',"no-act","APT::Get::Simulate",0},
  3016. {'y',"yes","APT::Get::Assume-Yes",0},
  3017. {'y',"assume-yes","APT::Get::Assume-Yes",0},
  3018. {0,"assume-no","APT::Get::Assume-No",0},
  3019. {'f',"fix-broken","APT::Get::Fix-Broken",0},
  3020. {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
  3021. {'m',"ignore-missing","APT::Get::Fix-Missing",0},
  3022. {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
  3023. {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
  3024. {'a',"host-architecture","APT::Get::Host-Architecture",CommandLine::HasArg},
  3025. {0,"download","APT::Get::Download",0},
  3026. {0,"fix-missing","APT::Get::Fix-Missing",0},
  3027. {0,"ignore-hold","APT::Ignore-Hold",0},
  3028. {0,"upgrade","APT::Get::upgrade",0},
  3029. {0,"only-upgrade","APT::Get::Only-Upgrade",0},
  3030. {0,"force-yes","APT::Get::force-yes",0},
  3031. {0,"print-uris","APT::Get::Print-URIs",0},
  3032. {0,"diff-only","APT::Get::Diff-Only",0},
  3033. {0,"debian-only","APT::Get::Diff-Only",0},
  3034. {0,"tar-only","APT::Get::Tar-Only",0},
  3035. {0,"dsc-only","APT::Get::Dsc-Only",0},
  3036. {0,"purge","APT::Get::Purge",0},
  3037. {0,"list-cleanup","APT::Get::List-Cleanup",0},
  3038. {0,"reinstall","APT::Get::ReInstall",0},
  3039. {0,"trivial-only","APT::Get::Trivial-Only",0},
  3040. {0,"remove","APT::Get::Remove",0},
  3041. {0,"only-source","APT::Get::Only-Source",0},
  3042. {0,"arch-only","APT::Get::Arch-Only",0},
  3043. {0,"auto-remove","APT::Get::AutomaticRemove",0},
  3044. {0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0},
  3045. {0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean},
  3046. {0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean},
  3047. {0,"fix-policy","APT::Get::Fix-Policy-Broken",0},
  3048. {0,"solver","APT::Solver",CommandLine::HasArg},
  3049. {'c',"config-file",0,CommandLine::ConfigFile},
  3050. {'o',"option",0,CommandLine::ArbItem},
  3051. {0,0,0,0}};
  3052. CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
  3053. {"upgrade",&DoUpgrade},
  3054. {"install",&DoInstall},
  3055. {"remove",&DoInstall},
  3056. {"purge",&DoInstall},
  3057. {"autoremove",&DoInstall},
  3058. {"markauto",&DoMarkAuto},
  3059. {"unmarkauto",&DoMarkAuto},
  3060. {"dist-upgrade",&DoDistUpgrade},
  3061. {"dselect-upgrade",&DoDSelectUpgrade},
  3062. {"build-dep",&DoBuildDep},
  3063. {"clean",&DoClean},
  3064. {"autoclean",&DoAutoClean},
  3065. {"check",&DoCheck},
  3066. {"source",&DoSource},
  3067. {"download",&DoDownload},
  3068. {"changelog",&DoChangelog},
  3069. {"moo",&DoMoo},
  3070. {"help",&ShowHelp},
  3071. {0,0}};
  3072. // Set up gettext support
  3073. setlocale(LC_ALL,"");
  3074. textdomain(PACKAGE);
  3075. // Parse the command line and initialize the package library
  3076. CommandLine CmdL(Args,_config);
  3077. if (pkgInitConfig(*_config) == false ||
  3078. CmdL.Parse(argc,argv) == false ||
  3079. pkgInitSystem(*_config,_system) == false)
  3080. {
  3081. if (_config->FindB("version") == true)
  3082. ShowHelp(CmdL);
  3083. _error->DumpErrors();
  3084. return 100;
  3085. }
  3086. // See if the help should be shown
  3087. if (_config->FindB("help") == true ||
  3088. _config->FindB("version") == true ||
  3089. CmdL.FileSize() == 0)
  3090. {
  3091. ShowHelp(CmdL);
  3092. return 0;
  3093. }
  3094. // simulate user-friendly if apt-get has no root privileges
  3095. if (getuid() != 0 && _config->FindB("APT::Get::Simulate") == true &&
  3096. (CmdL.FileSize() == 0 ||
  3097. (strcmp(CmdL.FileList[0], "source") != 0 && strcmp(CmdL.FileList[0], "download") != 0 &&
  3098. strcmp(CmdL.FileList[0], "changelog") != 0)))
  3099. {
  3100. if (_config->FindB("APT::Get::Show-User-Simulation-Note",true) == true)
  3101. cout << _("NOTE: This is only a simulation!\n"
  3102. " apt-get needs root privileges for real execution.\n"
  3103. " Keep also in mind that locking is deactivated,\n"
  3104. " so don't depend on the relevance to the real current situation!"
  3105. ) << std::endl;
  3106. _config->Set("Debug::NoLocking",true);
  3107. }
  3108. // Deal with stdout not being a tty
  3109. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  3110. _config->Set("quiet","1");
  3111. // Setup the output streams
  3112. c0out.rdbuf(cout.rdbuf());
  3113. c1out.rdbuf(cout.rdbuf());
  3114. c2out.rdbuf(cout.rdbuf());
  3115. if (_config->FindI("quiet",0) > 0)
  3116. c0out.rdbuf(devnull.rdbuf());
  3117. if (_config->FindI("quiet",0) > 1)
  3118. c1out.rdbuf(devnull.rdbuf());
  3119. // Setup the signals
  3120. signal(SIGPIPE,SIG_IGN);
  3121. signal(SIGWINCH,SigWinch);
  3122. SigWinch(0);
  3123. // Match the operation
  3124. CmdL.DispatchArg(Cmds);
  3125. // Print any errors or warnings found during parsing
  3126. bool const Errors = _error->PendingError();
  3127. if (_config->FindI("quiet",0) > 0)
  3128. _error->DumpErrors();
  3129. else
  3130. _error->DumpErrors(GlobalError::DEBUG);
  3131. return Errors == true ? 100 : 0;
  3132. }
  3133. /*}}}*/