apt-get.cc 66 KB

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