apt-get.cc 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  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()
  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 true;
  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. /*}}}*/
  590. // InstallPackages - Actually download and install the packages /*{{{*/
  591. // ---------------------------------------------------------------------
  592. /* This displays the informative messages describing what is going to
  593. happen and then calls the download routines */
  594. bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
  595. bool Safety = true)
  596. {
  597. if (_config->FindB("APT::Get::Purge",false) == true)
  598. {
  599. pkgCache::PkgIterator I = Cache->PkgBegin();
  600. for (; I.end() == false; I++)
  601. {
  602. if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
  603. Cache->MarkDelete(I,true);
  604. }
  605. }
  606. bool Fail = false;
  607. bool Essential = false;
  608. // Show all the various warning indicators
  609. ShowDel(c1out,Cache);
  610. ShowNew(c1out,Cache);
  611. if (ShwKept == true)
  612. ShowKept(c1out,Cache);
  613. Fail |= !ShowHold(c1out,Cache);
  614. if (_config->FindB("APT::Get::Show-Upgraded",true) == true)
  615. ShowUpgraded(c1out,Cache);
  616. Fail |= !ShowDowngraded(c1out,Cache);
  617. if (_config->FindB("APT::Get::Download-Only",false) == false)
  618. Essential = !ShowEssential(c1out,Cache);
  619. Fail |= Essential;
  620. Stats(c1out,Cache);
  621. // Sanity check
  622. if (Cache->BrokenCount() != 0)
  623. {
  624. ShowBroken(c1out,Cache,false);
  625. return _error->Error("Internal Error, InstallPackages was called with broken packages!");
  626. }
  627. if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
  628. Cache->BadCount() == 0)
  629. return true;
  630. // No remove flag
  631. if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
  632. return _error->Error(_("Packages need to be removed but Remove is disabled."));
  633. // Run the simulator ..
  634. if (_config->FindB("APT::Get::Simulate") == true)
  635. {
  636. pkgSimulate PM(Cache);
  637. pkgPackageManager::OrderResult Res = PM.DoInstall();
  638. if (Res == pkgPackageManager::Failed)
  639. return false;
  640. if (Res != pkgPackageManager::Completed)
  641. return _error->Error("Internal Error, Ordering didn't finish");
  642. return true;
  643. }
  644. // Create the text record parser
  645. pkgRecords Recs(Cache);
  646. if (_error->PendingError() == true)
  647. return false;
  648. // Lock the archive directory
  649. FileFd Lock;
  650. if (_config->FindB("Debug::NoLocking",false) == false &&
  651. _config->FindB("APT::Get::Print-URIs") == false)
  652. {
  653. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  654. if (_error->PendingError() == true)
  655. return _error->Error(_("Unable to lock the download directory"));
  656. }
  657. // Create the download object
  658. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  659. pkgAcquire Fetcher(&Stat);
  660. // Read the source list
  661. pkgSourceList List;
  662. if (List.ReadMainList() == false)
  663. return _error->Error(_("The list of sources could not be read."));
  664. // Create the package manager and prepare to download
  665. SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
  666. if (PM->GetArchives(&Fetcher,&List,&Recs) == false ||
  667. _error->PendingError() == true)
  668. return false;
  669. // Display statistics
  670. double FetchBytes = Fetcher.FetchNeeded();
  671. double FetchPBytes = Fetcher.PartialPresent();
  672. double DebBytes = Fetcher.TotalNeeded();
  673. if (DebBytes != Cache->DebSize())
  674. {
  675. c0out << DebBytes << ',' << Cache->DebSize() << endl;
  676. c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl;
  677. }
  678. // Number of bytes
  679. if (DebBytes != FetchBytes)
  680. ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"),
  681. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  682. else
  683. ioprintf(c1out,_("Need to get %sB of archives.\n"),
  684. SizeToStr(DebBytes).c_str());
  685. // Size delta
  686. if (Cache->UsrSize() >= 0)
  687. ioprintf(c1out,_("After unpacking %sB of additional disk space will be used.\n"),
  688. SizeToStr(Cache->UsrSize()).c_str());
  689. else
  690. ioprintf(c1out,_("After unpacking %sB disk space will be freed.\n"),
  691. SizeToStr(-1*Cache->UsrSize()).c_str());
  692. if (_error->PendingError() == true)
  693. return false;
  694. /* Check for enough free space, but only if we are actually going to
  695. download */
  696. if (_config->FindB("APT::Get::Print-URIs") == false &&
  697. _config->FindB("APT::Get::Download",true) == true)
  698. {
  699. struct statvfs Buf;
  700. string OutputDir = _config->FindDir("Dir::Cache::Archives");
  701. if (statvfs(OutputDir.c_str(),&Buf) != 0)
  702. return _error->Errno("statvfs","Couldn't determine free space in %s",
  703. OutputDir.c_str());
  704. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  705. return _error->Error(_("You don't have enough free space in %s."),
  706. OutputDir.c_str());
  707. }
  708. // Fail safe check
  709. if (_config->FindI("quiet",0) >= 2 ||
  710. _config->FindB("APT::Get::Assume-Yes",false) == true)
  711. {
  712. if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
  713. return _error->Error(_("There are problems and -y was used without --force-yes"));
  714. }
  715. if (Essential == true && Safety == true)
  716. {
  717. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  718. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  719. const char *Prompt = _("Yes, do as I say!");
  720. ioprintf(c2out,
  721. _("You are about to do something potentially harmful\n"
  722. "To continue type in the phrase '%s'\n"
  723. " ?] "),Prompt);
  724. c2out << flush;
  725. if (AnalPrompt(Prompt) == false)
  726. {
  727. c2out << _("Abort.") << endl;
  728. exit(1);
  729. }
  730. }
  731. else
  732. {
  733. // Prompt to continue
  734. if (Ask == true || Fail == true)
  735. {
  736. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  737. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  738. if (_config->FindI("quiet",0) < 2 &&
  739. _config->FindB("APT::Get::Assume-Yes",false) == false)
  740. {
  741. c2out << _("Do you want to continue? [Y/n] ") << flush;
  742. if (YnPrompt() == false)
  743. {
  744. c2out << _("Abort.") << endl;
  745. exit(1);
  746. }
  747. }
  748. }
  749. }
  750. // Just print out the uris an exit if the --print-uris flag was used
  751. if (_config->FindB("APT::Get::Print-URIs") == true)
  752. {
  753. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  754. for (; I != Fetcher.UriEnd(); I++)
  755. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  756. I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
  757. return true;
  758. }
  759. /* Unlock the dpkg lock if we are not going to be doing an install
  760. after. */
  761. if (_config->FindB("APT::Get::Download-Only",false) == true)
  762. _system->UnLock();
  763. // Run it
  764. while (1)
  765. {
  766. bool Transient = false;
  767. if (_config->FindB("APT::Get::Download",true) == false)
  768. {
  769. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
  770. {
  771. if ((*I)->Local == true)
  772. {
  773. I++;
  774. continue;
  775. }
  776. // Close the item and check if it was found in cache
  777. (*I)->Finished();
  778. if ((*I)->Complete == false)
  779. Transient = true;
  780. // Clear it out of the fetch list
  781. delete *I;
  782. I = Fetcher.ItemsBegin();
  783. }
  784. }
  785. if (Fetcher.Run() == pkgAcquire::Failed)
  786. return false;
  787. // Print out errors
  788. bool Failed = false;
  789. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  790. {
  791. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  792. (*I)->Complete == true)
  793. continue;
  794. if ((*I)->Status == pkgAcquire::Item::StatIdle)
  795. {
  796. Transient = true;
  797. // Failed = true;
  798. continue;
  799. }
  800. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  801. (*I)->ErrorText.c_str());
  802. Failed = true;
  803. }
  804. /* If we are in no download mode and missing files and there were
  805. 'failures' then the user must specify -m. Furthermore, there
  806. is no such thing as a transient error in no-download mode! */
  807. if (Transient == true &&
  808. _config->FindB("APT::Get::Download",true) == false)
  809. {
  810. Transient = false;
  811. Failed = true;
  812. }
  813. if (_config->FindB("APT::Get::Download-Only",false) == true)
  814. {
  815. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  816. return _error->Error(_("Some files failed to download"));
  817. c1out << _("Download complete and in download only mode") << endl;
  818. return true;
  819. }
  820. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  821. {
  822. return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
  823. }
  824. if (Transient == true && Failed == true)
  825. return _error->Error(_("--fix-missing and media swapping is not currently supported"));
  826. // Try to deal with missing package files
  827. if (Failed == true && PM->FixMissing() == false)
  828. {
  829. cerr << _("Unable to correct missing packages.") << endl;
  830. return _error->Error(_("Aborting Install."));
  831. }
  832. _system->UnLock();
  833. pkgPackageManager::OrderResult Res = PM->DoInstall();
  834. if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
  835. return false;
  836. if (Res == pkgPackageManager::Completed)
  837. return true;
  838. // Reload the fetcher object and loop again for media swapping
  839. Fetcher.Shutdown();
  840. if (PM->GetArchives(&Fetcher,&List,&Recs) == false)
  841. return false;
  842. _system->Lock();
  843. }
  844. }
  845. /*}}}*/
  846. // TryToInstall - Try to install a single package /*{{{*/
  847. // ---------------------------------------------------------------------
  848. /* This used to be inlined in DoInstall, but with the advent of regex package
  849. name matching it was split out.. */
  850. bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
  851. pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
  852. unsigned int &ExpectedInst,bool AllowFail = true)
  853. {
  854. /* This is a pure virtual package and there is a single available
  855. provides */
  856. if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0 &&
  857. Pkg.ProvidesList()->NextProvides == 0)
  858. {
  859. pkgCache::PkgIterator Tmp = Pkg.ProvidesList().OwnerPkg();
  860. ioprintf(c1out,_("Note, selecting %s instead of %s\n"),
  861. Tmp.Name(),Pkg.Name());
  862. Pkg = Tmp;
  863. }
  864. // Handle the no-upgrade case
  865. if (_config->FindB("APT::Get::upgrade",true) == false &&
  866. Pkg->CurrentVer != 0)
  867. {
  868. if (AllowFail == true)
  869. ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
  870. Pkg.Name());
  871. return true;
  872. }
  873. // Check if there is something at all to install
  874. pkgDepCache::StateCache &State = Cache[Pkg];
  875. if (Remove == true && Pkg->CurrentVer == 0)
  876. {
  877. Fix.Clear(Pkg);
  878. Fix.Protect(Pkg);
  879. Fix.Remove(Pkg);
  880. /* We want to continue searching for regex hits, so we return false here
  881. otherwise this is not really an error. */
  882. if (AllowFail == false)
  883. return false;
  884. ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.Name());
  885. return true;
  886. }
  887. if (State.CandidateVer == 0 && Remove == false)
  888. {
  889. if (AllowFail == false)
  890. return false;
  891. if (Pkg->ProvidesList != 0)
  892. {
  893. ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
  894. Pkg.Name());
  895. pkgCache::PrvIterator I = Pkg.ProvidesList();
  896. for (; I.end() == false; I++)
  897. {
  898. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  899. if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer())
  900. {
  901. if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
  902. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
  903. _(" [Installed]") << endl;
  904. else
  905. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
  906. }
  907. }
  908. c1out << _("You should explicitly select one to install.") << endl;
  909. }
  910. else
  911. {
  912. ioprintf(c1out,
  913. _("Package %s is not available, but is referred to by another package.\n"
  914. "This may mean that the package is missing, has been obsoleted, or\n"
  915. "is only available from another source\n"),Pkg.Name());
  916. string List;
  917. string VersionsList;
  918. SPtrArray<bool> Seen = new bool[Cache.Head().PackageCount];
  919. memset(Seen,0,Cache.Head().PackageCount*sizeof(*Seen));
  920. pkgCache::DepIterator Dep = Pkg.RevDependsList();
  921. for (; Dep.end() == false; Dep++)
  922. {
  923. if (Dep->Type != pkgCache::Dep::Replaces)
  924. continue;
  925. if (Seen[Dep.ParentPkg()->ID] == true)
  926. continue;
  927. Seen[Dep.ParentPkg()->ID] = true;
  928. List += string(Dep.ParentPkg().Name()) + " ";
  929. //VersionsList += string(Dep.ParentPkg().CurVersion) + "\n"; ???
  930. }
  931. ShowList(c1out,_("However the following packages replace it:"),List,VersionsList);
  932. }
  933. _error->Error(_("Package %s has no installation candidate"),Pkg.Name());
  934. return false;
  935. }
  936. Fix.Clear(Pkg);
  937. Fix.Protect(Pkg);
  938. if (Remove == true)
  939. {
  940. Fix.Remove(Pkg);
  941. Cache.MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
  942. return true;
  943. }
  944. // Install it
  945. Cache.MarkInstall(Pkg,false);
  946. if (State.Install() == false)
  947. {
  948. if (_config->FindB("APT::Get::ReInstall",false) == true)
  949. {
  950. if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
  951. ioprintf(c1out,_("Reinstallation of %s is not possible, it cannot be downloaded.\n"),
  952. Pkg.Name());
  953. else
  954. Cache.SetReInstall(Pkg,true);
  955. }
  956. else
  957. {
  958. if (AllowFail == true)
  959. ioprintf(c1out,_("%s is already the newest version.\n"),
  960. Pkg.Name());
  961. }
  962. }
  963. else
  964. ExpectedInst++;
  965. // Install it with autoinstalling enabled.
  966. if (State.InstBroken() == true && BrokenFix == false)
  967. Cache.MarkInstall(Pkg,true);
  968. return true;
  969. }
  970. /*}}}*/
  971. // TryToChangeVer - Try to change a candidate version /*{{{*/
  972. // ---------------------------------------------------------------------
  973. /* */
  974. bool TryToChangeVer(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
  975. const char *VerTag,bool IsRel)
  976. {
  977. pkgVersionMatch Match(VerTag,(IsRel == true?pkgVersionMatch::Release :
  978. pkgVersionMatch::Version));
  979. pkgCache::VerIterator Ver = Match.Find(Pkg);
  980. if (Ver.end() == true)
  981. {
  982. if (IsRel == true)
  983. return _error->Error(_("Release '%s' for '%s' was not found"),
  984. VerTag,Pkg.Name());
  985. return _error->Error(_("Version '%s' for '%s' was not found"),
  986. VerTag,Pkg.Name());
  987. }
  988. if (strcmp(VerTag,Ver.VerStr()) != 0)
  989. {
  990. ioprintf(c1out,_("Selected version %s (%s) for %s\n"),
  991. Ver.VerStr(),Ver.RelStr().c_str(),Pkg.Name());
  992. }
  993. Cache.SetCandidateVersion(Ver);
  994. return true;
  995. }
  996. /*}}}*/
  997. // FindSrc - Find a source record /*{{{*/
  998. // ---------------------------------------------------------------------
  999. /* */
  1000. pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
  1001. pkgSrcRecords &SrcRecs,string &Src,
  1002. pkgDepCache &Cache)
  1003. {
  1004. // We want to pull the version off the package specification..
  1005. string VerTag;
  1006. string TmpSrc = Name;
  1007. string::size_type Slash = TmpSrc.rfind('=');
  1008. if (Slash != string::npos)
  1009. {
  1010. VerTag = string(TmpSrc.begin() + Slash + 1,TmpSrc.end());
  1011. TmpSrc = string(TmpSrc.begin(),TmpSrc.begin() + Slash);
  1012. }
  1013. /* Lookup the version of the package we would install if we were to
  1014. install a version and determine the source package name, then look
  1015. in the archive for a source package of the same name. In theory
  1016. we could stash the version string as well and match that too but
  1017. today there aren't multi source versions in the archive. */
  1018. if (_config->FindB("APT::Get::Only-Source") == false &&
  1019. VerTag.empty() == true)
  1020. {
  1021. pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
  1022. if (Pkg.end() == false)
  1023. {
  1024. pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
  1025. if (Ver.end() == false)
  1026. {
  1027. pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
  1028. Src = Parse.SourcePkg();
  1029. }
  1030. }
  1031. }
  1032. // No source package name..
  1033. if (Src.empty() == true)
  1034. Src = TmpSrc;
  1035. // The best hit
  1036. pkgSrcRecords::Parser *Last = 0;
  1037. unsigned long Offset = 0;
  1038. string Version;
  1039. bool IsMatch = false;
  1040. // If we are matching by version then we need exact matches to be happy
  1041. if (VerTag.empty() == false)
  1042. IsMatch = true;
  1043. /* Iterate over all of the hits, which includes the resulting
  1044. binary packages in the search */
  1045. pkgSrcRecords::Parser *Parse;
  1046. SrcRecs.Restart();
  1047. while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
  1048. {
  1049. string Ver = Parse->Version();
  1050. // Skip name mismatches
  1051. if (IsMatch == true && Parse->Package() != Src)
  1052. continue;
  1053. if (VerTag.empty() == false)
  1054. {
  1055. /* Don't want to fall through because we are doing exact version
  1056. matching. */
  1057. if (Cache.VS().CmpVersion(VerTag,Ver) != 0)
  1058. continue;
  1059. Last = Parse;
  1060. Offset = Parse->Offset();
  1061. break;
  1062. }
  1063. // Newer version or an exact match
  1064. if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0 ||
  1065. (Parse->Package() == Src && IsMatch == false))
  1066. {
  1067. IsMatch = Parse->Package() == Src;
  1068. Last = Parse;
  1069. Offset = Parse->Offset();
  1070. Version = Ver;
  1071. }
  1072. }
  1073. if (Last == 0)
  1074. return 0;
  1075. if (Last->Jump(Offset) == false)
  1076. return 0;
  1077. return Last;
  1078. }
  1079. /*}}}*/
  1080. // DoUpdate - Update the package lists /*{{{*/
  1081. // ---------------------------------------------------------------------
  1082. /* */
  1083. bool DoUpdate(CommandLine &CmdL)
  1084. {
  1085. if (CmdL.FileSize() != 1)
  1086. return _error->Error(_("The update command takes no arguments"));
  1087. // Get the source list
  1088. pkgSourceList List;
  1089. if (List.ReadMainList() == false)
  1090. return false;
  1091. // Lock the list directory
  1092. FileFd Lock;
  1093. if (_config->FindB("Debug::NoLocking",false) == false)
  1094. {
  1095. Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
  1096. if (_error->PendingError() == true)
  1097. return _error->Error(_("Unable to lock the list directory"));
  1098. }
  1099. // Create the download object
  1100. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1101. pkgAcquire Fetcher(&Stat);
  1102. // Populate it with the source selection
  1103. if (List.GetIndexes(&Fetcher) == false)
  1104. return false;
  1105. // Just print out the uris an exit if the --print-uris flag was used
  1106. if (_config->FindB("APT::Get::Print-URIs") == true)
  1107. {
  1108. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  1109. for (; I != Fetcher.UriEnd(); I++)
  1110. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  1111. I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
  1112. return true;
  1113. }
  1114. // Run it
  1115. if (Fetcher.Run() == pkgAcquire::Failed)
  1116. return false;
  1117. bool Failed = false;
  1118. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  1119. {
  1120. if ((*I)->Status == pkgAcquire::Item::StatDone)
  1121. continue;
  1122. (*I)->Finished();
  1123. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  1124. (*I)->ErrorText.c_str());
  1125. Failed = true;
  1126. }
  1127. // Clean out any old list files
  1128. if (_config->FindB("APT::Get::List-Cleanup",true) == true)
  1129. {
  1130. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  1131. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  1132. return false;
  1133. }
  1134. // Prepare the cache.
  1135. CacheFile Cache;
  1136. if (Cache.BuildCaches() == false)
  1137. return false;
  1138. if (Failed == true)
  1139. return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  1140. return true;
  1141. }
  1142. /*}}}*/
  1143. // DoUpgrade - Upgrade all packages /*{{{*/
  1144. // ---------------------------------------------------------------------
  1145. /* Upgrade all packages without installing new packages or erasing old
  1146. packages */
  1147. bool DoUpgrade(CommandLine &CmdL)
  1148. {
  1149. CacheFile Cache;
  1150. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1151. return false;
  1152. // Do the upgrade
  1153. if (pkgAllUpgrade(Cache) == false)
  1154. {
  1155. ShowBroken(c1out,Cache,false);
  1156. return _error->Error(_("Internal Error, AllUpgrade broke stuff"));
  1157. }
  1158. return InstallPackages(Cache,true);
  1159. }
  1160. /*}}}*/
  1161. // DoInstall - Install packages from the command line /*{{{*/
  1162. // ---------------------------------------------------------------------
  1163. /* Install named packages */
  1164. bool DoInstall(CommandLine &CmdL)
  1165. {
  1166. CacheFile Cache;
  1167. if (Cache.OpenForInstall() == false ||
  1168. Cache.CheckDeps(CmdL.FileSize() != 1) == false)
  1169. return false;
  1170. // Enter the special broken fixing mode if the user specified arguments
  1171. bool BrokenFix = false;
  1172. if (Cache->BrokenCount() != 0)
  1173. BrokenFix = true;
  1174. unsigned int ExpectedInst = 0;
  1175. unsigned int Packages = 0;
  1176. pkgProblemResolver Fix(Cache);
  1177. bool DefRemove = false;
  1178. if (strcasecmp(CmdL.FileList[0],"remove") == 0)
  1179. DefRemove = true;
  1180. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  1181. {
  1182. // Duplicate the string
  1183. unsigned int Length = strlen(*I);
  1184. char S[300];
  1185. if (Length >= sizeof(S))
  1186. continue;
  1187. strcpy(S,*I);
  1188. // See if we are removing and special indicators..
  1189. bool Remove = DefRemove;
  1190. char *VerTag = 0;
  1191. bool VerIsRel = false;
  1192. while (Cache->FindPkg(S).end() == true)
  1193. {
  1194. // Handle an optional end tag indicating what to do
  1195. if (Length >= 1 && S[Length - 1] == '-')
  1196. {
  1197. Remove = true;
  1198. S[--Length] = 0;
  1199. continue;
  1200. }
  1201. if (Length >= 1 && S[Length - 1] == '+')
  1202. {
  1203. Remove = false;
  1204. S[--Length] = 0;
  1205. continue;
  1206. }
  1207. char *Slash = strchr(S,'=');
  1208. if (Slash != 0)
  1209. {
  1210. VerIsRel = false;
  1211. *Slash = 0;
  1212. VerTag = Slash + 1;
  1213. }
  1214. Slash = strchr(S,'/');
  1215. if (Slash != 0)
  1216. {
  1217. VerIsRel = true;
  1218. *Slash = 0;
  1219. VerTag = Slash + 1;
  1220. }
  1221. break;
  1222. }
  1223. // Locate the package
  1224. pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
  1225. Packages++;
  1226. if (Pkg.end() == true)
  1227. {
  1228. // Check if the name is a regex
  1229. const char *I;
  1230. for (I = S; *I != 0; I++)
  1231. if (*I == '?' || *I == '*' || *I == '|' ||
  1232. *I == '[' || *I == '^' || *I == '$')
  1233. break;
  1234. if (*I == 0)
  1235. return _error->Error(_("Couldn't find package %s"),S);
  1236. // Regexs must always be confirmed
  1237. ExpectedInst += 1000;
  1238. // Compile the regex pattern
  1239. regex_t Pattern;
  1240. int Res;
  1241. if ((Res = regcomp(&Pattern,S,REG_EXTENDED | REG_ICASE |
  1242. REG_NOSUB)) != 0)
  1243. {
  1244. char Error[300];
  1245. regerror(Res,&Pattern,Error,sizeof(Error));
  1246. return _error->Error(_("Regex compilation error - %s"),Error);
  1247. }
  1248. // Run over the matches
  1249. bool Hit = false;
  1250. for (Pkg = Cache->PkgBegin(); Pkg.end() == false; Pkg++)
  1251. {
  1252. if (regexec(&Pattern,Pkg.Name(),0,0,0) != 0)
  1253. continue;
  1254. ioprintf(c1out,_("Note, selecting %s for regex '%s'\n"),
  1255. Pkg.Name(),S);
  1256. if (VerTag != 0)
  1257. if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
  1258. return false;
  1259. Hit |= TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,
  1260. ExpectedInst,false);
  1261. }
  1262. regfree(&Pattern);
  1263. if (Hit == false)
  1264. return _error->Error(_("Couldn't find package %s"),S);
  1265. }
  1266. else
  1267. {
  1268. if (VerTag != 0)
  1269. if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
  1270. return false;
  1271. if (TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,ExpectedInst) == false)
  1272. return false;
  1273. }
  1274. }
  1275. /* If we are in the Broken fixing mode we do not attempt to fix the
  1276. problems. This is if the user invoked install without -f and gave
  1277. packages */
  1278. if (BrokenFix == true && Cache->BrokenCount() != 0)
  1279. {
  1280. c1out << _("You might want to run `apt-get -f install' to correct these:") << endl;
  1281. ShowBroken(c1out,Cache,false);
  1282. return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
  1283. }
  1284. // Call the scored problem resolver
  1285. Fix.InstallProtect();
  1286. if (Fix.Resolve(true) == false)
  1287. _error->Discard();
  1288. // Now we check the state of the packages,
  1289. if (Cache->BrokenCount() != 0)
  1290. {
  1291. c1out <<
  1292. _("Some packages could not be installed. This may mean that you have\n"
  1293. "requested an impossible situation or if you are using the unstable\n"
  1294. "distribution that some required packages have not yet been created\n"
  1295. "or been moved out of Incoming.") << endl;
  1296. if (Packages == 1)
  1297. {
  1298. c1out << endl;
  1299. c1out <<
  1300. _("Since you only requested a single operation it is extremely likely that\n"
  1301. "the package is simply not installable and a bug report against\n"
  1302. "that package should be filed.") << endl;
  1303. }
  1304. c1out << _("The following information may help to resolve the situation:") << endl;
  1305. c1out << endl;
  1306. ShowBroken(c1out,Cache,false);
  1307. return _error->Error(_("Broken packages"));
  1308. }
  1309. /* Print out a list of packages that are going to be installed extra
  1310. to what the user asked */
  1311. if (Cache->InstCount() != ExpectedInst)
  1312. {
  1313. string List;
  1314. string VersionsList;
  1315. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  1316. {
  1317. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  1318. if ((*Cache)[I].Install() == false)
  1319. continue;
  1320. const char **J;
  1321. for (J = CmdL.FileList + 1; *J != 0; J++)
  1322. if (strcmp(*J,I.Name()) == 0)
  1323. break;
  1324. if (*J == 0) {
  1325. List += string(I.Name()) + " ";
  1326. VersionsList += string(Cache[I].CandVersion) + "\n";
  1327. }
  1328. }
  1329. ShowList(c1out,_("The following extra packages will be installed:"),List,VersionsList);
  1330. }
  1331. /* Print out a list of suggested and recommended packages */
  1332. {
  1333. string SuggestsList, RecommendsList, List;
  1334. string SuggestsVersions, RecommendsVersions;
  1335. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  1336. {
  1337. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  1338. /* Just look at the ones we want to install */
  1339. if ((*Cache)[I].Install() == false)
  1340. continue;
  1341. for (pkgCache::VerIterator V = I.VersionList(); V.end() == false; V++)
  1342. {
  1343. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; )
  1344. {
  1345. pkgCache::DepIterator Start;
  1346. pkgCache::DepIterator End;
  1347. D.GlobOr(Start,End); // advances D
  1348. /*
  1349. * If this is a virtual package, we need to check the list of
  1350. * packages that provide it and see if any of those are
  1351. * installed
  1352. */
  1353. bool providedBySomething = false;
  1354. for (pkgCache::PrvIterator Prv = Start.TargetPkg().ProvidesList();
  1355. Prv.end() != true;
  1356. Prv++)
  1357. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  1358. {
  1359. providedBySomething = true;
  1360. break;
  1361. }
  1362. if (providedBySomething) continue;
  1363. for(;;)
  1364. {
  1365. /* Skip if package is installed already, or is about to be */
  1366. string target = string(Start.TargetPkg().Name()) + " ";
  1367. if ((*Start.TargetPkg()).SelectedState == pkgCache::State::Install
  1368. || Cache[Start.TargetPkg()].Install())
  1369. break;
  1370. /* Skip if we already saw it */
  1371. if (int(SuggestsList.find(target)) != -1 || int(RecommendsList.find(target)) != -1)
  1372. break;
  1373. if (Start->Type == pkgCache::Dep::Suggests) {
  1374. SuggestsList += target;
  1375. SuggestsVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
  1376. }
  1377. if (Start->Type == pkgCache::Dep::Recommends) {
  1378. RecommendsList += target;
  1379. RecommendsVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
  1380. }
  1381. if (Start >= End)
  1382. break;
  1383. Start++;
  1384. }
  1385. }
  1386. }
  1387. }
  1388. ShowList(c1out,_("Suggested packages:"),SuggestsList,SuggestsVersions);
  1389. ShowList(c1out,_("Recommended packages:"),RecommendsList,RecommendsVersions);
  1390. }
  1391. // See if we need to prompt
  1392. if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
  1393. return InstallPackages(Cache,false,false);
  1394. return InstallPackages(Cache,false);
  1395. }
  1396. /*}}}*/
  1397. // DoDistUpgrade - Automatic smart upgrader /*{{{*/
  1398. // ---------------------------------------------------------------------
  1399. /* Intelligent upgrader that will install and remove packages at will */
  1400. bool DoDistUpgrade(CommandLine &CmdL)
  1401. {
  1402. CacheFile Cache;
  1403. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1404. return false;
  1405. c0out << _("Calculating Upgrade... ") << flush;
  1406. if (pkgDistUpgrade(*Cache) == false)
  1407. {
  1408. c0out << _("Failed") << endl;
  1409. ShowBroken(c1out,Cache,false);
  1410. return false;
  1411. }
  1412. c0out << _("Done") << endl;
  1413. return InstallPackages(Cache,true);
  1414. }
  1415. /*}}}*/
  1416. // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
  1417. // ---------------------------------------------------------------------
  1418. /* Follows dselect's selections */
  1419. bool DoDSelectUpgrade(CommandLine &CmdL)
  1420. {
  1421. CacheFile Cache;
  1422. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1423. return false;
  1424. // Install everything with the install flag set
  1425. pkgCache::PkgIterator I = Cache->PkgBegin();
  1426. for (;I.end() != true; I++)
  1427. {
  1428. /* Install the package only if it is a new install, the autoupgrader
  1429. will deal with the rest */
  1430. if (I->SelectedState == pkgCache::State::Install)
  1431. Cache->MarkInstall(I,false);
  1432. }
  1433. /* Now install their deps too, if we do this above then order of
  1434. the status file is significant for | groups */
  1435. for (I = Cache->PkgBegin();I.end() != true; I++)
  1436. {
  1437. /* Install the package only if it is a new install, the autoupgrader
  1438. will deal with the rest */
  1439. if (I->SelectedState == pkgCache::State::Install)
  1440. Cache->MarkInstall(I,true);
  1441. }
  1442. // Apply erasures now, they override everything else.
  1443. for (I = Cache->PkgBegin();I.end() != true; I++)
  1444. {
  1445. // Remove packages
  1446. if (I->SelectedState == pkgCache::State::DeInstall ||
  1447. I->SelectedState == pkgCache::State::Purge)
  1448. Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
  1449. }
  1450. /* Resolve any problems that dselect created, allupgrade cannot handle
  1451. such things. We do so quite agressively too.. */
  1452. if (Cache->BrokenCount() != 0)
  1453. {
  1454. pkgProblemResolver Fix(Cache);
  1455. // Hold back held packages.
  1456. if (_config->FindB("APT::Ignore-Hold",false) == false)
  1457. {
  1458. for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
  1459. {
  1460. if (I->SelectedState == pkgCache::State::Hold)
  1461. {
  1462. Fix.Protect(I);
  1463. Cache->MarkKeep(I);
  1464. }
  1465. }
  1466. }
  1467. if (Fix.Resolve() == false)
  1468. {
  1469. ShowBroken(c1out,Cache,false);
  1470. return _error->Error("Internal Error, problem resolver broke stuff");
  1471. }
  1472. }
  1473. // Now upgrade everything
  1474. if (pkgAllUpgrade(Cache) == false)
  1475. {
  1476. ShowBroken(c1out,Cache,false);
  1477. return _error->Error("Internal Error, problem resolver broke stuff");
  1478. }
  1479. return InstallPackages(Cache,false);
  1480. }
  1481. /*}}}*/
  1482. // DoClean - Remove download archives /*{{{*/
  1483. // ---------------------------------------------------------------------
  1484. /* */
  1485. bool DoClean(CommandLine &CmdL)
  1486. {
  1487. if (_config->FindB("APT::Get::Simulate") == true)
  1488. {
  1489. cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
  1490. _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
  1491. return true;
  1492. }
  1493. // Lock the archive directory
  1494. FileFd Lock;
  1495. if (_config->FindB("Debug::NoLocking",false) == false)
  1496. {
  1497. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1498. if (_error->PendingError() == true)
  1499. return _error->Error(_("Unable to lock the download directory"));
  1500. }
  1501. pkgAcquire Fetcher;
  1502. Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
  1503. Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
  1504. return true;
  1505. }
  1506. /*}}}*/
  1507. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  1508. // ---------------------------------------------------------------------
  1509. /* This is similar to clean but it only purges things that cannot be
  1510. downloaded, that is old versions of cached packages. */
  1511. class LogCleaner : public pkgArchiveCleaner
  1512. {
  1513. protected:
  1514. virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
  1515. {
  1516. c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
  1517. if (_config->FindB("APT::Get::Simulate") == false)
  1518. unlink(File);
  1519. };
  1520. };
  1521. bool DoAutoClean(CommandLine &CmdL)
  1522. {
  1523. // Lock the archive directory
  1524. FileFd Lock;
  1525. if (_config->FindB("Debug::NoLocking",false) == false)
  1526. {
  1527. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1528. if (_error->PendingError() == true)
  1529. return _error->Error(_("Unable to lock the download directory"));
  1530. }
  1531. CacheFile Cache;
  1532. if (Cache.Open() == false)
  1533. return false;
  1534. LogCleaner Cleaner;
  1535. return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
  1536. Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
  1537. }
  1538. /*}}}*/
  1539. // DoCheck - Perform the check operation /*{{{*/
  1540. // ---------------------------------------------------------------------
  1541. /* Opening automatically checks the system, this command is mostly used
  1542. for debugging */
  1543. bool DoCheck(CommandLine &CmdL)
  1544. {
  1545. CacheFile Cache;
  1546. Cache.Open();
  1547. Cache.CheckDeps();
  1548. return true;
  1549. }
  1550. /*}}}*/
  1551. // DoSource - Fetch a source archive /*{{{*/
  1552. // ---------------------------------------------------------------------
  1553. /* Fetch souce packages */
  1554. struct DscFile
  1555. {
  1556. string Package;
  1557. string Version;
  1558. string Dsc;
  1559. };
  1560. bool DoSource(CommandLine &CmdL)
  1561. {
  1562. CacheFile Cache;
  1563. if (Cache.Open(false) == false)
  1564. return false;
  1565. if (CmdL.FileSize() <= 1)
  1566. return _error->Error(_("Must specify at least one package to fetch source for"));
  1567. // Read the source list
  1568. pkgSourceList List;
  1569. if (List.ReadMainList() == false)
  1570. return _error->Error(_("The list of sources could not be read."));
  1571. // Create the text record parsers
  1572. pkgRecords Recs(Cache);
  1573. pkgSrcRecords SrcRecs(List);
  1574. if (_error->PendingError() == true)
  1575. return false;
  1576. // Create the download object
  1577. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1578. pkgAcquire Fetcher(&Stat);
  1579. DscFile *Dsc = new DscFile[CmdL.FileSize()];
  1580. // Load the requestd sources into the fetcher
  1581. unsigned J = 0;
  1582. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  1583. {
  1584. string Src;
  1585. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  1586. if (Last == 0)
  1587. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  1588. // Back track
  1589. vector<pkgSrcRecords::File> Lst;
  1590. if (Last->Files(Lst) == false)
  1591. return false;
  1592. // Load them into the fetcher
  1593. for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
  1594. I != Lst.end(); I++)
  1595. {
  1596. // Try to guess what sort of file it is we are getting.
  1597. if (I->Type == "dsc")
  1598. {
  1599. Dsc[J].Package = Last->Package();
  1600. Dsc[J].Version = Last->Version();
  1601. Dsc[J].Dsc = flNotDir(I->Path);
  1602. }
  1603. // Diff only mode only fetches .diff files
  1604. if (_config->FindB("APT::Get::Diff-Only",false) == true &&
  1605. I->Type != "diff")
  1606. continue;
  1607. // Tar only mode only fetches .tar files
  1608. if (_config->FindB("APT::Get::Tar-Only",false) == true &&
  1609. I->Type != "tar")
  1610. continue;
  1611. new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
  1612. I->MD5Hash,I->Size,
  1613. Last->Index().SourceInfo(*Last,*I),Src);
  1614. }
  1615. }
  1616. // Display statistics
  1617. double FetchBytes = Fetcher.FetchNeeded();
  1618. double FetchPBytes = Fetcher.PartialPresent();
  1619. double DebBytes = Fetcher.TotalNeeded();
  1620. // Check for enough free space
  1621. struct statvfs Buf;
  1622. string OutputDir = ".";
  1623. if (statvfs(OutputDir.c_str(),&Buf) != 0)
  1624. return _error->Errno("statvfs","Couldn't determine free space in %s",
  1625. OutputDir.c_str());
  1626. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  1627. return _error->Error(_("You don't have enough free space in %s"),
  1628. OutputDir.c_str());
  1629. // Number of bytes
  1630. if (DebBytes != FetchBytes)
  1631. ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
  1632. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  1633. else
  1634. ioprintf(c1out,_("Need to get %sB of source archives.\n"),
  1635. SizeToStr(DebBytes).c_str());
  1636. if (_config->FindB("APT::Get::Simulate",false) == true)
  1637. {
  1638. for (unsigned I = 0; I != J; I++)
  1639. ioprintf(cout,_("Fetch Source %s\n"),Dsc[I].Package.c_str());
  1640. return true;
  1641. }
  1642. // Just print out the uris an exit if the --print-uris flag was used
  1643. if (_config->FindB("APT::Get::Print-URIs") == true)
  1644. {
  1645. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  1646. for (; I != Fetcher.UriEnd(); I++)
  1647. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  1648. I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
  1649. return true;
  1650. }
  1651. // Run it
  1652. if (Fetcher.Run() == pkgAcquire::Failed)
  1653. return false;
  1654. // Print error messages
  1655. bool Failed = false;
  1656. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  1657. {
  1658. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  1659. (*I)->Complete == true)
  1660. continue;
  1661. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  1662. (*I)->ErrorText.c_str());
  1663. Failed = true;
  1664. }
  1665. if (Failed == true)
  1666. return _error->Error(_("Failed to fetch some archives."));
  1667. if (_config->FindB("APT::Get::Download-only",false) == true)
  1668. {
  1669. c1out << _("Download complete and in download only mode") << endl;
  1670. return true;
  1671. }
  1672. // Unpack the sources
  1673. pid_t Process = ExecFork();
  1674. if (Process == 0)
  1675. {
  1676. for (unsigned I = 0; I != J; I++)
  1677. {
  1678. string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
  1679. // Diff only mode only fetches .diff files
  1680. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  1681. _config->FindB("APT::Get::Tar-Only",false) == true ||
  1682. Dsc[I].Dsc.empty() == true)
  1683. continue;
  1684. // See if the package is already unpacked
  1685. struct stat Stat;
  1686. if (stat(Dir.c_str(),&Stat) == 0 &&
  1687. S_ISDIR(Stat.st_mode) != 0)
  1688. {
  1689. ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
  1690. Dir.c_str());
  1691. }
  1692. else
  1693. {
  1694. // Call dpkg-source
  1695. char S[500];
  1696. snprintf(S,sizeof(S),"%s -x %s",
  1697. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  1698. Dsc[I].Dsc.c_str());
  1699. if (system(S) != 0)
  1700. {
  1701. fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
  1702. _exit(1);
  1703. }
  1704. }
  1705. // Try to compile it with dpkg-buildpackage
  1706. if (_config->FindB("APT::Get::Compile",false) == true)
  1707. {
  1708. // Call dpkg-buildpackage
  1709. char S[500];
  1710. snprintf(S,sizeof(S),"cd %s && %s %s",
  1711. Dir.c_str(),
  1712. _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
  1713. _config->Find("DPkg::Build-Options","-b -uc").c_str());
  1714. if (system(S) != 0)
  1715. {
  1716. fprintf(stderr,_("Build command '%s' failed.\n"),S);
  1717. _exit(1);
  1718. }
  1719. }
  1720. }
  1721. _exit(0);
  1722. }
  1723. // Wait for the subprocess
  1724. int Status = 0;
  1725. while (waitpid(Process,&Status,0) != Process)
  1726. {
  1727. if (errno == EINTR)
  1728. continue;
  1729. return _error->Errno("waitpid","Couldn't wait for subprocess");
  1730. }
  1731. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  1732. return _error->Error(_("Child process failed"));
  1733. return true;
  1734. }
  1735. /*}}}*/
  1736. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  1737. // ---------------------------------------------------------------------
  1738. /* This function will look at the build depends list of the given source
  1739. package and install the necessary packages to make it true, or fail. */
  1740. bool DoBuildDep(CommandLine &CmdL)
  1741. {
  1742. CacheFile Cache;
  1743. if (Cache.Open(true) == false)
  1744. return false;
  1745. if (CmdL.FileSize() <= 1)
  1746. return _error->Error(_("Must specify at least one package to check builddeps for"));
  1747. // Read the source list
  1748. pkgSourceList List;
  1749. if (List.ReadMainList() == false)
  1750. return _error->Error(_("The list of sources could not be read."));
  1751. // Create the text record parsers
  1752. pkgRecords Recs(Cache);
  1753. pkgSrcRecords SrcRecs(List);
  1754. if (_error->PendingError() == true)
  1755. return false;
  1756. // Create the download object
  1757. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1758. pkgAcquire Fetcher(&Stat);
  1759. unsigned J = 0;
  1760. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  1761. {
  1762. string Src;
  1763. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  1764. if (Last == 0)
  1765. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  1766. // Process the build-dependencies
  1767. vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  1768. if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only",false)) == false)
  1769. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  1770. // Also ensure that build-essential packages are present
  1771. Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
  1772. if (Opts)
  1773. Opts = Opts->Child;
  1774. for (; Opts; Opts = Opts->Next)
  1775. {
  1776. if (Opts->Value.empty() == true)
  1777. continue;
  1778. pkgSrcRecords::Parser::BuildDepRec rec;
  1779. rec.Package = Opts->Value;
  1780. rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
  1781. rec.Op = 0;
  1782. BuildDeps.push_back(rec);
  1783. }
  1784. if (BuildDeps.size() == 0)
  1785. {
  1786. ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
  1787. continue;
  1788. }
  1789. // Install the requested packages
  1790. unsigned int ExpectedInst = 0;
  1791. vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
  1792. pkgProblemResolver Fix(Cache);
  1793. bool skipAlternatives = false; // skip remaining alternatives in an or group
  1794. for (D = BuildDeps.begin(); D != BuildDeps.end(); D++)
  1795. {
  1796. bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
  1797. if (skipAlternatives == true)
  1798. {
  1799. if (!hasAlternatives)
  1800. skipAlternatives = false; // end of or group
  1801. continue;
  1802. }
  1803. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  1804. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  1805. {
  1806. pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
  1807. // Build-conflicts on unknown packages are silently ignored
  1808. if (Pkg.end() == true)
  1809. continue;
  1810. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  1811. /*
  1812. * Remove if we have an installed version that satisfies the
  1813. * version criteria
  1814. */
  1815. if (IV.end() == false &&
  1816. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  1817. TryToInstall(Pkg,Cache,Fix,true,false,ExpectedInst);
  1818. }
  1819. else // BuildDep || BuildDepIndep
  1820. {
  1821. pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
  1822. if (_config->FindB("Debug::BuildDeps",false) == true)
  1823. cout << "Looking for " << (*D).Package << "...\n";
  1824. if (Pkg.end() == true)
  1825. {
  1826. if (_config->FindB("Debug::BuildDeps",false) == true)
  1827. cout << " (not found)" << (*D).Package << endl;
  1828. if (hasAlternatives)
  1829. continue;
  1830. return _error->Error(_("%s dependency for %s cannot be satisfied "
  1831. "because the package %s cannot be found"),
  1832. Last->BuildDepType((*D).Type),Src.c_str(),
  1833. (*D).Package.c_str());
  1834. }
  1835. /*
  1836. * if there are alternatives, we've already picked one, so skip
  1837. * the rest
  1838. *
  1839. * TODO: this means that if there's a build-dep on A|B and B is
  1840. * installed, we'll still try to install A; more importantly,
  1841. * if A is currently broken, we cannot go back and try B. To fix
  1842. * this would require we do a Resolve cycle for each package we
  1843. * add to the install list. Ugh
  1844. */
  1845. /*
  1846. * If this is a virtual package, we need to check the list of
  1847. * packages that provide it and see if any of those are
  1848. * installed
  1849. */
  1850. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  1851. for (; Prv.end() != true; Prv++)
  1852. {
  1853. if (_config->FindB("Debug::BuildDeps",false) == true)
  1854. cout << " Checking provider " << Prv.OwnerPkg().Name() << endl;
  1855. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  1856. break;
  1857. }
  1858. // Get installed version and version we are going to install
  1859. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  1860. if ((*D).Version[0] != '\0') {
  1861. // Versioned dependency
  1862. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  1863. for (; CV.end() != true; CV++)
  1864. {
  1865. if (Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  1866. break;
  1867. }
  1868. if (CV.end() == true)
  1869. if (hasAlternatives)
  1870. {
  1871. continue;
  1872. }
  1873. else
  1874. {
  1875. return _error->Error(_("%s dependency for %s cannot be satisfied "
  1876. "because no available versions of package %s "
  1877. "can satisfy version requirements"),
  1878. Last->BuildDepType((*D).Type),Src.c_str(),
  1879. (*D).Package.c_str());
  1880. }
  1881. }
  1882. else
  1883. {
  1884. // Only consider virtual packages if there is no versioned dependency
  1885. if (Prv.end() == false)
  1886. {
  1887. if (_config->FindB("Debug::BuildDeps",false) == true)
  1888. cout << " Is provided by installed package " << Prv.OwnerPkg().Name() << endl;
  1889. skipAlternatives = hasAlternatives;
  1890. continue;
  1891. }
  1892. }
  1893. if (IV.end() == false)
  1894. {
  1895. if (_config->FindB("Debug::BuildDeps",false) == true)
  1896. cout << " Is installed\n";
  1897. if (Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  1898. {
  1899. skipAlternatives = hasAlternatives;
  1900. continue;
  1901. }
  1902. if (_config->FindB("Debug::BuildDeps",false) == true)
  1903. cout << " ...but the installed version doesn't meet the version requirement\n";
  1904. if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
  1905. {
  1906. return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
  1907. Last->BuildDepType((*D).Type),
  1908. Src.c_str(),
  1909. Pkg.Name());
  1910. }
  1911. }
  1912. if (_config->FindB("Debug::BuildDeps",false) == true)
  1913. cout << " Trying to install " << (*D).Package << endl;
  1914. if (TryToInstall(Pkg,Cache,Fix,false,false,ExpectedInst) == true)
  1915. {
  1916. // We successfully installed something; skip remaining alternatives
  1917. skipAlternatives = hasAlternatives;
  1918. continue;
  1919. }
  1920. else if (hasAlternatives)
  1921. {
  1922. if (_config->FindB("Debug::BuildDeps",false) == true)
  1923. cout << " Unsatisfiable, trying alternatives\n";
  1924. continue;
  1925. }
  1926. else
  1927. {
  1928. return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
  1929. Last->BuildDepType((*D).Type),
  1930. Src.c_str(),
  1931. (*D).Package.c_str());
  1932. }
  1933. }
  1934. }
  1935. Fix.InstallProtect();
  1936. if (Fix.Resolve(true) == false)
  1937. _error->Discard();
  1938. // Now we check the state of the packages,
  1939. if (Cache->BrokenCount() != 0)
  1940. return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
  1941. }
  1942. if (InstallPackages(Cache, false, true) == false)
  1943. return _error->Error(_("Failed to process build dependencies"));
  1944. return true;
  1945. }
  1946. /*}}}*/
  1947. // DoMoo - Never Ask, Never Tell /*{{{*/
  1948. // ---------------------------------------------------------------------
  1949. /* */
  1950. bool DoMoo(CommandLine &CmdL)
  1951. {
  1952. cout <<
  1953. " (__) \n"
  1954. " (oo) \n"
  1955. " /------\\/ \n"
  1956. " / | || \n"
  1957. " * /\\---/\\ \n"
  1958. " ~~ ~~ \n"
  1959. "....\"Have you mooed today?\"...\n";
  1960. return true;
  1961. }
  1962. /*}}}*/
  1963. // ShowHelp - Show a help screen /*{{{*/
  1964. // ---------------------------------------------------------------------
  1965. /* */
  1966. bool ShowHelp(CommandLine &CmdL)
  1967. {
  1968. ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
  1969. COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
  1970. if (_config->FindB("version") == true)
  1971. {
  1972. cout << _("Supported Modules:") << endl;
  1973. for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
  1974. {
  1975. pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
  1976. if (_system != 0 && _system->VS == VS)
  1977. cout << '*';
  1978. else
  1979. cout << ' ';
  1980. cout << "Ver: " << VS->Label << endl;
  1981. /* Print out all the packaging systems that will work with
  1982. this VS */
  1983. for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
  1984. {
  1985. pkgSystem *Sys = pkgSystem::GlobalList[J];
  1986. if (_system == Sys)
  1987. cout << '*';
  1988. else
  1989. cout << ' ';
  1990. if (Sys->VS->TestCompatibility(*VS) == true)
  1991. cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
  1992. }
  1993. }
  1994. for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
  1995. {
  1996. pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
  1997. cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
  1998. }
  1999. for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
  2000. {
  2001. pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
  2002. cout << " Idx: " << Type->Label << endl;
  2003. }
  2004. return true;
  2005. }
  2006. cout <<
  2007. _("Usage: apt-get [options] command\n"
  2008. " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
  2009. " apt-get [options] source pkg1 [pkg2 ...]\n"
  2010. "\n"
  2011. "apt-get is a simple command line interface for downloading and\n"
  2012. "installing packages. The most frequently used commands are update\n"
  2013. "and install.\n"
  2014. "\n"
  2015. "Commands:\n"
  2016. " update - Retrieve new lists of packages\n"
  2017. " upgrade - Perform an upgrade\n"
  2018. " install - Install new packages (pkg is libc6 not libc6.deb)\n"
  2019. " remove - Remove packages\n"
  2020. " source - Download source archives\n"
  2021. " build-dep - Configure build-dependencies for source packages\n"
  2022. " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
  2023. " dselect-upgrade - Follow dselect selections\n"
  2024. " clean - Erase downloaded archive files\n"
  2025. " autoclean - Erase old downloaded archive files\n"
  2026. " check - Verify that there are no broken dependencies\n"
  2027. "\n"
  2028. "Options:\n"
  2029. " -h This help text.\n"
  2030. " -q Loggable output - no progress indicator\n"
  2031. " -qq No output except for errors\n"
  2032. " -d Download only - do NOT install or unpack archives\n"
  2033. " -s No-act. Perform ordering simulation\n"
  2034. " -y Assume Yes to all queries and do not prompt\n"
  2035. " -f Attempt to continue if the integrity check fails\n"
  2036. " -m Attempt to continue if archives are unlocatable\n"
  2037. " -u Show a list of upgraded packages as well\n"
  2038. " -b Build the source package after fetching it\n"
  2039. " -V Show verbose version numbers\n"
  2040. " -c=? Read this configuration file\n"
  2041. " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n"
  2042. "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
  2043. "pages for more information and options.\n"
  2044. " This APT has Super Cow Powers.\n");
  2045. return true;
  2046. }
  2047. /*}}}*/
  2048. // GetInitialize - Initialize things for apt-get /*{{{*/
  2049. // ---------------------------------------------------------------------
  2050. /* */
  2051. void GetInitialize()
  2052. {
  2053. _config->Set("quiet",0);
  2054. _config->Set("help",false);
  2055. _config->Set("APT::Get::Download-Only",false);
  2056. _config->Set("APT::Get::Simulate",false);
  2057. _config->Set("APT::Get::Assume-Yes",false);
  2058. _config->Set("APT::Get::Fix-Broken",false);
  2059. _config->Set("APT::Get::Force-Yes",false);
  2060. _config->Set("APT::Get::List-Cleanup",true);
  2061. }
  2062. /*}}}*/
  2063. // SigWinch - Window size change signal handler /*{{{*/
  2064. // ---------------------------------------------------------------------
  2065. /* */
  2066. void SigWinch(int)
  2067. {
  2068. // Riped from GNU ls
  2069. #ifdef TIOCGWINSZ
  2070. struct winsize ws;
  2071. if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
  2072. ScreenWidth = ws.ws_col - 1;
  2073. #endif
  2074. }
  2075. /*}}}*/
  2076. int main(int argc,const char *argv[])
  2077. {
  2078. CommandLine::Args Args[] = {
  2079. {'h',"help","help",0},
  2080. {'v',"version","version",0},
  2081. {'V',"verbose-versions","APT::Get::Show-Versions",0},
  2082. {'q',"quiet","quiet",CommandLine::IntLevel},
  2083. {'q',"silent","quiet",CommandLine::IntLevel},
  2084. {'d',"download-only","APT::Get::Download-Only",0},
  2085. {'b',"compile","APT::Get::Compile",0},
  2086. {'b',"build","APT::Get::Compile",0},
  2087. {'s',"simulate","APT::Get::Simulate",0},
  2088. {'s',"just-print","APT::Get::Simulate",0},
  2089. {'s',"recon","APT::Get::Simulate",0},
  2090. {'s',"dry-run","APT::Get::Simulate",0},
  2091. {'s',"no-act","APT::Get::Simulate",0},
  2092. {'y',"yes","APT::Get::Assume-Yes",0},
  2093. {'y',"assume-yes","APT::Get::Assume-Yes",0},
  2094. {'f',"fix-broken","APT::Get::Fix-Broken",0},
  2095. {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
  2096. {'m',"ignore-missing","APT::Get::Fix-Missing",0},
  2097. {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
  2098. {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
  2099. {0,"download","APT::Get::Download",0},
  2100. {0,"fix-missing","APT::Get::Fix-Missing",0},
  2101. {0,"ignore-hold","APT::Ignore-Hold",0},
  2102. {0,"upgrade","APT::Get::upgrade",0},
  2103. {0,"force-yes","APT::Get::force-yes",0},
  2104. {0,"print-uris","APT::Get::Print-URIs",0},
  2105. {0,"diff-only","APT::Get::Diff-Only",0},
  2106. {0,"tar-only","APT::Get::tar-Only",0},
  2107. {0,"purge","APT::Get::Purge",0},
  2108. {0,"list-cleanup","APT::Get::List-Cleanup",0},
  2109. {0,"reinstall","APT::Get::ReInstall",0},
  2110. {0,"trivial-only","APT::Get::Trivial-Only",0},
  2111. {0,"remove","APT::Get::Remove",0},
  2112. {0,"only-source","APT::Get::Only-Source",0},
  2113. {0,"arch-only","APT::Get::Arch-Only",0},
  2114. {'c',"config-file",0,CommandLine::ConfigFile},
  2115. {'o',"option",0,CommandLine::ArbItem},
  2116. {0,0,0,0}};
  2117. CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
  2118. {"upgrade",&DoUpgrade},
  2119. {"install",&DoInstall},
  2120. {"remove",&DoInstall},
  2121. {"dist-upgrade",&DoDistUpgrade},
  2122. {"dselect-upgrade",&DoDSelectUpgrade},
  2123. {"build-dep",&DoBuildDep},
  2124. {"clean",&DoClean},
  2125. {"autoclean",&DoAutoClean},
  2126. {"check",&DoCheck},
  2127. {"source",&DoSource},
  2128. {"moo",&DoMoo},
  2129. {"help",&ShowHelp},
  2130. {0,0}};
  2131. // Set up gettext support
  2132. setlocale(LC_ALL,"");
  2133. textdomain(PACKAGE);
  2134. // Parse the command line and initialize the package library
  2135. CommandLine CmdL(Args,_config);
  2136. if (pkgInitConfig(*_config) == false ||
  2137. CmdL.Parse(argc,argv) == false ||
  2138. pkgInitSystem(*_config,_system) == false)
  2139. {
  2140. if (_config->FindB("version") == true)
  2141. ShowHelp(CmdL);
  2142. _error->DumpErrors();
  2143. return 100;
  2144. }
  2145. // See if the help should be shown
  2146. if (_config->FindB("help") == true ||
  2147. _config->FindB("version") == true ||
  2148. CmdL.FileSize() == 0)
  2149. {
  2150. ShowHelp(CmdL);
  2151. return 0;
  2152. }
  2153. // Deal with stdout not being a tty
  2154. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
  2155. _config->Set("quiet","1");
  2156. // Setup the output streams
  2157. c0out.rdbuf(cout.rdbuf());
  2158. c1out.rdbuf(cout.rdbuf());
  2159. c2out.rdbuf(cout.rdbuf());
  2160. if (_config->FindI("quiet",0) > 0)
  2161. c0out.rdbuf(devnull.rdbuf());
  2162. if (_config->FindI("quiet",0) > 1)
  2163. c1out.rdbuf(devnull.rdbuf());
  2164. // Setup the signals
  2165. signal(SIGPIPE,SIG_IGN);
  2166. signal(SIGWINCH,SigWinch);
  2167. SigWinch(0);
  2168. // Match the operation
  2169. CmdL.DispatchArg(Cmds);
  2170. // Print any errors or warnings found during parsing
  2171. if (_error->empty() == false)
  2172. {
  2173. bool Errors = _error->PendingError();
  2174. _error->DumpErrors();
  2175. return Errors == true?100:0;
  2176. }
  2177. return 0;
  2178. }