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