apt-get.cc 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-get.cc,v 1.108 2001/07/01 20:49:08 jgg 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/versionmatch.h>
  38. #include <config.h>
  39. #include <apti18n.h>
  40. #include "acqprogress.h"
  41. #include <fstream.h>
  42. #include <termios.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/stat.h>
  45. #include <sys/statvfs.h>
  46. #include <signal.h>
  47. #include <unistd.h>
  48. #include <stdio.h>
  49. #include <errno.h>
  50. #include <regex.h>
  51. #include <sys/wait.h>
  52. /*}}}*/
  53. using namespace std;
  54. ostream c0out(0);
  55. ostream c1out(0);
  56. ostream c2out(0);
  57. ofstream devnull("/dev/null");
  58. unsigned int ScreenWidth = 80;
  59. // class CacheFile - Cover class for some dependency cache functions /*{{{*/
  60. // ---------------------------------------------------------------------
  61. /* */
  62. class CacheFile : public pkgCacheFile
  63. {
  64. static pkgCache *SortCache;
  65. static int NameComp(const void *a,const void *b);
  66. public:
  67. pkgCache::Package **List;
  68. void Sort();
  69. bool CheckDeps(bool AllowBroken = false);
  70. bool Open(bool WithLock = true)
  71. {
  72. OpTextProgress Prog(*_config);
  73. if (pkgCacheFile::Open(Prog,WithLock) == false)
  74. return false;
  75. Sort();
  76. return true;
  77. };
  78. bool OpenForInstall()
  79. {
  80. if (_config->FindB("APT::Get::Print-URIs") == true)
  81. Open(false);
  82. else
  83. Open(true);
  84. }
  85. CacheFile() : List(0) {};
  86. };
  87. /*}}}*/
  88. // YnPrompt - Yes No Prompt. /*{{{*/
  89. // ---------------------------------------------------------------------
  90. /* Returns true on a Yes.*/
  91. bool YnPrompt()
  92. {
  93. // This needs to be a capital
  94. const char *Yes = _("Y");
  95. if (_config->FindB("APT::Get::Assume-Yes",false) == true)
  96. {
  97. c1out << Yes << endl;
  98. return true;
  99. }
  100. char C = 0;
  101. char Jnk = 0;
  102. if (read(STDIN_FILENO,&C,1) != 1)
  103. return false;
  104. while (C != '\n' && Jnk != '\n')
  105. if (read(STDIN_FILENO,&Jnk,1) != 1)
  106. return false;
  107. if (!(toupper(C) == *Yes || C == '\n' || C == '\r'))
  108. return false;
  109. return true;
  110. }
  111. /*}}}*/
  112. // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* Returns true on a Yes.*/
  115. bool AnalPrompt(const char *Text)
  116. {
  117. char Buf[1024];
  118. cin.getline(Buf,sizeof(Buf));
  119. if (strcmp(Buf,Text) == 0)
  120. return true;
  121. return false;
  122. }
  123. /*}}}*/
  124. // ShowList - Show a list /*{{{*/
  125. // ---------------------------------------------------------------------
  126. /* This prints out a string of space separated words with a title and
  127. a two space indent line wraped to the current screen width. */
  128. bool ShowList(ostream &out,string Title,string List)
  129. {
  130. if (List.empty() == true)
  131. return true;
  132. // Acount for the leading space
  133. int ScreenWidth = ::ScreenWidth - 3;
  134. out << Title << endl;
  135. string::size_type Start = 0;
  136. while (Start < List.size())
  137. {
  138. string::size_type End;
  139. if (Start + ScreenWidth >= List.size())
  140. End = List.size();
  141. else
  142. End = List.rfind(' ',Start+ScreenWidth);
  143. if (End == string::npos || End < Start)
  144. End = Start + ScreenWidth;
  145. out << " " << string(List,Start,End - Start) << endl;
  146. Start = End + 1;
  147. }
  148. return false;
  149. }
  150. /*}}}*/
  151. // ShowBroken - Debugging aide /*{{{*/
  152. // ---------------------------------------------------------------------
  153. /* This prints out the names of all the packages that are broken along
  154. with the name of each each broken dependency and a quite version
  155. description.
  156. The output looks like:
  157. Sorry, but the following packages have unmet dependencies:
  158. exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
  159. Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
  160. Depends: libsasl7 but it is not going to be installed
  161. */
  162. void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
  163. {
  164. out << _("Sorry, but the following packages have unmet dependencies:") << endl;
  165. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  166. {
  167. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  168. if (Cache[I].InstBroken() == false)
  169. continue;
  170. // Print out each package and the failed dependencies
  171. out <<" " << I.Name() << ":";
  172. unsigned Indent = strlen(I.Name()) + 3;
  173. bool First = true;
  174. if (Cache[I].InstVerIter(Cache).end() == true)
  175. {
  176. cout << endl;
  177. continue;
  178. }
  179. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
  180. {
  181. // Compute a single dependency element (glob or)
  182. pkgCache::DepIterator Start;
  183. pkgCache::DepIterator End;
  184. D.GlobOr(Start,End);
  185. if (Cache->IsImportantDep(End) == false ||
  186. (Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  187. continue;
  188. bool FirstOr = true;
  189. while (1)
  190. {
  191. if (First == false)
  192. for (unsigned J = 0; J != Indent; J++)
  193. out << ' ';
  194. First = false;
  195. if (FirstOr == false)
  196. {
  197. for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
  198. out << ' ';
  199. }
  200. else
  201. out << ' ' << End.DepType() << ": ";
  202. FirstOr = false;
  203. out << Start.TargetPkg().Name();
  204. // Show a quick summary of the version requirements
  205. if (Start.TargetVer() != 0)
  206. out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
  207. /* Show a summary of the target package if possible. In the case
  208. of virtual packages we show nothing */
  209. pkgCache::PkgIterator Targ = Start.TargetPkg();
  210. if (Targ->ProvidesList == 0)
  211. {
  212. out << ' ';
  213. pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
  214. if (Now == true)
  215. Ver = Targ.CurrentVer();
  216. if (Ver.end() == false)
  217. {
  218. if (Now == true)
  219. ioprintf(out,_("but %s is installed"),Ver.VerStr());
  220. else
  221. ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
  222. }
  223. else
  224. {
  225. if (Cache[Targ].CandidateVerIter(Cache).end() == true)
  226. {
  227. if (Targ->ProvidesList == 0)
  228. out << _("but it is not installable");
  229. else
  230. out << _("but it is a virtual package");
  231. }
  232. else
  233. out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
  234. }
  235. }
  236. if (Start != End)
  237. out << _(" or");
  238. out << endl;
  239. if (Start == End)
  240. break;
  241. Start++;
  242. }
  243. }
  244. }
  245. }
  246. /*}}}*/
  247. // ShowNew - Show packages to newly install /*{{{*/
  248. // ---------------------------------------------------------------------
  249. /* */
  250. void ShowNew(ostream &out,CacheFile &Cache)
  251. {
  252. /* Print out a list of packages that are going to be removed extra
  253. to what the user asked */
  254. string List;
  255. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  256. {
  257. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  258. if (Cache[I].NewInstall() == true)
  259. List += string(I.Name()) + " ";
  260. }
  261. ShowList(out,_("The following NEW packages will be installed:"),List);
  262. }
  263. /*}}}*/
  264. // ShowDel - Show packages to delete /*{{{*/
  265. // ---------------------------------------------------------------------
  266. /* */
  267. void ShowDel(ostream &out,CacheFile &Cache)
  268. {
  269. /* Print out a list of packages that are going to be removed extra
  270. to what the user asked */
  271. string List;
  272. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  273. {
  274. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  275. if (Cache[I].Delete() == true)
  276. {
  277. if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
  278. List += string(I.Name()) + "* ";
  279. else
  280. List += string(I.Name()) + " ";
  281. }
  282. }
  283. ShowList(out,_("The following packages will be REMOVED:"),List);
  284. }
  285. /*}}}*/
  286. // ShowKept - Show kept packages /*{{{*/
  287. // ---------------------------------------------------------------------
  288. /* */
  289. void ShowKept(ostream &out,CacheFile &Cache)
  290. {
  291. string List;
  292. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  293. {
  294. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  295. // Not interesting
  296. if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
  297. I->CurrentVer == 0 || Cache[I].Delete() == true)
  298. continue;
  299. List += string(I.Name()) + " ";
  300. }
  301. ShowList(out,_("The following packages have been kept back"),List);
  302. }
  303. /*}}}*/
  304. // ShowUpgraded - Show upgraded packages /*{{{*/
  305. // ---------------------------------------------------------------------
  306. /* */
  307. void ShowUpgraded(ostream &out,CacheFile &Cache)
  308. {
  309. string List;
  310. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  311. {
  312. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  313. // Not interesting
  314. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  315. continue;
  316. List += string(I.Name()) + " ";
  317. }
  318. ShowList(out,_("The following packages will be upgraded"),List);
  319. }
  320. /*}}}*/
  321. // ShowDowngraded - Show downgraded packages /*{{{*/
  322. // ---------------------------------------------------------------------
  323. /* */
  324. bool ShowDowngraded(ostream &out,CacheFile &Cache)
  325. {
  326. string List;
  327. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  328. {
  329. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  330. // Not interesting
  331. if (Cache[I].Downgrade() == false || Cache[I].NewInstall() == true)
  332. continue;
  333. List += string(I.Name()) + " ";
  334. }
  335. return ShowList(out,_("The following packages will be DOWNGRADED"),List);
  336. }
  337. /*}}}*/
  338. // ShowHold - Show held but changed packages /*{{{*/
  339. // ---------------------------------------------------------------------
  340. /* */
  341. bool ShowHold(ostream &out,CacheFile &Cache)
  342. {
  343. string List;
  344. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  345. {
  346. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  347. if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
  348. I->SelectedState == pkgCache::State::Hold)
  349. List += string(I.Name()) + " ";
  350. }
  351. return ShowList(out,_("The following held packages will be changed:"),List);
  352. }
  353. /*}}}*/
  354. // ShowEssential - Show an essential package warning /*{{{*/
  355. // ---------------------------------------------------------------------
  356. /* This prints out a warning message that is not to be ignored. It shows
  357. all essential packages and their dependents that are to be removed.
  358. It is insanely risky to remove the dependents of an essential package! */
  359. bool ShowEssential(ostream &out,CacheFile &Cache)
  360. {
  361. string List;
  362. bool *Added = new bool[Cache->Head().PackageCount];
  363. for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
  364. Added[I] = false;
  365. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  366. {
  367. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  368. if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
  369. (I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
  370. continue;
  371. // The essential package is being removed
  372. if (Cache[I].Delete() == true)
  373. {
  374. if (Added[I->ID] == false)
  375. {
  376. Added[I->ID] = true;
  377. List += string(I.Name()) + " ";
  378. }
  379. }
  380. if (I->CurrentVer == 0)
  381. continue;
  382. // Print out any essential package depenendents that are to be removed
  383. for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
  384. {
  385. // Skip everything but depends
  386. if (D->Type != pkgCache::Dep::PreDepends &&
  387. D->Type != pkgCache::Dep::Depends)
  388. continue;
  389. pkgCache::PkgIterator P = D.SmartTargetPkg();
  390. if (Cache[P].Delete() == true)
  391. {
  392. if (Added[P->ID] == true)
  393. continue;
  394. Added[P->ID] = true;
  395. char S[300];
  396. snprintf(S,sizeof(S),_("%s (due to %s) "),P.Name(),I.Name());
  397. List += S;
  398. }
  399. }
  400. }
  401. delete [] Added;
  402. return ShowList(out,_("WARNING: The following essential packages will be removed\n"
  403. "This should NOT be done unless you know exactly what you are doing!"),List);
  404. }
  405. /*}}}*/
  406. // Stats - Show some statistics /*{{{*/
  407. // ---------------------------------------------------------------------
  408. /* */
  409. void Stats(ostream &out,pkgDepCache &Dep)
  410. {
  411. unsigned long Upgrade = 0;
  412. unsigned long Downgrade = 0;
  413. unsigned long Install = 0;
  414. unsigned long ReInstall = 0;
  415. for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
  416. {
  417. if (Dep[I].NewInstall() == true)
  418. Install++;
  419. else
  420. {
  421. if (Dep[I].Upgrade() == true)
  422. Upgrade++;
  423. else
  424. if (Dep[I].Downgrade() == true)
  425. Downgrade++;
  426. }
  427. if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
  428. ReInstall++;
  429. }
  430. ioprintf(out,_("%lu packages upgraded, %lu newly installed, "),
  431. Upgrade,Install);
  432. if (ReInstall != 0)
  433. ioprintf(out,_("%lu reinstalled, "),ReInstall);
  434. if (Downgrade != 0)
  435. ioprintf(out,_("%lu downgraded, "),Downgrade);
  436. ioprintf(out,_("%lu to remove and %lu not upgraded.\n"),
  437. Dep.DelCount(),Dep.KeepCount());
  438. if (Dep.BadCount() != 0)
  439. ioprintf(out,_("%lu packages not fully installed or removed.\n"),
  440. Dep.BadCount());
  441. }
  442. /*}}}*/
  443. // CacheFile::NameComp - QSort compare by name /*{{{*/
  444. // ---------------------------------------------------------------------
  445. /* */
  446. pkgCache *CacheFile::SortCache = 0;
  447. int CacheFile::NameComp(const void *a,const void *b)
  448. {
  449. if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
  450. return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
  451. const pkgCache::Package &A = **(pkgCache::Package **)a;
  452. const pkgCache::Package &B = **(pkgCache::Package **)b;
  453. return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
  454. }
  455. /*}}}*/
  456. // CacheFile::Sort - Sort by name /*{{{*/
  457. // ---------------------------------------------------------------------
  458. /* */
  459. void CacheFile::Sort()
  460. {
  461. delete [] List;
  462. List = new pkgCache::Package *[Cache->Head().PackageCount];
  463. memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
  464. pkgCache::PkgIterator I = Cache->PkgBegin();
  465. for (;I.end() != true; I++)
  466. List[I->ID] = I;
  467. SortCache = *this;
  468. qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
  469. }
  470. /*}}}*/
  471. // CacheFile::CheckDeps - Open the cache file /*{{{*/
  472. // ---------------------------------------------------------------------
  473. /* This routine generates the caches and then opens the dependency cache
  474. and verifies that the system is OK. */
  475. bool CacheFile::CheckDeps(bool AllowBroken)
  476. {
  477. if (_error->PendingError() == true)
  478. return false;
  479. // Check that the system is OK
  480. if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
  481. return _error->Error("Internal Error, non-zero counts");
  482. // Apply corrections for half-installed packages
  483. if (pkgApplyStatus(*DCache) == false)
  484. return false;
  485. // Nothing is broken
  486. if (DCache->BrokenCount() == 0 || AllowBroken == true)
  487. return true;
  488. // Attempt to fix broken things
  489. if (_config->FindB("APT::Get::Fix-Broken",false) == true)
  490. {
  491. c1out << _("Correcting dependencies...") << flush;
  492. if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
  493. {
  494. c1out << _(" failed.") << endl;
  495. ShowBroken(c1out,*this,true);
  496. return _error->Error(_("Unable to correct dependencies"));
  497. }
  498. if (pkgMinimizeUpgrade(*DCache) == false)
  499. return _error->Error(_("Unable to minimize the upgrade set"));
  500. c1out << _(" Done") << endl;
  501. }
  502. else
  503. {
  504. c1out << _("You might want to run `apt-get -f install' to correct these.") << endl;
  505. ShowBroken(c1out,*this,true);
  506. return _error->Error(_("Unmet dependencies. Try using -f."));
  507. }
  508. return true;
  509. }
  510. /*}}}*/
  511. // InstallPackages - Actually download and install the packages /*{{{*/
  512. // ---------------------------------------------------------------------
  513. /* This displays the informative messages describing what is going to
  514. happen and then calls the download routines */
  515. bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
  516. bool Saftey = true)
  517. {
  518. if (_config->FindB("APT::Get::Purge",false) == true)
  519. {
  520. pkgCache::PkgIterator I = Cache->PkgBegin();
  521. for (; I.end() == false; I++)
  522. {
  523. if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
  524. Cache->MarkDelete(I,true);
  525. }
  526. }
  527. bool Fail = false;
  528. bool Essential = false;
  529. // Show all the various warning indicators
  530. ShowDel(c1out,Cache);
  531. ShowNew(c1out,Cache);
  532. if (ShwKept == true)
  533. ShowKept(c1out,Cache);
  534. Fail |= !ShowHold(c1out,Cache);
  535. if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
  536. ShowUpgraded(c1out,Cache);
  537. Fail |= !ShowDowngraded(c1out,Cache);
  538. Essential = !ShowEssential(c1out,Cache);
  539. Fail |= Essential;
  540. Stats(c1out,Cache);
  541. // Sanity check
  542. if (Cache->BrokenCount() != 0)
  543. {
  544. ShowBroken(c1out,Cache,false);
  545. return _error->Error("Internal Error, InstallPackages was called with broken packages!");
  546. }
  547. if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
  548. Cache->BadCount() == 0)
  549. return true;
  550. // No remove flag
  551. if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
  552. return _error->Error(_("Packages need to be removed but Remove is disabled."));
  553. // Run the simulator ..
  554. if (_config->FindB("APT::Get::Simulate") == true)
  555. {
  556. pkgSimulate PM(Cache);
  557. pkgPackageManager::OrderResult Res = PM.DoInstall();
  558. if (Res == pkgPackageManager::Failed)
  559. return false;
  560. if (Res != pkgPackageManager::Completed)
  561. return _error->Error("Internal Error, Ordering didn't finish");
  562. return true;
  563. }
  564. // Create the text record parser
  565. pkgRecords Recs(Cache);
  566. if (_error->PendingError() == true)
  567. return false;
  568. // Lock the archive directory
  569. FileFd Lock;
  570. if (_config->FindB("Debug::NoLocking",false) == false &&
  571. _config->FindB("APT::Get::Print-URIs") == false)
  572. {
  573. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  574. if (_error->PendingError() == true)
  575. return _error->Error(_("Unable to lock the download directory"));
  576. }
  577. // Create the download object
  578. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  579. pkgAcquire Fetcher(&Stat);
  580. // Read the source list
  581. pkgSourceList List;
  582. if (List.ReadMainList() == false)
  583. return _error->Error(_("The list of sources could not be read."));
  584. // Create the package manager and prepare to download
  585. SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
  586. if (PM->GetArchives(&Fetcher,&List,&Recs) == false ||
  587. _error->PendingError() == true)
  588. return false;
  589. // Display statistics
  590. double FetchBytes = Fetcher.FetchNeeded();
  591. double FetchPBytes = Fetcher.PartialPresent();
  592. double DebBytes = Fetcher.TotalNeeded();
  593. if (DebBytes != Cache->DebSize())
  594. {
  595. c0out << DebBytes << ',' << Cache->DebSize() << endl;
  596. c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl;
  597. }
  598. // Number of bytes
  599. if (DebBytes != FetchBytes)
  600. ioprintf(c1out,_("Need to get %sB/%sB of archives. "),
  601. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  602. else
  603. ioprintf(c1out,_("Need to get %sB of archives. "),
  604. SizeToStr(DebBytes).c_str());
  605. // Size delta
  606. if (Cache->UsrSize() >= 0)
  607. ioprintf(c1out,_("After unpacking %sB will be used.\n"),
  608. SizeToStr(Cache->UsrSize()).c_str());
  609. else
  610. ioprintf(c1out,_("After unpacking %sB will be freed.\n"),
  611. SizeToStr(-1*Cache->UsrSize()).c_str());
  612. if (_error->PendingError() == true)
  613. return false;
  614. /* Check for enough free space, but only if we are actually going to
  615. download */
  616. if (_config->FindB("APT::Get::Print-URIs") == false)
  617. {
  618. struct statvfs Buf;
  619. string OutputDir = _config->FindDir("Dir::Cache::Archives");
  620. if (statvfs(OutputDir.c_str(),&Buf) != 0)
  621. return _error->Errno("statvfs","Couldn't determine free space in %s",
  622. OutputDir.c_str());
  623. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  624. return _error->Error(_("Sorry, you don't have enough free space in %s to hold all the .debs."),
  625. OutputDir.c_str());
  626. }
  627. // Fail safe check
  628. if (_config->FindI("quiet",0) >= 2 ||
  629. _config->FindB("APT::Get::Assume-Yes",false) == true)
  630. {
  631. if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
  632. return _error->Error(_("There are problems and -y was used without --force-yes"));
  633. }
  634. if (Essential == true && Saftey == true)
  635. {
  636. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  637. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  638. const char *Prompt = _("Yes, do as I say!");
  639. ioprintf(c2out,
  640. _("You are about to do something potentially harmful\n"
  641. "To continue type in the phrase '%s'\n"
  642. " ?] "),Prompt);
  643. c2out << flush;
  644. if (AnalPrompt(Prompt) == false)
  645. {
  646. c2out << _("Abort.") << endl;
  647. exit(1);
  648. }
  649. }
  650. else
  651. {
  652. // Prompt to continue
  653. if (Ask == true || Fail == true)
  654. {
  655. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  656. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  657. if (_config->FindI("quiet",0) < 2 &&
  658. _config->FindB("APT::Get::Assume-Yes",false) == false)
  659. {
  660. c2out << _("Do you want to continue? [Y/n] ") << flush;
  661. if (YnPrompt() == false)
  662. {
  663. c2out << _("Abort.") << endl;
  664. exit(1);
  665. }
  666. }
  667. }
  668. }
  669. // Just print out the uris an exit if the --print-uris flag was used
  670. if (_config->FindB("APT::Get::Print-URIs") == true)
  671. {
  672. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  673. for (; I != Fetcher.UriEnd(); I++)
  674. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  675. I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
  676. return true;
  677. }
  678. /* Unlock the dpkg lock if we are not going to be doing an install
  679. after. */
  680. if (_config->FindB("APT::Get::Download-Only",false) == true)
  681. _system->UnLock();
  682. // Run it
  683. while (1)
  684. {
  685. bool Transient = false;
  686. if (_config->FindB("APT::Get::Download",true) == false)
  687. {
  688. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
  689. {
  690. if ((*I)->Local == true)
  691. {
  692. I++;
  693. continue;
  694. }
  695. // Close the item and check if it was found in cache
  696. (*I)->Finished();
  697. if ((*I)->Complete == false)
  698. Transient = true;
  699. // Clear it out of the fetch list
  700. delete *I;
  701. I = Fetcher.ItemsBegin();
  702. }
  703. }
  704. if (Fetcher.Run() == pkgAcquire::Failed)
  705. return false;
  706. // Print out errors
  707. bool Failed = false;
  708. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  709. {
  710. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  711. (*I)->Complete == true)
  712. continue;
  713. if ((*I)->Status == pkgAcquire::Item::StatIdle)
  714. {
  715. Transient = true;
  716. // Failed = true;
  717. continue;
  718. }
  719. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  720. (*I)->ErrorText.c_str());
  721. Failed = true;
  722. }
  723. /* If we are in no download mode and missing files and there were
  724. 'failures' then the user must specify -m. Furthermore, there
  725. is no such thing as a transient error in no-download mode! */
  726. if (Transient == true &&
  727. _config->FindB("APT::Get::Download",true) == false)
  728. {
  729. Transient = false;
  730. Failed = true;
  731. }
  732. if (_config->FindB("APT::Get::Download-Only",false) == true)
  733. {
  734. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  735. return _error->Error(_("Some files failed to download"));
  736. c1out << _("Download complete and in download only mode") << endl;
  737. return true;
  738. }
  739. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  740. {
  741. return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
  742. }
  743. if (Transient == true && Failed == true)
  744. return _error->Error(_("--fix-missing and media swapping is not currently supported"));
  745. // Try to deal with missing package files
  746. if (Failed == true && PM->FixMissing() == false)
  747. {
  748. cerr << _("Unable to correct missing packages.") << endl;
  749. return _error->Error(_("Aborting Install."));
  750. }
  751. _system->UnLock();
  752. pkgPackageManager::OrderResult Res = PM->DoInstall();
  753. if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
  754. return false;
  755. if (Res == pkgPackageManager::Completed)
  756. return true;
  757. // Reload the fetcher object and loop again for media swapping
  758. Fetcher.Shutdown();
  759. if (PM->GetArchives(&Fetcher,&List,&Recs) == false)
  760. return false;
  761. _system->Lock();
  762. }
  763. }
  764. /*}}}*/
  765. // TryToInstall - Try to install a single package /*{{{*/
  766. // ---------------------------------------------------------------------
  767. /* This used to be inlined in DoInstall, but with the advent of regex package
  768. name matching it was split out.. */
  769. bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
  770. pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
  771. unsigned int &ExpectedInst,bool AllowFail = true)
  772. {
  773. /* This is a pure virtual package and there is a single available
  774. provides */
  775. if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0 &&
  776. Pkg.ProvidesList()->NextProvides == 0)
  777. {
  778. pkgCache::PkgIterator Tmp = Pkg.ProvidesList().OwnerPkg();
  779. ioprintf(c1out,_("Note, selecting %s instead of %s\n"),
  780. Tmp.Name(),Pkg.Name());
  781. Pkg = Tmp;
  782. }
  783. // Handle the no-upgrade case
  784. if (_config->FindB("APT::Get::upgrade",true) == false &&
  785. Pkg->CurrentVer != 0)
  786. {
  787. if (AllowFail == true)
  788. ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
  789. Pkg.Name());
  790. return true;
  791. }
  792. // Check if there is something at all to install
  793. pkgDepCache::StateCache &State = Cache[Pkg];
  794. if (Remove == true && Pkg->CurrentVer == 0)
  795. {
  796. /* We want to continue searching for regex hits, so we return false here
  797. otherwise this is not really an error. */
  798. if (AllowFail == false)
  799. return false;
  800. ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.Name());
  801. return true;
  802. }
  803. if (State.CandidateVer == 0 && Remove == false)
  804. {
  805. if (AllowFail == false)
  806. return false;
  807. if (Pkg->ProvidesList != 0)
  808. {
  809. ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
  810. Pkg.Name());
  811. pkgCache::PrvIterator I = Pkg.ProvidesList();
  812. for (; I.end() == false; I++)
  813. {
  814. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  815. if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer())
  816. {
  817. if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
  818. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
  819. _(" [Installed]") << endl;
  820. else
  821. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
  822. }
  823. }
  824. c1out << _("You should explicitly select one to install.") << endl;
  825. }
  826. else
  827. {
  828. ioprintf(c1out,
  829. _("Package %s has no available version, but exists in the database.\n"
  830. "This typically means that the package was mentioned in a dependency and\n"
  831. "never uploaded, has been obsoleted or is not available with the contents\n"
  832. "of sources.list\n"),Pkg.Name());
  833. string List;
  834. SPtrArray<bool> Seen = new bool[Cache.Head().PackageCount];
  835. memset(Seen,0,Cache.Head().PackageCount*sizeof(*Seen));
  836. pkgCache::DepIterator Dep = Pkg.RevDependsList();
  837. for (; Dep.end() == false; Dep++)
  838. {
  839. if (Dep->Type != pkgCache::Dep::Replaces)
  840. continue;
  841. if (Seen[Dep.ParentPkg()->ID] == true)
  842. continue;
  843. Seen[Dep.ParentPkg()->ID] = true;
  844. List += string(Dep.ParentPkg().Name()) + " ";
  845. }
  846. ShowList(c1out,_("However the following packages replace it:"),List);
  847. }
  848. _error->Error(_("Package %s has no installation candidate"),Pkg.Name());
  849. return false;
  850. }
  851. Fix.Clear(Pkg);
  852. Fix.Protect(Pkg);
  853. if (Remove == true)
  854. {
  855. Fix.Remove(Pkg);
  856. Cache.MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
  857. return true;
  858. }
  859. // Install it
  860. Cache.MarkInstall(Pkg,false);
  861. if (State.Install() == false)
  862. {
  863. if (_config->FindB("APT::Get::ReInstall",false) == true)
  864. {
  865. if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
  866. ioprintf(c1out,_("Sorry, re-installation of %s is not possible, it cannot be downloaded.\n"),
  867. Pkg.Name());
  868. else
  869. Cache.SetReInstall(Pkg,true);
  870. }
  871. else
  872. {
  873. if (AllowFail == true)
  874. ioprintf(c1out,_("Sorry, %s is already the newest version.\n"),
  875. Pkg.Name());
  876. }
  877. }
  878. else
  879. ExpectedInst++;
  880. // Install it with autoinstalling enabled.
  881. if (State.InstBroken() == true && BrokenFix == false)
  882. Cache.MarkInstall(Pkg,true);
  883. return true;
  884. }
  885. /*}}}*/
  886. // TryToChangeVer - Try to change a candidate version /*{{{*/
  887. // ---------------------------------------------------------------------
  888. /* */
  889. bool TryToChangeVer(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
  890. const char *VerTag,bool IsRel)
  891. {
  892. pkgVersionMatch Match(VerTag,(IsRel == true?pkgVersionMatch::Release:pkgVersionMatch::Version));
  893. pkgCache::VerIterator Ver = Match.Find(Pkg);
  894. if (Ver.end() == true)
  895. {
  896. if (IsRel == true)
  897. return _error->Error(_("Release '%s' for '%s' was not found"),
  898. VerTag,Pkg.Name());
  899. return _error->Error(_("Version '%s' for '%s' was not found"),
  900. VerTag,Pkg.Name());
  901. }
  902. if (strcmp(VerTag,Ver.VerStr()) != 0)
  903. {
  904. ioprintf(c1out,_("Selected version %s (%s) for %s\n"),
  905. Ver.VerStr(),Ver.RelStr().c_str(),Pkg.Name());
  906. }
  907. Cache.SetCandidateVersion(Ver);
  908. return true;
  909. }
  910. /*}}}*/
  911. // FindSrc - Find a source record /*{{{*/
  912. // ---------------------------------------------------------------------
  913. /* */
  914. pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
  915. pkgSrcRecords &SrcRecs,string &Src,
  916. pkgDepCache &Cache)
  917. {
  918. // We want to pull the version off the package specification..
  919. string VerTag;
  920. string TmpSrc = Name;
  921. string::size_type Slash = TmpSrc.rfind('=');
  922. if (Slash != string::npos)
  923. {
  924. VerTag = string(TmpSrc.begin() + Slash + 1,TmpSrc.end());
  925. TmpSrc = string(TmpSrc.begin(),TmpSrc.begin() + Slash);
  926. }
  927. /* Lookup the version of the package we would install if we were to
  928. install a version and determine the source package name, then look
  929. in the archive for a source package of the same name. In theory
  930. we could stash the version string as well and match that too but
  931. today there aren't multi source versions in the archive. */
  932. if (_config->FindB("APT::Get::Only-Source") == false &&
  933. VerTag.empty() == true)
  934. {
  935. pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
  936. if (Pkg.end() == false)
  937. {
  938. pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
  939. if (Ver.end() == false)
  940. {
  941. pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
  942. Src = Parse.SourcePkg();
  943. }
  944. }
  945. }
  946. // No source package name..
  947. if (Src.empty() == true)
  948. Src = TmpSrc;
  949. // The best hit
  950. pkgSrcRecords::Parser *Last = 0;
  951. unsigned long Offset = 0;
  952. string Version;
  953. bool IsMatch = false;
  954. // If we are matching by version then we need exact matches to be happy
  955. if (VerTag.empty() == false)
  956. IsMatch = true;
  957. /* Iterate over all of the hits, which includes the resulting
  958. binary packages in the search */
  959. pkgSrcRecords::Parser *Parse;
  960. SrcRecs.Restart();
  961. while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
  962. {
  963. string Ver = Parse->Version();
  964. // Skip name mismatches
  965. if (IsMatch == true && Parse->Package() != Src)
  966. continue;
  967. if (VerTag.empty() == false)
  968. {
  969. /* Don't want to fall through because we are doing exact version
  970. matching. */
  971. if (Cache.VS().CmpVersion(VerTag,Ver) != 0)
  972. continue;
  973. Last = Parse;
  974. Offset = Parse->Offset();
  975. break;
  976. }
  977. // Newer version or an exact match
  978. if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0 ||
  979. (Parse->Package() == Src && IsMatch == false))
  980. {
  981. IsMatch = Parse->Package() == Src;
  982. Last = Parse;
  983. Offset = Parse->Offset();
  984. Version = Ver;
  985. }
  986. }
  987. if (Last == 0)
  988. return 0;
  989. if (Last->Jump(Offset) == false)
  990. return 0;
  991. return Last;
  992. }
  993. /*}}}*/
  994. // DoUpdate - Update the package lists /*{{{*/
  995. // ---------------------------------------------------------------------
  996. /* */
  997. bool DoUpdate(CommandLine &CmdL)
  998. {
  999. if (CmdL.FileSize() != 1)
  1000. return _error->Error(_("The update command takes no arguments"));
  1001. // Get the source list
  1002. pkgSourceList List;
  1003. if (List.ReadMainList() == false)
  1004. return false;
  1005. // Lock the list directory
  1006. FileFd Lock;
  1007. if (_config->FindB("Debug::NoLocking",false) == false)
  1008. {
  1009. Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
  1010. if (_error->PendingError() == true)
  1011. return _error->Error(_("Unable to lock the list directory"));
  1012. }
  1013. // Create the download object
  1014. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1015. pkgAcquire Fetcher(&Stat);
  1016. // Populate it with the source selection
  1017. if (List.GetIndexes(&Fetcher) == false)
  1018. return false;
  1019. // Run it
  1020. if (Fetcher.Run() == pkgAcquire::Failed)
  1021. return false;
  1022. bool Failed = false;
  1023. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  1024. {
  1025. if ((*I)->Status == pkgAcquire::Item::StatDone)
  1026. continue;
  1027. (*I)->Finished();
  1028. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  1029. (*I)->ErrorText.c_str());
  1030. Failed = true;
  1031. }
  1032. // Clean out any old list files
  1033. if (_config->FindB("APT::Get::List-Cleanup",true) == true)
  1034. {
  1035. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  1036. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  1037. return false;
  1038. }
  1039. // Prepare the cache.
  1040. CacheFile Cache;
  1041. if (Cache.Open() == false)
  1042. return false;
  1043. if (Failed == true)
  1044. return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  1045. return true;
  1046. }
  1047. /*}}}*/
  1048. // DoUpgrade - Upgrade all packages /*{{{*/
  1049. // ---------------------------------------------------------------------
  1050. /* Upgrade all packages without installing new packages or erasing old
  1051. packages */
  1052. bool DoUpgrade(CommandLine &CmdL)
  1053. {
  1054. CacheFile Cache;
  1055. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1056. return false;
  1057. // Do the upgrade
  1058. if (pkgAllUpgrade(Cache) == false)
  1059. {
  1060. ShowBroken(c1out,Cache,false);
  1061. return _error->Error(_("Internal Error, AllUpgrade broke stuff"));
  1062. }
  1063. return InstallPackages(Cache,true);
  1064. }
  1065. /*}}}*/
  1066. // DoInstall - Install packages from the command line /*{{{*/
  1067. // ---------------------------------------------------------------------
  1068. /* Install named packages */
  1069. bool DoInstall(CommandLine &CmdL)
  1070. {
  1071. CacheFile Cache;
  1072. if (Cache.OpenForInstall() == false ||
  1073. Cache.CheckDeps(CmdL.FileSize() != 1) == false)
  1074. return false;
  1075. // Enter the special broken fixing mode if the user specified arguments
  1076. bool BrokenFix = false;
  1077. if (Cache->BrokenCount() != 0)
  1078. BrokenFix = true;
  1079. unsigned int ExpectedInst = 0;
  1080. unsigned int Packages = 0;
  1081. pkgProblemResolver Fix(Cache);
  1082. bool DefRemove = false;
  1083. if (strcasecmp(CmdL.FileList[0],"remove") == 0)
  1084. DefRemove = true;
  1085. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  1086. {
  1087. // Duplicate the string
  1088. unsigned int Length = strlen(*I);
  1089. char S[300];
  1090. if (Length >= sizeof(S))
  1091. continue;
  1092. strcpy(S,*I);
  1093. // See if we are removing and special indicators..
  1094. bool Remove = DefRemove;
  1095. char *VerTag = 0;
  1096. bool VerIsRel = false;
  1097. while (Cache->FindPkg(S).end() == true)
  1098. {
  1099. // Handle an optional end tag indicating what to do
  1100. if (S[Length - 1] == '-')
  1101. {
  1102. Remove = true;
  1103. S[--Length] = 0;
  1104. continue;
  1105. }
  1106. if (S[Length - 1] == '+')
  1107. {
  1108. Remove = false;
  1109. S[--Length] = 0;
  1110. continue;
  1111. }
  1112. char *Slash = strchr(S,'=');
  1113. if (Slash != 0)
  1114. {
  1115. VerIsRel = false;
  1116. *Slash = 0;
  1117. VerTag = Slash + 1;
  1118. }
  1119. Slash = strchr(S,'/');
  1120. if (Slash != 0)
  1121. {
  1122. VerIsRel = true;
  1123. *Slash = 0;
  1124. VerTag = Slash + 1;
  1125. }
  1126. break;
  1127. }
  1128. // Locate the package
  1129. pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
  1130. Packages++;
  1131. if (Pkg.end() == true)
  1132. {
  1133. // Check if the name is a regex
  1134. const char *I;
  1135. for (I = S; *I != 0; I++)
  1136. if (*I == '.' || *I == '?' || *I == '*' || *I == '|')
  1137. break;
  1138. if (*I == 0)
  1139. return _error->Error(_("Couldn't find package %s"),S);
  1140. // Regexs must always be confirmed
  1141. ExpectedInst += 1000;
  1142. // Compile the regex pattern
  1143. regex_t Pattern;
  1144. int Res;
  1145. if ((Res = regcomp(&Pattern,S,REG_EXTENDED | REG_ICASE |
  1146. REG_NOSUB)) != 0)
  1147. {
  1148. char Error[300];
  1149. regerror(Res,&Pattern,Error,sizeof(Error));
  1150. return _error->Error(_("Regex compilation error - %s"),Error);
  1151. }
  1152. // Run over the matches
  1153. bool Hit = false;
  1154. for (Pkg = Cache->PkgBegin(); Pkg.end() == false; Pkg++)
  1155. {
  1156. if (regexec(&Pattern,Pkg.Name(),0,0,0) != 0)
  1157. continue;
  1158. if (VerTag != 0)
  1159. if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
  1160. return false;
  1161. Hit |= TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,
  1162. ExpectedInst,false);
  1163. }
  1164. regfree(&Pattern);
  1165. if (Hit == false)
  1166. return _error->Error(_("Couldn't find package %s"),S);
  1167. }
  1168. else
  1169. {
  1170. if (VerTag != 0)
  1171. if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
  1172. return false;
  1173. if (TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,ExpectedInst) == false)
  1174. return false;
  1175. }
  1176. }
  1177. /* If we are in the Broken fixing mode we do not attempt to fix the
  1178. problems. This is if the user invoked install without -f and gave
  1179. packages */
  1180. if (BrokenFix == true && Cache->BrokenCount() != 0)
  1181. {
  1182. c1out << _("You might want to run `apt-get -f install' to correct these:") << endl;
  1183. ShowBroken(c1out,Cache,false);
  1184. return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
  1185. }
  1186. // Call the scored problem resolver
  1187. Fix.InstallProtect();
  1188. if (Fix.Resolve(true) == false)
  1189. _error->Discard();
  1190. // Now we check the state of the packages,
  1191. if (Cache->BrokenCount() != 0)
  1192. {
  1193. c1out <<
  1194. _("Some packages could not be installed. This may mean that you have\n"
  1195. "requested an impossible situation or if you are using the unstable\n"
  1196. "distribution that some required packages have not yet been created\n"
  1197. "or been moved out of Incoming.") << endl;
  1198. if (Packages == 1)
  1199. {
  1200. c1out << endl;
  1201. c1out <<
  1202. _("Since you only requested a single operation it is extremely likely that\n"
  1203. "the package is simply not installable and a bug report against\n"
  1204. "that package should be filed.") << endl;
  1205. }
  1206. c1out << _("The following information may help to resolve the situation:") << endl;
  1207. c1out << endl;
  1208. ShowBroken(c1out,Cache,false);
  1209. return _error->Error(_("Sorry, broken packages"));
  1210. }
  1211. /* Print out a list of packages that are going to be installed extra
  1212. to what the user asked */
  1213. if (Cache->InstCount() != ExpectedInst)
  1214. {
  1215. string List;
  1216. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  1217. {
  1218. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  1219. if ((*Cache)[I].Install() == false)
  1220. continue;
  1221. const char **J;
  1222. for (J = CmdL.FileList + 1; *J != 0; J++)
  1223. if (strcmp(*J,I.Name()) == 0)
  1224. break;
  1225. if (*J == 0)
  1226. List += string(I.Name()) + " ";
  1227. }
  1228. ShowList(c1out,_("The following extra packages will be installed:"),List);
  1229. }
  1230. // See if we need to prompt
  1231. if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
  1232. return InstallPackages(Cache,false,false);
  1233. return InstallPackages(Cache,false);
  1234. }
  1235. /*}}}*/
  1236. // DoDistUpgrade - Automatic smart upgrader /*{{{*/
  1237. // ---------------------------------------------------------------------
  1238. /* Intelligent upgrader that will install and remove packages at will */
  1239. bool DoDistUpgrade(CommandLine &CmdL)
  1240. {
  1241. CacheFile Cache;
  1242. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1243. return false;
  1244. c0out << _("Calculating Upgrade... ") << flush;
  1245. if (pkgDistUpgrade(*Cache) == false)
  1246. {
  1247. c0out << _("Failed") << endl;
  1248. ShowBroken(c1out,Cache,false);
  1249. return false;
  1250. }
  1251. c0out << _("Done") << endl;
  1252. return InstallPackages(Cache,true);
  1253. }
  1254. /*}}}*/
  1255. // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
  1256. // ---------------------------------------------------------------------
  1257. /* Follows dselect's selections */
  1258. bool DoDSelectUpgrade(CommandLine &CmdL)
  1259. {
  1260. CacheFile Cache;
  1261. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1262. return false;
  1263. // Install everything with the install flag set
  1264. pkgCache::PkgIterator I = Cache->PkgBegin();
  1265. for (;I.end() != true; I++)
  1266. {
  1267. /* Install the package only if it is a new install, the autoupgrader
  1268. will deal with the rest */
  1269. if (I->SelectedState == pkgCache::State::Install)
  1270. Cache->MarkInstall(I,false);
  1271. }
  1272. /* Now install their deps too, if we do this above then order of
  1273. the status file is significant for | groups */
  1274. for (I = Cache->PkgBegin();I.end() != true; I++)
  1275. {
  1276. /* Install the package only if it is a new install, the autoupgrader
  1277. will deal with the rest */
  1278. if (I->SelectedState == pkgCache::State::Install)
  1279. Cache->MarkInstall(I,true);
  1280. }
  1281. // Apply erasures now, they override everything else.
  1282. for (I = Cache->PkgBegin();I.end() != true; I++)
  1283. {
  1284. // Remove packages
  1285. if (I->SelectedState == pkgCache::State::DeInstall ||
  1286. I->SelectedState == pkgCache::State::Purge)
  1287. Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
  1288. }
  1289. /* Resolve any problems that dselect created, allupgrade cannot handle
  1290. such things. We do so quite agressively too.. */
  1291. if (Cache->BrokenCount() != 0)
  1292. {
  1293. pkgProblemResolver Fix(Cache);
  1294. // Hold back held packages.
  1295. if (_config->FindB("APT::Ignore-Hold",false) == false)
  1296. {
  1297. for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
  1298. {
  1299. if (I->SelectedState == pkgCache::State::Hold)
  1300. {
  1301. Fix.Protect(I);
  1302. Cache->MarkKeep(I);
  1303. }
  1304. }
  1305. }
  1306. if (Fix.Resolve() == false)
  1307. {
  1308. ShowBroken(c1out,Cache,false);
  1309. return _error->Error("Internal Error, problem resolver broke stuff");
  1310. }
  1311. }
  1312. // Now upgrade everything
  1313. if (pkgAllUpgrade(Cache) == false)
  1314. {
  1315. ShowBroken(c1out,Cache,false);
  1316. return _error->Error("Internal Error, problem resolver broke stuff");
  1317. }
  1318. return InstallPackages(Cache,false);
  1319. }
  1320. /*}}}*/
  1321. // DoClean - Remove download archives /*{{{*/
  1322. // ---------------------------------------------------------------------
  1323. /* */
  1324. bool DoClean(CommandLine &CmdL)
  1325. {
  1326. if (_config->FindB("APT::Get::Simulate") == true)
  1327. {
  1328. cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
  1329. _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
  1330. return true;
  1331. }
  1332. // Lock the archive directory
  1333. FileFd Lock;
  1334. if (_config->FindB("Debug::NoLocking",false) == false)
  1335. {
  1336. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1337. if (_error->PendingError() == true)
  1338. return _error->Error(_("Unable to lock the download directory"));
  1339. }
  1340. pkgAcquire Fetcher;
  1341. Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
  1342. Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
  1343. return true;
  1344. }
  1345. /*}}}*/
  1346. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  1347. // ---------------------------------------------------------------------
  1348. /* This is similar to clean but it only purges things that cannot be
  1349. downloaded, that is old versions of cached packages. */
  1350. class LogCleaner : public pkgArchiveCleaner
  1351. {
  1352. protected:
  1353. virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
  1354. {
  1355. c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
  1356. if (_config->FindB("APT::Get::Simulate") == false)
  1357. unlink(File);
  1358. };
  1359. };
  1360. bool DoAutoClean(CommandLine &CmdL)
  1361. {
  1362. // Lock the archive directory
  1363. FileFd Lock;
  1364. if (_config->FindB("Debug::NoLocking",false) == false)
  1365. {
  1366. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1367. if (_error->PendingError() == true)
  1368. return _error->Error(_("Unable to lock the download directory"));
  1369. }
  1370. CacheFile Cache;
  1371. if (Cache.Open() == false)
  1372. return false;
  1373. LogCleaner Cleaner;
  1374. return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
  1375. Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
  1376. }
  1377. /*}}}*/
  1378. // DoCheck - Perform the check operation /*{{{*/
  1379. // ---------------------------------------------------------------------
  1380. /* Opening automatically checks the system, this command is mostly used
  1381. for debugging */
  1382. bool DoCheck(CommandLine &CmdL)
  1383. {
  1384. CacheFile Cache;
  1385. Cache.Open();
  1386. Cache.CheckDeps();
  1387. return true;
  1388. }
  1389. /*}}}*/
  1390. // DoSource - Fetch a source archive /*{{{*/
  1391. // ---------------------------------------------------------------------
  1392. /* Fetch souce packages */
  1393. struct DscFile
  1394. {
  1395. string Package;
  1396. string Version;
  1397. string Dsc;
  1398. };
  1399. bool DoSource(CommandLine &CmdL)
  1400. {
  1401. CacheFile Cache;
  1402. if (Cache.Open(false) == false)
  1403. return false;
  1404. if (CmdL.FileSize() <= 1)
  1405. return _error->Error(_("Must specify at least one package to fetch source for"));
  1406. // Read the source list
  1407. pkgSourceList List;
  1408. if (List.ReadMainList() == false)
  1409. return _error->Error(_("The list of sources could not be read."));
  1410. // Create the text record parsers
  1411. pkgRecords Recs(Cache);
  1412. pkgSrcRecords SrcRecs(List);
  1413. if (_error->PendingError() == true)
  1414. return false;
  1415. // Create the download object
  1416. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1417. pkgAcquire Fetcher(&Stat);
  1418. DscFile *Dsc = new DscFile[CmdL.FileSize()];
  1419. // Load the requestd sources into the fetcher
  1420. unsigned J = 0;
  1421. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  1422. {
  1423. string Src;
  1424. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  1425. if (Last == 0)
  1426. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  1427. // Back track
  1428. vector<pkgSrcRecords::File> Lst;
  1429. if (Last->Files(Lst) == false)
  1430. return false;
  1431. // Load them into the fetcher
  1432. for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
  1433. I != Lst.end(); I++)
  1434. {
  1435. // Try to guess what sort of file it is we are getting.
  1436. if (I->Type == "dsc")
  1437. {
  1438. Dsc[J].Package = Last->Package();
  1439. Dsc[J].Version = Last->Version();
  1440. Dsc[J].Dsc = flNotDir(I->Path);
  1441. }
  1442. // Diff only mode only fetches .diff files
  1443. if (_config->FindB("APT::Get::Diff-Only",false) == true &&
  1444. I->Type != "diff")
  1445. continue;
  1446. // Tar only mode only fetches .tar files
  1447. if (_config->FindB("APT::Get::Tar-Only",false) == true &&
  1448. I->Type != "tar")
  1449. continue;
  1450. new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
  1451. I->MD5Hash,I->Size,
  1452. Last->Index().SourceInfo(*Last,*I),Src);
  1453. }
  1454. }
  1455. // Display statistics
  1456. double FetchBytes = Fetcher.FetchNeeded();
  1457. double FetchPBytes = Fetcher.PartialPresent();
  1458. double DebBytes = Fetcher.TotalNeeded();
  1459. // Check for enough free space
  1460. struct statvfs Buf;
  1461. string OutputDir = ".";
  1462. if (statvfs(OutputDir.c_str(),&Buf) != 0)
  1463. return _error->Errno("statvfs","Couldn't determine free space in %s",
  1464. OutputDir.c_str());
  1465. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  1466. return _error->Error(_("Sorry, you don't have enough free space in %s"),
  1467. OutputDir.c_str());
  1468. // Number of bytes
  1469. if (DebBytes != FetchBytes)
  1470. ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
  1471. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  1472. else
  1473. ioprintf(c1out,_("Need to get %sB of source archives.\n"),
  1474. SizeToStr(DebBytes).c_str());
  1475. if (_config->FindB("APT::Get::Simulate",false) == true)
  1476. {
  1477. for (unsigned I = 0; I != J; I++)
  1478. ioprintf(cout,_("Fetch Source %s\n"),Dsc[I].Package.c_str());
  1479. return true;
  1480. }
  1481. // Just print out the uris an exit if the --print-uris flag was used
  1482. if (_config->FindB("APT::Get::Print-URIs") == true)
  1483. {
  1484. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  1485. for (; I != Fetcher.UriEnd(); I++)
  1486. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  1487. I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
  1488. return true;
  1489. }
  1490. // Run it
  1491. if (Fetcher.Run() == pkgAcquire::Failed)
  1492. return false;
  1493. // Print error messages
  1494. bool Failed = false;
  1495. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  1496. {
  1497. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  1498. (*I)->Complete == true)
  1499. continue;
  1500. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  1501. (*I)->ErrorText.c_str());
  1502. Failed = true;
  1503. }
  1504. if (Failed == true)
  1505. return _error->Error(_("Failed to fetch some archives."));
  1506. if (_config->FindB("APT::Get::Download-only",false) == true)
  1507. {
  1508. c1out << _("Download complete and in download only mode") << endl;
  1509. return true;
  1510. }
  1511. // Unpack the sources
  1512. pid_t Process = ExecFork();
  1513. if (Process == 0)
  1514. {
  1515. for (unsigned I = 0; I != J; I++)
  1516. {
  1517. string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
  1518. // Diff only mode only fetches .diff files
  1519. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  1520. _config->FindB("APT::Get::Tar-Only",false) == true ||
  1521. Dsc[I].Dsc.empty() == true)
  1522. continue;
  1523. // See if the package is already unpacked
  1524. struct stat Stat;
  1525. if (stat(Dir.c_str(),&Stat) == 0 &&
  1526. S_ISDIR(Stat.st_mode) != 0)
  1527. {
  1528. ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
  1529. Dir.c_str());
  1530. }
  1531. else
  1532. {
  1533. // Call dpkg-source
  1534. char S[500];
  1535. snprintf(S,sizeof(S),"%s -x %s",
  1536. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  1537. Dsc[I].Dsc.c_str());
  1538. if (system(S) != 0)
  1539. {
  1540. fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
  1541. _exit(1);
  1542. }
  1543. }
  1544. // Try to compile it with dpkg-buildpackage
  1545. if (_config->FindB("APT::Get::Compile",false) == true)
  1546. {
  1547. // Call dpkg-buildpackage
  1548. char S[500];
  1549. snprintf(S,sizeof(S),"cd %s && %s %s",
  1550. Dir.c_str(),
  1551. _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
  1552. _config->Find("DPkg::Build-Options","-b -uc").c_str());
  1553. if (system(S) != 0)
  1554. {
  1555. fprintf(stderr,_("Build command '%s' failed.\n"),S);
  1556. _exit(1);
  1557. }
  1558. }
  1559. }
  1560. _exit(0);
  1561. }
  1562. // Wait for the subprocess
  1563. int Status = 0;
  1564. while (waitpid(Process,&Status,0) != Process)
  1565. {
  1566. if (errno == EINTR)
  1567. continue;
  1568. return _error->Errno("waitpid","Couldn't wait for subprocess");
  1569. }
  1570. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  1571. return _error->Error(_("Child process failed"));
  1572. return true;
  1573. }
  1574. /*}}}*/
  1575. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  1576. // ---------------------------------------------------------------------
  1577. /* This function will look at the build depends list of the given source
  1578. package and install the necessary packages to make it true, or fail. */
  1579. bool DoBuildDep(CommandLine &CmdL)
  1580. {
  1581. CacheFile Cache;
  1582. if (Cache.Open(true) == false)
  1583. return false;
  1584. if (CmdL.FileSize() <= 1)
  1585. return _error->Error(_("Must specify at least one package to check builddeps for"));
  1586. // Read the source list
  1587. pkgSourceList List;
  1588. if (List.ReadMainList() == false)
  1589. return _error->Error(_("The list of sources could not be read."));
  1590. // Create the text record parsers
  1591. pkgRecords Recs(Cache);
  1592. pkgSrcRecords SrcRecs(List);
  1593. if (_error->PendingError() == true)
  1594. return false;
  1595. // Create the download object
  1596. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1597. pkgAcquire Fetcher(&Stat);
  1598. unsigned J = 0;
  1599. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  1600. {
  1601. string Src;
  1602. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  1603. if (Last == 0)
  1604. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  1605. // Process the build-dependencies
  1606. vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  1607. if (Last->BuildDepends(BuildDeps) == false)
  1608. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  1609. if (BuildDeps.size() == 0)
  1610. {
  1611. ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
  1612. continue;
  1613. }
  1614. // Install the requested packages
  1615. unsigned int ExpectedInst = 0;
  1616. vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
  1617. pkgProblemResolver Fix(Cache);
  1618. for (D = BuildDeps.begin(); D != BuildDeps.end(); D++)
  1619. {
  1620. pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
  1621. if (Pkg.end() == true)
  1622. return _error->Error(_("%s dependency on %s cannot be satisfied because the package %s cannot be found"),
  1623. Last->BuildDepType((*D).Type),Src.c_str(),(*D).Package.c_str());
  1624. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  1625. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  1626. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  1627. {
  1628. /*
  1629. * conflict; need to remove if we have an installed version
  1630. * that satisfies the version criterial
  1631. */
  1632. if (IV.end() == false &&
  1633. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  1634. TryToInstall(Pkg,Cache,Fix,true,false,ExpectedInst);
  1635. }
  1636. else
  1637. {
  1638. /*
  1639. * If this is a virtual package, we need to check the list of
  1640. * packages that provide it and see if any of those are
  1641. * installed
  1642. */
  1643. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  1644. for (; Prv.end() != true; Prv++)
  1645. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  1646. break;
  1647. if (Prv.end() == true)
  1648. {
  1649. /*
  1650. * depends; need to install or upgrade if we don't have the
  1651. * package installed or if the version does not satisfy the
  1652. * build dep. This is complicated by the fact that if we
  1653. * depend on a version lower than what we already have
  1654. * installed it is not clear what should be done; in practice
  1655. * this case should be rare though and right now nothing
  1656. * is done about it :-(
  1657. */
  1658. if (IV.end() == true ||
  1659. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
  1660. TryToInstall(Pkg,Cache,Fix,false,false,ExpectedInst);
  1661. }
  1662. }
  1663. }
  1664. Fix.InstallProtect();
  1665. if (Fix.Resolve(true) == false)
  1666. _error->Discard();
  1667. // Now we check the state of the packages,
  1668. if (Cache->BrokenCount() != 0)
  1669. return _error->Error(_("Some broken packages were found while trying to process build-dependencies.\n"
  1670. "You might want to run `apt-get -f install' to correct these."));
  1671. }
  1672. if (InstallPackages(Cache, false, true) == false)
  1673. return _error->Error(_("Failed to process build dependencies"));
  1674. return true;
  1675. }
  1676. /*}}}*/
  1677. // DoMoo - Never Ask, Never Tell /*{{{*/
  1678. // ---------------------------------------------------------------------
  1679. /* */
  1680. bool DoMoo(CommandLine &CmdL)
  1681. {
  1682. cout <<
  1683. " (__) \n"
  1684. " (oo) \n"
  1685. " /------\\/ \n"
  1686. " / | || \n"
  1687. " * /\\---/\\ \n"
  1688. " ~~ ~~ \n"
  1689. "....\"Have you mooed today?\"...\n";
  1690. return true;
  1691. }
  1692. /*}}}*/
  1693. // ShowHelp - Show a help screen /*{{{*/
  1694. // ---------------------------------------------------------------------
  1695. /* */
  1696. bool ShowHelp(CommandLine &CmdL)
  1697. {
  1698. ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
  1699. COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
  1700. if (_config->FindB("version") == true)
  1701. {
  1702. cout << _("Supported Modules:") << endl;
  1703. for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
  1704. {
  1705. pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
  1706. if (_system != 0 && _system->VS == VS)
  1707. cout << '*';
  1708. else
  1709. cout << ' ';
  1710. cout << "Ver: " << VS->Label << endl;
  1711. /* Print out all the packaging systems that will work with
  1712. this VS */
  1713. for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
  1714. {
  1715. pkgSystem *Sys = pkgSystem::GlobalList[J];
  1716. if (_system == Sys)
  1717. cout << '*';
  1718. else
  1719. cout << ' ';
  1720. if (Sys->VS->TestCompatibility(*VS) == true)
  1721. cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
  1722. }
  1723. }
  1724. for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
  1725. {
  1726. pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
  1727. cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
  1728. }
  1729. for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
  1730. {
  1731. pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
  1732. cout << " Idx: " << Type->Label << endl;
  1733. }
  1734. return true;
  1735. }
  1736. cout <<
  1737. _("Usage: apt-get [options] command\n"
  1738. " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
  1739. " apt-get [options] source pkg1 [pkg2 ...]\n"
  1740. "\n"
  1741. "apt-get is a simple command line interface for downloading and\n"
  1742. "installing packages. The most frequently used commands are update\n"
  1743. "and install.\n"
  1744. "\n"
  1745. "Commands:\n"
  1746. " update - Retrieve new lists of packages\n"
  1747. " upgrade - Perform an upgrade\n"
  1748. " install - Install new packages (pkg is libc6 not libc6.deb)\n"
  1749. " remove - Remove packages\n"
  1750. " source - Download source archives\n"
  1751. " build-dep - Configure build-dependencies for source packages\n"
  1752. " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
  1753. " dselect-upgrade - Follow dselect selections\n"
  1754. " clean - Erase downloaded archive files\n"
  1755. " autoclean - Erase old downloaded archive files\n"
  1756. " check - Verify that there are no broken dependencies\n"
  1757. "\n"
  1758. "Options:\n"
  1759. " -h This help text.\n"
  1760. " -q Loggable output - no progress indicator\n"
  1761. " -qq No output except for errors\n"
  1762. " -d Download only - do NOT install or unpack archives\n"
  1763. " -s No-act. Perform ordering simulation\n"
  1764. " -y Assume Yes to all queries and do not prompt\n"
  1765. " -f Attempt to continue if the integrity check fails\n"
  1766. " -m Attempt to continue if archives are unlocatable\n"
  1767. " -u Show a list of upgraded packages as well\n"
  1768. " -b Build the source package after fetching it\n"
  1769. " -c=? Read this configuration file\n"
  1770. " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n"
  1771. "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
  1772. "pages for more information and options.\n"
  1773. " This APT has Super Cow Powers.\n");
  1774. return true;
  1775. }
  1776. /*}}}*/
  1777. // GetInitialize - Initialize things for apt-get /*{{{*/
  1778. // ---------------------------------------------------------------------
  1779. /* */
  1780. void GetInitialize()
  1781. {
  1782. _config->Set("quiet",0);
  1783. _config->Set("help",false);
  1784. _config->Set("APT::Get::Download-Only",false);
  1785. _config->Set("APT::Get::Simulate",false);
  1786. _config->Set("APT::Get::Assume-Yes",false);
  1787. _config->Set("APT::Get::Fix-Broken",false);
  1788. _config->Set("APT::Get::Force-Yes",false);
  1789. _config->Set("APT::Get::APT::Get::No-List-Cleanup",true);
  1790. }
  1791. /*}}}*/
  1792. // SigWinch - Window size change signal handler /*{{{*/
  1793. // ---------------------------------------------------------------------
  1794. /* */
  1795. void SigWinch(int)
  1796. {
  1797. // Riped from GNU ls
  1798. #ifdef TIOCGWINSZ
  1799. struct winsize ws;
  1800. if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
  1801. ScreenWidth = ws.ws_col - 1;
  1802. #endif
  1803. }
  1804. /*}}}*/
  1805. int main(int argc,const char *argv[])
  1806. {
  1807. CommandLine::Args Args[] = {
  1808. {'h',"help","help",0},
  1809. {'v',"version","version",0},
  1810. {'q',"quiet","quiet",CommandLine::IntLevel},
  1811. {'q',"silent","quiet",CommandLine::IntLevel},
  1812. {'d',"download-only","APT::Get::Download-Only",0},
  1813. {'b',"compile","APT::Get::Compile",0},
  1814. {'b',"build","APT::Get::Compile",0},
  1815. {'s',"simulate","APT::Get::Simulate",0},
  1816. {'s',"just-print","APT::Get::Simulate",0},
  1817. {'s',"recon","APT::Get::Simulate",0},
  1818. {'s',"dry-run","APT::Get::Simulate",0},
  1819. {'s',"no-act","APT::Get::Simulate",0},
  1820. {'y',"yes","APT::Get::Assume-Yes",0},
  1821. {'y',"assume-yes","APT::Get::Assume-Yes",0},
  1822. {'f',"fix-broken","APT::Get::Fix-Broken",0},
  1823. {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
  1824. {'m',"ignore-missing","APT::Get::Fix-Missing",0},
  1825. {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
  1826. {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
  1827. {0,"download","APT::Get::Download",0},
  1828. {0,"fix-missing","APT::Get::Fix-Missing",0},
  1829. {0,"ignore-hold","APT::Ignore-Hold",0},
  1830. {0,"upgrade","APT::Get::upgrade",0},
  1831. {0,"force-yes","APT::Get::force-yes",0},
  1832. {0,"print-uris","APT::Get::Print-URIs",0},
  1833. {0,"diff-only","APT::Get::Diff-Only",0},
  1834. {0,"tar-only","APT::Get::tar-Only",0},
  1835. {0,"purge","APT::Get::Purge",0},
  1836. {0,"list-cleanup","APT::Get::List-Cleanup",0},
  1837. {0,"reinstall","APT::Get::ReInstall",0},
  1838. {0,"trivial-only","APT::Get::Trivial-Only",0},
  1839. {0,"remove","APT::Get::Remove",0},
  1840. {0,"only-source","APT::Get::Only-Source",0},
  1841. {'c',"config-file",0,CommandLine::ConfigFile},
  1842. {'o',"option",0,CommandLine::ArbItem},
  1843. {0,0,0,0}};
  1844. CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
  1845. {"upgrade",&DoUpgrade},
  1846. {"install",&DoInstall},
  1847. {"remove",&DoInstall},
  1848. {"dist-upgrade",&DoDistUpgrade},
  1849. {"dselect-upgrade",&DoDSelectUpgrade},
  1850. {"build-dep",&DoBuildDep},
  1851. {"clean",&DoClean},
  1852. {"autoclean",&DoAutoClean},
  1853. {"check",&DoCheck},
  1854. {"source",&DoSource},
  1855. {"moo",&DoMoo},
  1856. {"help",&ShowHelp},
  1857. {0,0}};
  1858. // Parse the command line and initialize the package library
  1859. CommandLine CmdL(Args,_config);
  1860. if (pkgInitConfig(*_config) == false ||
  1861. CmdL.Parse(argc,argv) == false ||
  1862. pkgInitSystem(*_config,_system) == false)
  1863. {
  1864. if (_config->FindB("version") == true)
  1865. ShowHelp(CmdL);
  1866. _error->DumpErrors();
  1867. return 100;
  1868. }
  1869. // See if the help should be shown
  1870. if (_config->FindB("help") == true ||
  1871. _config->FindB("version") == true ||
  1872. CmdL.FileSize() == 0)
  1873. {
  1874. ShowHelp(CmdL);
  1875. return 0;
  1876. }
  1877. // Deal with stdout not being a tty
  1878. if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
  1879. _config->Set("quiet","1");
  1880. // Setup the output streams
  1881. c0out.rdbuf(cout.rdbuf());
  1882. c1out.rdbuf(cout.rdbuf());
  1883. c2out.rdbuf(cout.rdbuf());
  1884. if (_config->FindI("quiet",0) > 0)
  1885. c0out.rdbuf(devnull.rdbuf());
  1886. if (_config->FindI("quiet",0) > 1)
  1887. c1out.rdbuf(devnull.rdbuf());
  1888. // Setup the signals
  1889. signal(SIGPIPE,SIG_IGN);
  1890. signal(SIGWINCH,SigWinch);
  1891. SigWinch(0);
  1892. // Match the operation
  1893. CmdL.DispatchArg(Cmds);
  1894. // Print any errors or warnings found during parsing
  1895. if (_error->empty() == false)
  1896. {
  1897. bool Errors = _error->PendingError();
  1898. _error->DumpErrors();
  1899. return Errors == true?100:0;
  1900. }
  1901. return 0;
  1902. }