apt-get.cc 49 KB

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