apt-get.cc 45 KB

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