apt-get.cc 116 KB

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