You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2842 lines
88 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-get.cc,v 1.156 2004/08/28 01:05:16 mdz 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/strutl.h>
  32. #include <apt-pkg/clean.h>
  33. #include <apt-pkg/srcrecords.h>
  34. #include <apt-pkg/version.h>
  35. #include <apt-pkg/cachefile.h>
  36. #include <apt-pkg/sptr.h>
  37. #include <apt-pkg/md5.h>
  38. #include <apt-pkg/versionmatch.h>
  39. #include <config.h>
  40. #include <apti18n.h>
  41. #include "acqprogress.h"
  42. #include <set>
  43. #include <locale.h>
  44. #include <langinfo.h>
  45. #include <fstream>
  46. #include <termios.h>
  47. #include <sys/ioctl.h>
  48. #include <sys/stat.h>
  49. #include <sys/statfs.h>
  50. #include <sys/statvfs.h>
  51. #include <signal.h>
  52. #include <unistd.h>
  53. #include <stdio.h>
  54. #include <errno.h>
  55. #include <regex.h>
  56. #include <sys/wait.h>
  57. #include <sstream>
  58. /*}}}*/
  59. #define RAMFS_MAGIC 0x858458f6
  60. using namespace std;
  61. ostream c0out(0);
  62. ostream c1out(0);
  63. ostream c2out(0);
  64. ofstream devnull("/dev/null");
  65. unsigned int ScreenWidth = 80 - 1; /* - 1 for the cursor */
  66. // class CacheFile - Cover class for some dependency cache functions /*{{{*/
  67. // ---------------------------------------------------------------------
  68. /* */
  69. class CacheFile : public pkgCacheFile
  70. {
  71. static pkgCache *SortCache;
  72. static int NameComp(const void *a,const void *b);
  73. public:
  74. pkgCache::Package **List;
  75. void Sort();
  76. bool CheckDeps(bool AllowBroken = false);
  77. bool BuildCaches(bool WithLock = true)
  78. {
  79. OpTextProgress Prog(*_config);
  80. if (pkgCacheFile::BuildCaches(Prog,WithLock) == false)
  81. return false;
  82. return true;
  83. }
  84. bool Open(bool WithLock = true)
  85. {
  86. OpTextProgress Prog(*_config);
  87. if (pkgCacheFile::Open(Prog,WithLock) == false)
  88. return false;
  89. Sort();
  90. return true;
  91. };
  92. bool OpenForInstall()
  93. {
  94. if (_config->FindB("APT::Get::Print-URIs") == true)
  95. return Open(false);
  96. else
  97. return Open(true);
  98. }
  99. CacheFile() : List(0) {};
  100. };
  101. /*}}}*/
  102. // YnPrompt - Yes No Prompt. /*{{{*/
  103. // ---------------------------------------------------------------------
  104. /* Returns true on a Yes.*/
  105. bool YnPrompt(bool Default=true)
  106. {
  107. if (_config->FindB("APT::Get::Assume-Yes",false) == true)
  108. {
  109. c1out << _("Y") << endl;
  110. return true;
  111. }
  112. char response[1024] = "";
  113. cin.getline(response, sizeof(response));
  114. if (!cin)
  115. return false;
  116. if (strlen(response) == 0)
  117. return Default;
  118. regex_t Pattern;
  119. int Res;
  120. Res = regcomp(&Pattern, nl_langinfo(YESEXPR),
  121. REG_EXTENDED|REG_ICASE|REG_NOSUB);
  122. if (Res != 0) {
  123. char Error[300];
  124. regerror(Res,&Pattern,Error,sizeof(Error));
  125. return _error->Error(_("Regex compilation error - %s"),Error);
  126. }
  127. Res = regexec(&Pattern, response, 0, NULL, 0);
  128. if (Res == 0)
  129. return true;
  130. return false;
  131. }
  132. /*}}}*/
  133. // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
  134. // ---------------------------------------------------------------------
  135. /* Returns true on a Yes.*/
  136. bool AnalPrompt(const char *Text)
  137. {
  138. char Buf[1024];
  139. cin.getline(Buf,sizeof(Buf));
  140. if (strcmp(Buf,Text) == 0)
  141. return true;
  142. return false;
  143. }
  144. /*}}}*/
  145. // ShowList - Show a list /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* This prints out a string of space separated words with a title and
  148. a two space indent line wraped to the current screen width. */
  149. bool ShowList(ostream &out,string Title,string List,string VersionsList)
  150. {
  151. if (List.empty() == true)
  152. return true;
  153. // trim trailing space
  154. int NonSpace = List.find_last_not_of(' ');
  155. if (NonSpace != -1)
  156. {
  157. List = List.erase(NonSpace + 1);
  158. if (List.empty() == true)
  159. return true;
  160. }
  161. // Acount for the leading space
  162. int ScreenWidth = ::ScreenWidth - 3;
  163. out << Title << endl;
  164. string::size_type Start = 0;
  165. string::size_type VersionsStart = 0;
  166. while (Start < List.size())
  167. {
  168. if(_config->FindB("APT::Get::Show-Versions",false) == true &&
  169. VersionsList.size() > 0) {
  170. string::size_type End;
  171. string::size_type VersionsEnd;
  172. End = List.find(' ',Start);
  173. VersionsEnd = VersionsList.find('\n', VersionsStart);
  174. out << " " << string(List,Start,End - Start) << " (" <<
  175. string(VersionsList,VersionsStart,VersionsEnd - VersionsStart) <<
  176. ")" << endl;
  177. if (End == string::npos || End < Start)
  178. End = Start + ScreenWidth;
  179. Start = End + 1;
  180. VersionsStart = VersionsEnd + 1;
  181. } else {
  182. string::size_type End;
  183. if (Start + ScreenWidth >= List.size())
  184. End = List.size();
  185. else
  186. End = List.rfind(' ',Start+ScreenWidth);
  187. if (End == string::npos || End < Start)
  188. End = Start + ScreenWidth;
  189. out << " " << string(List,Start,End - Start) << endl;
  190. Start = End + 1;
  191. }
  192. }
  193. return false;
  194. }
  195. /*}}}*/
  196. // ShowBroken - Debugging aide /*{{{*/
  197. // ---------------------------------------------------------------------
  198. /* This prints out the names of all the packages that are broken along
  199. with the name of each each broken dependency and a quite version
  200. description.
  201. The output looks like:
  202. The following packages have unmet dependencies:
  203. exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
  204. Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
  205. Depends: libsasl7 but it is not going to be installed
  206. */
  207. void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
  208. {
  209. out << _("The following packages have unmet dependencies:") << endl;
  210. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  211. {
  212. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  213. if (Now == true)
  214. {
  215. if (Cache[I].NowBroken() == false)
  216. continue;
  217. }
  218. else
  219. {
  220. if (Cache[I].InstBroken() == false)
  221. continue;
  222. }
  223. // Print out each package and the failed dependencies
  224. out <<" " << I.Name() << ":";
  225. unsigned Indent = strlen(I.Name()) + 3;
  226. bool First = true;
  227. pkgCache::VerIterator Ver;
  228. if (Now == true)
  229. Ver = I.CurrentVer();
  230. else
  231. Ver = Cache[I].InstVerIter(Cache);
  232. if (Ver.end() == true)
  233. {
  234. out << endl;
  235. continue;
  236. }
  237. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false;)
  238. {
  239. // Compute a single dependency element (glob or)
  240. pkgCache::DepIterator Start;
  241. pkgCache::DepIterator End;
  242. D.GlobOr(Start,End); // advances D
  243. if (Cache->IsImportantDep(End) == false)
  244. continue;
  245. if (Now == true)
  246. {
  247. if ((Cache[End] & pkgDepCache::DepGNow) == pkgDepCache::DepGNow)
  248. continue;
  249. }
  250. else
  251. {
  252. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  253. continue;
  254. }
  255. bool FirstOr = true;
  256. while (1)
  257. {
  258. if (First == false)
  259. for (unsigned J = 0; J != Indent; J++)
  260. out << ' ';
  261. First = false;
  262. if (FirstOr == false)
  263. {
  264. for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
  265. out << ' ';
  266. }
  267. else
  268. out << ' ' << End.DepType() << ": ";
  269. FirstOr = false;
  270. out << Start.TargetPkg().Name();
  271. // Show a quick summary of the version requirements
  272. if (Start.TargetVer() != 0)
  273. out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
  274. /* Show a summary of the target package if possible. In the case
  275. of virtual packages we show nothing */
  276. pkgCache::PkgIterator Targ = Start.TargetPkg();
  277. if (Targ->ProvidesList == 0)
  278. {
  279. out << ' ';
  280. pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
  281. if (Now == true)
  282. Ver = Targ.CurrentVer();
  283. if (Ver.end() == false)
  284. {
  285. if (Now == true)
  286. ioprintf(out,_("but %s is installed"),Ver.VerStr());
  287. else
  288. ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
  289. }
  290. else
  291. {
  292. if (Cache[Targ].CandidateVerIter(Cache).end() == true)
  293. {
  294. if (Targ->ProvidesList == 0)
  295. out << _("but it is not installable");
  296. else
  297. out << _("but it is a virtual package");
  298. }
  299. else
  300. out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
  301. }
  302. }
  303. if (Start != End)
  304. out << _(" or");
  305. out << endl;
  306. if (Start == End)
  307. break;
  308. Start++;
  309. }
  310. }
  311. }
  312. }
  313. /*}}}*/
  314. // ShowNew - Show packages to newly install /*{{{*/
  315. // ---------------------------------------------------------------------
  316. /* */
  317. void ShowNew(ostream &out,CacheFile &Cache)
  318. {
  319. /* Print out a list of packages that are going to be installed extra
  320. to what the user asked */
  321. string List;
  322. string VersionsList;
  323. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  324. {
  325. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  326. if (Cache[I].NewInstall() == true) {
  327. List += string(I.Name()) + " ";
  328. VersionsList += string(Cache[I].CandVersion) + "\n";
  329. }
  330. }
  331. ShowList(out,_("The following NEW packages will be installed:"),List,VersionsList);
  332. }
  333. /*}}}*/
  334. // ShowDel - Show packages to delete /*{{{*/
  335. // ---------------------------------------------------------------------
  336. /* */
  337. void ShowDel(ostream &out,CacheFile &Cache)
  338. {
  339. /* Print out a list of packages that are going to be removed extra
  340. to what the user asked */
  341. string List;
  342. string VersionsList;
  343. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  344. {
  345. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  346. if (Cache[I].Delete() == true)
  347. {
  348. if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
  349. List += string(I.Name()) + "* ";
  350. else
  351. List += string(I.Name()) + " ";
  352. VersionsList += string(Cache[I].CandVersion)+ "\n";
  353. }
  354. }
  355. ShowList(out,_("The following packages will be REMOVED:"),List,VersionsList);
  356. }
  357. /*}}}*/
  358. // ShowKept - Show kept packages /*{{{*/
  359. // ---------------------------------------------------------------------
  360. /* */
  361. void ShowKept(ostream &out,CacheFile &Cache)
  362. {
  363. string List;
  364. string VersionsList;
  365. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  366. {
  367. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  368. // Not interesting
  369. if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
  370. I->CurrentVer == 0 || Cache[I].Delete() == true)
  371. continue;
  372. List += string(I.Name()) + " ";
  373. VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
  374. }
  375. ShowList(out,_("The following packages have been kept back:"),List,VersionsList);
  376. }
  377. /*}}}*/
  378. // ShowUpgraded - Show upgraded packages /*{{{*/
  379. // ---------------------------------------------------------------------
  380. /* */
  381. void ShowUpgraded(ostream &out,CacheFile &Cache)
  382. {
  383. string List;
  384. string VersionsList;
  385. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  386. {
  387. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  388. // Not interesting
  389. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  390. continue;
  391. List += string(I.Name()) + " ";
  392. VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
  393. }
  394. ShowList(out,_("The following packages will be upgraded:"),List,VersionsList);
  395. }
  396. /*}}}*/
  397. // ShowDowngraded - Show downgraded packages /*{{{*/
  398. // ---------------------------------------------------------------------
  399. /* */
  400. bool ShowDowngraded(ostream &out,CacheFile &Cache)
  401. {
  402. string List;
  403. string VersionsList;
  404. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  405. {
  406. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  407. // Not interesting
  408. if (Cache[I].Downgrade() == false || Cache[I].NewInstall() == true)
  409. continue;
  410. List += string(I.Name()) + " ";
  411. VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
  412. }
  413. return ShowList(out,_("The following packages will be DOWNGRADED:"),List,VersionsList);
  414. }
  415. /*}}}*/
  416. // ShowHold - Show held but changed packages /*{{{*/
  417. // ---------------------------------------------------------------------
  418. /* */
  419. bool ShowHold(ostream &out,CacheFile &Cache)
  420. {
  421. string List;
  422. string VersionsList;
  423. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  424. {
  425. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  426. if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
  427. I->SelectedState == pkgCache::State::Hold) {
  428. List += string(I.Name()) + " ";
  429. VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
  430. }
  431. }
  432. return ShowList(out,_("The following held packages will be changed:"),List,VersionsList);
  433. }
  434. /*}}}*/
  435. // ShowEssential - Show an essential package warning /*{{{*/
  436. // ---------------------------------------------------------------------
  437. /* This prints out a warning message that is not to be ignored. It shows
  438. all essential packages and their dependents that are to be removed.
  439. It is insanely risky to remove the dependents of an essential package! */
  440. bool ShowEssential(ostream &out,CacheFile &Cache)
  441. {
  442. string List;
  443. string VersionsList;
  444. bool *Added = new bool[Cache->Head().PackageCount];
  445. for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
  446. Added[I] = false;
  447. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  448. {
  449. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  450. if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
  451. (I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
  452. continue;
  453. // The essential package is being removed
  454. if (Cache[I].Delete() == true)
  455. {
  456. if (Added[I->ID] == false)
  457. {
  458. Added[I->ID] = true;
  459. List += string(I.Name()) + " ";
  460. //VersionsList += string(Cache[I].CurVersion) + "\n"; ???
  461. }
  462. }
  463. if (I->CurrentVer == 0)
  464. continue;
  465. // Print out any essential package depenendents that are to be removed
  466. for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
  467. {
  468. // Skip everything but depends
  469. if (D->Type != pkgCache::Dep::PreDepends &&
  470. D->Type != pkgCache::Dep::Depends)
  471. continue;
  472. pkgCache::PkgIterator P = D.SmartTargetPkg();
  473. if (Cache[P].Delete() == true)
  474. {
  475. if (Added[P->ID] == true)
  476. continue;
  477. Added[P->ID] = true;
  478. char S[300];
  479. snprintf(S,sizeof(S),_("%s (due to %s) "),P.Name(),I.Name());
  480. List += S;
  481. //VersionsList += "\n"; ???
  482. }
  483. }
  484. }
  485. delete [] Added;
  486. return ShowList(out,_("WARNING: The following essential packages will be removed.\n"
  487. "This should NOT be done unless you know exactly what you are doing!"),List,VersionsList);
  488. }
  489. /*}}}*/
  490. // Stats - Show some statistics /*{{{*/
  491. // ---------------------------------------------------------------------
  492. /* */
  493. void Stats(ostream &out,pkgDepCache &Dep)
  494. {
  495. unsigned long Upgrade = 0;
  496. unsigned long Downgrade = 0;
  497. unsigned long Install = 0;
  498. unsigned long ReInstall = 0;
  499. for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
  500. {
  501. if (Dep[I].NewInstall() == true)
  502. Install++;
  503. else
  504. {
  505. if (Dep[I].Upgrade() == true)
  506. Upgrade++;
  507. else
  508. if (Dep[I].Downgrade() == true)
  509. Downgrade++;
  510. }
  511. if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
  512. ReInstall++;
  513. }
  514. ioprintf(out,_("%lu upgraded, %lu newly installed, "),
  515. Upgrade,Install);
  516. if (ReInstall != 0)
  517. ioprintf(out,_("%lu reinstalled, "),ReInstall);
  518. if (Downgrade != 0)
  519. ioprintf(out,_("%lu downgraded, "),Downgrade);
  520. ioprintf(out,_("%lu to remove and %lu not upgraded.\n"),
  521. Dep.DelCount(),Dep.KeepCount());
  522. if (Dep.BadCount() != 0)
  523. ioprintf(out,_("%lu not fully installed or removed.\n"),
  524. Dep.BadCount());
  525. }
  526. /*}}}*/
  527. // CacheFile::NameComp - QSort compare by name /*{{{*/
  528. // ---------------------------------------------------------------------
  529. /* */
  530. pkgCache *CacheFile::SortCache = 0;
  531. int CacheFile::NameComp(const void *a,const void *b)
  532. {
  533. if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
  534. return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
  535. const pkgCache::Package &A = **(pkgCache::Package **)a;
  536. const pkgCache::Package &B = **(pkgCache::Package **)b;
  537. return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
  538. }
  539. /*}}}*/
  540. // CacheFile::Sort - Sort by name /*{{{*/
  541. // ---------------------------------------------------------------------
  542. /* */
  543. void CacheFile::Sort()
  544. {
  545. delete [] List;
  546. List = new pkgCache::Package *[Cache->Head().PackageCount];
  547. memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
  548. pkgCache::PkgIterator I = Cache->PkgBegin();
  549. for (;I.end() != true; I++)
  550. List[I->ID] = I;
  551. SortCache = *this;
  552. qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
  553. }
  554. /*}}}*/
  555. // CacheFile::CheckDeps - Open the cache file /*{{{*/
  556. // ---------------------------------------------------------------------
  557. /* This routine generates the caches and then opens the dependency cache
  558. and verifies that the system is OK. */
  559. bool CacheFile::CheckDeps(bool AllowBroken)
  560. {
  561. bool FixBroken = _config->FindB("APT::Get::Fix-Broken",false);
  562. if (_error->PendingError() == true)
  563. return false;
  564. // Check that the system is OK
  565. if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
  566. return _error->Error("Internal error, non-zero counts");
  567. // Apply corrections for half-installed packages
  568. if (pkgApplyStatus(*DCache) == false)
  569. return false;
  570. if (_config->FindB("APT::Get::Fix-Policy-Broken",false) == true)
  571. {
  572. FixBroken = true;
  573. if ((DCache->PolicyBrokenCount() > 0))
  574. {
  575. // upgrade all policy-broken packages with ForceImportantDeps=True
  576. for (pkgCache::PkgIterator I = Cache->PkgBegin(); !I.end(); I++)
  577. if ((*DCache)[I].NowPolicyBroken() == true)
  578. DCache->MarkInstall(I,true,0, false, true);
  579. }
  580. }
  581. // Nothing is broken
  582. if (DCache->BrokenCount() == 0 || AllowBroken == true)
  583. return true;
  584. // Attempt to fix broken things
  585. if (FixBroken == true)
  586. {
  587. c1out << _("Correcting dependencies...") << flush;
  588. if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
  589. {
  590. c1out << _(" failed.") << endl;
  591. ShowBroken(c1out,*this,true);
  592. return _error->Error(_("Unable to correct dependencies"));
  593. }
  594. if (pkgMinimizeUpgrade(*DCache) == false)
  595. return _error->Error(_("Unable to minimize the upgrade set"));
  596. c1out << _(" Done") << endl;
  597. }
  598. else
  599. {
  600. c1out << _("You might want to run `apt-get -f install' to correct these.") << endl;
  601. ShowBroken(c1out,*this,true);
  602. return _error->Error(_("Unmet dependencies. Try using -f."));
  603. }
  604. return true;
  605. }
  606. static bool CheckAuth(pkgAcquire& Fetcher)
  607. {
  608. string UntrustedList;
  609. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
  610. {
  611. if (!(*I)->IsTrusted())
  612. {
  613. UntrustedList += string((*I)->ShortDesc()) + " ";
  614. }
  615. }
  616. if (UntrustedList == "")
  617. {
  618. return true;
  619. }
  620. ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"),UntrustedList,"");
  621. if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
  622. {
  623. c2out << _("Authentication warning overridden.\n");
  624. return true;
  625. }
  626. if (_config->FindI("quiet",0) < 2
  627. && _config->FindB("APT::Get::Assume-Yes",false) == false)
  628. {
  629. c2out << _("Install these packages without verification [y/N]? ") << flush;
  630. if (!YnPrompt(false))
  631. return _error->Error(_("Some packages could not be authenticated"));
  632. return true;
  633. }
  634. else if (_config->FindB("APT::Get::Force-Yes",false) == true)
  635. {
  636. return true;
  637. }
  638. return _error->Error(_("There are problems and -y was used without --force-yes"));
  639. }
  640. /*}}}*/
  641. // InstallPackages - Actually download and install the packages /*{{{*/
  642. // ---------------------------------------------------------------------
  643. /* This displays the informative messages describing what is going to
  644. happen and then calls the download routines */
  645. bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
  646. bool Safety = true)
  647. {
  648. if (_config->FindB("APT::Get::Purge",false) == true)
  649. {
  650. pkgCache::PkgIterator I = Cache->PkgBegin();
  651. for (; I.end() == false; I++)
  652. {
  653. if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
  654. Cache->MarkDelete(I,true);
  655. }
  656. }
  657. bool Fail = false;
  658. bool Essential = false;
  659. // Show all the various warning indicators
  660. ShowDel(c1out,Cache);
  661. ShowNew(c1out,Cache);
  662. if (ShwKept == true)
  663. ShowKept(c1out,Cache);
  664. Fail |= !ShowHold(c1out,Cache);
  665. if (_config->FindB("APT::Get::Show-Upgraded",true) == true)
  666. ShowUpgraded(c1out,Cache);
  667. Fail |= !ShowDowngraded(c1out,Cache);
  668. if (_config->FindB("APT::Get::Download-Only",false) == false)
  669. Essential = !ShowEssential(c1out,Cache);
  670. Fail |= Essential;
  671. Stats(c1out,Cache);
  672. // Sanity check
  673. if (Cache->BrokenCount() != 0)
  674. {
  675. ShowBroken(c1out,Cache,false);
  676. return _error->Error(_("Internal error, InstallPackages was called with broken packages!"));
  677. }
  678. if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
  679. Cache->BadCount() == 0)
  680. return true;
  681. // No remove flag
  682. if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
  683. return _error->Error(_("Packages need to be removed but remove is disabled."));
  684. // Run the simulator ..
  685. if (_config->FindB("APT::Get::Simulate") == true)
  686. {
  687. pkgSimulate PM(Cache);
  688. int status_fd = _config->FindI("APT::Status-Fd",-1);
  689. pkgPackageManager::OrderResult Res = PM.DoInstall(status_fd);
  690. if (Res == pkgPackageManager::Failed)
  691. return false;
  692. if (Res != pkgPackageManager::Completed)
  693. return _error->Error(_("Internal error, Ordering didn't finish"));
  694. return true;
  695. }
  696. // Create the text record parser
  697. pkgRecords Recs(Cache);
  698. if (_error->PendingError() == true)
  699. return false;
  700. // Lock the archive directory
  701. FileFd Lock;
  702. if (_config->FindB("Debug::NoLocking",false) == false &&
  703. _config->FindB("APT::Get::Print-URIs") == false)
  704. {
  705. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  706. if (_error->PendingError() == true)
  707. return _error->Error(_("Unable to lock the download directory"));
  708. }
  709. // Create the download object
  710. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  711. pkgAcquire Fetcher(&Stat);
  712. // Read the source list
  713. pkgSourceList List;
  714. if (List.ReadMainList() == false)
  715. return _error->Error(_("The list of sources could not be read."));
  716. // Create the package manager and prepare to download
  717. SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
  718. if (PM->GetArchives(&Fetcher,&List,&Recs) == false ||
  719. _error->PendingError() == true)
  720. return false;
  721. // Display statistics
  722. double FetchBytes = Fetcher.FetchNeeded();
  723. double FetchPBytes = Fetcher.PartialPresent();
  724. double DebBytes = Fetcher.TotalNeeded();
  725. if (DebBytes != Cache->DebSize())
  726. {
  727. c0out << DebBytes << ',' << Cache->DebSize() << endl;
  728. c0out << _("How odd.. The sizes didn't match, email apt@packages.debian.org") << endl;
  729. }
  730. // Number of bytes
  731. if (DebBytes != FetchBytes)
  732. ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"),
  733. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  734. else
  735. ioprintf(c1out,_("Need to get %sB of archives.\n"),
  736. SizeToStr(DebBytes).c_str());
  737. // Size delta
  738. if (Cache->UsrSize() >= 0)
  739. ioprintf(c1out,_("After unpacking %sB of additional disk space will be used.\n"),
  740. SizeToStr(Cache->UsrSize()).c_str());
  741. else
  742. ioprintf(c1out,_("After unpacking %sB disk space will be freed.\n"),
  743. SizeToStr(-1*Cache->UsrSize()).c_str());
  744. if (_error->PendingError() == true)
  745. return false;
  746. /* Check for enough free space, but only if we are actually going to
  747. download */
  748. if (_config->FindB("APT::Get::Print-URIs") == false &&
  749. _config->FindB("APT::Get::Download",true) == true)
  750. {
  751. struct statvfs Buf;
  752. string OutputDir = _config->FindDir("Dir::Cache::Archives");
  753. if (statvfs(OutputDir.c_str(),&Buf) != 0)
  754. return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
  755. OutputDir.c_str());
  756. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  757. {
  758. struct statfs Stat;
  759. if (statfs(OutputDir.c_str(),&Stat) != 0 ||
  760. unsigned(Stat.f_type) != RAMFS_MAGIC)
  761. return _error->Error(_("You don't have enough free space in %s."),
  762. OutputDir.c_str());
  763. }
  764. }
  765. // Fail safe check
  766. if (_config->FindI("quiet",0) >= 2 ||
  767. _config->FindB("APT::Get::Assume-Yes",false) == true)
  768. {
  769. if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
  770. return _error->Error(_("There are problems and -y was used without --force-yes"));
  771. }
  772. if (Essential == true && Safety == true)
  773. {
  774. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  775. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  776. const char *Prompt = _("Yes, do as I say!");
  777. ioprintf(c2out,
  778. _("You are about to do something potentially harmful.\n"
  779. "To continue type in the phrase '%s'\n"
  780. " ?] "),Prompt);
  781. c2out << flush;
  782. if (AnalPrompt(Prompt) == false)
  783. {
  784. c2out << _("Abort.") << endl;
  785. exit(1);
  786. }
  787. }
  788. else
  789. {
  790. // Prompt to continue
  791. if (Ask == true || Fail == true)
  792. {
  793. if (_config->FindB("APT::Get::Trivial-Only",false) == true)
  794. return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
  795. if (_config->FindI("quiet",0) < 2 &&
  796. _config->FindB("APT::Get::Assume-Yes",false) == false)
  797. {
  798. c2out << _("Do you want to continue [Y/n]? ") << flush;
  799. if (YnPrompt() == false)
  800. {
  801. c2out << _("Abort.") << endl;
  802. exit(1);
  803. }
  804. }
  805. }
  806. }
  807. // Just print out the uris an exit if the --print-uris flag was used
  808. if (_config->FindB("APT::Get::Print-URIs") == true)
  809. {
  810. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  811. for (; I != Fetcher.UriEnd(); I++)
  812. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  813. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  814. return true;
  815. }
  816. if (!CheckAuth(Fetcher))
  817. return false;
  818. /* Unlock the dpkg lock if we are not going to be doing an install
  819. after. */
  820. if (_config->FindB("APT::Get::Download-Only",false) == true)
  821. _system->UnLock();
  822. // Run it
  823. while (1)
  824. {
  825. bool Transient = false;
  826. if (_config->FindB("APT::Get::Download",true) == false)
  827. {
  828. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
  829. {
  830. if ((*I)->Local == true)
  831. {
  832. I++;
  833. continue;
  834. }
  835. // Close the item and check if it was found in cache
  836. (*I)->Finished();
  837. if ((*I)->Complete == false)
  838. Transient = true;
  839. // Clear it out of the fetch list
  840. delete *I;
  841. I = Fetcher.ItemsBegin();
  842. }
  843. }
  844. if (Fetcher.Run() == pkgAcquire::Failed)
  845. return false;
  846. // Print out errors
  847. bool Failed = false;
  848. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  849. {
  850. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  851. (*I)->Complete == true)
  852. continue;
  853. if ((*I)->Status == pkgAcquire::Item::StatIdle)
  854. {
  855. Transient = true;
  856. // Failed = true;
  857. continue;
  858. }
  859. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  860. (*I)->ErrorText.c_str());
  861. Failed = true;
  862. }
  863. /* If we are in no download mode and missing files and there were
  864. 'failures' then the user must specify -m. Furthermore, there
  865. is no such thing as a transient error in no-download mode! */
  866. if (Transient == true &&
  867. _config->FindB("APT::Get::Download",true) == false)
  868. {
  869. Transient = false;
  870. Failed = true;
  871. }
  872. if (_config->FindB("APT::Get::Download-Only",false) == true)
  873. {
  874. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  875. return _error->Error(_("Some files failed to download"));
  876. c1out << _("Download complete and in download only mode") << endl;
  877. return true;
  878. }
  879. if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
  880. {
  881. return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
  882. }
  883. if (Transient == true && Failed == true)
  884. return _error->Error(_("--fix-missing and media swapping is not currently supported"));
  885. // Try to deal with missing package files
  886. if (Failed == true && PM->FixMissing() == false)
  887. {
  888. cerr << _("Unable to correct missing packages.") << endl;
  889. return _error->Error(_("Aborting install."));
  890. }
  891. _system->UnLock();
  892. int status_fd = _config->FindI("APT::Status-Fd",-1);
  893. pkgPackageManager::OrderResult Res = PM->DoInstall(status_fd);
  894. if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
  895. return false;
  896. if (Res == pkgPackageManager::Completed)
  897. return true;
  898. // Reload the fetcher object and loop again for media swapping
  899. Fetcher.Shutdown();
  900. if (PM->GetArchives(&Fetcher,&List,&Recs) == false)
  901. return false;
  902. _system->Lock();
  903. }
  904. }
  905. /*}}}*/
  906. // TryToInstall - Try to install a single package /*{{{*/
  907. // ---------------------------------------------------------------------
  908. /* This used to be inlined in DoInstall, but with the advent of regex package
  909. name matching it was split out.. */
  910. bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
  911. pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
  912. unsigned int &ExpectedInst,bool AllowFail = true)
  913. {
  914. /* This is a pure virtual package and there is a single available
  915. provides */
  916. if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0 &&
  917. Pkg.ProvidesList()->NextProvides == 0)
  918. {
  919. pkgCache::PkgIterator Tmp = Pkg.ProvidesList().OwnerPkg();
  920. ioprintf(c1out,_("Note, selecting %s instead of %s\n"),
  921. Tmp.Name(),Pkg.Name());
  922. Pkg = Tmp;
  923. }
  924. // Handle the no-upgrade case
  925. if (_config->FindB("APT::Get::upgrade",true) == false &&
  926. Pkg->CurrentVer != 0)
  927. {
  928. if (AllowFail == true)
  929. ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
  930. Pkg.Name());
  931. return true;
  932. }
  933. // Check if there is something at all to install
  934. pkgDepCache::StateCache &State = Cache[Pkg];
  935. if (Remove == true && Pkg->CurrentVer == 0)
  936. {
  937. Fix.Clear(Pkg);
  938. Fix.Protect(Pkg);
  939. Fix.Remove(Pkg);
  940. /* We want to continue searching for regex hits, so we return false here
  941. otherwise this is not really an error. */
  942. if (AllowFail == false)
  943. return false;
  944. ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.Name());
  945. return true;
  946. }
  947. if (State.CandidateVer == 0 && Remove == false)
  948. {
  949. if (AllowFail == false)
  950. return false;
  951. if (Pkg->ProvidesList != 0)
  952. {
  953. ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
  954. Pkg.Name());
  955. pkgCache::PrvIterator I = Pkg.ProvidesList();
  956. for (; I.end() == false; I++)
  957. {
  958. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  959. if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer())
  960. {
  961. if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
  962. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
  963. _(" [Installed]") << endl;
  964. else
  965. c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
  966. }
  967. }
  968. c1out << _("You should explicitly select one to install.") << endl;
  969. }
  970. else
  971. {
  972. ioprintf(c1out,
  973. _("Package %s is not available, but is referred to by another package.\n"
  974. "This may mean that the package is missing, has been obsoleted, or\n"
  975. "is only available from another source\n"),Pkg.Name());
  976. string List;
  977. string VersionsList;
  978. SPtrArray<bool> Seen = new bool[Cache.Head().PackageCount];
  979. memset(Seen,0,Cache.Head().PackageCount*sizeof(*Seen));
  980. pkgCache::DepIterator Dep = Pkg.RevDependsList();
  981. for (; Dep.end() == false; Dep++)
  982. {
  983. if (Dep->Type != pkgCache::Dep::Replaces)
  984. continue;
  985. if (Seen[Dep.ParentPkg()->ID] == true)
  986. continue;
  987. Seen[Dep.ParentPkg()->ID] = true;
  988. List += string(Dep.ParentPkg().Name()) + " ";
  989. //VersionsList += string(Dep.ParentPkg().CurVersion) + "\n"; ???
  990. }
  991. ShowList(c1out,_("However the following packages replace it:"),List,VersionsList);
  992. }
  993. _error->Error(_("Package %s has no installation candidate"),Pkg.Name());
  994. return false;
  995. }
  996. Fix.Clear(Pkg);
  997. Fix.Protect(Pkg);
  998. if (Remove == true)
  999. {
  1000. Fix.Remove(Pkg);
  1001. Cache.MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
  1002. return true;
  1003. }
  1004. // Install it
  1005. Cache.MarkInstall(Pkg,false);
  1006. if (State.Install() == false)
  1007. {
  1008. if (_config->FindB("APT::Get::ReInstall",false) == true)
  1009. {
  1010. if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
  1011. ioprintf(c1out,_("Reinstallation of %s is not possible, it cannot be downloaded.\n"),
  1012. Pkg.Name());
  1013. else
  1014. Cache.SetReInstall(Pkg,true);
  1015. }
  1016. else
  1017. {
  1018. if (AllowFail == true)
  1019. ioprintf(c1out,_("%s is already the newest version.\n"),
  1020. Pkg.Name());
  1021. }
  1022. }
  1023. else
  1024. ExpectedInst++;
  1025. // Install it with autoinstalling enabled (if we not respect the minial
  1026. // required deps or the policy)
  1027. if ((State.InstBroken() == true || State.InstPolicyBroken() == true) && BrokenFix == false)
  1028. Cache.MarkInstall(Pkg,true);
  1029. return true;
  1030. }
  1031. /*}}}*/
  1032. // TryToChangeVer - Try to change a candidate version /*{{{*/
  1033. // ---------------------------------------------------------------------
  1034. /* */
  1035. bool TryToChangeVer(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
  1036. const char *VerTag,bool IsRel)
  1037. {
  1038. pkgVersionMatch Match(VerTag,(IsRel == true?pkgVersionMatch::Release :
  1039. pkgVersionMatch::Version));
  1040. pkgCache::VerIterator Ver = Match.Find(Pkg);
  1041. if (Ver.end() == true)
  1042. {
  1043. if (IsRel == true)
  1044. return _error->Error(_("Release '%s' for '%s' was not found"),
  1045. VerTag,Pkg.Name());
  1046. return _error->Error(_("Version '%s' for '%s' was not found"),
  1047. VerTag,Pkg.Name());
  1048. }
  1049. if (strcmp(VerTag,Ver.VerStr()) != 0)
  1050. {
  1051. ioprintf(c1out,_("Selected version %s (%s) for %s\n"),
  1052. Ver.VerStr(),Ver.RelStr().c_str(),Pkg.Name());
  1053. }
  1054. Cache.SetCandidateVersion(Ver);
  1055. return true;
  1056. }
  1057. /*}}}*/
  1058. // FindSrc - Find a source record /*{{{*/
  1059. // ---------------------------------------------------------------------
  1060. /* */
  1061. pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
  1062. pkgSrcRecords &SrcRecs,string &Src,
  1063. pkgDepCache &Cache)
  1064. {
  1065. // We want to pull the version off the package specification..
  1066. string VerTag;
  1067. string TmpSrc = Name;
  1068. string::size_type Slash = TmpSrc.rfind('=');
  1069. // honor default release
  1070. string DefRel = _config->Find("APT::Default-Release");
  1071. pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
  1072. if (Slash != string::npos)
  1073. {
  1074. VerTag = string(TmpSrc.begin() + Slash + 1,TmpSrc.end());
  1075. TmpSrc = string(TmpSrc.begin(),TmpSrc.begin() + Slash);
  1076. }
  1077. else if(!Pkg.end() && DefRel.empty() == false)
  1078. {
  1079. // we have a default release, try to locate the pkg. we do it like
  1080. // this because GetCandidateVer() will not "downgrade", that means
  1081. // "apt-get source -t stable apt" won't work on a unstable system
  1082. for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false;
  1083. Ver++)
  1084. {
  1085. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false;
  1086. VF++)
  1087. {
  1088. /* If this is the status file, and the current version is not the
  1089. version in the status file (ie it is not installed, or somesuch)
  1090. then it is not a candidate for installation, ever. This weeds
  1091. out bogus entries that may be due to config-file states, or
  1092. other. */
  1093. if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
  1094. pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
  1095. continue;
  1096. //std::cout << VF.File().Archive() << std::endl;
  1097. if(VF.File().Archive() && (VF.File().Archive() == DefRel))
  1098. {
  1099. VerTag = Ver.VerStr();
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. }
  1105. /* Lookup the version of the package we would install if we were to
  1106. install a version and determine the source package name, then look
  1107. in the archive for a source package of the same name. */
  1108. if (_config->FindB("APT::Get::Only-Source") == false)
  1109. {
  1110. if (Pkg.end() == false)
  1111. {
  1112. pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
  1113. if (Ver.end() == false)
  1114. {
  1115. pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
  1116. Src = Parse.SourcePkg();
  1117. }
  1118. }
  1119. }
  1120. // No source package name..
  1121. if (Src.empty() == true)
  1122. Src = TmpSrc;
  1123. // The best hit
  1124. pkgSrcRecords::Parser *Last = 0;
  1125. unsigned long Offset = 0;
  1126. string Version;
  1127. bool IsMatch = false;
  1128. // If we are matching by version then we need exact matches to be happy
  1129. if (VerTag.empty() == false)
  1130. IsMatch = true;
  1131. /* Iterate over all of the hits, which includes the resulting
  1132. binary packages in the search */
  1133. pkgSrcRecords::Parser *Parse;
  1134. SrcRecs.Restart();
  1135. while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
  1136. {
  1137. string Ver = Parse->Version();
  1138. // Skip name mismatches
  1139. if (IsMatch == true && Parse->Package() != Src)
  1140. continue;
  1141. if (VerTag.empty() == false)
  1142. {
  1143. /* Don't want to fall through because we are doing exact version
  1144. matching. */
  1145. if (Cache.VS().CmpVersion(VerTag,Ver) != 0)
  1146. continue;
  1147. Last = Parse;
  1148. Offset = Parse->Offset();
  1149. break;
  1150. }
  1151. // Newer version or an exact match
  1152. if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0 ||
  1153. (Parse->Package() == Src && IsMatch == false))
  1154. {
  1155. IsMatch = Parse->Package() == Src;
  1156. Last = Parse;
  1157. Offset = Parse->Offset();
  1158. Version = Ver;
  1159. }
  1160. }
  1161. if (Last == 0 || Last->Jump(Offset) == false)
  1162. return 0;
  1163. return Last;
  1164. }
  1165. /*}}}*/
  1166. // DoUpdate - Update the package lists /*{{{*/
  1167. // ---------------------------------------------------------------------
  1168. /* */
  1169. bool DoUpdate(CommandLine &CmdL)
  1170. {
  1171. if (CmdL.FileSize() != 1)
  1172. return _error->Error(_("The update command takes no arguments"));
  1173. // Get the source list
  1174. pkgSourceList List;
  1175. if (List.ReadMainList() == false)
  1176. return false;
  1177. // Lock the list directory
  1178. FileFd Lock;
  1179. if (_config->FindB("Debug::NoLocking",false) == false)
  1180. {
  1181. Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
  1182. if (_error->PendingError() == true)
  1183. return _error->Error(_("Unable to lock the list directory"));
  1184. }
  1185. // Create the download object
  1186. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1187. pkgAcquire Fetcher(&Stat);
  1188. // Just print out the uris an exit if the --print-uris flag was used
  1189. if (_config->FindB("APT::Get::Print-URIs") == true)
  1190. {
  1191. // Populate it with the source selection and get all Indexes
  1192. // (GetAll=true)
  1193. if (List.GetIndexes(&Fetcher,true) == false)
  1194. return false;
  1195. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  1196. for (; I != Fetcher.UriEnd(); I++)
  1197. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  1198. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  1199. return true;
  1200. }
  1201. // Populate it with the source selection
  1202. if (List.GetIndexes(&Fetcher) == false)
  1203. return false;
  1204. // Run it
  1205. if (Fetcher.Run() == pkgAcquire::Failed)
  1206. return false;
  1207. bool Failed = false;
  1208. bool TransientNetworkFailure = false;
  1209. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  1210. {
  1211. if ((*I)->Status == pkgAcquire::Item::StatDone)
  1212. continue;
  1213. (*I)->Finished();
  1214. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  1215. (*I)->ErrorText.c_str());
  1216. if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
  1217. {
  1218. TransientNetworkFailure = true;
  1219. continue;
  1220. }
  1221. Failed = true;
  1222. }
  1223. // Clean out any old list files
  1224. if (!TransientNetworkFailure &&
  1225. _config->FindB("APT::Get::List-Cleanup",true) == true)
  1226. {
  1227. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  1228. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  1229. return false;
  1230. }
  1231. // Prepare the cache.
  1232. CacheFile Cache;
  1233. if (Cache.BuildCaches() == false)
  1234. return false;
  1235. if (TransientNetworkFailure == true)
  1236. _error->Warning(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  1237. else if (Failed == true)
  1238. return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  1239. return true;
  1240. }
  1241. /*}}}*/
  1242. // DoAutomaticRemove - Remove all automatic unused packages /*{{{*/
  1243. // ---------------------------------------------------------------------
  1244. /* Remove unused automatic packages */
  1245. bool DoAutomaticRemove(CacheFile &Cache)
  1246. {
  1247. bool Debug = _config->FindI("Debug::pkgAutoRemove",false);
  1248. bool doAutoRemove = _config->FindB("APT::Get::AutomaticRemove", false);
  1249. bool hideAutoRemove = _config->FindB("APT::Get::HideAutoRemove");
  1250. pkgDepCache::ActionGroup group(*Cache);
  1251. if(Debug)
  1252. std::cout << "DoAutomaticRemove()" << std::endl;
  1253. if (_config->FindB("APT::Get::Remove",true) == false &&
  1254. doAutoRemove == true)
  1255. {
  1256. c1out << _("We are not supposed to delete stuff, can't start "
  1257. "AutoRemover") << std::endl;
  1258. doAutoRemove = false;
  1259. }
  1260. string autoremovelist, autoremoveversions;
  1261. // look over the cache to see what can be removed
  1262. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); ! Pkg.end(); ++Pkg)
  1263. {
  1264. if (Cache[Pkg].Garbage)
  1265. {
  1266. if(Pkg.CurrentVer() != 0 || Cache[Pkg].Install())
  1267. if(Debug)
  1268. std::cout << "We could delete %s" << Pkg.Name() << std::endl;
  1269. // only show stuff in the list that is not yet marked for removal
  1270. if(Cache[Pkg].Delete() == false)
  1271. {
  1272. autoremovelist += string(Pkg.Name()) + " ";
  1273. autoremoveversions += string(Cache[Pkg].CandVersion) + "\n";
  1274. }
  1275. if (doAutoRemove)
  1276. {
  1277. if(Pkg.CurrentVer() != 0 &&
  1278. Pkg->CurrentState != pkgCache::State::ConfigFiles)
  1279. Cache->MarkDelete(Pkg, _config->FindB("APT::Get::Purge", false));
  1280. else
  1281. Cache->MarkKeep(Pkg, false, false);
  1282. }
  1283. }
  1284. }
  1285. if (!hideAutoRemove)
  1286. ShowList(c1out, _("The following packages were automatically installed and are no longer required:"), autoremovelist, autoremoveversions);
  1287. if (!doAutoRemove && !hideAutoRemove && autoremovelist.size() > 0)
  1288. c1out << _("Use 'apt-get autoremove' to remove them.") << std::endl;
  1289. // Now see if we destroyed anything
  1290. if (Cache->BrokenCount() != 0)
  1291. {
  1292. c1out << _("Hmm, seems like the AutoRemover destroyed something which really\n"
  1293. "shouldn't happen. Please file a bug report against apt.") << endl;
  1294. c1out << endl;
  1295. c1out << _("The following information may help to resolve the situation:") << endl;
  1296. c1out << endl;
  1297. ShowBroken(c1out,Cache,false);
  1298. return _error->Error(_("Internal Error, AutoRemover broke stuff"));
  1299. }
  1300. return true;
  1301. }
  1302. // DoUpgrade - Upgrade all packages /*{{{*/
  1303. // ---------------------------------------------------------------------
  1304. /* Upgrade all packages without installing new packages or erasing old
  1305. packages */
  1306. bool DoUpgrade(CommandLine &CmdL)
  1307. {
  1308. CacheFile Cache;
  1309. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1310. return false;
  1311. // Do the upgrade
  1312. if (pkgAllUpgrade(Cache) == false)
  1313. {
  1314. ShowBroken(c1out,Cache,false);
  1315. return _error->Error(_("Internal error, AllUpgrade broke stuff"));
  1316. }
  1317. return InstallPackages(Cache,true);
  1318. }
  1319. /*}}}*/
  1320. // DoInstallTask - Install task from the command line /*{{{*/
  1321. // ---------------------------------------------------------------------
  1322. /* Install named task */
  1323. bool TryInstallTask(pkgDepCache &Cache, pkgProblemResolver &Fix,
  1324. bool BrokenFix,
  1325. unsigned int& ExpectedInst,
  1326. const char *taskname,
  1327. bool Remove)
  1328. {
  1329. const char *start, *end;
  1330. pkgCache::PkgIterator Pkg;
  1331. char buf[64*1024];
  1332. regex_t Pattern;
  1333. // get the records
  1334. pkgRecords Recs(Cache);
  1335. // build regexp for the task
  1336. char S[300];
  1337. snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", taskname);
  1338. if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0)
  1339. return _error->Error("Failed to compile task regexp");
  1340. bool found = false;
  1341. bool res = true;
  1342. for (Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
  1343. {
  1344. pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
  1345. if(ver.end())
  1346. continue;
  1347. pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
  1348. parser.GetRec(start,end);
  1349. strncpy(buf, start, end-start);
  1350. buf[end-start] = 0x0;
  1351. if (regexec(&Pattern,buf,0,0,0) != 0)
  1352. continue;
  1353. res &= TryToInstall(Pkg,Cache,Fix,Remove,true,ExpectedInst);
  1354. found = true;
  1355. }
  1356. if(!found)
  1357. _error->Error(_("Couldn't find task %s"),taskname);
  1358. regfree(&Pattern);
  1359. return res;
  1360. }
  1361. // DoInstall - Install packages from the command line /*{{{*/
  1362. // ---------------------------------------------------------------------
  1363. /* Install named packages */
  1364. bool DoInstall(CommandLine &CmdL)
  1365. {
  1366. CacheFile Cache;
  1367. if (Cache.OpenForInstall() == false ||
  1368. Cache.CheckDeps(CmdL.FileSize() != 1) == false)
  1369. return false;
  1370. // Enter the special broken fixing mode if the user specified arguments
  1371. bool BrokenFix = false;
  1372. if (Cache->BrokenCount() != 0)
  1373. BrokenFix = true;
  1374. unsigned int AutoMarkChanged = 0;
  1375. unsigned int ExpectedInst = 0;
  1376. unsigned int Packages = 0;
  1377. pkgProblemResolver Fix(Cache);
  1378. bool DefRemove = false;
  1379. if (strcasecmp(CmdL.FileList[0],"remove") == 0)
  1380. DefRemove = true;
  1381. else if (strcasecmp(CmdL.FileList[0], "purge") == 0)
  1382. {
  1383. _config->Set("APT::Get::Purge", true);
  1384. DefRemove = true;
  1385. }
  1386. else if (strcasecmp(CmdL.FileList[0], "autoremove") == 0)
  1387. {
  1388. _config->Set("APT::Get::AutomaticRemove", "true");
  1389. DefRemove = true;
  1390. }
  1391. // new scope for the ActionGroup
  1392. {
  1393. pkgDepCache::ActionGroup group(Cache);
  1394. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  1395. {
  1396. // Duplicate the string
  1397. unsigned int Length = strlen(*I);
  1398. char S[300];
  1399. if (Length >= sizeof(S))
  1400. continue;
  1401. strcpy(S,*I);
  1402. // See if we are removing and special indicators..
  1403. bool Remove = DefRemove;
  1404. char *VerTag = 0;
  1405. bool VerIsRel = false;
  1406. // this is a task!
  1407. if (Length >= 1 && S[Length - 1] == '^')
  1408. {
  1409. S[--Length] = 0;
  1410. // tasks must always be confirmed
  1411. ExpectedInst += 1000;
  1412. // see if we can install it
  1413. TryInstallTask(Cache, Fix, BrokenFix, ExpectedInst, S, Remove);
  1414. continue;
  1415. }
  1416. while (Cache->FindPkg(S).end() == true)
  1417. {
  1418. // Handle an optional end tag indicating what to do
  1419. if (Length >= 1 && S[Length - 1] == '-')
  1420. {
  1421. Remove = true;
  1422. S[--Length] = 0;
  1423. continue;
  1424. }
  1425. if (Length >= 1 && S[Length - 1] == '+')
  1426. {
  1427. Remove = false;
  1428. S[--Length] = 0;
  1429. continue;
  1430. }
  1431. char *Slash = strchr(S,'=');
  1432. if (Slash != 0)
  1433. {
  1434. VerIsRel = false;
  1435. *Slash = 0;
  1436. VerTag = Slash + 1;
  1437. }
  1438. Slash = strchr(S,'/');
  1439. if (Slash != 0)
  1440. {
  1441. VerIsRel = true;
  1442. *Slash = 0;
  1443. VerTag = Slash + 1;
  1444. }
  1445. break;
  1446. }
  1447. // Locate the package
  1448. pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
  1449. Packages++;
  1450. if (Pkg.end() == true)
  1451. {
  1452. // Check if the name is a regex
  1453. const char *I;
  1454. for (I = S; *I != 0; I++)
  1455. if (*I == '?' || *I == '*' || *I == '|' ||
  1456. *I == '[' || *I == '^' || *I == '$')
  1457. break;
  1458. if (*I == 0)
  1459. return _error->Error(_("Couldn't find package %s"),S);
  1460. // Regexs must always be confirmed
  1461. ExpectedInst += 1000;
  1462. // Compile the regex pattern
  1463. regex_t Pattern;
  1464. int Res;
  1465. if ((Res = regcomp(&Pattern,S,REG_EXTENDED | REG_ICASE |
  1466. REG_NOSUB)) != 0)
  1467. {
  1468. char Error[300];
  1469. regerror(Res,&Pattern,Error,sizeof(Error));
  1470. return _error->Error(_("Regex compilation error - %s"),Error);
  1471. }
  1472. // Run over the matches
  1473. bool Hit = false;
  1474. for (Pkg = Cache->PkgBegin(); Pkg.end() == false; Pkg++)
  1475. {
  1476. if (regexec(&Pattern,Pkg.Name(),0,0,0) != 0)
  1477. continue;
  1478. ioprintf(c1out,_("Note, selecting %s for regex '%s'\n"),
  1479. Pkg.Name(),S);
  1480. if (VerTag != 0)
  1481. if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
  1482. return false;
  1483. Hit |= TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,
  1484. ExpectedInst,false);
  1485. }
  1486. regfree(&Pattern);
  1487. if (Hit == false)
  1488. return _error->Error(_("Couldn't find package %s"),S);
  1489. }
  1490. else
  1491. {
  1492. if (VerTag != 0)
  1493. if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
  1494. return false;
  1495. if (TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,ExpectedInst) == false)
  1496. return false;
  1497. // see if we need to fix the auto-mark flag
  1498. // e.g. apt-get install foo
  1499. // where foo is marked automatic
  1500. if(!Remove &&
  1501. Cache[Pkg].Install() == false &&
  1502. (Cache[Pkg].Flags & pkgCache::Flag::Auto) &&
  1503. _config->FindB("APT::Get::ReInstall",false) == false)
  1504. {
  1505. ioprintf(c1out,_("%s set to manually installed.\n"),
  1506. Pkg.Name());
  1507. Cache->MarkAuto(Pkg,false);
  1508. AutoMarkChanged++;
  1509. }
  1510. }
  1511. }
  1512. /* If we are in the Broken fixing mode we do not attempt to fix the
  1513. problems. This is if the user invoked install without -f and gave
  1514. packages */
  1515. if (BrokenFix == true && Cache->BrokenCount() != 0)
  1516. {
  1517. c1out << _("You might want to run `apt-get -f install' to correct these:") << endl;
  1518. ShowBroken(c1out,Cache,false);
  1519. return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
  1520. }
  1521. // Call the scored problem resolver
  1522. Fix.InstallProtect();
  1523. if (Fix.Resolve(true) == false)
  1524. _error->Discard();
  1525. // Now we check the state of the packages,
  1526. if (Cache->BrokenCount() != 0)
  1527. {
  1528. c1out <<
  1529. _("Some packages could not be installed. This may mean that you have\n"
  1530. "requested an impossible situation or if you are using the unstable\n"
  1531. "distribution that some required packages have not yet been created\n"
  1532. "or been moved out of Incoming.") << endl;
  1533. if (Packages == 1)
  1534. {
  1535. c1out << endl;
  1536. c1out <<
  1537. _("Since you only requested a single operation it is extremely likely that\n"
  1538. "the package is simply not installable and a bug report against\n"
  1539. "that package should be filed.") << endl;
  1540. }
  1541. c1out << _("The following information may help to resolve the situation:") << endl;
  1542. c1out << endl;
  1543. ShowBroken(c1out,Cache,false);
  1544. return _error->Error(_("Broken packages"));
  1545. }
  1546. }
  1547. if (!DoAutomaticRemove(Cache))
  1548. return false;
  1549. /* Print out a list of packages that are going to be installed extra
  1550. to what the user asked */
  1551. if (Cache->InstCount() != ExpectedInst)
  1552. {
  1553. string List;
  1554. string VersionsList;
  1555. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  1556. {
  1557. pkgCache::PkgIterator I(Cache,Cache.List[J]);
  1558. if ((*Cache)[I].Install() == false)
  1559. continue;
  1560. const char **J;
  1561. for (J = CmdL.FileList + 1; *J != 0; J++)
  1562. if (strcmp(*J,I.Name()) == 0)
  1563. break;
  1564. if (*J == 0) {
  1565. List += string(I.Name()) + " ";
  1566. VersionsList += string(Cache[I].CandVersion) + "\n";
  1567. }
  1568. }
  1569. ShowList(c1out,_("The following extra packages will be installed:"),List,VersionsList);
  1570. }
  1571. /* Print out a list of suggested and recommended packages */
  1572. {
  1573. string SuggestsList, RecommendsList, List;
  1574. string SuggestsVersions, RecommendsVersions;
  1575. for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
  1576. {
  1577. pkgCache::PkgIterator Pkg(Cache,Cache.List[J]);
  1578. /* Just look at the ones we want to install */
  1579. if ((*Cache)[Pkg].Install() == false)
  1580. continue;
  1581. // get the recommends/suggests for the candidate ver
  1582. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  1583. for (pkgCache::DepIterator D = CV.DependsList(); D.end() == false; )
  1584. {
  1585. pkgCache::DepIterator Start;
  1586. pkgCache::DepIterator End;
  1587. D.GlobOr(Start,End); // advances D
  1588. // FIXME: we really should display a or-group as a or-group to the user
  1589. // the problem is that ShowList is incapable of doing this
  1590. string RecommendsOrList,RecommendsOrVersions;
  1591. string SuggestsOrList,SuggestsOrVersions;
  1592. bool foundInstalledInOrGroup = false;
  1593. for(;;)
  1594. {
  1595. /* Skip if package is installed already, or is about to be */
  1596. string target = string(Start.TargetPkg().Name()) + " ";
  1597. if ((*Start.TargetPkg()).SelectedState == pkgCache::State::Install
  1598. || Cache[Start.TargetPkg()].Install())
  1599. {
  1600. foundInstalledInOrGroup=true;
  1601. break;
  1602. }
  1603. /* Skip if we already saw it */
  1604. if (int(SuggestsList.find(target)) != -1 || int(RecommendsList.find(target)) != -1)
  1605. {
  1606. foundInstalledInOrGroup=true;
  1607. break;
  1608. }
  1609. // this is a dep on a virtual pkg, check if any package that provides it
  1610. // should be installed
  1611. if(Start.TargetPkg().ProvidesList() != 0)
  1612. {
  1613. pkgCache::PrvIterator I = Start.TargetPkg().ProvidesList();
  1614. for (; I.end() == false; I++)
  1615. {
  1616. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  1617. if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer() &&
  1618. Pkg.CurrentVer() != 0)
  1619. foundInstalledInOrGroup=true;
  1620. }
  1621. }
  1622. if (Start->Type == pkgCache::Dep::Suggests)
  1623. {
  1624. SuggestsOrList += target;
  1625. SuggestsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
  1626. }
  1627. if (Start->Type == pkgCache::Dep::Recommends)
  1628. {
  1629. RecommendsOrList += target;
  1630. RecommendsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
  1631. }
  1632. if (Start >= End)
  1633. break;
  1634. Start++;
  1635. }
  1636. if(foundInstalledInOrGroup == false)
  1637. {
  1638. RecommendsList += RecommendsOrList;
  1639. RecommendsVersions += RecommendsOrVersions;
  1640. SuggestsList += SuggestsOrList;
  1641. SuggestsVersions += SuggestsOrVersions;
  1642. }
  1643. }
  1644. }
  1645. ShowList(c1out,_("Suggested packages:"),SuggestsList,SuggestsVersions);
  1646. ShowList(c1out,_("Recommended packages:"),RecommendsList,RecommendsVersions);
  1647. }
  1648. // if nothing changed in the cache, but only the automark information
  1649. // we write the StateFile here, otherwise it will be written in
  1650. // cache.commit()
  1651. if (AutoMarkChanged > 0 &&
  1652. Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
  1653. Cache->BadCount() == 0)
  1654. Cache->writeStateFile(NULL);
  1655. // See if we need to prompt
  1656. if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
  1657. return InstallPackages(Cache,false,false);
  1658. return InstallPackages(Cache,false);
  1659. }
  1660. /*}}}*/
  1661. // DoDistUpgrade - Automatic smart upgrader /*{{{*/
  1662. // ---------------------------------------------------------------------
  1663. /* Intelligent upgrader that will install and remove packages at will */
  1664. bool DoDistUpgrade(CommandLine &CmdL)
  1665. {
  1666. CacheFile Cache;
  1667. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1668. return false;
  1669. c0out << _("Calculating upgrade... ") << flush;
  1670. if (pkgDistUpgrade(*Cache) == false)
  1671. {
  1672. c0out << _("Failed") << endl;
  1673. ShowBroken(c1out,Cache,false);
  1674. return false;
  1675. }
  1676. c0out << _("Done") << endl;
  1677. return InstallPackages(Cache,true);
  1678. }
  1679. /*}}}*/
  1680. // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
  1681. // ---------------------------------------------------------------------
  1682. /* Follows dselect's selections */
  1683. bool DoDSelectUpgrade(CommandLine &CmdL)
  1684. {
  1685. CacheFile Cache;
  1686. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  1687. return false;
  1688. pkgDepCache::ActionGroup group(Cache);
  1689. // Install everything with the install flag set
  1690. pkgCache::PkgIterator I = Cache->PkgBegin();
  1691. for (;I.end() != true; I++)
  1692. {
  1693. /* Install the package only if it is a new install, the autoupgrader
  1694. will deal with the rest */
  1695. if (I->SelectedState == pkgCache::State::Install)
  1696. Cache->MarkInstall(I,false);
  1697. }
  1698. /* Now install their deps too, if we do this above then order of
  1699. the status file is significant for | groups */
  1700. for (I = Cache->PkgBegin();I.end() != true; I++)
  1701. {
  1702. /* Install the package only if it is a new install, the autoupgrader
  1703. will deal with the rest */
  1704. if (I->SelectedState == pkgCache::State::Install)
  1705. Cache->MarkInstall(I,true);
  1706. }
  1707. // Apply erasures now, they override everything else.
  1708. for (I = Cache->PkgBegin();I.end() != true; I++)
  1709. {
  1710. // Remove packages
  1711. if (I->SelectedState == pkgCache::State::DeInstall ||
  1712. I->SelectedState == pkgCache::State::Purge)
  1713. Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
  1714. }
  1715. /* Resolve any problems that dselect created, allupgrade cannot handle
  1716. such things. We do so quite agressively too.. */
  1717. if (Cache->BrokenCount() != 0)
  1718. {
  1719. pkgProblemResolver Fix(Cache);
  1720. // Hold back held packages.
  1721. if (_config->FindB("APT::Ignore-Hold",false) == false)
  1722. {
  1723. for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
  1724. {
  1725. if (I->SelectedState == pkgCache::State::Hold)
  1726. {
  1727. Fix.Protect(I);
  1728. Cache->MarkKeep(I);
  1729. }
  1730. }
  1731. }
  1732. if (Fix.Resolve() == false)
  1733. {
  1734. ShowBroken(c1out,Cache,false);
  1735. return _error->Error(_("Internal error, problem resolver broke stuff"));
  1736. }
  1737. }
  1738. // Now upgrade everything
  1739. if (pkgAllUpgrade(Cache) == false)
  1740. {
  1741. ShowBroken(c1out,Cache,false);
  1742. return _error->Error(_("Internal error, problem resolver broke stuff"));
  1743. }
  1744. return InstallPackages(Cache,false);
  1745. }
  1746. /*}}}*/
  1747. // DoClean - Remove download archives /*{{{*/
  1748. // ---------------------------------------------------------------------
  1749. /* */
  1750. bool DoClean(CommandLine &CmdL)
  1751. {
  1752. if (_config->FindB("APT::Get::Simulate") == true)
  1753. {
  1754. cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
  1755. _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
  1756. return true;
  1757. }
  1758. // Lock the archive directory
  1759. FileFd Lock;
  1760. if (_config->FindB("Debug::NoLocking",false) == false)
  1761. {
  1762. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1763. if (_error->PendingError() == true)
  1764. return _error->Error(_("Unable to lock the download directory"));
  1765. }
  1766. pkgAcquire Fetcher;
  1767. Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
  1768. Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
  1769. return true;
  1770. }
  1771. /*}}}*/
  1772. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  1773. // ---------------------------------------------------------------------
  1774. /* This is similar to clean but it only purges things that cannot be
  1775. downloaded, that is old versions of cached packages. */
  1776. class LogCleaner : public pkgArchiveCleaner
  1777. {
  1778. protected:
  1779. virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
  1780. {
  1781. c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
  1782. if (_config->FindB("APT::Get::Simulate") == false)
  1783. unlink(File);
  1784. };
  1785. };
  1786. bool DoAutoClean(CommandLine &CmdL)
  1787. {
  1788. // Lock the archive directory
  1789. FileFd Lock;
  1790. if (_config->FindB("Debug::NoLocking",false) == false)
  1791. {
  1792. Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
  1793. if (_error->PendingError() == true)
  1794. return _error->Error(_("Unable to lock the download directory"));
  1795. }
  1796. CacheFile Cache;
  1797. if (Cache.Open() == false)
  1798. return false;
  1799. LogCleaner Cleaner;
  1800. return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
  1801. Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
  1802. }
  1803. /*}}}*/
  1804. // DoCheck - Perform the check operation /*{{{*/
  1805. // ---------------------------------------------------------------------
  1806. /* Opening automatically checks the system, this command is mostly used
  1807. for debugging */
  1808. bool DoCheck(CommandLine &CmdL)
  1809. {
  1810. CacheFile Cache;
  1811. Cache.Open();
  1812. Cache.CheckDeps();
  1813. return true;
  1814. }
  1815. /*}}}*/
  1816. // DoSource - Fetch a source archive /*{{{*/
  1817. // ---------------------------------------------------------------------
  1818. /* Fetch souce packages */
  1819. struct DscFile
  1820. {
  1821. string Package;
  1822. string Version;
  1823. string Dsc;
  1824. };
  1825. bool DoSource(CommandLine &CmdL)
  1826. {
  1827. CacheFile Cache;
  1828. if (Cache.Open(false) == false)
  1829. return false;
  1830. if (CmdL.FileSize() <= 1)
  1831. return _error->Error(_("Must specify at least one package to fetch source for"));
  1832. // Read the source list
  1833. pkgSourceList List;
  1834. if (List.ReadMainList() == false)
  1835. return _error->Error(_("The list of sources could not be read."));
  1836. // Create the text record parsers
  1837. pkgRecords Recs(Cache);
  1838. pkgSrcRecords SrcRecs(List);
  1839. if (_error->PendingError() == true)
  1840. return false;
  1841. // Create the download object
  1842. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  1843. pkgAcquire Fetcher(&Stat);
  1844. DscFile *Dsc = new DscFile[CmdL.FileSize()];
  1845. // insert all downloaded uris into this set to avoid downloading them
  1846. // twice
  1847. set<string> queued;
  1848. // Load the requestd sources into the fetcher
  1849. unsigned J = 0;
  1850. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  1851. {
  1852. string Src;
  1853. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  1854. if (Last == 0)
  1855. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  1856. // Back track
  1857. vector<pkgSrcRecords::File> Lst;
  1858. if (Last->Files(Lst) == false)
  1859. return false;
  1860. // Load them into the fetcher
  1861. for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
  1862. I != Lst.end(); I++)
  1863. {
  1864. // Try to guess what sort of file it is we are getting.
  1865. if (I->Type == "dsc")
  1866. {
  1867. Dsc[J].Package = Last->Package();
  1868. Dsc[J].Version = Last->Version();
  1869. Dsc[J].Dsc = flNotDir(I->Path);
  1870. }
  1871. // Diff only mode only fetches .diff files
  1872. if (_config->FindB("APT::Get::Diff-Only",false) == true &&
  1873. I->Type != "diff")
  1874. continue;
  1875. // Tar only mode only fetches .tar files
  1876. if (_config->FindB("APT::Get::Tar-Only",false) == true &&
  1877. I->Type != "tar")
  1878. continue;
  1879. // Dsc only mode only fetches .dsc files
  1880. if (_config->FindB("APT::Get::Dsc-Only",false) == true &&
  1881. I->Type != "dsc")
  1882. continue;
  1883. // don't download the same uri twice (should this be moved to
  1884. // the fetcher interface itself?)
  1885. if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
  1886. continue;
  1887. queued.insert(Last->Index().ArchiveURI(I->Path));
  1888. // check if we have a file with that md5 sum already localy
  1889. if(!I->MD5Hash.empty() && FileExists(flNotDir(I->Path)))
  1890. {
  1891. FileFd Fd(flNotDir(I->Path), FileFd::ReadOnly);
  1892. MD5Summation sum;
  1893. sum.AddFD(Fd.Fd(), Fd.Size());
  1894. Fd.Close();
  1895. if((string)sum.Result() == I->MD5Hash)
  1896. {
  1897. ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
  1898. flNotDir(I->Path).c_str());
  1899. continue;
  1900. }
  1901. }
  1902. new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
  1903. I->MD5Hash,I->Size,
  1904. Last->Index().SourceInfo(*Last,*I),Src);
  1905. }
  1906. }
  1907. // Display statistics
  1908. double FetchBytes = Fetcher.FetchNeeded();
  1909. double FetchPBytes = Fetcher.PartialPresent();
  1910. double DebBytes = Fetcher.TotalNeeded();
  1911. // Check for enough free space
  1912. struct statvfs Buf;
  1913. string OutputDir = ".";
  1914. if (statvfs(OutputDir.c_str(),&Buf) != 0)
  1915. return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
  1916. OutputDir.c_str());
  1917. if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  1918. {
  1919. struct statfs Stat;
  1920. if (statfs(OutputDir.c_str(),&Stat) != 0 ||
  1921. unsigned(Stat.f_type) != RAMFS_MAGIC)
  1922. return _error->Error(_("You don't have enough free space in %s"),
  1923. OutputDir.c_str());
  1924. }
  1925. // Number of bytes
  1926. if (DebBytes != FetchBytes)
  1927. ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
  1928. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  1929. else
  1930. ioprintf(c1out,_("Need to get %sB of source archives.\n"),
  1931. SizeToStr(DebBytes).c_str());
  1932. if (_config->FindB("APT::Get::Simulate",false) == true)
  1933. {
  1934. for (unsigned I = 0; I != J; I++)
  1935. ioprintf(cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
  1936. return true;
  1937. }
  1938. // Just print out the uris an exit if the --print-uris flag was used
  1939. if (_config->FindB("APT::Get::Print-URIs") == true)
  1940. {
  1941. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  1942. for (; I != Fetcher.UriEnd(); I++)
  1943. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  1944. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  1945. return true;
  1946. }
  1947. // Run it
  1948. if (Fetcher.Run() == pkgAcquire::Failed)
  1949. return false;
  1950. // Print error messages
  1951. bool Failed = false;
  1952. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  1953. {
  1954. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  1955. (*I)->Complete == true)
  1956. continue;
  1957. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  1958. (*I)->ErrorText.c_str());
  1959. Failed = true;
  1960. }
  1961. if (Failed == true)
  1962. return _error->Error(_("Failed to fetch some archives."));
  1963. if (_config->FindB("APT::Get::Download-only",false) == true)
  1964. {
  1965. c1out << _("Download complete and in download only mode") << endl;
  1966. return true;
  1967. }
  1968. // Unpack the sources
  1969. pid_t Process = ExecFork();
  1970. if (Process == 0)
  1971. {
  1972. for (unsigned I = 0; I != J; I++)
  1973. {
  1974. string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
  1975. // Diff only mode only fetches .diff files
  1976. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  1977. _config->FindB("APT::Get::Tar-Only",false) == true ||
  1978. Dsc[I].Dsc.empty() == true)
  1979. continue;
  1980. // See if the package is already unpacked
  1981. struct stat Stat;
  1982. if (stat(Dir.c_str(),&Stat) == 0 &&
  1983. S_ISDIR(Stat.st_mode) != 0)
  1984. {
  1985. ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
  1986. Dir.c_str());
  1987. }
  1988. else
  1989. {
  1990. // Call dpkg-source
  1991. char S[500];
  1992. snprintf(S,sizeof(S),"%s -x %s",
  1993. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  1994. Dsc[I].Dsc.c_str());
  1995. if (system(S) != 0)
  1996. {
  1997. fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
  1998. fprintf(stderr,_("Check if the 'dpkg-dev' package is installed.\n"));
  1999. _exit(1);
  2000. }
  2001. }
  2002. // Try to compile it with dpkg-buildpackage
  2003. if (_config->FindB("APT::Get::Compile",false) == true)
  2004. {
  2005. // Call dpkg-buildpackage
  2006. char S[500];
  2007. snprintf(S,sizeof(S),"cd %s && %s %s",
  2008. Dir.c_str(),
  2009. _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
  2010. _config->Find("DPkg::Build-Options","-b -uc").c_str());
  2011. if (system(S) != 0)
  2012. {
  2013. fprintf(stderr,_("Build command '%s' failed.\n"),S);
  2014. _exit(1);
  2015. }
  2016. }
  2017. }
  2018. _exit(0);
  2019. }
  2020. // Wait for the subprocess
  2021. int Status = 0;
  2022. while (waitpid(Process,&Status,0) != Process)
  2023. {
  2024. if (errno == EINTR)
  2025. continue;
  2026. return _error->Errno("waitpid","Couldn't wait for subprocess");
  2027. }
  2028. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  2029. return _error->Error(_("Child process failed"));
  2030. return true;
  2031. }
  2032. /*}}}*/
  2033. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  2034. // ---------------------------------------------------------------------
  2035. /* This function will look at the build depends list of the given source
  2036. package and install the necessary packages to make it true, or fail. */
  2037. bool DoBuildDep(CommandLine &CmdL)
  2038. {
  2039. CacheFile Cache;
  2040. if (Cache.Open(true) == false)
  2041. return false;
  2042. if (CmdL.FileSize() <= 1)
  2043. return _error->Error(_("Must specify at least one package to check builddeps for"));
  2044. // Read the source list
  2045. pkgSourceList List;
  2046. if (List.ReadMainList() == false)
  2047. return _error->Error(_("The list of sources could not be read."));
  2048. // Create the text record parsers
  2049. pkgRecords Recs(Cache);
  2050. pkgSrcRecords SrcRecs(List);
  2051. if (_error->PendingError() == true)
  2052. return false;
  2053. // Create the download object
  2054. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  2055. pkgAcquire Fetcher(&Stat);
  2056. unsigned J = 0;
  2057. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  2058. {
  2059. string Src;
  2060. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
  2061. if (Last == 0)
  2062. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  2063. // Process the build-dependencies
  2064. vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  2065. if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only",false)) == false)
  2066. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  2067. // Also ensure that build-essential packages are present
  2068. Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
  2069. if (Opts)
  2070. Opts = Opts->Child;
  2071. for (; Opts; Opts = Opts->Next)
  2072. {
  2073. if (Opts->Value.empty() == true)
  2074. continue;
  2075. pkgSrcRecords::Parser::BuildDepRec rec;
  2076. rec.Package = Opts->Value;
  2077. rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
  2078. rec.Op = 0;
  2079. BuildDeps.push_back(rec);
  2080. }
  2081. if (BuildDeps.size() == 0)
  2082. {
  2083. ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
  2084. continue;
  2085. }
  2086. // Install the requested packages
  2087. unsigned int ExpectedInst = 0;
  2088. vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
  2089. pkgProblemResolver Fix(Cache);
  2090. bool skipAlternatives = false; // skip remaining alternatives in an or group
  2091. for (D = BuildDeps.begin(); D != BuildDeps.end(); D++)
  2092. {
  2093. bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
  2094. if (skipAlternatives == true)
  2095. {
  2096. if (!hasAlternatives)
  2097. skipAlternatives = false; // end of or group
  2098. continue;
  2099. }
  2100. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  2101. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  2102. {
  2103. pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
  2104. // Build-conflicts on unknown packages are silently ignored
  2105. if (Pkg.end() == true)
  2106. continue;
  2107. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  2108. /*
  2109. * Remove if we have an installed version that satisfies the
  2110. * version criteria
  2111. */
  2112. if (IV.end() == false &&
  2113. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  2114. TryToInstall(Pkg,Cache,Fix,true,false,ExpectedInst);
  2115. }
  2116. else // BuildDep || BuildDepIndep
  2117. {
  2118. pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
  2119. if (_config->FindB("Debug::BuildDeps",false) == true)
  2120. cout << "Looking for " << (*D).Package << "...\n";
  2121. if (Pkg.end() == true)
  2122. {
  2123. if (_config->FindB("Debug::BuildDeps",false) == true)
  2124. cout << " (not found)" << (*D).Package << endl;
  2125. if (hasAlternatives)
  2126. continue;
  2127. return _error->Error(_("%s dependency for %s cannot be satisfied "
  2128. "because the package %s cannot be found"),
  2129. Last->BuildDepType((*D).Type),Src.c_str(),
  2130. (*D).Package.c_str());
  2131. }
  2132. /*
  2133. * if there are alternatives, we've already picked one, so skip
  2134. * the rest
  2135. *
  2136. * TODO: this means that if there's a build-dep on A|B and B is
  2137. * installed, we'll still try to install A; more importantly,
  2138. * if A is currently broken, we cannot go back and try B. To fix
  2139. * this would require we do a Resolve cycle for each package we
  2140. * add to the install list. Ugh
  2141. */
  2142. /*
  2143. * If this is a virtual package, we need to check the list of
  2144. * packages that provide it and see if any of those are
  2145. * installed
  2146. */
  2147. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  2148. for (; Prv.end() != true; Prv++)
  2149. {
  2150. if (_config->FindB("Debug::BuildDeps",false) == true)
  2151. cout << " Checking provider " << Prv.OwnerPkg().Name() << endl;
  2152. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  2153. break;
  2154. }
  2155. // Get installed version and version we are going to install
  2156. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  2157. if ((*D).Version[0] != '\0') {
  2158. // Versioned dependency
  2159. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  2160. for (; CV.end() != true; CV++)
  2161. {
  2162. if (Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  2163. break;
  2164. }
  2165. if (CV.end() == true)
  2166. {
  2167. if (hasAlternatives)
  2168. {
  2169. continue;
  2170. }
  2171. else
  2172. {
  2173. return _error->Error(_("%s dependency for %s cannot be satisfied "
  2174. "because no available versions of package %s "
  2175. "can satisfy version requirements"),
  2176. Last->BuildDepType((*D).Type),Src.c_str(),
  2177. (*D).Package.c_str());
  2178. }
  2179. }
  2180. }
  2181. else
  2182. {
  2183. // Only consider virtual packages if there is no versioned dependency
  2184. if (Prv.end() == false)
  2185. {
  2186. if (_config->FindB("Debug::BuildDeps",false) == true)
  2187. cout << " Is provided by installed package " << Prv.OwnerPkg().Name() << endl;
  2188. skipAlternatives = hasAlternatives;
  2189. continue;
  2190. }
  2191. }
  2192. if (IV.end() == false)
  2193. {
  2194. if (_config->FindB("Debug::BuildDeps",false) == true)
  2195. cout << " Is installed\n";
  2196. if (Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  2197. {
  2198. skipAlternatives = hasAlternatives;
  2199. continue;
  2200. }
  2201. if (_config->FindB("Debug::BuildDeps",false) == true)
  2202. cout << " ...but the installed version doesn't meet the version requirement\n";
  2203. if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
  2204. {
  2205. return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
  2206. Last->BuildDepType((*D).Type),
  2207. Src.c_str(),
  2208. Pkg.Name());
  2209. }
  2210. }
  2211. if (_config->FindB("Debug::BuildDeps",false) == true)
  2212. cout << " Trying to install " << (*D).Package << endl;
  2213. if (TryToInstall(Pkg,Cache,Fix,false,false,ExpectedInst) == true)
  2214. {
  2215. // We successfully installed something; skip remaining alternatives
  2216. skipAlternatives = hasAlternatives;
  2217. continue;
  2218. }
  2219. else if (hasAlternatives)
  2220. {
  2221. if (_config->FindB("Debug::BuildDeps",false) == true)
  2222. cout << " Unsatisfiable, trying alternatives\n";
  2223. continue;
  2224. }
  2225. else
  2226. {
  2227. return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
  2228. Last->BuildDepType((*D).Type),
  2229. Src.c_str(),
  2230. (*D).Package.c_str());
  2231. }
  2232. }
  2233. }
  2234. Fix.InstallProtect();
  2235. if (Fix.Resolve(true) == false)
  2236. _error->Discard();
  2237. // Now we check the state of the packages,
  2238. if (Cache->BrokenCount() != 0)
  2239. return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
  2240. }
  2241. if (InstallPackages(Cache, false, true) == false)
  2242. return _error->Error(_("Failed to process build dependencies"));
  2243. return true;
  2244. }
  2245. /*}}}*/
  2246. // DoMoo - Never Ask, Never Tell /*{{{*/
  2247. // ---------------------------------------------------------------------
  2248. /* */
  2249. bool DoMoo(CommandLine &CmdL)
  2250. {
  2251. cout <<
  2252. " (__) \n"
  2253. " (oo) \n"
  2254. " /------\\/ \n"
  2255. " / | || \n"
  2256. " * /\\---/\\ \n"
  2257. " ~~ ~~ \n"
  2258. "....\"Have you mooed today?\"...\n";
  2259. return true;
  2260. }
  2261. /*}}}*/
  2262. // ShowHelp - Show a help screen /*{{{*/
  2263. // ---------------------------------------------------------------------
  2264. /* */
  2265. bool ShowHelp(CommandLine &CmdL)
  2266. {
  2267. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
  2268. COMMON_ARCH,__DATE__,__TIME__);
  2269. if (_config->FindB("version") == true)
  2270. {
  2271. cout << _("Supported modules:") << endl;
  2272. for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
  2273. {
  2274. pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
  2275. if (_system != 0 && _system->VS == VS)
  2276. cout << '*';
  2277. else
  2278. cout << ' ';
  2279. cout << "Ver: " << VS->Label << endl;
  2280. /* Print out all the packaging systems that will work with
  2281. this VS */
  2282. for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
  2283. {
  2284. pkgSystem *Sys = pkgSystem::GlobalList[J];
  2285. if (_system == Sys)
  2286. cout << '*';
  2287. else
  2288. cout << ' ';
  2289. if (Sys->VS->TestCompatibility(*VS) == true)
  2290. cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
  2291. }
  2292. }
  2293. for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
  2294. {
  2295. pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
  2296. cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
  2297. }
  2298. for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
  2299. {
  2300. pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
  2301. cout << " Idx: " << Type->Label << endl;
  2302. }
  2303. return true;
  2304. }
  2305. cout <<
  2306. _("Usage: apt-get [options] command\n"
  2307. " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
  2308. " apt-get [options] source pkg1 [pkg2 ...]\n"
  2309. "\n"
  2310. "apt-get is a simple command line interface for downloading and\n"
  2311. "installing packages. The most frequently used commands are update\n"
  2312. "and install.\n"
  2313. "\n"
  2314. "Commands:\n"
  2315. " update - Retrieve new lists of packages\n"
  2316. " upgrade - Perform an upgrade\n"
  2317. " install - Install new packages (pkg is libc6 not libc6.deb)\n"
  2318. " remove - Remove packages\n"
  2319. " autoremove - Remove all automatic unused packages\n"
  2320. " purge - Remove and purge packages\n"
  2321. " source - Download source archives\n"
  2322. " build-dep - Configure build-dependencies for source packages\n"
  2323. " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
  2324. " dselect-upgrade - Follow dselect selections\n"
  2325. " clean - Erase downloaded archive files\n"
  2326. " autoclean - Erase old downloaded archive files\n"
  2327. " check - Verify that there are no broken dependencies\n"
  2328. "\n"
  2329. "Options:\n"
  2330. " -h This help text.\n"
  2331. " -q Loggable output - no progress indicator\n"
  2332. " -qq No output except for errors\n"
  2333. " -d Download only - do NOT install or unpack archives\n"
  2334. " -s No-act. Perform ordering simulation\n"
  2335. " -y Assume Yes to all queries and do not prompt\n"
  2336. " -f Attempt to continue if the integrity check fails\n"
  2337. " -m Attempt to continue if archives are unlocatable\n"
  2338. " -u Show a list of upgraded packages as well\n"
  2339. " -b Build the source package after fetching it\n"
  2340. " -V Show verbose version numbers\n"
  2341. " -c=? Read this configuration file\n"
  2342. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  2343. "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
  2344. "pages for more information and options.\n"
  2345. " This APT has Super Cow Powers.\n");
  2346. return true;
  2347. }
  2348. /*}}}*/
  2349. // GetInitialize - Initialize things for apt-get /*{{{*/
  2350. // ---------------------------------------------------------------------
  2351. /* */
  2352. void GetInitialize()
  2353. {
  2354. _config->Set("quiet",0);
  2355. _config->Set("help",false);
  2356. _config->Set("APT::Get::Download-Only",false);
  2357. _config->Set("APT::Get::Simulate",false);
  2358. _config->Set("APT::Get::Assume-Yes",false);
  2359. _config->Set("APT::Get::Fix-Broken",false);
  2360. _config->Set("APT::Get::Force-Yes",false);
  2361. _config->Set("APT::Get::List-Cleanup",true);
  2362. _config->Set("APT::Get::AutomaticRemove",false);
  2363. }
  2364. /*}}}*/
  2365. // SigWinch - Window size change signal handler /*{{{*/
  2366. // ---------------------------------------------------------------------
  2367. /* */
  2368. void SigWinch(int)
  2369. {
  2370. // Riped from GNU ls
  2371. #ifdef TIOCGWINSZ
  2372. struct winsize ws;
  2373. if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
  2374. ScreenWidth = ws.ws_col - 1;
  2375. #endif
  2376. }
  2377. /*}}}*/
  2378. int main(int argc,const char *argv[])
  2379. {
  2380. CommandLine::Args Args[] = {
  2381. {'h',"help","help",0},
  2382. {'v',"version","version",0},
  2383. {'V',"verbose-versions","APT::Get::Show-Versions",0},
  2384. {'q',"quiet","quiet",CommandLine::IntLevel},
  2385. {'q',"silent","quiet",CommandLine::IntLevel},
  2386. {'d',"download-only","APT::Get::Download-Only",0},
  2387. {'b',"compile","APT::Get::Compile",0},
  2388. {'b',"build","APT::Get::Compile",0},
  2389. {'s',"simulate","APT::Get::Simulate",0},
  2390. {'s',"just-print","APT::Get::Simulate",0},
  2391. {'s',"recon","APT::Get::Simulate",0},
  2392. {'s',"dry-run","APT::Get::Simulate",0},
  2393. {'s',"no-act","APT::Get::Simulate",0},
  2394. {'y',"yes","APT::Get::Assume-Yes",0},
  2395. {'y',"assume-yes","APT::Get::Assume-Yes",0},
  2396. {'f',"fix-broken","APT::Get::Fix-Broken",0},
  2397. {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
  2398. {'m',"ignore-missing","APT::Get::Fix-Missing",0},
  2399. {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
  2400. {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
  2401. {0,"download","APT::Get::Download",0},
  2402. {0,"fix-missing","APT::Get::Fix-Missing",0},
  2403. {0,"ignore-hold","APT::Ignore-Hold",0},
  2404. {0,"upgrade","APT::Get::upgrade",0},
  2405. {0,"force-yes","APT::Get::force-yes",0},
  2406. {0,"print-uris","APT::Get::Print-URIs",0},
  2407. {0,"diff-only","APT::Get::Diff-Only",0},
  2408. {0,"tar-only","APT::Get::Tar-Only",0},
  2409. {0,"dsc-only","APT::Get::Dsc-Only",0},
  2410. {0,"purge","APT::Get::Purge",0},
  2411. {0,"list-cleanup","APT::Get::List-Cleanup",0},
  2412. {0,"reinstall","APT::Get::ReInstall",0},
  2413. {0,"trivial-only","APT::Get::Trivial-Only",0},
  2414. {0,"remove","APT::Get::Remove",0},
  2415. {0,"only-source","APT::Get::Only-Source",0},
  2416. {0,"arch-only","APT::Get::Arch-Only",0},
  2417. {0,"auto-remove","APT::Get::AutomaticRemove",0},
  2418. {0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0},
  2419. {0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean},
  2420. {0,"fix-policy","APT::Get::Fix-Policy-Broken",0},
  2421. {'c',"config-file",0,CommandLine::ConfigFile},
  2422. {'o',"option",0,CommandLine::ArbItem},
  2423. {0,0,0,0}};
  2424. CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
  2425. {"upgrade",&DoUpgrade},
  2426. {"install",&DoInstall},
  2427. {"remove",&DoInstall},
  2428. {"purge",&DoInstall},
  2429. {"autoremove",&DoInstall},
  2430. {"purge",&DoInstall},
  2431. {"dist-upgrade",&DoDistUpgrade},
  2432. {"dselect-upgrade",&DoDSelectUpgrade},
  2433. {"build-dep",&DoBuildDep},
  2434. {"clean",&DoClean},
  2435. {"autoclean",&DoAutoClean},
  2436. {"check",&DoCheck},
  2437. {"source",&DoSource},
  2438. {"moo",&DoMoo},
  2439. {"help",&ShowHelp},
  2440. {0,0}};
  2441. // Set up gettext support
  2442. setlocale(LC_ALL,"");
  2443. textdomain(PACKAGE);
  2444. // Parse the command line and initialize the package library
  2445. CommandLine CmdL(Args,_config);
  2446. if (pkgInitConfig(*_config) == false ||
  2447. CmdL.Parse(argc,argv) == false ||
  2448. pkgInitSystem(*_config,_system) == false)
  2449. {
  2450. if (_config->FindB("version") == true)
  2451. ShowHelp(CmdL);
  2452. _error->DumpErrors();
  2453. return 100;
  2454. }
  2455. // See if the help should be shown
  2456. if (_config->FindB("help") == true ||
  2457. _config->FindB("version") == true ||
  2458. CmdL.FileSize() == 0)
  2459. {
  2460. ShowHelp(CmdL);
  2461. return 0;
  2462. }
  2463. // Deal with stdout not being a tty
  2464. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
  2465. _config->Set("quiet","1");
  2466. // Setup the output streams
  2467. c0out.rdbuf(cout.rdbuf());
  2468. c1out.rdbuf(cout.rdbuf());
  2469. c2out.rdbuf(cout.rdbuf());
  2470. if (_config->FindI("quiet",0) > 0)
  2471. c0out.rdbuf(devnull.rdbuf());
  2472. if (_config->FindI("quiet",0) > 1)
  2473. c1out.rdbuf(devnull.rdbuf());
  2474. // Setup the signals
  2475. signal(SIGPIPE,SIG_IGN);
  2476. signal(SIGWINCH,SigWinch);
  2477. SigWinch(0);
  2478. // Match the operation
  2479. CmdL.DispatchArg(Cmds);
  2480. // Print any errors or warnings found during parsing
  2481. if (_error->empty() == false)
  2482. {
  2483. bool Errors = _error->PendingError();
  2484. _error->DumpErrors();
  2485. return Errors == true?100:0;
  2486. }
  2487. return 0;
  2488. }