apt-get.cc 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-get.cc,v 1.88 1999/11/16 03:16: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/algorithms.h>
  30. #include <apt-pkg/acquire-item.h>
  31. #include <apt-pkg/dpkgpm.h>
  32. #include <apt-pkg/strutl.h>
  33. #include <apt-pkg/clean.h>
  34. #include <apt-pkg/srcrecords.h>
  35. #include <apt-pkg/version.h>
  36. #include <apt-pkg/cachefile.h>
  37. #include <config.h>
  38. #include "acqprogress.h"
  39. #include <fstream.h>
  40. #include <termios.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/stat.h>
  43. #include <sys/vfs.h>
  44. #include <signal.h>
  45. #include <unistd.h>
  46. #include <stdio.h>
  47. #include <errno.h>
  48. #include <regex.h>
  49. #include <sys/wait.h>
  50. /*}}}*/
  51. ostream c0out;
  52. ostream c1out;
  53. ostream c2out;
  54. ofstream devnull("/dev/null");
  55. unsigned int ScreenWidth = 80;
  56. // class CacheFile - Cover class for some dependency cache functions /*{{{*/
  57. // ---------------------------------------------------------------------
  58. /* */
  59. class CacheFile : public pkgCacheFile
  60. {
  61. static pkgCache *SortCache;
  62. static int NameComp(const void *a,const void *b);
  63. public:
  64. pkgCache::Package **List;
  65. void Sort();
  66. bool CheckDeps(bool AllowBroken = false);
  67. bool Open(bool WithLock = true)
  68. {
  69. OpTextProgress Prog(*_config);
  70. if (pkgCacheFile::Open(Prog,WithLock) == false)
  71. return false;
  72. Sort();
  73. return true;
  74. };
  75. CacheFile() : List(0) {};
  76. };
  77. /*}}}*/
  78. // YnPrompt - Yes No Prompt. /*{{{*/
  79. // ---------------------------------------------------------------------
  80. /* Returns true on a Yes.*/
  81. bool YnPrompt()
  82. {
  83. if (_config->FindB("APT::Get::Assume-Yes",false) == true)
  84. {
  85. c1out << 'Y' << endl;
  86. return true;
  87. }
  88. char C = 0;
  89. char Jnk = 0;
  90. read(STDIN_FILENO,&C,1);
  91. while (C != '\n' && Jnk != '\n') read(STDIN_FILENO,&Jnk,1);
  92. if (!(C == 'Y' || C == 'y' || C == '\n' || C == '\r'))
  93. return false;
  94. return true;
  95. }
  96. /*}}}*/
  97. // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
  98. // ---------------------------------------------------------------------
  99. /* Returns true on a Yes.*/
  100. bool AnalPrompt(const char *Text)
  101. {
  102. char Buf[1024];
  103. cin.getline(Buf,sizeof(Buf));
  104. if (strcmp(Buf,Text) == 0)
  105. return true;
  106. return false;
  107. }
  108. /*}}}*/
  109. // ShowList - Show a list /*{{{*/
  110. // ---------------------------------------------------------------------
  111. /* This prints out a string of space seperated words with a title and
  112. a two space indent line wraped to the current screen width. */
  113. bool ShowList(ostream &out,string Title,string List)
  114. {
  115. if (List.empty() == true)
  116. return true;
  117. // Acount for the leading space
  118. int ScreenWidth = ::ScreenWidth - 3;
  119. out << Title << endl;
  120. string::size_type Start = 0;
  121. while (Start < List.size())
  122. {
  123. string::size_type End;
  124. if (Start + ScreenWidth >= List.size())
  125. End = List.size();
  126. else
  127. End = List.rfind(' ',Start+ScreenWidth);
  128. if (End == string::npos || End < Start)
  129. End = Start + ScreenWidth;
  130. out << " " << string(List,Start,End - Start) << endl;
  131. Start = End + 1;
  132. }
  133. return false;
  134. }
  135. /*}}}*/
  136. // ShowBroken - Debugging aide /*{{{*/
  137. // ---------------------------------------------------------------------
  138. /* This prints out the names of all the packages that are broken along
  139. with the name of each each broken dependency and a quite version
  140. description. */
  141. void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
  142. {
  143. out << "Sorry, but the following packages have unmet dependencies:" << endl;
  144. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  145. {
  146. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  147. if (Cache[I].InstBroken() == false)
  148. continue;
  149. // Print out each package and the failed dependencies
  150. out <<" " << I.Name() << ":";
  151. unsigned Indent = strlen(I.Name()) + 3;
  152. bool First = true;
  153. if (Cache[I].InstVerIter(Cache).end() == true)
  154. {
  155. cout << endl;
  156. continue;
  157. }
  158. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
  159. {
  160. // Compute a single dependency element (glob or)
  161. pkgCache::DepIterator Start;
  162. pkgCache::DepIterator End;
  163. D.GlobOr(Start,End);
  164. if (Cache->IsImportantDep(End) == false ||
  165. (Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  166. continue;
  167. bool FirstOr = true;
  168. while (1)
  169. {
  170. if (First == false)
  171. for (unsigned J = 0; J != Indent; J++)
  172. out << ' ';
  173. First = false;
  174. if (FirstOr == false)
  175. {
  176. for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
  177. out << ' ';
  178. }
  179. else
  180. out << ' ' << End.DepType() << ": ";
  181. FirstOr = false;
  182. out << Start.TargetPkg().Name();
  183. // Show a quick summary of the version requirements
  184. if (Start.TargetVer() != 0)
  185. out << " (" << Start.CompType() << " " << Start.TargetVer() <<
  186. ")";
  187. /* Show a summary of the target package if possible. In the case
  188. of virtual packages we show nothing */
  189. pkgCache::PkgIterator Targ = Start.TargetPkg();
  190. if (Targ->ProvidesList == 0)
  191. {
  192. out << " but ";
  193. pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
  194. if (Ver.end() == false)
  195. out << Ver.VerStr() << (Now?" is installed":" is to be installed");
  196. else
  197. {
  198. if (Cache[Targ].CandidateVerIter(Cache).end() == true)
  199. {
  200. if (Targ->ProvidesList == 0)
  201. out << "it is not installable";
  202. else
  203. out << "it is a virtual package";
  204. }
  205. else
  206. out << (Now?"it is not installed":"it is not going to be installed");
  207. }
  208. }
  209. if (Start != End)
  210. cout << " or";
  211. out << endl;
  212. if (Start == End)
  213. break;
  214. Start++;
  215. }
  216. }
  217. }
  218. }
  219. /*}}}*/
  220. // ShowNew - Show packages to newly install /*{{{*/
  221. // ---------------------------------------------------------------------
  222. /* */
  223. void ShowNew(ostream &out,CacheFile &Cache)
  224. {
  225. /* Print out a list of packages that are going to be removed extra
  226. to what the user asked */
  227. string List;
  228. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  229. {
  230. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  231. if (Cache[I].NewInstall() == true)
  232. List += string(I.Name()) + " ";
  233. }
  234. ShowList(out,"The following NEW packages will be installed:",List);
  235. }
  236. /*}}}*/
  237. // ShowDel - Show packages to delete /*{{{*/
  238. // ---------------------------------------------------------------------
  239. /* */
  240. void ShowDel(ostream &out,CacheFile &Cache)
  241. {
  242. /* Print out a list of packages that are going to be removed extra
  243. to what the user asked */
  244. string List;
  245. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  246. {
  247. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  248. if (Cache[I].Delete() == true)
  249. {
  250. if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
  251. List += string(I.Name()) + "* ";
  252. else
  253. List += string(I.Name()) + " ";
  254. }
  255. }
  256. ShowList(out,"The following packages will be REMOVED:",List);
  257. }
  258. /*}}}*/
  259. // ShowKept - Show kept packages /*{{{*/
  260. // ---------------------------------------------------------------------
  261. /* */
  262. void ShowKept(ostream &out,CacheFile &Cache)
  263. {
  264. string List;
  265. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  266. {
  267. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  268. // Not interesting
  269. if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
  270. I->CurrentVer == 0 || Cache[I].Delete() == true)
  271. continue;
  272. List += string(I.Name()) + " ";
  273. }
  274. ShowList(out,"The following packages have been kept back",List);
  275. }
  276. /*}}}*/
  277. // ShowUpgraded - Show upgraded packages /*{{{*/
  278. // ---------------------------------------------------------------------
  279. /* */
  280. void ShowUpgraded(ostream &out,CacheFile &Cache)
  281. {
  282. string List;
  283. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  284. {
  285. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  286. // Not interesting
  287. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  288. continue;
  289. List += string(I.Name()) + " ";
  290. }
  291. ShowList(out,"The following packages will be upgraded",List);
  292. }
  293. /*}}}*/
  294. // ShowHold - Show held but changed packages /*{{{*/
  295. // ---------------------------------------------------------------------
  296. /* */
  297. bool ShowHold(ostream &out,CacheFile &Cache)
  298. {
  299. string List;
  300. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  301. {
  302. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  303. if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
  304. I->SelectedState == pkgCache::State::Hold)
  305. List += string(I.Name()) + " ";
  306. }
  307. return ShowList(out,"The following held packages will be changed:",List);
  308. }
  309. /*}}}*/
  310. // ShowEssential - Show an essential package warning /*{{{*/
  311. // ---------------------------------------------------------------------
  312. /* This prints out a warning message that is not to be ignored. It shows
  313. all essential packages and their dependents that are to be removed.
  314. It is insanely risky to remove the dependents of an essential package! */
  315. bool ShowEssential(ostream &out,CacheFile &Cache)
  316. {
  317. string List;
  318. bool *Added = new bool[Cache->HeaderP->PackageCount];
  319. for (unsigned int I = 0; I != Cache->HeaderP->PackageCount; I++)
  320. Added[I] = false;
  321. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  322. {
  323. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  324. if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
  325. continue;
  326. // The essential package is being removed
  327. if (Cache[I].Delete() == true)
  328. {
  329. if (Added[I->ID] == false)
  330. {
  331. Added[I->ID] = true;
  332. List += string(I.Name()) + " ";
  333. }
  334. }
  335. if (I->CurrentVer == 0)
  336. continue;
  337. // Print out any essential package depenendents that are to be removed
  338. for (pkgDepCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
  339. {
  340. // Skip everything but depends
  341. if (D->Type != pkgCache::Dep::PreDepends &&
  342. D->Type != pkgCache::Dep::Depends)
  343. continue;
  344. pkgCache::PkgIterator P = D.SmartTargetPkg();
  345. if (Cache[P].Delete() == true)
  346. {
  347. if (Added[P->ID] == true)
  348. continue;
  349. Added[P->ID] = true;
  350. char S[300];
  351. sprintf(S,"%s (due to %s) ",P.Name(),I.Name());
  352. List += S;
  353. }
  354. }
  355. }
  356. delete [] Added;
  357. if (List.empty() == false)
  358. out << "WARNING: The following essential packages will be removed" << endl;
  359. return ShowList(out,"This should NOT be done unless you know exactly what you are doing!",List);
  360. }
  361. /*}}}*/
  362. // Stats - Show some statistics /*{{{*/
  363. // ---------------------------------------------------------------------
  364. /* */
  365. void Stats(ostream &out,pkgDepCache &Dep)
  366. {
  367. unsigned long Upgrade = 0;
  368. unsigned long Install = 0;
  369. unsigned long ReInstall = 0;
  370. for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
  371. {
  372. if (Dep[I].NewInstall() == true)
  373. Install++;
  374. else
  375. if (Dep[I].Upgrade() == true)
  376. Upgrade++;
  377. if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
  378. ReInstall++;
  379. }
  380. out << Upgrade << " packages upgraded, " <<
  381. Install << " newly installed, ";
  382. if (ReInstall != 0)
  383. out << ReInstall << " reinstalled, ";
  384. out << Dep.DelCount() << " to remove and " <<
  385. Dep.KeepCount() << " not upgraded." << endl;
  386. if (Dep.BadCount() != 0)
  387. out << Dep.BadCount() << " packages not fully installed or removed." << endl;
  388. }
  389. /*}}}*/
  390. // CacheFile::NameComp - QSort compare by name /*{{{*/
  391. // ---------------------------------------------------------------------
  392. /* */
  393. pkgCache *CacheFile::SortCache = 0;
  394. int CacheFile::NameComp(const void *a,const void *b)
  395. {
  396. if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
  397. return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
  398. const pkgCache::Package &A = **(pkgCache::Package **)a;
  399. const pkgCache::Package &B = **(pkgCache::Package **)b;
  400. return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
  401. }
  402. /*}}}*/
  403. // CacheFile::Sort - Sort by name /*{{{*/
  404. // ---------------------------------------------------------------------
  405. /* */
  406. void CacheFile::Sort()
  407. {
  408. delete [] List;
  409. List = new pkgCache::Package *[Cache->Head().PackageCount];
  410. memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
  411. pkgCache::PkgIterator I = Cache->PkgBegin();
  412. for (;I.end() != true; I++)
  413. List[I->ID] = I;
  414. SortCache = *this;
  415. qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
  416. }
  417. /*}}}*/
  418. // CacheFile::Open - Open the cache file /*{{{*/
  419. // ---------------------------------------------------------------------
  420. /* This routine generates the caches and then opens the dependency cache
  421. and verifies that the system is OK. */
  422. bool CacheFile::CheckDeps(bool AllowBroken)
  423. {
  424. if (_error->PendingError() == true)
  425. return false;
  426. // Check that the system is OK
  427. if (Cache->DelCount() != 0 || Cache->InstCount() != 0)
  428. return _error->Error("Internal Error, non-zero counts");
  429. // Apply corrections for half-installed packages
  430. if (pkgApplyStatus(*Cache) == false)
  431. return false;
  432. // Nothing is broken
  433. if (Cache->BrokenCount() == 0 || AllowBroken == true)
  434. return true;
  435. // Attempt to fix broken things
  436. if (_config->FindB("APT::Get::Fix-Broken",false) == true)
  437. {
  438. c1out << "Correcting dependencies..." << flush;
  439. if (pkgFixBroken(*Cache) == false || Cache->BrokenCount() != 0)
  440. {
  441. c1out << " failed." << endl;
  442. ShowBroken(c1out,*this,true);
  443. return _error->Error("Unable to correct dependencies");
  444. }
  445. if (pkgMinimizeUpgrade(*Cache) == false)
  446. return _error->Error("Unable to minimize the upgrade set");
  447. c1out << " Done" << endl;
  448. }
  449. else
  450. {
  451. c1out << "You might want to run `apt-get -f install' to correct these." << endl;
  452. ShowBroken(c1out,*this,true);
  453. return _error->Error("Unmet dependencies. Try using -f.");
  454. }
  455. return true;
  456. }
  457. /*}}}*/
  458. // InstallPackages - Actually download and install the packages /*{{{*/
  459. // ---------------------------------------------------------------------
  460. /* This displays the informative messages describing what is going to
  461. happen and then calls the download routines */
  462. bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,bool Saftey = true)
  463. {
  464. if (_config->FindB("APT::Get::Purge",false) == true)
  465. {
  466. pkgCache::PkgIterator I = Cache->PkgBegin();
  467. for (; I.end() == false; I++)
  468. {
  469. if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
  470. Cache->MarkDelete(I,true);
  471. }
  472. }
  473. bool Fail = false;
  474. bool Essential = false;
  475. // Show all the various warning indicators
  476. ShowDel(c1out,Cache);
  477. ShowNew(c1out,Cache);
  478. if (ShwKept == true)
  479. ShowKept(c1out,Cache);
  480. Fail |= !ShowHold(c1out,Cache);
  481. if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
  482. ShowUpgraded(c1out,Cache);
  483. Essential = !ShowEssential(c1out,Cache);
  484. Fail |= Essential;
  485. Stats(c1out,Cache);
  486. // Sanity check
  487. if (Cache->BrokenCount() != 0)
  488. {
  489. ShowBroken(c1out,Cache,false);
  490. return _error->Error("Internal Error, InstallPackages was called with broken packages!");
  491. }
  492. if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
  493. Cache->BadCount() == 0)
  494. return true;
  495. // No remove flag
  496. if (Cache->DelCount() != 0 && _config->FindB("APT::Get::No-Remove",false) == true)
  497. return _error->Error("Packages need to be removed but No Remove was specified.");
  498. // Run the simulator ..
  499. if (_config->FindB("APT::Get::Simulate") == true)
  500. {
  501. pkgSimulate PM(Cache);
  502. pkgPackageManager::OrderResult Res = PM.DoInstall();
  503. if (Res == pkgPackageManager::Failed)
  504. return false;
  505. if (Res != pkgPackageManager::Completed)
  506. return _error->Error("Internal Error, Ordering didn't finish");
  507. return true;
  508. }
  509. // Create the text record parser
  510. pkgRecords Recs(Cache);
  511. if (_error->PendingError() == true)
  512. return false;
  513. // Lock the archive directory
  514. FileFd Lock;
  515. if (_config->FindB("Debug::NoLocking",false) == false)
  516. {
  517. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  518. if (_error->PendingError() == true)
  519. return _error->Error("Unable to lock the download directory");
  520. }
  521. // Create the download object
  522. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  523. pkgAcquire Fetcher(&Stat);
  524. // Read the source list
  525. pkgSourceList List;
  526. if (List.ReadMainList() == false)
  527. return _error->Error("The list of sources could not be read.");
  528. // Create the package manager and prepare to download
  529. pkgDPkgPM PM(Cache);
  530. if (PM.GetArchives(&Fetcher,&List,&Recs) == false ||
  531. _error->PendingError() == true)
  532. return false;
  533. // Display statistics
  534. unsigned long FetchBytes = Fetcher.FetchNeeded();
  535. unsigned long FetchPBytes = Fetcher.PartialPresent();
  536. unsigned long DebBytes = Fetcher.TotalNeeded();
  537. if (DebBytes != Cache->DebSize())
  538. {
  539. c0out << DebBytes << ',' << Cache->DebSize() << endl;
  540. c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl;
  541. }
  542. // Number of bytes
  543. c1out << "Need to get ";
  544. if (DebBytes != FetchBytes)
  545. c1out << SizeToStr(FetchBytes) << "B/" << SizeToStr(DebBytes) << 'B';
  546. else
  547. c1out << SizeToStr(DebBytes) << 'B';
  548. c1out << " of archives. After unpacking ";
  549. // Check for enough free space
  550. struct statfs Buf;
  551. string OutputDir = _config->FindDir("Dir::Cache::Archives");
  552. if (statfs(OutputDir.c_str(),&Buf) != 0)
  553. return _error->Errno("statfs","Couldn't determine free space in %s",
  554. OutputDir.c_str());
  555. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  556. return _error->Error("Sorry, you don't have enough free space in %s to hold all the .debs.",
  557. OutputDir.c_str());
  558. // Size delta
  559. if (Cache->UsrSize() >= 0)
  560. c1out << SizeToStr(Cache->UsrSize()) << "B will be used." << endl;
  561. else
  562. c1out << SizeToStr(-1*Cache->UsrSize()) << "B will be freed." << endl;
  563. if (_error->PendingError() == true)
  564. return false;
  565. // Fail safe check
  566. if (_config->FindI("quiet",0) >= 2 ||
  567. _config->FindB("APT::Get::Assume-Yes",false) == true)
  568. {
  569. if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
  570. return _error->Error("There are problems and -y was used without --force-yes");
  571. }
  572. if (Essential == true && Saftey == true)
  573. {
  574. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  575. return _error->Error("Trivial Only specified but this is not a trivial operation.");
  576. c2out << "You are about to do something potentially harmful" << endl;
  577. c2out << "To continue type in the phrase 'Yes, I understand this may be bad'" << endl;
  578. c2out << " ?] " << flush;
  579. if (AnalPrompt("Yes, I understand this may be bad") == false)
  580. {
  581. c2out << "Abort." << endl;
  582. exit(1);
  583. }
  584. }
  585. else
  586. {
  587. // Prompt to continue
  588. if (Ask == true || Fail == true)
  589. {
  590. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  591. return _error->Error("Trivial Only specified but this is not a trivial operation.");
  592. if (_config->FindI("quiet",0) < 2 &&
  593. _config->FindB("APT::Get::Assume-Yes",false) == false)
  594. {
  595. c2out << "Do you want to continue? [Y/n] " << flush;
  596. if (YnPrompt() == false)
  597. {
  598. c2out << "Abort." << endl;
  599. exit(1);
  600. }
  601. }
  602. }
  603. }
  604. // Just print out the uris an exit if the --print-uris flag was used
  605. if (_config->FindB("APT::Get::Print-URIs") == true)
  606. {
  607. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  608. for (; I != Fetcher.UriEnd(); I++)
  609. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  610. I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
  611. return true;
  612. }
  613. // Run it
  614. while (1)
  615. {
  616. if (_config->FindB("APT::Get::No-Download",false) == false)
  617. if (Fetcher.Run() == pkgAcquire::Failed)
  618. return false;
  619. // Print out errors
  620. bool Failed = false;
  621. bool Transient = false;
  622. for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  623. {
  624. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  625. (*I)->Complete == true)
  626. continue;
  627. if ((*I)->Status == pkgAcquire::Item::StatIdle)
  628. {
  629. Transient = true;
  630. // Failed = true;
  631. continue;
  632. }
  633. cerr << "Failed to fetch " << (*I)->DescURI() << endl;
  634. cerr << " " << (*I)->ErrorText << endl;
  635. Failed = true;
  636. }
  637. /* If we are in no download mode and missing files then there were
  638. 'failures' then the user must specify -m. Furthermore, there
  639. is no such thing as a transient error in no-download mode! */
  640. if (Transient == true &&
  641. _config->FindB("APT::Get::No-Download",false) == true)
  642. {
  643. Transient = false;
  644. Failed = true;
  645. }
  646. if (_config->FindB("APT::Get::Download-Only",false) == true)
  647. {
  648. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  649. return _error->Error("Some files failed to download");
  650. return true;
  651. }
  652. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  653. {
  654. return _error->Error("Unable to fetch some archives, maybe try with --fix-missing?");
  655. }
  656. if (Transient == true && Failed == true)
  657. return _error->Error("--fix-missing and media swapping is not currently supported");
  658. // Try to deal with missing package files
  659. if (Failed == true && PM.FixMissing() == false)
  660. {
  661. cerr << "Unable to correct missing packages." << endl;
  662. return _error->Error("Aborting Install.");
  663. }
  664. Cache.ReleaseLock();
  665. pkgPackageManager::OrderResult Res = PM.DoInstall();
  666. if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
  667. return false;
  668. if (Res == pkgPackageManager::Completed)
  669. return true;
  670. // Reload the fetcher object and loop again for media swapping
  671. Fetcher.Shutdown();
  672. if (PM.GetArchives(&Fetcher,&List,&Recs) == false)
  673. return false;
  674. }
  675. }
  676. /*}}}*/
  677. // TryToInstall - Try to install a single package /*{{{*/
  678. // ---------------------------------------------------------------------
  679. /* This used to be inlined in DoInstall, but with the advent of regex package
  680. name matching it was split out.. */
  681. bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
  682. pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
  683. unsigned int &ExpectedInst,bool AllowFail = true)
  684. {
  685. /* This is a pure virtual package and there is a single available
  686. provides */
  687. if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0 &&
  688. Pkg.ProvidesList()->NextProvides == 0)
  689. {
  690. pkgCache::PkgIterator Tmp = Pkg.ProvidesList().OwnerPkg();
  691. c1out << "Note, installing " << Tmp.Name() << " instead of " << Pkg.Name() << endl;
  692. Pkg = Tmp;
  693. }
  694. // Handle the no-upgrade case
  695. if (_config->FindB("APT::Get::no-upgrade",false) == true &&
  696. Pkg->CurrentVer != 0)
  697. {
  698. if (AllowFail == true)
  699. c1out << "Skipping " << Pkg.Name() << ", it is already installed and no-upgrade is set." << endl;
  700. return true;
  701. }
  702. // Check if there is something at all to install
  703. pkgDepCache::StateCache &State = Cache[Pkg];
  704. if (State.CandidateVer == 0)
  705. {
  706. if (AllowFail == false)
  707. return false;
  708. if (Pkg->ProvidesList != 0)
  709. {
  710. c1out << "Package " << Pkg.Name() << " is a virtual package provided by:" << endl;
  711. pkgCache::PrvIterator I = Pkg.ProvidesList();
  712. for (; I.end() == false; I++)
  713. {
  714. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  715. if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer())
  716. {
  717. if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
  718. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
  719. " [Installed]"<< endl;
  720. else
  721. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
  722. }
  723. }
  724. c1out << "You should explicitly select one to install." << endl;
  725. }
  726. else
  727. {
  728. c1out << "Package " << Pkg.Name() << " has no available version, but exists in the database." << endl;
  729. c1out << "This typically means that the package was mentioned in a dependency and " << endl;
  730. c1out << "never uploaded, has been obsoleted or is not available with the contents " << endl;
  731. c1out << "of sources.list" << endl;
  732. string List;
  733. pkgCache::DepIterator Dep = Pkg.RevDependsList();
  734. for (; Dep.end() == false; Dep++)
  735. {
  736. if (Dep->Type != pkgCache::Dep::Replaces)
  737. continue;
  738. List += string(Dep.ParentPkg().Name()) + " ";
  739. }
  740. ShowList(c1out,"However the following packages replace it:",List);
  741. }
  742. _error->Error("Package %s has no installation candidate",Pkg.Name());
  743. return false;
  744. }
  745. Fix.Clear(Pkg);
  746. Fix.Protect(Pkg);
  747. if (Remove == true)
  748. {
  749. Fix.Remove(Pkg);
  750. Cache.MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
  751. return true;
  752. }
  753. // Install it
  754. Cache.MarkInstall(Pkg,false);
  755. if (State.Install() == false)
  756. {
  757. if (_config->FindB("APT::Get::ReInstall",false) == true)
  758. {
  759. if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
  760. c1out << "Sorry, re-installation of " << Pkg.Name() << " is not possible, it cannot be downloaded" << endl;
  761. else
  762. Cache.SetReInstall(Pkg,true);
  763. }
  764. else
  765. {
  766. if (AllowFail == true)
  767. c1out << "Sorry, " << Pkg.Name() << " is already the newest version" << endl;
  768. }
  769. }
  770. else
  771. ExpectedInst++;
  772. // Install it with autoinstalling enabled.
  773. if (State.InstBroken() == true && BrokenFix == false)
  774. Cache.MarkInstall(Pkg,true);
  775. return true;
  776. }
  777. /*}}}*/
  778. // DoUpdate - Update the package lists /*{{{*/
  779. // ---------------------------------------------------------------------
  780. /* */
  781. bool DoUpdate(CommandLine &)
  782. {
  783. // Get the source list
  784. pkgSourceList List;
  785. if (List.ReadMainList() == false)
  786. return false;
  787. // Lock the list directory
  788. FileFd Lock;
  789. if (_config->FindB("Debug::NoLocking",false) == false)
  790. {
  791. Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
  792. if (_error->PendingError() == true)
  793. return _error->Error("Unable to lock the list directory");
  794. }
  795. // Create the download object
  796. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  797. pkgAcquire Fetcher(&Stat);
  798. // Populate it with the source selection
  799. pkgSourceList::const_iterator I;
  800. for (I = List.begin(); I != List.end(); I++)
  801. {
  802. new pkgAcqIndex(&Fetcher,I);
  803. if (_error->PendingError() == true)
  804. return false;
  805. }
  806. // Run it
  807. if (Fetcher.Run() == pkgAcquire::Failed)
  808. return false;
  809. bool Failed = false;
  810. for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  811. {
  812. if ((*I)->Status == pkgAcquire::Item::StatDone)
  813. continue;
  814. (*I)->Finished();
  815. cerr << "Failed to fetch " << (*I)->DescURI() << endl;
  816. cerr << " " << (*I)->ErrorText << endl;
  817. Failed = true;
  818. }
  819. // Clean out any old list files
  820. if (_config->FindB("APT::Get::List-Cleanup",false) == false)
  821. {
  822. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  823. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  824. return false;
  825. }
  826. // Prepare the cache.
  827. CacheFile Cache;
  828. if (Cache.Open() == false)
  829. return false;
  830. if (Failed == true)
  831. return _error->Error("Some index files failed to download, they have been ignored, or old ones used instead.");
  832. return true;
  833. }
  834. /*}}}*/
  835. // DoUpgrade - Upgrade all packages /*{{{*/
  836. // ---------------------------------------------------------------------
  837. /* Upgrade all packages without installing new packages or erasing old
  838. packages */
  839. bool DoUpgrade(CommandLine &CmdL)
  840. {
  841. CacheFile Cache;
  842. if (Cache.Open() == false || Cache.CheckDeps() == false)
  843. return false;
  844. // Do the upgrade
  845. if (pkgAllUpgrade(Cache) == false)
  846. {
  847. ShowBroken(c1out,Cache,false);
  848. return _error->Error("Internal Error, AllUpgrade broke stuff");
  849. }
  850. return InstallPackages(Cache,true);
  851. }
  852. /*}}}*/
  853. // DoInstall - Install packages from the command line /*{{{*/
  854. // ---------------------------------------------------------------------
  855. /* Install named packages */
  856. bool DoInstall(CommandLine &CmdL)
  857. {
  858. CacheFile Cache;
  859. if (Cache.Open() == false || Cache.CheckDeps(CmdL.FileSize() != 1) == false)
  860. return false;
  861. // Enter the special broken fixing mode if the user specified arguments
  862. bool BrokenFix = false;
  863. if (Cache->BrokenCount() != 0)
  864. BrokenFix = true;
  865. unsigned int ExpectedInst = 0;
  866. unsigned int Packages = 0;
  867. pkgProblemResolver Fix(Cache);
  868. bool DefRemove = false;
  869. if (strcasecmp(CmdL.FileList[0],"remove") == 0)
  870. DefRemove = true;
  871. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  872. {
  873. // Duplicate the string
  874. unsigned int Length = strlen(*I);
  875. char S[300];
  876. if (Length >= sizeof(S))
  877. continue;
  878. strcpy(S,*I);
  879. // See if we are removing the package
  880. bool Remove = DefRemove;
  881. while (Cache->FindPkg(S).end() == true)
  882. {
  883. // Handle an optional end tag indicating what to do
  884. if (S[Length - 1] == '-')
  885. {
  886. Remove = true;
  887. S[--Length] = 0;
  888. continue;
  889. }
  890. if (S[Length - 1] == '+')
  891. {
  892. Remove = false;
  893. S[--Length] = 0;
  894. continue;
  895. }
  896. break;
  897. }
  898. // Locate the package
  899. pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
  900. Packages++;
  901. if (Pkg.end() == true)
  902. {
  903. // Check if the name is a regex
  904. const char *I;
  905. for (I = S; *I != 0; I++)
  906. if (*I == '.' || *I == '?' || *I == '*')
  907. break;
  908. if (*I == 0)
  909. return _error->Error("Couldn't find package %s",S);
  910. // Regexs must always be confirmed
  911. ExpectedInst += 1000;
  912. // Compile the regex pattern
  913. regex_t Pattern;
  914. if (regcomp(&Pattern,S,REG_EXTENDED | REG_ICASE |
  915. REG_NOSUB) != 0)
  916. return _error->Error("Regex compilation error");
  917. // Run over the matches
  918. bool Hit = false;
  919. for (Pkg = Cache->PkgBegin(); Pkg.end() == false; Pkg++)
  920. {
  921. if (regexec(&Pattern,Pkg.Name(),0,0,0) != 0)
  922. continue;
  923. Hit |= TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,
  924. ExpectedInst,false);
  925. }
  926. regfree(&Pattern);
  927. if (Hit == false)
  928. return _error->Error("Couldn't find package %s",S);
  929. }
  930. else
  931. {
  932. if (TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,ExpectedInst) == false)
  933. return false;
  934. }
  935. }
  936. /* If we are in the Broken fixing mode we do not attempt to fix the
  937. problems. This is if the user invoked install without -f and gave
  938. packages */
  939. if (BrokenFix == true && Cache->BrokenCount() != 0)
  940. {
  941. c1out << "You might want to run `apt-get -f install' to correct these:" << endl;
  942. ShowBroken(c1out,Cache,false);
  943. return _error->Error("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).");
  944. }
  945. // Call the scored problem resolver
  946. Fix.InstallProtect();
  947. if (Fix.Resolve(true) == false)
  948. _error->Discard();
  949. // Now we check the state of the packages,
  950. if (Cache->BrokenCount() != 0)
  951. {
  952. c1out << "Some packages could not be installed. This may mean that you have" << endl;
  953. c1out << "requested an impossible situation or if you are using the unstable" << endl;
  954. c1out << "distribution that some required packages have not yet been created" << endl;
  955. c1out << "or been moved out of Incoming." << endl;
  956. if (Packages == 1)
  957. {
  958. c1out << endl;
  959. c1out << "Since you only requested a single operation it is extremely likely that" << endl;
  960. c1out << "the package is simply not installable and a bug report against" << endl;
  961. c1out << "that package should be filed." << endl;
  962. }
  963. c1out << "The following information may help to resolve the situation:" << endl;
  964. c1out << endl;
  965. ShowBroken(c1out,Cache,false);
  966. return _error->Error("Sorry, broken packages");
  967. }
  968. /* Print out a list of packages that are going to be installed extra
  969. to what the user asked */
  970. if (Cache->InstCount() != ExpectedInst)
  971. {
  972. string List;
  973. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  974. {
  975. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  976. if ((*Cache)[I].Install() == false)
  977. continue;
  978. const char **J;
  979. for (J = CmdL.FileList + 1; *J != 0; J++)
  980. if (strcmp(*J,I.Name()) == 0)
  981. break;
  982. if (*J == 0)
  983. List += string(I.Name()) + " ";
  984. }
  985. ShowList(c1out,"The following extra packages will be installed:",List);
  986. }
  987. // See if we need to prompt
  988. if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
  989. return InstallPackages(Cache,false,false);
  990. return InstallPackages(Cache,false);
  991. }
  992. /*}}}*/
  993. // DoDistUpgrade - Automatic smart upgrader /*{{{*/
  994. // ---------------------------------------------------------------------
  995. /* Intelligent upgrader that will install and remove packages at will */
  996. bool DoDistUpgrade(CommandLine &CmdL)
  997. {
  998. CacheFile Cache;
  999. if (Cache.Open() == false || Cache.CheckDeps() == false)
  1000. return false;
  1001. c0out << "Calculating Upgrade... " << flush;
  1002. if (pkgDistUpgrade(*Cache) == false)
  1003. {
  1004. c0out << "Failed" << endl;
  1005. ShowBroken(c1out,Cache,false);
  1006. return false;
  1007. }
  1008. c0out << "Done" << endl;
  1009. return InstallPackages(Cache,true);
  1010. }
  1011. /*}}}*/
  1012. // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
  1013. // ---------------------------------------------------------------------
  1014. /* Follows dselect's selections */
  1015. bool DoDSelectUpgrade(CommandLine &CmdL)
  1016. {
  1017. CacheFile Cache;
  1018. if (Cache.Open() == false || Cache.CheckDeps() == false)
  1019. return false;
  1020. // Install everything with the install flag set
  1021. pkgCache::PkgIterator I = Cache->PkgBegin();
  1022. for (;I.end() != true; I++)
  1023. {
  1024. /* Install the package only if it is a new install, the autoupgrader
  1025. will deal with the rest */
  1026. if (I->SelectedState == pkgCache::State::Install)
  1027. Cache->MarkInstall(I,false);
  1028. }
  1029. /* Now install their deps too, if we do this above then order of
  1030. the status file is significant for | groups */
  1031. for (I = Cache->PkgBegin();I.end() != true; I++)
  1032. {
  1033. /* Install the package only if it is a new install, the autoupgrader
  1034. will deal with the rest */
  1035. if (I->SelectedState == pkgCache::State::Install)
  1036. Cache->MarkInstall(I,true);
  1037. }
  1038. // Apply erasures now, they override everything else.
  1039. for (I = Cache->PkgBegin();I.end() != true; I++)
  1040. {
  1041. // Remove packages
  1042. if (I->SelectedState == pkgCache::State::DeInstall ||
  1043. I->SelectedState == pkgCache::State::Purge)
  1044. Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
  1045. }
  1046. /* Resolve any problems that dselect created, allupgrade cannot handle
  1047. such things. We do so quite agressively too.. */
  1048. if (Cache->BrokenCount() != 0)
  1049. {
  1050. pkgProblemResolver Fix(Cache);
  1051. // Hold back held packages.
  1052. if (_config->FindB("APT::Ingore-Hold",false) == false)
  1053. {
  1054. for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
  1055. {
  1056. if (I->SelectedState == pkgCache::State::Hold)
  1057. {
  1058. Fix.Protect(I);
  1059. Cache->MarkKeep(I);
  1060. }
  1061. }
  1062. }
  1063. if (Fix.Resolve() == false)
  1064. {
  1065. ShowBroken(c1out,Cache,false);
  1066. return _error->Error("Internal Error, problem resolver broke stuff");
  1067. }
  1068. }
  1069. // Now upgrade everything
  1070. if (pkgAllUpgrade(Cache) == false)
  1071. {
  1072. ShowBroken(c1out,Cache,false);
  1073. return _error->Error("Internal Error, problem resolver broke stuff");
  1074. }
  1075. return InstallPackages(Cache,false);
  1076. }
  1077. /*}}}*/
  1078. // DoClean - Remove download archives /*{{{*/
  1079. // ---------------------------------------------------------------------
  1080. /* */
  1081. bool DoClean(CommandLine &CmdL)
  1082. {
  1083. if (_config->FindB("APT::Get::Simulate") == true)
  1084. {
  1085. cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
  1086. _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
  1087. return true;
  1088. }
  1089. // Lock the archive directory
  1090. FileFd Lock;
  1091. if (_config->FindB("Debug::NoLocking",false) == false)
  1092. {
  1093. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1094. if (_error->PendingError() == true)
  1095. return _error->Error("Unable to lock the download directory");
  1096. }
  1097. pkgAcquire Fetcher;
  1098. Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
  1099. Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
  1100. return true;
  1101. }
  1102. /*}}}*/
  1103. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  1104. // ---------------------------------------------------------------------
  1105. /* This is similar to clean but it only purges things that cannot be
  1106. downloaded, that is old versions of cached packages. */
  1107. class LogCleaner : public pkgArchiveCleaner
  1108. {
  1109. protected:
  1110. virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
  1111. {
  1112. cout << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
  1113. if (_config->FindB("APT::Get::Simulate") == false)
  1114. unlink(File);
  1115. };
  1116. };
  1117. bool DoAutoClean(CommandLine &CmdL)
  1118. {
  1119. // Lock the archive directory
  1120. FileFd Lock;
  1121. if (_config->FindB("Debug::NoLocking",false) == false)
  1122. {
  1123. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1124. if (_error->PendingError() == true)
  1125. return _error->Error("Unable to lock the download directory");
  1126. }
  1127. CacheFile Cache;
  1128. if (Cache.Open() == false)
  1129. return false;
  1130. LogCleaner Cleaner;
  1131. return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
  1132. Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
  1133. }
  1134. /*}}}*/
  1135. // DoCheck - Perform the check operation /*{{{*/
  1136. // ---------------------------------------------------------------------
  1137. /* Opening automatically checks the system, this command is mostly used
  1138. for debugging */
  1139. bool DoCheck(CommandLine &CmdL)
  1140. {
  1141. CacheFile Cache;
  1142. Cache.Open();
  1143. Cache.CheckDeps();
  1144. return true;
  1145. }
  1146. /*}}}*/
  1147. // DoSource - Fetch a source archive /*{{{*/
  1148. // ---------------------------------------------------------------------
  1149. /* Fetch souce packages */
  1150. struct DscFile
  1151. {
  1152. string Package;
  1153. string Version;
  1154. string Dsc;
  1155. };
  1156. bool DoSource(CommandLine &CmdL)
  1157. {
  1158. CacheFile Cache;
  1159. if (Cache.Open(false) == false)
  1160. return false;
  1161. if (CmdL.FileSize() <= 1)
  1162. return _error->Error("Must specify at least one package to fetch source for");
  1163. // Read the source list
  1164. pkgSourceList List;
  1165. if (List.ReadMainList() == false)
  1166. return _error->Error("The list of sources could not be read.");
  1167. // Create the text record parsers
  1168. pkgRecords Recs(Cache);
  1169. pkgSrcRecords SrcRecs(List);
  1170. if (_error->PendingError() == true)
  1171. return false;
  1172. // Create the download object
  1173. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1174. pkgAcquire Fetcher(&Stat);
  1175. DscFile *Dsc = new DscFile[CmdL.FileSize()];
  1176. // Load the requestd sources into the fetcher
  1177. unsigned J = 0;
  1178. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  1179. {
  1180. string Src;
  1181. /* Lookup the version of the package we would install if we were to
  1182. install a version and determine the source package name, then look
  1183. in the archive for a source package of the same name. In theory
  1184. we could stash the version string as well and match that too but
  1185. today there aren't multi source versions in the archive. */
  1186. pkgCache::PkgIterator Pkg = Cache->FindPkg(*I);
  1187. if (Pkg.end() == false)
  1188. {
  1189. pkgCache::VerIterator Ver = Cache->GetCandidateVer(Pkg);
  1190. if (Ver.end() == false)
  1191. {
  1192. pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
  1193. Src = Parse.SourcePkg();
  1194. }
  1195. }
  1196. // No source package name..
  1197. if (Src.empty() == true)
  1198. Src = *I;
  1199. // The best hit
  1200. pkgSrcRecords::Parser *Last = 0;
  1201. unsigned long Offset = 0;
  1202. string Version;
  1203. bool IsMatch = false;
  1204. // Iterate over all of the hits
  1205. pkgSrcRecords::Parser *Parse;
  1206. SrcRecs.Restart();
  1207. while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
  1208. {
  1209. string Ver = Parse->Version();
  1210. // Skip name mismatches
  1211. if (IsMatch == true && Parse->Package() != Src)
  1212. continue;
  1213. // Newer version or an exact match
  1214. if (Last == 0 || pkgVersionCompare(Version,Ver) < 0 ||
  1215. (Parse->Package() == Src && IsMatch == false))
  1216. {
  1217. IsMatch = Parse->Package() == Src;
  1218. Last = Parse;
  1219. Offset = Parse->Offset();
  1220. Version = Ver;
  1221. }
  1222. }
  1223. if (Last == 0)
  1224. return _error->Error("Unable to find a source package for %s",Src.c_str());
  1225. // Back track
  1226. vector<pkgSrcRecords::File> Lst;
  1227. if (Last->Jump(Offset) == false || Last->Files(Lst) == false)
  1228. return false;
  1229. // Load them into the fetcher
  1230. for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
  1231. I != Lst.end(); I++)
  1232. {
  1233. // Try to guess what sort of file it is we are getting.
  1234. string Comp;
  1235. if (I->Path.find(".dsc") != string::npos)
  1236. {
  1237. Comp = "dsc";
  1238. Dsc[J].Package = Last->Package();
  1239. Dsc[J].Version = Last->Version();
  1240. Dsc[J].Dsc = flNotDir(I->Path);
  1241. }
  1242. if (I->Path.find(".tar.gz") != string::npos)
  1243. Comp = "tar";
  1244. if (I->Path.find(".diff.gz") != string::npos)
  1245. Comp = "diff";
  1246. // Diff only mode only fetches .diff files
  1247. if (_config->FindB("APT::Get::Diff-Only",false) == true &&
  1248. Comp != "diff")
  1249. continue;
  1250. // Tar only mode only fetches .tar files
  1251. if (_config->FindB("APT::Get::Tar-Only",false) == true &&
  1252. Comp != "tar")
  1253. continue;
  1254. new pkgAcqFile(&Fetcher,Last->Source()->ArchiveURI(I->Path),
  1255. I->MD5Hash,I->Size,Last->Source()->SourceInfo(Src,
  1256. Last->Version(),Comp),Src);
  1257. }
  1258. }
  1259. // Display statistics
  1260. unsigned long FetchBytes = Fetcher.FetchNeeded();
  1261. unsigned long FetchPBytes = Fetcher.PartialPresent();
  1262. unsigned long DebBytes = Fetcher.TotalNeeded();
  1263. // Check for enough free space
  1264. struct statfs Buf;
  1265. string OutputDir = ".";
  1266. if (statfs(OutputDir.c_str(),&Buf) != 0)
  1267. return _error->Errno("statfs","Couldn't determine free space in %s",
  1268. OutputDir.c_str());
  1269. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  1270. return _error->Error("Sorry, you don't have enough free space in %s",
  1271. OutputDir.c_str());
  1272. // Number of bytes
  1273. c1out << "Need to get ";
  1274. if (DebBytes != FetchBytes)
  1275. c1out << SizeToStr(FetchBytes) << "B/" << SizeToStr(DebBytes) << 'B';
  1276. else
  1277. c1out << SizeToStr(DebBytes) << 'B';
  1278. c1out << " of source archives." << endl;
  1279. if (_config->FindB("APT::Get::Simulate",false) == true)
  1280. {
  1281. for (unsigned I = 0; I != J; I++)
  1282. cout << "Fetch Source " << Dsc[I].Package << endl;
  1283. return true;
  1284. }
  1285. // Just print out the uris an exit if the --print-uris flag was used
  1286. if (_config->FindB("APT::Get::Print-URIs") == true)
  1287. {
  1288. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  1289. for (; I != Fetcher.UriEnd(); I++)
  1290. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  1291. I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
  1292. return true;
  1293. }
  1294. // Run it
  1295. if (Fetcher.Run() == pkgAcquire::Failed)
  1296. return false;
  1297. // Print error messages
  1298. bool Failed = false;
  1299. for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  1300. {
  1301. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  1302. (*I)->Complete == true)
  1303. continue;
  1304. cerr << "Failed to fetch " << (*I)->DescURI() << endl;
  1305. cerr << " " << (*I)->ErrorText << endl;
  1306. Failed = true;
  1307. }
  1308. if (Failed == true)
  1309. return _error->Error("Failed to fetch some archives.");
  1310. if (_config->FindB("APT::Get::Download-only",false) == true)
  1311. return true;
  1312. // Unpack the sources
  1313. pid_t Process = ExecFork();
  1314. if (Process == 0)
  1315. {
  1316. for (unsigned I = 0; I != J; I++)
  1317. {
  1318. string Dir = Dsc[I].Package + '-' + pkgBaseVersion(Dsc[I].Version.c_str());
  1319. // Diff only mode only fetches .diff files
  1320. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  1321. _config->FindB("APT::Get::Tar-Only",false) == true)
  1322. continue;
  1323. // See if the package is already unpacked
  1324. struct stat Stat;
  1325. if (stat(Dir.c_str(),&Stat) == 0 &&
  1326. S_ISDIR(Stat.st_mode) != 0)
  1327. {
  1328. c0out << "Skipping unpack of already unpacked source in " << Dir << endl;
  1329. }
  1330. else
  1331. {
  1332. // Call dpkg-source
  1333. char S[500];
  1334. snprintf(S,sizeof(S),"%s -x %s",
  1335. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  1336. Dsc[I].Dsc.c_str());
  1337. if (system(S) != 0)
  1338. {
  1339. cerr << "Unpack command '" << S << "' failed." << endl;
  1340. _exit(1);
  1341. }
  1342. }
  1343. // Try to compile it with dpkg-buildpackage
  1344. if (_config->FindB("APT::Get::Compile",false) == true)
  1345. {
  1346. // Call dpkg-buildpackage
  1347. char S[500];
  1348. snprintf(S,sizeof(S),"cd %s && %s %s",
  1349. Dir.c_str(),
  1350. _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
  1351. _config->Find("DPkg::Build-Options","-b -uc").c_str());
  1352. if (system(S) != 0)
  1353. {
  1354. cerr << "Build command '" << S << "' failed." << endl;
  1355. _exit(1);
  1356. }
  1357. }
  1358. }
  1359. _exit(0);
  1360. }
  1361. // Wait for the subprocess
  1362. int Status = 0;
  1363. while (waitpid(Process,&Status,0) != Process)
  1364. {
  1365. if (errno == EINTR)
  1366. continue;
  1367. return _error->Errno("waitpid","Couldn't wait for subprocess");
  1368. }
  1369. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  1370. return _error->Error("Child process failed");
  1371. return true;
  1372. }
  1373. /*}}}*/
  1374. // ShowHelp - Show a help screen /*{{{*/
  1375. // ---------------------------------------------------------------------
  1376. /* */
  1377. bool ShowHelp(CommandLine &CmdL)
  1378. {
  1379. cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
  1380. " compiled on " << __DATE__ << " " << __TIME__ << endl;
  1381. if (_config->FindB("version") == true)
  1382. return 100;
  1383. cout << "Usage: apt-get [options] command" << endl;
  1384. cout << " apt-get [options] install pkg1 [pkg2 ...]" << endl;
  1385. cout << endl;
  1386. cout << "apt-get is a simple command line interface for downloading and" << endl;
  1387. cout << "installing packages. The most frequently used commands are update" << endl;
  1388. cout << "and install." << endl;
  1389. cout << endl;
  1390. cout << "Commands:" << endl;
  1391. cout << " update - Retrieve new lists of packages" << endl;
  1392. cout << " upgrade - Perform an upgrade" << endl;
  1393. cout << " install - Install new packages (pkg is libc6 not libc6.deb)" << endl;
  1394. cout << " remove - Remove packages" << endl;
  1395. cout << " source - Download source archives" << endl;
  1396. cout << " dist-upgrade - Distribution upgrade, see apt-get(8)" << endl;
  1397. cout << " dselect-upgrade - Follow dselect selections" << endl;
  1398. cout << " clean - Erase downloaded archive files" << endl;
  1399. cout << " autoclean - Erase old downloaded archive files" << endl;
  1400. cout << " check - Verify that there are no broken dependencies" << endl;
  1401. cout << endl;
  1402. cout << "Options:" << endl;
  1403. cout << " -h This help text." << endl;
  1404. cout << " -q Loggable output - no progress indicator" << endl;
  1405. cout << " -qq No output except for errors" << endl;
  1406. cout << " -d Download only - do NOT install or unpack archives" << endl;
  1407. cout << " -s No-act. Perform ordering simulation" << endl;
  1408. cout << " -y Assume Yes to all queries and do not prompt" << endl;
  1409. cout << " -f Attempt to continue if the integrity check fails" << endl;
  1410. cout << " -m Attempt to continue if archives are unlocatable" << endl;
  1411. cout << " -u Show a list of upgraded packages as well" << endl;
  1412. cout << " -b Build the source package after fetching it" << endl;
  1413. cout << " -c=? Read this configuration file" << endl;
  1414. cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
  1415. cout << "See the apt-get(8), sources.list(5) and apt.conf(5) manual" << endl;
  1416. cout << "pages for more information and options." << endl;
  1417. return 100;
  1418. }
  1419. /*}}}*/
  1420. // GetInitialize - Initialize things for apt-get /*{{{*/
  1421. // ---------------------------------------------------------------------
  1422. /* */
  1423. void GetInitialize()
  1424. {
  1425. _config->Set("quiet",0);
  1426. _config->Set("help",false);
  1427. _config->Set("APT::Get::Download-Only",false);
  1428. _config->Set("APT::Get::Simulate",false);
  1429. _config->Set("APT::Get::Assume-Yes",false);
  1430. _config->Set("APT::Get::Fix-Broken",false);
  1431. _config->Set("APT::Get::Force-Yes",false);
  1432. _config->Set("APT::Get::APT::Get::No-List-Cleanup",true);
  1433. }
  1434. /*}}}*/
  1435. // SigWinch - Window size change signal handler /*{{{*/
  1436. // ---------------------------------------------------------------------
  1437. /* */
  1438. void SigWinch(int)
  1439. {
  1440. // Riped from GNU ls
  1441. #ifdef TIOCGWINSZ
  1442. struct winsize ws;
  1443. if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
  1444. ScreenWidth = ws.ws_col - 1;
  1445. #endif
  1446. }
  1447. /*}}}*/
  1448. int main(int argc,const char *argv[])
  1449. {
  1450. CommandLine::Args Args[] = {
  1451. {'h',"help","help",0},
  1452. {'v',"version","version",0},
  1453. {'q',"quiet","quiet",CommandLine::IntLevel},
  1454. {'q',"silent","quiet",CommandLine::IntLevel},
  1455. {'d',"download-only","APT::Get::Download-Only",0},
  1456. {'b',"compile","APT::Get::Compile",0},
  1457. {'b',"build","APT::Get::Compile",0},
  1458. {'s',"simulate","APT::Get::Simulate",0},
  1459. {'s',"just-print","APT::Get::Simulate",0},
  1460. {'s',"recon","APT::Get::Simulate",0},
  1461. {'s',"no-act","APT::Get::Simulate",0},
  1462. {'y',"yes","APT::Get::Assume-Yes",0},
  1463. {'y',"assume-yes","APT::Get::Assume-Yes",0},
  1464. {'f',"fix-broken","APT::Get::Fix-Broken",0},
  1465. {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
  1466. {'m',"ignore-missing","APT::Get::Fix-Missing",0},
  1467. {0,"no-download","APT::Get::No-Download",0},
  1468. {0,"fix-missing","APT::Get::Fix-Missing",0},
  1469. {0,"ignore-hold","APT::Ingore-Hold",0},
  1470. {0,"no-upgrade","APT::Get::no-upgrade",0},
  1471. {0,"force-yes","APT::Get::force-yes",0},
  1472. {0,"print-uris","APT::Get::Print-URIs",0},
  1473. {0,"diff-only","APT::Get::Diff-Only",0},
  1474. {0,"tar-only","APT::Get::tar-Only",0},
  1475. {0,"purge","APT::Get::Purge",0},
  1476. {0,"list-cleanup","APT::Get::List-Cleanup",0},
  1477. {0,"reinstall","APT::Get::ReInstall",0},
  1478. {0,"trivial-only","APT::Get::Trivial-Only",0},
  1479. {0,"no-remove","APT::Get::No-Remove",0},
  1480. {'c',"config-file",0,CommandLine::ConfigFile},
  1481. {'o',"option",0,CommandLine::ArbItem},
  1482. {0,0,0,0}};
  1483. CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
  1484. {"upgrade",&DoUpgrade},
  1485. {"install",&DoInstall},
  1486. {"remove",&DoInstall},
  1487. {"dist-upgrade",&DoDistUpgrade},
  1488. {"dselect-upgrade",&DoDSelectUpgrade},
  1489. {"clean",&DoClean},
  1490. {"autoclean",&DoAutoClean},
  1491. {"check",&DoCheck},
  1492. {"source",&DoSource},
  1493. {"help",&ShowHelp},
  1494. {0,0}};
  1495. // Parse the command line and initialize the package library
  1496. CommandLine CmdL(Args,_config);
  1497. if (pkgInitialize(*_config) == false ||
  1498. CmdL.Parse(argc,argv) == false)
  1499. {
  1500. _error->DumpErrors();
  1501. return 100;
  1502. }
  1503. // See if the help should be shown
  1504. if (_config->FindB("help") == true ||
  1505. _config->FindB("version") == true ||
  1506. CmdL.FileSize() == 0)
  1507. return ShowHelp(CmdL);
  1508. // Deal with stdout not being a tty
  1509. if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
  1510. _config->Set("quiet","1");
  1511. // Setup the output streams
  1512. c0out.rdbuf(cout.rdbuf());
  1513. c1out.rdbuf(cout.rdbuf());
  1514. c2out.rdbuf(cout.rdbuf());
  1515. if (_config->FindI("quiet",0) > 0)
  1516. c0out.rdbuf(devnull.rdbuf());
  1517. if (_config->FindI("quiet",0) > 1)
  1518. c1out.rdbuf(devnull.rdbuf());
  1519. // Setup the signals
  1520. signal(SIGPIPE,SIG_IGN);
  1521. signal(SIGWINCH,SigWinch);
  1522. SigWinch(0);
  1523. // Match the operation
  1524. CmdL.DispatchArg(Cmds);
  1525. // Print any errors or warnings found during parsing
  1526. if (_error->empty() == false)
  1527. {
  1528. bool Errors = _error->PendingError();
  1529. _error->DumpErrors();
  1530. return Errors == true?100:0;
  1531. }
  1532. return 0;
  1533. }