apt-get.cc 92 KB

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