apt-get.cc 110 KB

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