apt-get.cc 48 KB

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