apt-get.cc 78 KB

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