apt-get.cc 32 KB

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