apt-get.cc 88 KB

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