apt-get.cc 26 KB

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