apt-get.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-get.cc,v 1.8 1998/11/12 05:30:10 jgg Exp $
  4. /* ######################################################################
  5. apt-get - Cover for dpkg
  6. This is an allout cover for dpkg implementing a safer front end. It is
  7. based largely on libapt-pkg.
  8. The syntax is different,
  9. apt-get [opt] command [things]
  10. Where command is:
  11. update - Resyncronize the package files from their sources
  12. upgrade - Smart-Download the newest versions of all packages
  13. dselect-upgrade - Follows dselect's changes to the Status: field
  14. and installes new and removes old packages
  15. dist-upgrade - Powerfull upgrader designed to handle the issues with
  16. a new distribution.
  17. install - Download and install a given package (by name, not by .deb)
  18. check - Update the package cache and check for broken packages
  19. clean - Erase the .debs downloaded to /var/cache/apt/archives and
  20. the partial dir too
  21. ##################################################################### */
  22. /*}}}*/
  23. // Include Files /*{{{*/
  24. #include <apt-pkg/error.h>
  25. #include <apt-pkg/cmndline.h>
  26. #include <apt-pkg/init.h>
  27. #include <apt-pkg/depcache.h>
  28. #include <apt-pkg/sourcelist.h>
  29. #include <apt-pkg/pkgcachegen.h>
  30. #include <apt-pkg/algorithms.h>
  31. #include <apt-pkg/acquire-item.h>
  32. #include <config.h>
  33. #include "acqprogress.h"
  34. #include <fstream.h>
  35. #include <termios.h>
  36. #include <sys/ioctl.h>
  37. #include <signal.h>
  38. /*}}}*/
  39. ostream c0out;
  40. ostream c1out;
  41. ostream c2out;
  42. ofstream devnull("/dev/null");
  43. unsigned int ScreenWidth = 80;
  44. // ShowList - Show a list /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* This prints out a string of space seperated words with a title and
  47. a two space indent line wraped to the current screen width. */
  48. void ShowList(ostream &out,string Title,string List)
  49. {
  50. if (List.empty() == true)
  51. return;
  52. // Acount for the leading space
  53. int ScreenWidth = ::ScreenWidth - 3;
  54. out << Title << endl;
  55. string::size_type Start = 0;
  56. while (Start < List.size())
  57. {
  58. string::size_type End;
  59. if (Start + ScreenWidth >= List.size())
  60. End = List.size();
  61. else
  62. End = List.rfind(' ',Start+ScreenWidth);
  63. if (End == string::npos || End < Start)
  64. End = Start + ScreenWidth;
  65. out << " " << string(List,Start,End - Start) << endl;
  66. Start = End + 1;
  67. }
  68. }
  69. /*}}}*/
  70. // ShowBroken - Debugging aide /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* This prints out the names of all the packages that are broken along
  73. with the name of each each broken dependency and a quite version
  74. description. */
  75. void ShowBroken(ostream &out,pkgDepCache &Cache)
  76. {
  77. out << "Sorry, but the following packages are broken - this means they have unmet" << endl;
  78. out << "dependencies:" << endl;
  79. pkgCache::PkgIterator I = Cache.PkgBegin();
  80. for (;I.end() != true; I++)
  81. {
  82. if (Cache[I].InstBroken() == false)
  83. continue;
  84. // Print out each package and the failed dependencies
  85. out <<" " << I.Name() << ":";
  86. int Indent = strlen(I.Name()) + 3;
  87. bool First = true;
  88. if (Cache[I].InstVerIter(Cache).end() == true)
  89. {
  90. cout << endl;
  91. continue;
  92. }
  93. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; D++)
  94. {
  95. if (Cache.IsImportantDep(D) == false || (Cache[D] &
  96. pkgDepCache::DepInstall) != 0)
  97. continue;
  98. if (First == false)
  99. for (int J = 0; J != Indent; J++)
  100. out << ' ';
  101. First = false;
  102. if (D->Type == pkgCache::Dep::Conflicts)
  103. out << " Conflicts:" << D.TargetPkg().Name();
  104. else
  105. out << " Depends:" << D.TargetPkg().Name();
  106. // Show a quick summary of the version requirements
  107. if (D.TargetVer() != 0)
  108. out << " (" << D.CompType() << " " << D.TargetVer() <<
  109. ")";
  110. /* Show a summary of the target package if possible. In the case
  111. of virtual packages we show nothing */
  112. pkgCache::PkgIterator Targ = D.TargetPkg();
  113. if (Targ->ProvidesList == 0)
  114. {
  115. out << " but ";
  116. pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
  117. if (Ver.end() == false)
  118. out << Ver.VerStr() << " is installed";
  119. else
  120. {
  121. if (Cache[Targ].CandidateVerIter(Cache).end() == true)
  122. {
  123. if (Targ->ProvidesList == 0)
  124. out << "it is not installable";
  125. else
  126. out << "it is a virtual package";
  127. }
  128. else
  129. out << "it is not installed";
  130. }
  131. }
  132. out << endl;
  133. }
  134. }
  135. }
  136. /*}}}*/
  137. // ShowNew - Show packages to newly install /*{{{*/
  138. // ---------------------------------------------------------------------
  139. /* */
  140. void ShowNew(ostream &out,pkgDepCache &Dep)
  141. {
  142. /* Print out a list of packages that are going to be removed extra
  143. to what the user asked */
  144. pkgCache::PkgIterator I = Dep.PkgBegin();
  145. string List;
  146. for (;I.end() != true; I++)
  147. if (Dep[I].NewInstall() == true)
  148. List += string(I.Name()) + " ";
  149. ShowList(out,"The following NEW packages will be installed:",List);
  150. }
  151. /*}}}*/
  152. // ShowDel - Show packages to delete /*{{{*/
  153. // ---------------------------------------------------------------------
  154. /* */
  155. void ShowDel(ostream &out,pkgDepCache &Dep)
  156. {
  157. /* Print out a list of packages that are going to be removed extra
  158. to what the user asked */
  159. pkgCache::PkgIterator I = Dep.PkgBegin();
  160. string List;
  161. for (;I.end() != true; I++)
  162. if (Dep[I].Delete() == true)
  163. List += string(I.Name()) + " ";
  164. ShowList(out,"The following packages will be REMOVED:",List);
  165. }
  166. /*}}}*/
  167. // ShowKept - Show kept packages /*{{{*/
  168. // ---------------------------------------------------------------------
  169. /* */
  170. void ShowKept(ostream &out,pkgDepCache &Dep)
  171. {
  172. pkgCache::PkgIterator I = Dep.PkgBegin();
  173. string List;
  174. for (;I.end() != true; I++)
  175. {
  176. // Not interesting
  177. if (Dep[I].Upgrade() == true || Dep[I].Upgradable() == false ||
  178. I->CurrentVer == 0 || Dep[I].Delete() == true)
  179. continue;
  180. List += string(I.Name()) + " ";
  181. }
  182. ShowList(out,"The following packages have been kept back",List);
  183. }
  184. /*}}}*/
  185. // ShowUpgraded - Show upgraded packages /*{{{*/
  186. // ---------------------------------------------------------------------
  187. /* */
  188. void ShowUpgraded(ostream &out,pkgDepCache &Dep)
  189. {
  190. pkgCache::PkgIterator I = Dep.PkgBegin();
  191. string List;
  192. for (;I.end() != true; I++)
  193. {
  194. // Not interesting
  195. if (Dep[I].Upgrade() == false || Dep[I].NewInstall() == true)
  196. continue;
  197. List += string(I.Name()) + " ";
  198. }
  199. ShowList(out,"The following packages will be upgraded",List);
  200. }
  201. /*}}}*/
  202. // ShowHold - Show held but changed packages /*{{{*/
  203. // ---------------------------------------------------------------------
  204. /* */
  205. void ShowHold(ostream &out,pkgDepCache &Dep)
  206. {
  207. pkgCache::PkgIterator I = Dep.PkgBegin();
  208. string List;
  209. for (;I.end() != true; I++)
  210. {
  211. if (Dep[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
  212. I->SelectedState == pkgCache::State::Hold)
  213. List += string(I.Name()) + " ";
  214. }
  215. ShowList(out,"The following held packages will be changed:",List);
  216. }
  217. /*}}}*/
  218. // ShowEssential - Show an essential package warning /*{{{*/
  219. // ---------------------------------------------------------------------
  220. /* This prints out a warning message that is not to be ignored. It shows
  221. all essential packages and their dependents that are to be removed.
  222. It is insanely risky to remove the dependents of an essential package! */
  223. void ShowEssential(ostream &out,pkgDepCache &Dep)
  224. {
  225. pkgCache::PkgIterator I = Dep.PkgBegin();
  226. string List;
  227. bool *Added = new bool[Dep.HeaderP->PackageCount];
  228. for (unsigned int I = 0; I != Dep.HeaderP->PackageCount; I++)
  229. Added[I] = false;
  230. for (;I.end() != true; I++)
  231. {
  232. if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
  233. continue;
  234. // The essential package is being removed
  235. if (Dep[I].Delete() == true)
  236. {
  237. if (Added[I->ID] == false)
  238. {
  239. Added[I->ID] = true;
  240. List += string(I.Name()) + " ";
  241. }
  242. }
  243. if (I->CurrentVer == 0)
  244. continue;
  245. // Print out any essential package depenendents that are to be removed
  246. for (pkgDepCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
  247. {
  248. pkgCache::PkgIterator P = D.SmartTargetPkg();
  249. if (Dep[P].Delete() == true)
  250. {
  251. if (Added[P->ID] == true)
  252. continue;
  253. Added[P->ID] = true;
  254. List += string(P.Name()) + " ";
  255. }
  256. }
  257. }
  258. if (List.empty() == false)
  259. out << "WARNING: The following essential packages will be removed" << endl;
  260. ShowList(out,"This should NOT be done unless you know exactly what you are doing!",List);
  261. delete [] Added;
  262. }
  263. /*}}}*/
  264. // Stats - Show some statistics /*{{{*/
  265. // ---------------------------------------------------------------------
  266. /* */
  267. void Stats(ostream &out,pkgDepCache &Dep)
  268. {
  269. unsigned long Upgrade = 0;
  270. unsigned long Install = 0;
  271. for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
  272. {
  273. if (Dep[I].NewInstall() == true)
  274. Install++;
  275. else
  276. if (Dep[I].Upgrade() == true)
  277. Upgrade++;
  278. }
  279. out << Upgrade << " packages upgraded, " <<
  280. Install << " newly installed, " <<
  281. Dep.DelCount() << " to remove and " <<
  282. Dep.KeepCount() << " not upgraded." << endl;
  283. if (Dep.BadCount() != 0)
  284. out << Dep.BadCount() << " packages not fully installed or removed." << endl;
  285. }
  286. /*}}}*/
  287. // class CacheFile - Cover class for some dependency cache functions /*{{{*/
  288. // ---------------------------------------------------------------------
  289. /* */
  290. class CacheFile
  291. {
  292. public:
  293. FileFd *File;
  294. MMap *Map;
  295. pkgDepCache *Cache;
  296. inline operator pkgDepCache &() {return *Cache;};
  297. inline pkgDepCache *operator ->() {return Cache;};
  298. inline pkgDepCache &operator *() {return *Cache;};
  299. bool Open();
  300. CacheFile() : File(0), Map(0), Cache(0) {};
  301. ~CacheFile()
  302. {
  303. delete Cache;
  304. delete Map;
  305. delete File;
  306. }
  307. };
  308. /*}}}*/
  309. // CacheFile::Open - Open the cache file /*{{{*/
  310. // ---------------------------------------------------------------------
  311. /* This routine generates the caches and then opens the dependency cache
  312. and verifies that the system is OK. */
  313. bool CacheFile::Open()
  314. {
  315. // Create a progress class
  316. OpTextProgress Progress(*_config);
  317. // Read the source list
  318. pkgSourceList List;
  319. if (List.ReadMainList() == false)
  320. return _error->Error("The list of sources could not be read.");
  321. // Build all of the caches
  322. pkgMakeStatusCache(List,Progress);
  323. if (_error->PendingError() == true)
  324. return _error->Error("The package lists or status file could not be parsed or opened.");
  325. Progress.Done();
  326. // Open the cache file
  327. File = new FileFd(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly);
  328. if (_error->PendingError() == true)
  329. return false;
  330. Map = new MMap(*File,MMap::Public | MMap::ReadOnly);
  331. if (_error->PendingError() == true)
  332. return false;
  333. Cache = new pkgDepCache(*Map,Progress);
  334. if (_error->PendingError() == true)
  335. return false;
  336. Progress.Done();
  337. // Check that the system is OK
  338. if (Cache->DelCount() != 0 || Cache->InstCount() != 0)
  339. return _error->Error("Internal Error, non-zero counts");
  340. // Apply corrections for half-installed packages
  341. if (pkgApplyStatus(*Cache) == false)
  342. return false;
  343. // Nothing is broken
  344. if (Cache->BrokenCount() == 0)
  345. return true;
  346. // Attempt to fix broken things
  347. if (_config->FindB("APT::Get::Fix-Broken",false) == true)
  348. {
  349. c1out << "Correcting dependencies..." << flush;
  350. if (pkgFixBroken(*Cache) == false || Cache->BrokenCount() != 0)
  351. {
  352. c1out << " failed." << endl;
  353. ShowBroken(c1out,*this);
  354. return _error->Error("Unable to correct dependencies");
  355. }
  356. if (pkgMinimizeUpgrade(*Cache) == false)
  357. return _error->Error("Unable to minimize the upgrade set");
  358. c1out << " Done" << endl;
  359. }
  360. else
  361. {
  362. c1out << "You might want to run `apt-get -f install' to correct these." << endl;
  363. ShowBroken(c1out,*this);
  364. return _error->Error("Unmet dependencies. Try using -f.");
  365. }
  366. return true;
  367. }
  368. /*}}}*/
  369. // InstallPackages - Actually download and install the packages /*{{{*/
  370. // ---------------------------------------------------------------------
  371. /* This displays the informative messages describing what is going to
  372. happen and then calls the download routines */
  373. bool InstallPackages(pkgDepCache &Cache,bool ShwKept)
  374. {
  375. ShowDel(c1out,Cache);
  376. ShowNew(c1out,Cache);
  377. if (ShwKept == true)
  378. ShowKept(c1out,Cache);
  379. ShowHold(c1out,Cache);
  380. if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
  381. ShowUpgraded(c1out,Cache);
  382. ShowEssential(c1out,Cache);
  383. Stats(c1out,Cache);
  384. // Sanity check
  385. if (Cache.BrokenCount() != 0)
  386. {
  387. ShowBroken(c1out,Cache);
  388. return _error->Error("Internal Error, InstallPackages was called with broken packages!");
  389. }
  390. if (Cache.DelCount() == 0 && Cache.InstCount() == 0 &&
  391. Cache.BadCount() == 0)
  392. return true;
  393. return true;
  394. }
  395. /*}}}*/
  396. // DoUpdate - Update the package lists /*{{{*/
  397. // ---------------------------------------------------------------------
  398. /* */
  399. bool DoUpdate(CommandLine &)
  400. {
  401. // Get the source list
  402. pkgSourceList List;
  403. if (List.ReadMainList() == false)
  404. return false;
  405. // Create the download object
  406. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  407. pkgAcquire Fetcher(&Stat);
  408. // Populate it with the source selection
  409. pkgSourceList::const_iterator I;
  410. for (I = List.begin(); I != List.end(); I++)
  411. {
  412. new pkgAcqIndex(&Fetcher,I);
  413. if (_error->PendingError() == true)
  414. return false;
  415. }
  416. // Run it
  417. if (Fetcher.Run() == false)
  418. return false;
  419. // Clean out any old list files
  420. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  421. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  422. return false;
  423. // Prepare the cache.
  424. CacheFile Cache;
  425. if (Cache.Open() == false)
  426. return false;
  427. return true;
  428. }
  429. /*}}}*/
  430. // DoUpgrade - Upgrade all packages /*{{{*/
  431. // ---------------------------------------------------------------------
  432. /* Upgrade all packages without installing new packages or erasing old
  433. packages */
  434. bool DoUpgrade(CommandLine &CmdL)
  435. {
  436. CacheFile Cache;
  437. if (Cache.Open() == false)
  438. return false;
  439. // Do the upgrade
  440. if (pkgAllUpgrade(Cache) == false)
  441. {
  442. ShowBroken(c1out,Cache);
  443. return _error->Error("Internal Error, AllUpgrade broke stuff");
  444. }
  445. return InstallPackages(Cache,true);
  446. }
  447. /*}}}*/
  448. // DoInstall - Install packages from the command line /*{{{*/
  449. // ---------------------------------------------------------------------
  450. /* Install named packages */
  451. bool DoInstall(CommandLine &CmdL)
  452. {
  453. CacheFile Cache;
  454. if (Cache.Open() == false)
  455. return false;
  456. int ExpectedInst = 0;
  457. int Packages = 0;
  458. pkgProblemResolver Fix(Cache);
  459. bool DefRemove = false;
  460. if (strcasecmp(CmdL.FileList[0],"remove") == 0)
  461. DefRemove = true;
  462. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  463. {
  464. // Duplicate the string
  465. unsigned int Length = strlen(*I);
  466. char S[300];
  467. if (Length >= sizeof(S))
  468. continue;
  469. strcpy(S,*I);
  470. // See if we are removing the package
  471. bool Remove = DefRemove;
  472. if (S[Length - 1] == '-')
  473. {
  474. Remove = true;
  475. S[--Length] = 0;
  476. }
  477. if (S[Length - 1] == '+')
  478. {
  479. Remove = false;
  480. S[--Length] = 0;
  481. }
  482. // Locate the package
  483. pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
  484. Packages++;
  485. if (Pkg.end() == true)
  486. return _error->Error("Couldn't find package %s",S);
  487. // Check if there is something new to install
  488. pkgDepCache::StateCache &State = (*Cache)[Pkg];
  489. if (State.CandidateVer == 0)
  490. {
  491. if (Pkg->ProvidesList != 0)
  492. {
  493. c1out << "Package " << S << " is a virtual package provided by:" << endl;
  494. pkgCache::PrvIterator I = Pkg.ProvidesList();
  495. for (; I.end() == false; I++)
  496. {
  497. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  498. if ((*Cache)[Pkg].CandidateVerIter(*Cache) == I.OwnerVer())
  499. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
  500. if ((*Cache)[Pkg].InstVerIter(*Cache) == I.OwnerVer())
  501. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
  502. " [Installed]"<< endl;
  503. }
  504. c1out << "You should explicly select one to install." << endl;
  505. }
  506. else
  507. {
  508. c1out << "Package " << S << " has no available version, but exists in the database." << endl;
  509. c1out << "This typically means that the package was mentioned in a dependency and " << endl;
  510. c1out << "never uploaded, or that it is an obsolete package." << endl;
  511. }
  512. return _error->Error("Package %s has no installation candidate",S);
  513. }
  514. Fix.Protect(Pkg);
  515. if (Remove == true)
  516. {
  517. Fix.Remove(Pkg);
  518. Cache->MarkDelete(Pkg);
  519. continue;
  520. }
  521. // Install it
  522. Cache->MarkInstall(Pkg,false);
  523. if (State.Install() == false)
  524. c1out << "Sorry, " << S << " is already the newest version" << endl;
  525. else
  526. ExpectedInst++;
  527. // Install it with autoinstalling enabled.
  528. if (State.InstBroken() == true)
  529. Cache->MarkInstall(Pkg,true);
  530. }
  531. // Call the scored problem resolver
  532. Fix.InstallProtect();
  533. if (Fix.Resolve(true) == false)
  534. _error->Discard();
  535. // Now we check the state of the packages,
  536. if (Cache->BrokenCount() != 0)
  537. {
  538. c1out << "Some packages could not be installed. This may mean that you have" << endl;
  539. c1out << "requested an impossible situation or if you are using the unstable" << endl;
  540. c1out << "distribution that some required packages have not yet been created" << endl;
  541. c1out << "or been moved out of Incoming." << endl;
  542. if (Packages == 1)
  543. {
  544. c1out << endl;
  545. c1out << "Since you only requested a single operation it is extremely likely that" << endl;
  546. c1out << "the package is simply not installable and a bug report against" << endl;
  547. c1out << "that package should be filed." << endl;
  548. }
  549. c1out << "The following information may help to resolve the situation:" << endl;
  550. c1out << endl;
  551. ShowBroken(c1out,Cache);
  552. return _error->Error("Sorry, broken packages");
  553. }
  554. /* Print out a list of packages that are going to be installed extra
  555. to what the user asked */
  556. if (Cache->InstCount() != ExpectedInst)
  557. {
  558. string List;
  559. pkgCache::PkgIterator I = Cache->PkgBegin();
  560. for (;I.end() != true; I++)
  561. {
  562. if ((*Cache)[I].Install() == false)
  563. continue;
  564. const char **J;
  565. for (J = CmdL.FileList + 1; *J != 0; J++)
  566. if (strcmp(*J,I.Name()) == 0)
  567. break;
  568. if (*J == 0)
  569. List += string(I.Name()) + " ";
  570. }
  571. ShowList(c1out,"The following extra packages will be installed:",List);
  572. }
  573. return InstallPackages(Cache,false);
  574. }
  575. /*}}}*/
  576. // DoDistUpgrade - Automatic smart upgrader /*{{{*/
  577. // ---------------------------------------------------------------------
  578. /* Intelligent upgrader that will install and remove packages at will */
  579. bool DoDistUpgrade(CommandLine &CmdL)
  580. {
  581. CacheFile Cache;
  582. if (Cache.Open() == false)
  583. return false;
  584. c0out << "Calculating Upgrade... " << flush;
  585. if (pkgDistUpgrade(*Cache) == false)
  586. {
  587. c0out << "Failed" << endl;
  588. ShowBroken(c1out,Cache);
  589. return false;
  590. }
  591. c0out << "Done" << endl;
  592. return InstallPackages(Cache,true);
  593. }
  594. /*}}}*/
  595. // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
  596. // ---------------------------------------------------------------------
  597. /* Follows dselect's selections */
  598. bool DoDSelectUpgrade(CommandLine &CmdL)
  599. {
  600. CacheFile Cache;
  601. if (Cache.Open() == false)
  602. return false;
  603. // Install everything with the install flag set
  604. pkgCache::PkgIterator I = Cache->PkgBegin();
  605. for (;I.end() != true; I++)
  606. {
  607. /* Install the package only if it is a new install, the autoupgrader
  608. will deal with the rest */
  609. if (I->SelectedState == pkgCache::State::Install)
  610. Cache->MarkInstall(I,false);
  611. }
  612. /* Now install their deps too, if we do this above then order of
  613. the status file is significant for | groups */
  614. for (I = Cache->PkgBegin();I.end() != true; I++)
  615. {
  616. /* Install the package only if it is a new install, the autoupgrader
  617. will deal with the rest */
  618. if (I->SelectedState == pkgCache::State::Install)
  619. Cache->MarkInstall(I);
  620. }
  621. // Apply erasures now, they override everything else.
  622. for (I = Cache->PkgBegin();I.end() != true; I++)
  623. {
  624. // Remove packages
  625. if (I->SelectedState == pkgCache::State::DeInstall ||
  626. I->SelectedState == pkgCache::State::Purge)
  627. Cache->MarkDelete(I);
  628. }
  629. /* Use updates smart upgrade to do the rest, it will automatically
  630. ignore held items */
  631. if (pkgAllUpgrade(Cache) == false)
  632. {
  633. ShowBroken(c1out,Cache);
  634. return _error->Error("Internal Error, AllUpgrade broke stuff");
  635. }
  636. return InstallPackages(Cache,false);
  637. }
  638. /*}}}*/
  639. // DoClean - Remove download archives /*{{{*/
  640. // ---------------------------------------------------------------------
  641. /* */
  642. bool DoClean(CommandLine &CmdL)
  643. {
  644. return true;
  645. }
  646. /*}}}*/
  647. // DoCheck - Perform the check operation /*{{{*/
  648. // ---------------------------------------------------------------------
  649. /* Opening automatically checks the system, this command is mostly used
  650. for debugging */
  651. bool DoCheck(CommandLine &CmdL)
  652. {
  653. CacheFile Cache;
  654. Cache.Open();
  655. return true;
  656. }
  657. /*}}}*/
  658. // ShowHelp - Show a help screen /*{{{*/
  659. // ---------------------------------------------------------------------
  660. /* */
  661. int ShowHelp()
  662. {
  663. cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
  664. " compiled on " << __DATE__ << " " << __TIME__ << endl;
  665. cout << "Usage: apt-get [options] command" << endl;
  666. cout << " apt-get [options] install pkg1 [pkg2 ...]" << endl;
  667. cout << endl;
  668. cout << "apt-get is a simple command line interface for downloading and" << endl;
  669. cout << "installing packages. The most frequently used commands are update" << endl;
  670. cout << "and install." << endl;
  671. cout << endl;
  672. cout << "Commands:" << endl;
  673. cout << " update - Retrieve new lists of packages" << endl;
  674. cout << " upgrade - Perform an upgrade" << endl;
  675. cout << " install - Install new packages (pkg is libc6 not libc6.deb)" << endl;
  676. cout << " remove - Remove packages" << endl;
  677. cout << " dist-upgrade - Distribution upgrade, see apt-get(8)" << endl;
  678. cout << " dselect-upgrade - Follow dselect selections" << endl;
  679. cout << " clean - Erase downloaded archive files" << endl;
  680. cout << " check - Verify that there are no broken dependencies" << endl;
  681. cout << endl;
  682. cout << "Options:" << endl;
  683. cout << " -h This help text." << endl;
  684. cout << " -q Loggable output - no progress indicator" << endl;
  685. cout << " -qq No output except for errors" << endl;
  686. cout << " -d Download only - do NOT install or unpack archives" << endl;
  687. cout << " -s No-act. Perform ordering simulation" << endl;
  688. cout << " -y Assume Yes to all queries and do not prompt" << endl;
  689. cout << " -f Attempt to continue if the integrity check fails" << endl;
  690. cout << " -m Attempt to continue if archives are unlocatable" << endl;
  691. cout << " -u Show a list of upgraded packages as well" << endl;
  692. cout << " -c=? Read this configuration file" << endl;
  693. cout << " -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl;
  694. cout << "See the apt-get(8), sources.list(8) and apt.conf(8) manual" << endl;
  695. cout << "pages for more information." << endl;
  696. return 100;
  697. }
  698. /*}}}*/
  699. // GetInitialize - Initialize things for apt-get /*{{{*/
  700. // ---------------------------------------------------------------------
  701. /* */
  702. void GetInitialize()
  703. {
  704. _config->Set("quiet",0);
  705. _config->Set("help",false);
  706. _config->Set("APT::Get::Download-Only",false);
  707. _config->Set("APT::Get::Simulate",false);
  708. _config->Set("APT::Get::Assume-Yes",false);
  709. _config->Set("APT::Get::Fix-Broken",false);
  710. }
  711. /*}}}*/
  712. // SigWinch - Window size change signal handler /*{{{*/
  713. // ---------------------------------------------------------------------
  714. /* */
  715. void SigWinch(int)
  716. {
  717. // Riped from GNU ls
  718. #ifdef TIOCGWINSZ
  719. struct winsize ws;
  720. if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
  721. ScreenWidth = ws.ws_col - 1;
  722. #endif
  723. }
  724. /*}}}*/
  725. int main(int argc,const char *argv[])
  726. {
  727. CommandLine::Args Args[] = {
  728. {'h',"help","help",0},
  729. {'q',"quiet","quiet",CommandLine::IntLevel},
  730. {'q',"silent","quiet",CommandLine::IntLevel},
  731. {'d',"download-only","APT::Get::Download-Only",0},
  732. {'s',"simulate","APT::Get::Simulate",0},
  733. {'s',"just-print","APT::Get::Simulate",0},
  734. {'s',"recon","APT::Get::Simulate",0},
  735. {'s',"no-act","APT::Get::Simulate",0},
  736. {'y',"yes","APT::Get::Assume-Yes",0},
  737. {'y',"assume-yes","APT::Get::Assume-Yes",0},
  738. {'f',"fix-broken","APT::Get::Fix-Broken",0},
  739. {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
  740. {'m',"ignore-missing","APT::Get::Fix-Broken",0},
  741. {0,"ignore-hold","APT::Ingore-Hold",0},
  742. {'c',"config-file",0,CommandLine::ConfigFile},
  743. {'o',"option",0,CommandLine::ArbItem},
  744. {0,0,0,0}};
  745. // Parse the command line and initialize the package library
  746. CommandLine CmdL(Args,_config);
  747. if (pkgInitialize(*_config) == false ||
  748. CmdL.Parse(argc,argv) == false)
  749. {
  750. _error->DumpErrors();
  751. return 100;
  752. }
  753. // See if the help should be shown
  754. if (_config->FindB("help") == true ||
  755. CmdL.FileSize() == 0)
  756. return ShowHelp();
  757. // Setup the output streams
  758. c0out.rdbuf(cout.rdbuf());
  759. c1out.rdbuf(cout.rdbuf());
  760. c2out.rdbuf(cout.rdbuf());
  761. if (_config->FindI("quiet",0) > 0)
  762. c0out.rdbuf(devnull.rdbuf());
  763. if (_config->FindI("quiet",0) > 1)
  764. c1out.rdbuf(devnull.rdbuf());
  765. // Setup the signals
  766. signal(SIGPIPE,SIG_IGN);
  767. signal(SIGWINCH,SigWinch);
  768. SigWinch(0);
  769. // Match the operation
  770. struct
  771. {
  772. const char *Match;
  773. bool (*Handler)(CommandLine &);
  774. } Map[] = {{"update",&DoUpdate},
  775. {"upgrade",&DoUpgrade},
  776. {"install",&DoInstall},
  777. {"remove",&DoInstall},
  778. {"dist-upgrade",&DoDistUpgrade},
  779. {"dselect-upgrade",&DoDSelectUpgrade},
  780. {"clean",&DoClean},
  781. {"check",&DoCheck},
  782. {0,0}};
  783. int I;
  784. for (I = 0; Map[I].Match != 0; I++)
  785. {
  786. if (strcmp(CmdL.FileList[0],Map[I].Match) == 0)
  787. {
  788. if (Map[I].Handler(CmdL) == false && _error->PendingError() == false)
  789. _error->Error("Handler silently failed");
  790. break;
  791. }
  792. }
  793. // No matching name
  794. if (Map[I].Match == 0)
  795. _error->Error("Invalid operation %s", CmdL.FileList[0]);
  796. // Print any errors or warnings found during parsing
  797. if (_error->empty() == false)
  798. {
  799. bool Errors = _error->PendingError();
  800. _error->DumpErrors();
  801. if (Errors == true)
  802. cout << "Returning 100." << endl;
  803. return Errors == true?100:0;
  804. }
  805. return 0;
  806. }