apt-get.cc 80 KB

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