apt-get.cc 33 KB

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