apt-get.cc 96 KB

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