apt-get.cc 37 KB

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