apt-get.cc 36 KB

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