apt-get.cc 79 KB

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