apt-get.cc 118 KB

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