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.
 
 
 
 
 
 

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