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