apt-get.cc 87 KB

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