apt-get.cc 114 KB

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