apt-get.cc 96 KB

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