More solaris support
[ntk/apt.git] / cmdline / apt-get.cc
CommitLineData
5ec427c2
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
a3eaf954 3// $Id: apt-get.cc,v 1.92 1999/12/09 05:22:33 jgg Exp $
5ec427c2
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 /*{{{*/
0a8e3465
AL
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>
0a8e3465 33#include <apt-pkg/algorithms.h>
0919e3f9 34#include <apt-pkg/acquire-item.h>
03e39e59 35#include <apt-pkg/dpkgpm.h>
cdcc6d34 36#include <apt-pkg/strutl.h>
1bc849af 37#include <apt-pkg/clean.h>
36375005
AL
38#include <apt-pkg/srcrecords.h>
39#include <apt-pkg/version.h>
2d11135a 40#include <apt-pkg/cachefile.h>
0a8e3465
AL
41
42#include <config.h>
43
0919e3f9
AL
44#include "acqprogress.h"
45
0a8e3465 46#include <fstream.h>
d7827aca
AL
47#include <termios.h>
48#include <sys/ioctl.h>
1bc849af 49#include <sys/stat.h>
138d4b3d 50#include <sys/vfs.h>
d7827aca 51#include <signal.h>
65a1e968 52#include <unistd.h>
3e3221ba 53#include <stdio.h>
d6e79b75 54#include <errno.h>
c373c37a 55#include <regex.h>
54676e1a 56#include <sys/wait.h>
0a8e3465
AL
57 /*}}}*/
58
59ostream c0out;
60ostream c1out;
61ostream c2out;
62ofstream devnull("/dev/null");
63unsigned int ScreenWidth = 80;
64
1089ca89
AL
65// class CacheFile - Cover class for some dependency cache functions /*{{{*/
66// ---------------------------------------------------------------------
67/* */
68class CacheFile : public pkgCacheFile
69{
70 static pkgCache *SortCache;
71 static int NameComp(const void *a,const void *b);
72
73 public:
74 pkgCache::Package **List;
75
76 void Sort();
77 bool CheckDeps(bool AllowBroken = false);
78 bool Open(bool WithLock = true)
79 {
80 OpTextProgress Prog(*_config);
81 if (pkgCacheFile::Open(Prog,WithLock) == false)
82 return false;
83 Sort();
84 return true;
85 };
86 CacheFile() : List(0) {};
87};
88 /*}}}*/
89
a6568219
AL
90// YnPrompt - Yes No Prompt. /*{{{*/
91// ---------------------------------------------------------------------
92/* Returns true on a Yes.*/
93bool YnPrompt()
94{
95 if (_config->FindB("APT::Get::Assume-Yes",false) == true)
96 {
738309d6 97 c1out << 'Y' << endl;
a6568219
AL
98 return true;
99 }
100
101 char C = 0;
102 char Jnk = 0;
103 read(STDIN_FILENO,&C,1);
104 while (C != '\n' && Jnk != '\n') read(STDIN_FILENO,&Jnk,1);
105
106 if (!(C == 'Y' || C == 'y' || C == '\n' || C == '\r'))
107 return false;
108 return true;
109}
110 /*}}}*/
6f86c974
AL
111// AnalPrompt - Annoying Yes No Prompt. /*{{{*/
112// ---------------------------------------------------------------------
113/* Returns true on a Yes.*/
114bool AnalPrompt(const char *Text)
115{
116 char Buf[1024];
117 cin.getline(Buf,sizeof(Buf));
118 if (strcmp(Buf,Text) == 0)
119 return true;
120 return false;
121}
122 /*}}}*/
0a8e3465
AL
123// ShowList - Show a list /*{{{*/
124// ---------------------------------------------------------------------
125/* This prints out a string of space seperated words with a title and
126 a two space indent line wraped to the current screen width. */
83d89a9f 127bool ShowList(ostream &out,string Title,string List)
0a8e3465
AL
128{
129 if (List.empty() == true)
83d89a9f 130 return true;
0a8e3465
AL
131
132 // Acount for the leading space
133 int ScreenWidth = ::ScreenWidth - 3;
134
135 out << Title << endl;
136 string::size_type Start = 0;
137 while (Start < List.size())
138 {
139 string::size_type End;
140 if (Start + ScreenWidth >= List.size())
141 End = List.size();
142 else
143 End = List.rfind(' ',Start+ScreenWidth);
144
145 if (End == string::npos || End < Start)
146 End = Start + ScreenWidth;
147 out << " " << string(List,Start,End - Start) << endl;
148 Start = End + 1;
149 }
83d89a9f 150 return false;
0a8e3465
AL
151}
152 /*}}}*/
153// ShowBroken - Debugging aide /*{{{*/
154// ---------------------------------------------------------------------
155/* This prints out the names of all the packages that are broken along
156 with the name of each each broken dependency and a quite version
157 description. */
421c8d10 158void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
0a8e3465 159{
30e1eab5 160 out << "Sorry, but the following packages have unmet dependencies:" << endl;
1089ca89 161 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 162 {
1089ca89
AL
163 pkgCache::PkgIterator I(Cache,Cache.List[J]);
164
303a1703
AL
165 if (Cache[I].InstBroken() == false)
166 continue;
167
168 // Print out each package and the failed dependencies
169 out <<" " << I.Name() << ":";
648e3cb4 170 unsigned Indent = strlen(I.Name()) + 3;
303a1703
AL
171 bool First = true;
172 if (Cache[I].InstVerIter(Cache).end() == true)
0a8e3465 173 {
303a1703
AL
174 cout << endl;
175 continue;
176 }
177
30e1eab5 178 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
303a1703 179 {
30e1eab5
AL
180 // Compute a single dependency element (glob or)
181 pkgCache::DepIterator Start;
182 pkgCache::DepIterator End;
183 D.GlobOr(Start,End);
76fbce56 184
1089ca89 185 if (Cache->IsImportantDep(End) == false ||
30e1eab5 186 (Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
303a1703 187 continue;
cc718e9a 188
648e3cb4
AL
189 bool FirstOr = true;
190 while (1)
0a8e3465 191 {
648e3cb4
AL
192 if (First == false)
193 for (unsigned J = 0; J != Indent; J++)
194 out << ' ';
195 First = false;
196
197 if (FirstOr == false)
198 {
199 for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
200 out << ' ';
201 }
0a8e3465 202 else
648e3cb4
AL
203 out << ' ' << End.DepType() << ": ";
204 FirstOr = false;
205
206 out << Start.TargetPkg().Name();
207
208 // Show a quick summary of the version requirements
209 if (Start.TargetVer() != 0)
210 out << " (" << Start.CompType() << " " << Start.TargetVer() <<
211 ")";
212
213 /* Show a summary of the target package if possible. In the case
214 of virtual packages we show nothing */
215 pkgCache::PkgIterator Targ = Start.TargetPkg();
216 if (Targ->ProvidesList == 0)
7e798dd7 217 {
648e3cb4
AL
218 out << " but ";
219 pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
220 if (Ver.end() == false)
221 out << Ver.VerStr() << (Now?" is installed":" is to be installed");
222 else
303a1703 223 {
648e3cb4
AL
224 if (Cache[Targ].CandidateVerIter(Cache).end() == true)
225 {
226 if (Targ->ProvidesList == 0)
227 out << "it is not installable";
228 else
229 out << "it is a virtual package";
230 }
303a1703 231 else
648e3cb4
AL
232 out << (Now?"it is not installed":"it is not going to be installed");
233 }
234 }
235
236 if (Start != End)
237 cout << " or";
238 out << endl;
239
240 if (Start == End)
241 break;
242 Start++;
243 }
303a1703 244 }
0a8e3465
AL
245 }
246}
247 /*}}}*/
248// ShowNew - Show packages to newly install /*{{{*/
249// ---------------------------------------------------------------------
250/* */
1089ca89 251void ShowNew(ostream &out,CacheFile &Cache)
0a8e3465
AL
252{
253 /* Print out a list of packages that are going to be removed extra
254 to what the user asked */
0a8e3465 255 string List;
1089ca89
AL
256 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
257 {
258 pkgCache::PkgIterator I(Cache,Cache.List[J]);
259 if (Cache[I].NewInstall() == true)
0a8e3465 260 List += string(I.Name()) + " ";
1089ca89
AL
261 }
262
0a8e3465
AL
263 ShowList(out,"The following NEW packages will be installed:",List);
264}
265 /*}}}*/
266// ShowDel - Show packages to delete /*{{{*/
267// ---------------------------------------------------------------------
268/* */
1089ca89 269void ShowDel(ostream &out,CacheFile &Cache)
0a8e3465
AL
270{
271 /* Print out a list of packages that are going to be removed extra
272 to what the user asked */
0a8e3465 273 string List;
1089ca89 274 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
fc4b5c9f 275 {
1089ca89
AL
276 pkgCache::PkgIterator I(Cache,Cache.List[J]);
277 if (Cache[I].Delete() == true)
fc4b5c9f 278 {
1089ca89 279 if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
fc4b5c9f
AL
280 List += string(I.Name()) + "* ";
281 else
282 List += string(I.Name()) + " ";
283 }
284 }
3d615484 285
0a8e3465
AL
286 ShowList(out,"The following packages will be REMOVED:",List);
287}
288 /*}}}*/
289// ShowKept - Show kept packages /*{{{*/
290// ---------------------------------------------------------------------
291/* */
f292686b 292void ShowKept(ostream &out,CacheFile &Cache)
0a8e3465 293{
0a8e3465 294 string List;
f292686b 295 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 296 {
f292686b
AL
297 pkgCache::PkgIterator I(Cache,Cache.List[J]);
298
0a8e3465 299 // Not interesting
f292686b
AL
300 if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
301 I->CurrentVer == 0 || Cache[I].Delete() == true)
0a8e3465
AL
302 continue;
303
304 List += string(I.Name()) + " ";
305 }
306 ShowList(out,"The following packages have been kept back",List);
307}
308 /*}}}*/
309// ShowUpgraded - Show upgraded packages /*{{{*/
310// ---------------------------------------------------------------------
311/* */
1089ca89 312void ShowUpgraded(ostream &out,CacheFile &Cache)
0a8e3465 313{
0a8e3465 314 string List;
1089ca89 315 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 316 {
1089ca89
AL
317 pkgCache::PkgIterator I(Cache,Cache.List[J]);
318
0a8e3465 319 // Not interesting
1089ca89 320 if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
0a8e3465
AL
321 continue;
322
323 List += string(I.Name()) + " ";
324 }
325 ShowList(out,"The following packages will be upgraded",List);
326}
327 /*}}}*/
328// ShowHold - Show held but changed packages /*{{{*/
329// ---------------------------------------------------------------------
330/* */
1089ca89 331bool ShowHold(ostream &out,CacheFile &Cache)
0a8e3465 332{
0a8e3465 333 string List;
1089ca89 334 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 335 {
1089ca89
AL
336 pkgCache::PkgIterator I(Cache,Cache.List[J]);
337 if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
0a8e3465
AL
338 I->SelectedState == pkgCache::State::Hold)
339 List += string(I.Name()) + " ";
340 }
341
83d89a9f 342 return ShowList(out,"The following held packages will be changed:",List);
0a8e3465
AL
343}
344 /*}}}*/
345// ShowEssential - Show an essential package warning /*{{{*/
346// ---------------------------------------------------------------------
347/* This prints out a warning message that is not to be ignored. It shows
348 all essential packages and their dependents that are to be removed.
349 It is insanely risky to remove the dependents of an essential package! */
1089ca89 350bool ShowEssential(ostream &out,CacheFile &Cache)
0a8e3465 351{
0a8e3465 352 string List;
1089ca89
AL
353 bool *Added = new bool[Cache->HeaderP->PackageCount];
354 for (unsigned int I = 0; I != Cache->HeaderP->PackageCount; I++)
0a8e3465
AL
355 Added[I] = false;
356
1089ca89 357 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 358 {
1089ca89 359 pkgCache::PkgIterator I(Cache,Cache.List[J]);
0a8e3465
AL
360 if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
361 continue;
362
363 // The essential package is being removed
1089ca89 364 if (Cache[I].Delete() == true)
0a8e3465
AL
365 {
366 if (Added[I->ID] == false)
367 {
368 Added[I->ID] = true;
369 List += string(I.Name()) + " ";
370 }
371 }
372
373 if (I->CurrentVer == 0)
374 continue;
375
376 // Print out any essential package depenendents that are to be removed
377 for (pkgDepCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
378 {
3e3221ba
AL
379 // Skip everything but depends
380 if (D->Type != pkgCache::Dep::PreDepends &&
381 D->Type != pkgCache::Dep::Depends)
382 continue;
383
0a8e3465 384 pkgCache::PkgIterator P = D.SmartTargetPkg();
1089ca89 385 if (Cache[P].Delete() == true)
0a8e3465
AL
386 {
387 if (Added[P->ID] == true)
388 continue;
389 Added[P->ID] = true;
3e3221ba
AL
390
391 char S[300];
392 sprintf(S,"%s (due to %s) ",P.Name(),I.Name());
393 List += S;
0a8e3465
AL
394 }
395 }
396 }
397
83d89a9f 398 delete [] Added;
0a8e3465
AL
399 if (List.empty() == false)
400 out << "WARNING: The following essential packages will be removed" << endl;
83d89a9f 401 return ShowList(out,"This should NOT be done unless you know exactly what you are doing!",List);
0a8e3465
AL
402}
403 /*}}}*/
404// Stats - Show some statistics /*{{{*/
405// ---------------------------------------------------------------------
406/* */
407void Stats(ostream &out,pkgDepCache &Dep)
408{
409 unsigned long Upgrade = 0;
410 unsigned long Install = 0;
d0c59649 411 unsigned long ReInstall = 0;
0a8e3465
AL
412 for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
413 {
414 if (Dep[I].NewInstall() == true)
415 Install++;
416 else
417 if (Dep[I].Upgrade() == true)
418 Upgrade++;
d0c59649
AL
419 if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
420 ReInstall++;
0a8e3465
AL
421 }
422
423 out << Upgrade << " packages upgraded, " <<
d0c59649
AL
424 Install << " newly installed, ";
425 if (ReInstall != 0)
426 out << ReInstall << " reinstalled, ";
427 out << Dep.DelCount() << " to remove and " <<
0a8e3465
AL
428 Dep.KeepCount() << " not upgraded." << endl;
429
430 if (Dep.BadCount() != 0)
431 out << Dep.BadCount() << " packages not fully installed or removed." << endl;
432}
433 /*}}}*/
434
1089ca89 435// CacheFile::NameComp - QSort compare by name /*{{{*/
0a8e3465
AL
436// ---------------------------------------------------------------------
437/* */
1089ca89
AL
438pkgCache *CacheFile::SortCache = 0;
439int CacheFile::NameComp(const void *a,const void *b)
0a8e3465 440{
8508b1df
AL
441 if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
442 return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
0a8e3465 443
1089ca89
AL
444 const pkgCache::Package &A = **(pkgCache::Package **)a;
445 const pkgCache::Package &B = **(pkgCache::Package **)b;
446
447 return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
448}
449 /*}}}*/
450// CacheFile::Sort - Sort by name /*{{{*/
451// ---------------------------------------------------------------------
452/* */
453void CacheFile::Sort()
454{
455 delete [] List;
456 List = new pkgCache::Package *[Cache->Head().PackageCount];
457 memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
458 pkgCache::PkgIterator I = Cache->PkgBegin();
459 for (;I.end() != true; I++)
460 List[I->ID] = I;
461
462 SortCache = *this;
463 qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
464}
0a8e3465
AL
465 /*}}}*/
466// CacheFile::Open - Open the cache file /*{{{*/
467// ---------------------------------------------------------------------
468/* This routine generates the caches and then opens the dependency cache
469 and verifies that the system is OK. */
2d11135a 470bool CacheFile::CheckDeps(bool AllowBroken)
0a8e3465 471{
d38b7b3d
AL
472 if (_error->PendingError() == true)
473 return false;
0a8e3465 474
0a8e3465
AL
475 // Check that the system is OK
476 if (Cache->DelCount() != 0 || Cache->InstCount() != 0)
477 return _error->Error("Internal Error, non-zero counts");
478
479 // Apply corrections for half-installed packages
480 if (pkgApplyStatus(*Cache) == false)
481 return false;
482
483 // Nothing is broken
7c57fe64 484 if (Cache->BrokenCount() == 0 || AllowBroken == true)
0a8e3465
AL
485 return true;
486
487 // Attempt to fix broken things
488 if (_config->FindB("APT::Get::Fix-Broken",false) == true)
489 {
490 c1out << "Correcting dependencies..." << flush;
491 if (pkgFixBroken(*Cache) == false || Cache->BrokenCount() != 0)
492 {
493 c1out << " failed." << endl;
421c8d10 494 ShowBroken(c1out,*this,true);
0a8e3465
AL
495
496 return _error->Error("Unable to correct dependencies");
497 }
7e798dd7
AL
498 if (pkgMinimizeUpgrade(*Cache) == false)
499 return _error->Error("Unable to minimize the upgrade set");
0a8e3465
AL
500
501 c1out << " Done" << endl;
502 }
503 else
504 {
505 c1out << "You might want to run `apt-get -f install' to correct these." << endl;
421c8d10 506 ShowBroken(c1out,*this,true);
0a8e3465
AL
507
508 return _error->Error("Unmet dependencies. Try using -f.");
509 }
510
511 return true;
512}
513 /*}}}*/
514
515// InstallPackages - Actually download and install the packages /*{{{*/
516// ---------------------------------------------------------------------
517/* This displays the informative messages describing what is going to
518 happen and then calls the download routines */
a3eaf954
AL
519bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
520 bool Saftey = true)
0a8e3465 521{
fc4b5c9f
AL
522 if (_config->FindB("APT::Get::Purge",false) == true)
523 {
524 pkgCache::PkgIterator I = Cache->PkgBegin();
525 for (; I.end() == false; I++)
d556d1a1
AL
526 {
527 if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
528 Cache->MarkDelete(I,true);
529 }
fc4b5c9f
AL
530 }
531
83d89a9f 532 bool Fail = false;
6f86c974 533 bool Essential = false;
83d89a9f 534
a6568219 535 // Show all the various warning indicators
0a8e3465
AL
536 ShowDel(c1out,Cache);
537 ShowNew(c1out,Cache);
538 if (ShwKept == true)
539 ShowKept(c1out,Cache);
7a215bee 540 Fail |= !ShowHold(c1out,Cache);
0a8e3465
AL
541 if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
542 ShowUpgraded(c1out,Cache);
6f86c974
AL
543 Essential = !ShowEssential(c1out,Cache);
544 Fail |= Essential;
0a8e3465
AL
545 Stats(c1out,Cache);
546
547 // Sanity check
d38b7b3d 548 if (Cache->BrokenCount() != 0)
0a8e3465 549 {
421c8d10 550 ShowBroken(c1out,Cache,false);
0a8e3465
AL
551 return _error->Error("Internal Error, InstallPackages was called with broken packages!");
552 }
553
d0c59649 554 if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
d38b7b3d 555 Cache->BadCount() == 0)
c60d151b 556 return true;
03e39e59 557
d150b09d
AL
558 // No remove flag
559 if (Cache->DelCount() != 0 && _config->FindB("APT::Get::No-Remove",false) == true)
560 return _error->Error("Packages need to be removed but No Remove was specified.");
561
03e39e59
AL
562 // Run the simulator ..
563 if (_config->FindB("APT::Get::Simulate") == true)
564 {
565 pkgSimulate PM(Cache);
281daf46
AL
566 pkgPackageManager::OrderResult Res = PM.DoInstall();
567 if (Res == pkgPackageManager::Failed)
568 return false;
569 if (Res != pkgPackageManager::Completed)
570 return _error->Error("Internal Error, Ordering didn't finish");
571 return true;
03e39e59
AL
572 }
573
574 // Create the text record parser
575 pkgRecords Recs(Cache);
83d89a9f
AL
576 if (_error->PendingError() == true)
577 return false;
578
d38b7b3d 579 // Lock the archive directory
36375005 580 FileFd Lock;
d38b7b3d
AL
581 if (_config->FindB("Debug::NoLocking",false) == false)
582 {
36375005 583 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
d38b7b3d
AL
584 if (_error->PendingError() == true)
585 return _error->Error("Unable to lock the download directory");
586 }
03e39e59
AL
587
588 // Create the download object
589 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
590 pkgAcquire Fetcher(&Stat);
591
592 // Read the source list
593 pkgSourceList List;
594 if (List.ReadMainList() == false)
595 return _error->Error("The list of sources could not be read.");
596
597 // Create the package manager and prepare to download
30e1eab5 598 pkgDPkgPM PM(Cache);
424c3bc0
AL
599 if (PM.GetArchives(&Fetcher,&List,&Recs) == false ||
600 _error->PendingError() == true)
03e39e59
AL
601 return false;
602
7a1b1f8b 603 // Display statistics
a6568219 604 unsigned long FetchBytes = Fetcher.FetchNeeded();
6b1ff003 605 unsigned long FetchPBytes = Fetcher.PartialPresent();
a6568219 606 unsigned long DebBytes = Fetcher.TotalNeeded();
d38b7b3d
AL
607 if (DebBytes != Cache->DebSize())
608 {
609 c0out << DebBytes << ',' << Cache->DebSize() << endl;
a6568219 610 c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl;
d38b7b3d 611 }
138d4b3d 612
7a1b1f8b 613 // Number of bytes
fc9d4b7b 614 c1out << "Need to get ";
a6568219 615 if (DebBytes != FetchBytes)
314037ba 616 c1out << SizeToStr(FetchBytes) << "B/" << SizeToStr(DebBytes) << 'B';
a6568219 617 else
314037ba 618 c1out << SizeToStr(DebBytes) << 'B';
a6568219
AL
619
620 c1out << " of archives. After unpacking ";
10bb1f5f
AL
621
622 // Size delta
623 if (Cache->UsrSize() >= 0)
624 c1out << SizeToStr(Cache->UsrSize()) << "B will be used." << endl;
625 else
626 c1out << SizeToStr(-1*Cache->UsrSize()) << "B will be freed." << endl;
627
628 if (_error->PendingError() == true)
629 return false;
31a0531d
AL
630
631 // Check for enough free space
632 struct statfs Buf;
633 string OutputDir = _config->FindDir("Dir::Cache::Archives");
634 if (statfs(OutputDir.c_str(),&Buf) != 0)
635 return _error->Errno("statfs","Couldn't determine free space in %s",
636 OutputDir.c_str());
637 if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
638 return _error->Error("Sorry, you don't have enough free space in %s to hold all the .debs.",
639 OutputDir.c_str());
a6568219 640
83d89a9f 641 // Fail safe check
0c95c765
AL
642 if (_config->FindI("quiet",0) >= 2 ||
643 _config->FindB("APT::Get::Assume-Yes",false) == true)
83d89a9f
AL
644 {
645 if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
646 return _error->Error("There are problems and -y was used without --force-yes");
647 }
83d89a9f 648
6f86c974
AL
649 if (Essential == true && Saftey == true)
650 {
d150b09d
AL
651 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
652 return _error->Error("Trivial Only specified but this is not a trivial operation.");
653
6f86c974 654 c2out << "You are about to do something potentially harmful" << endl;
36375005 655 c2out << "To continue type in the phrase 'Yes, I understand this may be bad'" << endl;
6f86c974 656 c2out << " ?] " << flush;
36375005 657 if (AnalPrompt("Yes, I understand this may be bad") == false)
6f86c974
AL
658 {
659 c2out << "Abort." << endl;
a6568219 660 exit(1);
6f86c974
AL
661 }
662 }
663 else
d150b09d 664 {
6f86c974 665 // Prompt to continue
38262e68 666 if (Ask == true || Fail == true)
6f86c974 667 {
d150b09d
AL
668 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
669 return _error->Error("Trivial Only specified but this is not a trivial operation.");
670
0c95c765 671 if (_config->FindI("quiet",0) < 2 &&
6f86c974 672 _config->FindB("APT::Get::Assume-Yes",false) == false)
0c95c765 673 {
6f86c974
AL
674 c2out << "Do you want to continue? [Y/n] " << flush;
675
0c95c765
AL
676 if (YnPrompt() == false)
677 {
678 c2out << "Abort." << endl;
679 exit(1);
680 }
681 }
6f86c974
AL
682 }
683 }
684
36375005 685 // Just print out the uris an exit if the --print-uris flag was used
f7a08e33
AL
686 if (_config->FindB("APT::Get::Print-URIs") == true)
687 {
688 pkgAcquire::UriIterator I = Fetcher.UriBegin();
689 for (; I != Fetcher.UriEnd(); I++)
690 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
691 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
692 return true;
693 }
83d89a9f 694
03e39e59 695 // Run it
281daf46 696 while (1)
30e1eab5 697 {
a3eaf954
AL
698 bool Transient = false;
699 if (_config->FindB("APT::Get::No-Download",false) == true)
700 {
701 for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
702 {
703 if ((*I)->Local == true)
704 {
705 I++;
706 continue;
707 }
708
709 // Close the item and check if it was found in cache
710 (*I)->Finished();
711 if ((*I)->Complete == false)
712 Transient = true;
713
714 // Clear it out of the fetch list
715 delete *I;
716 I = Fetcher.ItemsBegin();
717 }
718 }
719
720 if (Fetcher.Run() == pkgAcquire::Failed)
721 return false;
30e1eab5 722
281daf46
AL
723 // Print out errors
724 bool Failed = false;
281daf46 725 for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
f01fe790 726 {
281daf46
AL
727 if ((*I)->Status == pkgAcquire::Item::StatDone &&
728 (*I)->Complete == true)
729 continue;
730
281daf46
AL
731 if ((*I)->Status == pkgAcquire::Item::StatIdle)
732 {
733 Transient = true;
734 // Failed = true;
735 continue;
736 }
a3eaf954 737
281daf46
AL
738 cerr << "Failed to fetch " << (*I)->DescURI() << endl;
739 cerr << " " << (*I)->ErrorText << endl;
f01fe790 740 Failed = true;
f01fe790 741 }
5ec427c2 742
a3eaf954 743 /* If we are in no download mode and missing files and there were
5ec427c2
AL
744 'failures' then the user must specify -m. Furthermore, there
745 is no such thing as a transient error in no-download mode! */
a3eaf954 746 if (Transient == true &&
5ec427c2
AL
747 _config->FindB("APT::Get::No-Download",false) == true)
748 {
749 Transient = false;
750 Failed = true;
751 }
f01fe790 752
281daf46
AL
753 if (_config->FindB("APT::Get::Download-Only",false) == true)
754 {
755 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
756 return _error->Error("Some files failed to download");
757 return true;
758 }
759
8195ae46 760 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
f01fe790 761 {
281daf46 762 return _error->Error("Unable to fetch some archives, maybe try with --fix-missing?");
f01fe790
AL
763 }
764
281daf46
AL
765 if (Transient == true && Failed == true)
766 return _error->Error("--fix-missing and media swapping is not currently supported");
767
768 // Try to deal with missing package files
769 if (Failed == true && PM.FixMissing() == false)
770 {
771 cerr << "Unable to correct missing packages." << endl;
772 return _error->Error("Aborting Install.");
773 }
a3eaf954 774
281daf46
AL
775 Cache.ReleaseLock();
776 pkgPackageManager::OrderResult Res = PM.DoInstall();
777 if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
778 return false;
779 if (Res == pkgPackageManager::Completed)
780 return true;
781
782 // Reload the fetcher object and loop again for media swapping
783 Fetcher.Shutdown();
784 if (PM.GetArchives(&Fetcher,&List,&Recs) == false)
785 return false;
786 }
0a8e3465
AL
787}
788 /*}}}*/
c373c37a
AL
789// TryToInstall - Try to install a single package /*{{{*/
790// ---------------------------------------------------------------------
791/* This used to be inlined in DoInstall, but with the advent of regex package
792 name matching it was split out.. */
793bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
794 pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
795 unsigned int &ExpectedInst,bool AllowFail = true)
796{
797 /* This is a pure virtual package and there is a single available
798 provides */
799 if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0 &&
800 Pkg.ProvidesList()->NextProvides == 0)
801 {
802 pkgCache::PkgIterator Tmp = Pkg.ProvidesList().OwnerPkg();
10bb1f5f 803 c1out << "Note, selecting " << Tmp.Name() << " instead of " << Pkg.Name() << endl;
c373c37a
AL
804 Pkg = Tmp;
805 }
806
807 // Handle the no-upgrade case
808 if (_config->FindB("APT::Get::no-upgrade",false) == true &&
809 Pkg->CurrentVer != 0)
810 {
811 if (AllowFail == true)
812 c1out << "Skipping " << Pkg.Name() << ", it is already installed and no-upgrade is set." << endl;
813 return true;
814 }
815
816 // Check if there is something at all to install
817 pkgDepCache::StateCache &State = Cache[Pkg];
a146c927 818 if (Remove == true && Pkg->CurrentVer == 0)
10bb1f5f
AL
819 {
820 if (AllowFail == false)
821 return false;
a146c927 822 return _error->Error("Package %s is not installed",Pkg.Name());
10bb1f5f 823 }
a146c927
AL
824
825 if (State.CandidateVer == 0 && Remove == false)
c373c37a
AL
826 {
827 if (AllowFail == false)
828 return false;
829
830 if (Pkg->ProvidesList != 0)
831 {
832 c1out << "Package " << Pkg.Name() << " is a virtual package provided by:" << endl;
833
834 pkgCache::PrvIterator I = Pkg.ProvidesList();
835 for (; I.end() == false; I++)
836 {
837 pkgCache::PkgIterator Pkg = I.OwnerPkg();
838
839 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer())
840 {
841 if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
842 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
843 " [Installed]"<< endl;
844 else
845 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
846 }
847 }
848 c1out << "You should explicitly select one to install." << endl;
849 }
850 else
851 {
852 c1out << "Package " << Pkg.Name() << " has no available version, but exists in the database." << endl;
853 c1out << "This typically means that the package was mentioned in a dependency and " << endl;
dca1e241
AL
854 c1out << "never uploaded, has been obsoleted or is not available with the contents " << endl;
855 c1out << "of sources.list" << endl;
c373c37a
AL
856
857 string List;
858 pkgCache::DepIterator Dep = Pkg.RevDependsList();
859 for (; Dep.end() == false; Dep++)
860 {
861 if (Dep->Type != pkgCache::Dep::Replaces)
862 continue;
863 List += string(Dep.ParentPkg().Name()) + " ";
864 }
865 ShowList(c1out,"However the following packages replace it:",List);
866 }
867
868 _error->Error("Package %s has no installation candidate",Pkg.Name());
869 return false;
870 }
61d6a8de
AL
871
872 Fix.Clear(Pkg);
70777d4b 873 Fix.Protect(Pkg);
c373c37a
AL
874 if (Remove == true)
875 {
876 Fix.Remove(Pkg);
877 Cache.MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
878 return true;
879 }
880
881 // Install it
882 Cache.MarkInstall(Pkg,false);
883 if (State.Install() == false)
884 {
d0c59649
AL
885 if (_config->FindB("APT::Get::ReInstall",false) == true)
886 {
887 if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
888 c1out << "Sorry, re-installation of " << Pkg.Name() << " is not possible, it cannot be downloaded" << endl;
889 else
890 Cache.SetReInstall(Pkg,true);
891 }
892 else
893 {
894 if (AllowFail == true)
895 c1out << "Sorry, " << Pkg.Name() << " is already the newest version" << endl;
896 }
c373c37a
AL
897 }
898 else
899 ExpectedInst++;
900
901 // Install it with autoinstalling enabled.
902 if (State.InstBroken() == true && BrokenFix == false)
903 Cache.MarkInstall(Pkg,true);
904 return true;
905}
906 /*}}}*/
0a8e3465
AL
907
908// DoUpdate - Update the package lists /*{{{*/
909// ---------------------------------------------------------------------
910/* */
0919e3f9 911bool DoUpdate(CommandLine &)
0a8e3465 912{
0919e3f9
AL
913 // Get the source list
914 pkgSourceList List;
915 if (List.ReadMainList() == false)
916 return false;
917
d38b7b3d 918 // Lock the list directory
36375005 919 FileFd Lock;
d38b7b3d
AL
920 if (_config->FindB("Debug::NoLocking",false) == false)
921 {
36375005 922 Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
d38b7b3d
AL
923 if (_error->PendingError() == true)
924 return _error->Error("Unable to lock the list directory");
925 }
926
0919e3f9
AL
927 // Create the download object
928 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
929 pkgAcquire Fetcher(&Stat);
930
931 // Populate it with the source selection
932 pkgSourceList::const_iterator I;
933 for (I = List.begin(); I != List.end(); I++)
934 {
935 new pkgAcqIndex(&Fetcher,I);
936 if (_error->PendingError() == true)
937 return false;
938 }
939
940 // Run it
024d1123 941 if (Fetcher.Run() == pkgAcquire::Failed)
0919e3f9
AL
942 return false;
943
9df71a5b
AL
944 bool Failed = false;
945 for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
946 {
947 if ((*I)->Status == pkgAcquire::Item::StatDone)
948 continue;
949
950 (*I)->Finished();
951
eecf9bf7
AL
952 cerr << "Failed to fetch " << (*I)->DescURI() << endl;
953 cerr << " " << (*I)->ErrorText << endl;
9df71a5b
AL
954 Failed = true;
955 }
956
7a7fa5f0 957 // Clean out any old list files
9df71a5b
AL
958 if (_config->FindB("APT::Get::List-Cleanup",false) == false)
959 {
960 if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
961 Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
962 return false;
963 }
7a7fa5f0 964
0919e3f9
AL
965 // Prepare the cache.
966 CacheFile Cache;
a65cbe33 967 if (Cache.Open() == false)
0919e3f9
AL
968 return false;
969
9df71a5b
AL
970 if (Failed == true)
971 return _error->Error("Some index files failed to download, they have been ignored, or old ones used instead.");
0919e3f9 972 return true;
0a8e3465
AL
973}
974 /*}}}*/
975// DoUpgrade - Upgrade all packages /*{{{*/
976// ---------------------------------------------------------------------
977/* Upgrade all packages without installing new packages or erasing old
978 packages */
979bool DoUpgrade(CommandLine &CmdL)
980{
981 CacheFile Cache;
2d11135a 982 if (Cache.Open() == false || Cache.CheckDeps() == false)
0a8e3465
AL
983 return false;
984
985 // Do the upgrade
0a8e3465
AL
986 if (pkgAllUpgrade(Cache) == false)
987 {
421c8d10 988 ShowBroken(c1out,Cache,false);
0a8e3465
AL
989 return _error->Error("Internal Error, AllUpgrade broke stuff");
990 }
991
992 return InstallPackages(Cache,true);
993}
994 /*}}}*/
995// DoInstall - Install packages from the command line /*{{{*/
996// ---------------------------------------------------------------------
997/* Install named packages */
998bool DoInstall(CommandLine &CmdL)
999{
1000 CacheFile Cache;
2d11135a 1001 if (Cache.Open() == false || Cache.CheckDeps(CmdL.FileSize() != 1) == false)
0a8e3465
AL
1002 return false;
1003
7c57fe64
AL
1004 // Enter the special broken fixing mode if the user specified arguments
1005 bool BrokenFix = false;
1006 if (Cache->BrokenCount() != 0)
1007 BrokenFix = true;
1008
a6568219
AL
1009 unsigned int ExpectedInst = 0;
1010 unsigned int Packages = 0;
0a8e3465
AL
1011 pkgProblemResolver Fix(Cache);
1012
303a1703
AL
1013 bool DefRemove = false;
1014 if (strcasecmp(CmdL.FileList[0],"remove") == 0)
1015 DefRemove = true;
1016
0a8e3465
AL
1017 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1018 {
1019 // Duplicate the string
1020 unsigned int Length = strlen(*I);
1021 char S[300];
1022 if (Length >= sizeof(S))
1023 continue;
1024 strcpy(S,*I);
1025
1026 // See if we are removing the package
303a1703 1027 bool Remove = DefRemove;
fc4b5c9f 1028 while (Cache->FindPkg(S).end() == true)
0a8e3465 1029 {
2c3bc8bb
AL
1030 // Handle an optional end tag indicating what to do
1031 if (S[Length - 1] == '-')
1032 {
1033 Remove = true;
1034 S[--Length] = 0;
fc4b5c9f 1035 continue;
2c3bc8bb 1036 }
fc4b5c9f 1037
2c3bc8bb
AL
1038 if (S[Length - 1] == '+')
1039 {
1040 Remove = false;
1041 S[--Length] = 0;
fc4b5c9f 1042 continue;
2c3bc8bb 1043 }
fc4b5c9f 1044 break;
303a1703 1045 }
0a8e3465
AL
1046
1047 // Locate the package
1048 pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
303a1703 1049 Packages++;
0a8e3465 1050 if (Pkg.end() == true)
2c3bc8bb 1051 {
c373c37a
AL
1052 // Check if the name is a regex
1053 const char *I;
1054 for (I = S; *I != 0; I++)
1055 if (*I == '.' || *I == '?' || *I == '*')
1056 break;
1057 if (*I == 0)
1058 return _error->Error("Couldn't find package %s",S);
303a1703 1059
c373c37a
AL
1060 // Regexs must always be confirmed
1061 ExpectedInst += 1000;
1062
1063 // Compile the regex pattern
1064 regex_t Pattern;
1065 if (regcomp(&Pattern,S,REG_EXTENDED | REG_ICASE |
1066 REG_NOSUB) != 0)
1067 return _error->Error("Regex compilation error");
1068
1069 // Run over the matches
1070 bool Hit = false;
1071 for (Pkg = Cache->PkgBegin(); Pkg.end() == false; Pkg++)
303a1703 1072 {
c373c37a
AL
1073 if (regexec(&Pattern,Pkg.Name(),0,0,0) != 0)
1074 continue;
07801a6d 1075
c373c37a
AL
1076 Hit |= TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,
1077 ExpectedInst,false);
303a1703 1078 }
c373c37a 1079 regfree(&Pattern);
303a1703 1080
c373c37a
AL
1081 if (Hit == false)
1082 return _error->Error("Couldn't find package %s",S);
0a8e3465 1083 }
0a8e3465 1084 else
c373c37a
AL
1085 {
1086 if (TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,ExpectedInst) == false)
1087 return false;
1088 }
0a8e3465
AL
1089 }
1090
7c57fe64
AL
1091 /* If we are in the Broken fixing mode we do not attempt to fix the
1092 problems. This is if the user invoked install without -f and gave
1093 packages */
1094 if (BrokenFix == true && Cache->BrokenCount() != 0)
1095 {
b5dc9785 1096 c1out << "You might want to run `apt-get -f install' to correct these:" << endl;
421c8d10 1097 ShowBroken(c1out,Cache,false);
7c57fe64 1098
b5dc9785 1099 return _error->Error("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).");
7c57fe64
AL
1100 }
1101
0a8e3465 1102 // Call the scored problem resolver
303a1703 1103 Fix.InstallProtect();
0a8e3465
AL
1104 if (Fix.Resolve(true) == false)
1105 _error->Discard();
1106
1107 // Now we check the state of the packages,
1108 if (Cache->BrokenCount() != 0)
1109 {
303a1703
AL
1110 c1out << "Some packages could not be installed. This may mean that you have" << endl;
1111 c1out << "requested an impossible situation or if you are using the unstable" << endl;
1112 c1out << "distribution that some required packages have not yet been created" << endl;
1113 c1out << "or been moved out of Incoming." << endl;
1114 if (Packages == 1)
1115 {
1116 c1out << endl;
1117 c1out << "Since you only requested a single operation it is extremely likely that" << endl;
1118 c1out << "the package is simply not installable and a bug report against" << endl;
1119 c1out << "that package should be filed." << endl;
1120 }
1121
1122 c1out << "The following information may help to resolve the situation:" << endl;
1123 c1out << endl;
421c8d10 1124 ShowBroken(c1out,Cache,false);
0a8e3465
AL
1125 return _error->Error("Sorry, broken packages");
1126 }
1127
1128 /* Print out a list of packages that are going to be installed extra
1129 to what the user asked */
1130 if (Cache->InstCount() != ExpectedInst)
1131 {
1132 string List;
1089ca89 1133 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 1134 {
1089ca89 1135 pkgCache::PkgIterator I(Cache,Cache.List[J]);
0a8e3465
AL
1136 if ((*Cache)[I].Install() == false)
1137 continue;
1138
1139 const char **J;
1140 for (J = CmdL.FileList + 1; *J != 0; J++)
1141 if (strcmp(*J,I.Name()) == 0)
1142 break;
1143
1144 if (*J == 0)
1145 List += string(I.Name()) + " ";
1146 }
1147
1148 ShowList(c1out,"The following extra packages will be installed:",List);
1149 }
1150
03e39e59 1151 // See if we need to prompt
2c3bc8bb 1152 if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
2c3bc8bb 1153 return InstallPackages(Cache,false,false);
2c3bc8bb 1154
03e39e59 1155 return InstallPackages(Cache,false);
0a8e3465
AL
1156}
1157 /*}}}*/
1158// DoDistUpgrade - Automatic smart upgrader /*{{{*/
1159// ---------------------------------------------------------------------
1160/* Intelligent upgrader that will install and remove packages at will */
1161bool DoDistUpgrade(CommandLine &CmdL)
1162{
1163 CacheFile Cache;
2d11135a 1164 if (Cache.Open() == false || Cache.CheckDeps() == false)
0a8e3465
AL
1165 return false;
1166
1167 c0out << "Calculating Upgrade... " << flush;
1168 if (pkgDistUpgrade(*Cache) == false)
1169 {
1170 c0out << "Failed" << endl;
421c8d10 1171 ShowBroken(c1out,Cache,false);
0a8e3465
AL
1172 return false;
1173 }
1174
1175 c0out << "Done" << endl;
1176
1177 return InstallPackages(Cache,true);
1178}
1179 /*}}}*/
1180// DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
1181// ---------------------------------------------------------------------
1182/* Follows dselect's selections */
1183bool DoDSelectUpgrade(CommandLine &CmdL)
1184{
1185 CacheFile Cache;
2d11135a 1186 if (Cache.Open() == false || Cache.CheckDeps() == false)
0a8e3465
AL
1187 return false;
1188
1189 // Install everything with the install flag set
1190 pkgCache::PkgIterator I = Cache->PkgBegin();
1191 for (;I.end() != true; I++)
1192 {
1193 /* Install the package only if it is a new install, the autoupgrader
1194 will deal with the rest */
1195 if (I->SelectedState == pkgCache::State::Install)
1196 Cache->MarkInstall(I,false);
1197 }
1198
1199 /* Now install their deps too, if we do this above then order of
1200 the status file is significant for | groups */
1201 for (I = Cache->PkgBegin();I.end() != true; I++)
1202 {
1203 /* Install the package only if it is a new install, the autoupgrader
1204 will deal with the rest */
1205 if (I->SelectedState == pkgCache::State::Install)
2f45c76a 1206 Cache->MarkInstall(I,true);
0a8e3465
AL
1207 }
1208
1209 // Apply erasures now, they override everything else.
1210 for (I = Cache->PkgBegin();I.end() != true; I++)
1211 {
1212 // Remove packages
1213 if (I->SelectedState == pkgCache::State::DeInstall ||
1214 I->SelectedState == pkgCache::State::Purge)
d556d1a1 1215 Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
0a8e3465
AL
1216 }
1217
2f45c76a
AL
1218 /* Resolve any problems that dselect created, allupgrade cannot handle
1219 such things. We do so quite agressively too.. */
1220 if (Cache->BrokenCount() != 0)
1221 {
1222 pkgProblemResolver Fix(Cache);
1223
1224 // Hold back held packages.
1225 if (_config->FindB("APT::Ingore-Hold",false) == false)
1226 {
1227 for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
1228 {
1229 if (I->SelectedState == pkgCache::State::Hold)
1230 {
1231 Fix.Protect(I);
1232 Cache->MarkKeep(I);
1233 }
1234 }
1235 }
1236
1237 if (Fix.Resolve() == false)
1238 {
421c8d10 1239 ShowBroken(c1out,Cache,false);
2f45c76a
AL
1240 return _error->Error("Internal Error, problem resolver broke stuff");
1241 }
1242 }
1243
1244 // Now upgrade everything
0a8e3465
AL
1245 if (pkgAllUpgrade(Cache) == false)
1246 {
421c8d10 1247 ShowBroken(c1out,Cache,false);
2f45c76a 1248 return _error->Error("Internal Error, problem resolver broke stuff");
0a8e3465
AL
1249 }
1250
1251 return InstallPackages(Cache,false);
1252}
1253 /*}}}*/
1254// DoClean - Remove download archives /*{{{*/
1255// ---------------------------------------------------------------------
1256/* */
1257bool DoClean(CommandLine &CmdL)
1258{
8b067c22
AL
1259 if (_config->FindB("APT::Get::Simulate") == true)
1260 {
1261 cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
1262 _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
1263 return true;
1264 }
1265
1b6d659c
AL
1266 // Lock the archive directory
1267 FileFd Lock;
1268 if (_config->FindB("Debug::NoLocking",false) == false)
1269 {
1270 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
1271 if (_error->PendingError() == true)
1272 return _error->Error("Unable to lock the download directory");
1273 }
1274
7a1b1f8b
AL
1275 pkgAcquire Fetcher;
1276 Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
1277 Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
0a8e3465
AL
1278 return true;
1279}
1280 /*}}}*/
1bc849af
AL
1281// DoAutoClean - Smartly remove downloaded archives /*{{{*/
1282// ---------------------------------------------------------------------
1283/* This is similar to clean but it only purges things that cannot be
1284 downloaded, that is old versions of cached packages. */
65a1e968
AL
1285class LogCleaner : public pkgArchiveCleaner
1286{
1287 protected:
1288 virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
1289 {
314037ba 1290 cout << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
c1e78ee5
AL
1291
1292 if (_config->FindB("APT::Get::Simulate") == false)
1293 unlink(File);
65a1e968
AL
1294 };
1295};
1296
1bc849af
AL
1297bool DoAutoClean(CommandLine &CmdL)
1298{
1b6d659c
AL
1299 // Lock the archive directory
1300 FileFd Lock;
1301 if (_config->FindB("Debug::NoLocking",false) == false)
1302 {
1303 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
1304 if (_error->PendingError() == true)
1305 return _error->Error("Unable to lock the download directory");
1306 }
1307
1bc849af 1308 CacheFile Cache;
2d11135a 1309 if (Cache.Open() == false)
1bc849af
AL
1310 return false;
1311
65a1e968 1312 LogCleaner Cleaner;
1bc849af
AL
1313
1314 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
1315 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
1316}
1317 /*}}}*/
0a8e3465
AL
1318// DoCheck - Perform the check operation /*{{{*/
1319// ---------------------------------------------------------------------
1320/* Opening automatically checks the system, this command is mostly used
1321 for debugging */
1322bool DoCheck(CommandLine &CmdL)
1323{
1324 CacheFile Cache;
1325 Cache.Open();
2d11135a 1326 Cache.CheckDeps();
0a8e3465
AL
1327
1328 return true;
1329}
1330 /*}}}*/
36375005
AL
1331// DoSource - Fetch a source archive /*{{{*/
1332// ---------------------------------------------------------------------
2d11135a 1333/* Fetch souce packages */
fb0ee66e
AL
1334struct DscFile
1335{
1336 string Package;
1337 string Version;
1338 string Dsc;
1339};
1340
36375005
AL
1341bool DoSource(CommandLine &CmdL)
1342{
1343 CacheFile Cache;
2d11135a 1344 if (Cache.Open(false) == false)
36375005
AL
1345 return false;
1346
2d11135a
AL
1347 if (CmdL.FileSize() <= 1)
1348 return _error->Error("Must specify at least one package to fetch source for");
1349
36375005
AL
1350 // Read the source list
1351 pkgSourceList List;
1352 if (List.ReadMainList() == false)
1353 return _error->Error("The list of sources could not be read.");
1354
1355 // Create the text record parsers
1356 pkgRecords Recs(Cache);
1357 pkgSrcRecords SrcRecs(List);
1358 if (_error->PendingError() == true)
1359 return false;
1360
1361 // Create the download object
1362 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1363 pkgAcquire Fetcher(&Stat);
fb0ee66e
AL
1364
1365 DscFile *Dsc = new DscFile[CmdL.FileSize()];
36375005
AL
1366
1367 // Load the requestd sources into the fetcher
fb0ee66e
AL
1368 unsigned J = 0;
1369 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
36375005
AL
1370 {
1371 string Src;
0a8e3465 1372
36375005
AL
1373 /* Lookup the version of the package we would install if we were to
1374 install a version and determine the source package name, then look
1375 in the archive for a source package of the same name. In theory
1376 we could stash the version string as well and match that too but
1377 today there aren't multi source versions in the archive. */
1378 pkgCache::PkgIterator Pkg = Cache->FindPkg(*I);
1379 if (Pkg.end() == false)
1380 {
1381 pkgCache::VerIterator Ver = Cache->GetCandidateVer(Pkg);
1382 if (Ver.end() == false)
1383 {
1384 pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
1385 Src = Parse.SourcePkg();
1386 }
1387 }
1388
1389 // No source package name..
1390 if (Src.empty() == true)
1391 Src = *I;
1392
1393 // The best hit
1394 pkgSrcRecords::Parser *Last = 0;
1395 unsigned long Offset = 0;
1396 string Version;
73c2c61b 1397 bool IsMatch = false;
36375005
AL
1398
1399 // Iterate over all of the hits
1400 pkgSrcRecords::Parser *Parse;
1401 SrcRecs.Restart();
1402 while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
1403 {
1404 string Ver = Parse->Version();
73c2c61b
AL
1405
1406 // Skip name mismatches
1407 if (IsMatch == true && Parse->Package() != Src)
1408 continue;
1409
1410 // Newer version or an exact match
1411 if (Last == 0 || pkgVersionCompare(Version,Ver) < 0 ||
1412 (Parse->Package() == Src && IsMatch == false))
36375005 1413 {
73c2c61b 1414 IsMatch = Parse->Package() == Src;
36375005
AL
1415 Last = Parse;
1416 Offset = Parse->Offset();
1417 Version = Ver;
1418 }
1419 }
1420
1421 if (Last == 0)
1422 return _error->Error("Unable to find a source package for %s",Src.c_str());
1423
1424 // Back track
1425 vector<pkgSrcRecords::File> Lst;
1426 if (Last->Jump(Offset) == false || Last->Files(Lst) == false)
1427 return false;
1428
1429 // Load them into the fetcher
1430 for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
1431 I != Lst.end(); I++)
1432 {
1433 // Try to guess what sort of file it is we are getting.
1434 string Comp;
1435 if (I->Path.find(".dsc") != string::npos)
fb0ee66e 1436 {
36375005 1437 Comp = "dsc";
fb0ee66e
AL
1438 Dsc[J].Package = Last->Package();
1439 Dsc[J].Version = Last->Version();
1440 Dsc[J].Dsc = flNotDir(I->Path);
1441 }
1442
36375005
AL
1443 if (I->Path.find(".tar.gz") != string::npos)
1444 Comp = "tar";
1445 if (I->Path.find(".diff.gz") != string::npos)
1446 Comp = "diff";
1447
17c0e8e1
AL
1448 // Diff only mode only fetches .diff files
1449 if (_config->FindB("APT::Get::Diff-Only",false) == true &&
1450 Comp != "diff")
1451 continue;
1452
1453 // Tar only mode only fetches .tar files
1454 if (_config->FindB("APT::Get::Tar-Only",false) == true &&
1455 Comp != "tar")
1456 continue;
1457
36375005
AL
1458 new pkgAcqFile(&Fetcher,Last->Source()->ArchiveURI(I->Path),
1459 I->MD5Hash,I->Size,Last->Source()->SourceInfo(Src,
1460 Last->Version(),Comp),Src);
1461 }
1462 }
1463
1464 // Display statistics
1465 unsigned long FetchBytes = Fetcher.FetchNeeded();
1466 unsigned long FetchPBytes = Fetcher.PartialPresent();
1467 unsigned long DebBytes = Fetcher.TotalNeeded();
1468
1469 // Check for enough free space
1470 struct statfs Buf;
1471 string OutputDir = ".";
1472 if (statfs(OutputDir.c_str(),&Buf) != 0)
1473 return _error->Errno("statfs","Couldn't determine free space in %s",
1474 OutputDir.c_str());
1475 if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
1476 return _error->Error("Sorry, you don't have enough free space in %s",
1477 OutputDir.c_str());
1478
1479 // Number of bytes
1480 c1out << "Need to get ";
1481 if (DebBytes != FetchBytes)
314037ba 1482 c1out << SizeToStr(FetchBytes) << "B/" << SizeToStr(DebBytes) << 'B';
36375005 1483 else
314037ba 1484 c1out << SizeToStr(DebBytes) << 'B';
36375005
AL
1485 c1out << " of source archives." << endl;
1486
2c0c53b3
AL
1487 if (_config->FindB("APT::Get::Simulate",false) == true)
1488 {
1489 for (unsigned I = 0; I != J; I++)
1490 cout << "Fetch Source " << Dsc[I].Package << endl;
1491 return true;
1492 }
1493
36375005
AL
1494 // Just print out the uris an exit if the --print-uris flag was used
1495 if (_config->FindB("APT::Get::Print-URIs") == true)
1496 {
1497 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1498 for (; I != Fetcher.UriEnd(); I++)
1499 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
1500 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
1501 return true;
1502 }
1503
1504 // Run it
024d1123 1505 if (Fetcher.Run() == pkgAcquire::Failed)
36375005
AL
1506 return false;
1507
1508 // Print error messages
fb0ee66e 1509 bool Failed = false;
36375005
AL
1510 for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
1511 {
1512 if ((*I)->Status == pkgAcquire::Item::StatDone &&
1513 (*I)->Complete == true)
1514 continue;
1515
1516 cerr << "Failed to fetch " << (*I)->DescURI() << endl;
1517 cerr << " " << (*I)->ErrorText << endl;
fb0ee66e 1518 Failed = true;
36375005 1519 }
fb0ee66e
AL
1520 if (Failed == true)
1521 return _error->Error("Failed to fetch some archives.");
1522
1523 if (_config->FindB("APT::Get::Download-only",false) == true)
1524 return true;
36375005 1525
fb0ee66e 1526 // Unpack the sources
54676e1a
AL
1527 pid_t Process = ExecFork();
1528
1529 if (Process == 0)
fb0ee66e 1530 {
54676e1a 1531 for (unsigned I = 0; I != J; I++)
fb0ee66e 1532 {
54676e1a 1533 string Dir = Dsc[I].Package + '-' + pkgBaseVersion(Dsc[I].Version.c_str());
fb0ee66e 1534
17c0e8e1
AL
1535 // Diff only mode only fetches .diff files
1536 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
a3eaf954
AL
1537 _config->FindB("APT::Get::Tar-Only",false) == true ||
1538 Dsc[I].Dsc.empty() == true)
17c0e8e1 1539 continue;
a3eaf954 1540
54676e1a
AL
1541 // See if the package is already unpacked
1542 struct stat Stat;
1543 if (stat(Dir.c_str(),&Stat) == 0 &&
1544 S_ISDIR(Stat.st_mode) != 0)
1545 {
1546 c0out << "Skipping unpack of already unpacked source in " << Dir << endl;
1547 }
1548 else
1549 {
1550 // Call dpkg-source
1551 char S[500];
1552 snprintf(S,sizeof(S),"%s -x %s",
1553 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
1554 Dsc[I].Dsc.c_str());
1555 if (system(S) != 0)
1556 {
1557 cerr << "Unpack command '" << S << "' failed." << endl;
1558 _exit(1);
1559 }
1560 }
1561
1562 // Try to compile it with dpkg-buildpackage
1563 if (_config->FindB("APT::Get::Compile",false) == true)
1564 {
1565 // Call dpkg-buildpackage
1566 char S[500];
1567 snprintf(S,sizeof(S),"cd %s && %s %s",
1568 Dir.c_str(),
1569 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
1570 _config->Find("DPkg::Build-Options","-b -uc").c_str());
1571
1572 if (system(S) != 0)
1573 {
1574 cerr << "Build command '" << S << "' failed." << endl;
1575 _exit(1);
1576 }
1577 }
1578 }
1579
1580 _exit(0);
1581 }
1582
1583 // Wait for the subprocess
1584 int Status = 0;
1585 while (waitpid(Process,&Status,0) != Process)
1586 {
1587 if (errno == EINTR)
1588 continue;
1589 return _error->Errno("waitpid","Couldn't wait for subprocess");
1590 }
1591
1592 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
1593 return _error->Error("Child process failed");
1594
36375005
AL
1595 return true;
1596}
1597 /*}}}*/
1598
0a8e3465
AL
1599// ShowHelp - Show a help screen /*{{{*/
1600// ---------------------------------------------------------------------
1601/* */
212ad54a 1602bool ShowHelp(CommandLine &CmdL)
0a8e3465
AL
1603{
1604 cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
1605 " compiled on " << __DATE__ << " " << __TIME__ << endl;
04aa15a8
AL
1606 if (_config->FindB("version") == true)
1607 return 100;
1608
0a8e3465
AL
1609 cout << "Usage: apt-get [options] command" << endl;
1610 cout << " apt-get [options] install pkg1 [pkg2 ...]" << endl;
1611 cout << endl;
1612 cout << "apt-get is a simple command line interface for downloading and" << endl;
1613 cout << "installing packages. The most frequently used commands are update" << endl;
1614 cout << "and install." << endl;
1615 cout << endl;
1616 cout << "Commands:" << endl;
1617 cout << " update - Retrieve new lists of packages" << endl;
1618 cout << " upgrade - Perform an upgrade" << endl;
1619 cout << " install - Install new packages (pkg is libc6 not libc6.deb)" << endl;
303a1703 1620 cout << " remove - Remove packages" << endl;
4994560c 1621 cout << " source - Download source archives" << endl;
0a8e3465
AL
1622 cout << " dist-upgrade - Distribution upgrade, see apt-get(8)" << endl;
1623 cout << " dselect-upgrade - Follow dselect selections" << endl;
1624 cout << " clean - Erase downloaded archive files" << endl;
1bc849af 1625 cout << " autoclean - Erase old downloaded archive files" << endl;
0a8e3465
AL
1626 cout << " check - Verify that there are no broken dependencies" << endl;
1627 cout << endl;
1628 cout << "Options:" << endl;
c217f42a
AL
1629 cout << " -h This help text." << endl;
1630 cout << " -q Loggable output - no progress indicator" << endl;
0a8e3465
AL
1631 cout << " -qq No output except for errors" << endl;
1632 cout << " -d Download only - do NOT install or unpack archives" << endl;
1633 cout << " -s No-act. Perform ordering simulation" << endl;
1634 cout << " -y Assume Yes to all queries and do not prompt" << endl;
1635 cout << " -f Attempt to continue if the integrity check fails" << endl;
1636 cout << " -m Attempt to continue if archives are unlocatable" << endl;
1637 cout << " -u Show a list of upgraded packages as well" << endl;
b5dc9785 1638 cout << " -b Build the source package after fetching it" << endl;
0a8e3465 1639 cout << " -c=? Read this configuration file" << endl;
7974b907 1640 cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
21ae3cae 1641 cout << "See the apt-get(8), sources.list(5) and apt.conf(5) manual" << endl;
fc4b5c9f 1642 cout << "pages for more information and options." << endl;
0a8e3465
AL
1643 return 100;
1644}
1645 /*}}}*/
1646// GetInitialize - Initialize things for apt-get /*{{{*/
1647// ---------------------------------------------------------------------
1648/* */
1649void GetInitialize()
1650{
1651 _config->Set("quiet",0);
1652 _config->Set("help",false);
1653 _config->Set("APT::Get::Download-Only",false);
1654 _config->Set("APT::Get::Simulate",false);
1655 _config->Set("APT::Get::Assume-Yes",false);
1656 _config->Set("APT::Get::Fix-Broken",false);
83d89a9f 1657 _config->Set("APT::Get::Force-Yes",false);
9df71a5b 1658 _config->Set("APT::Get::APT::Get::No-List-Cleanup",true);
0a8e3465
AL
1659}
1660 /*}}}*/
d7827aca
AL
1661// SigWinch - Window size change signal handler /*{{{*/
1662// ---------------------------------------------------------------------
1663/* */
1664void SigWinch(int)
1665{
1666 // Riped from GNU ls
1667#ifdef TIOCGWINSZ
1668 struct winsize ws;
1669
1670 if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
1671 ScreenWidth = ws.ws_col - 1;
1672#endif
1673}
1674 /*}}}*/
0a8e3465
AL
1675
1676int main(int argc,const char *argv[])
1677{
1678 CommandLine::Args Args[] = {
1679 {'h',"help","help",0},
04aa15a8 1680 {'v',"version","version",0},
0a8e3465
AL
1681 {'q',"quiet","quiet",CommandLine::IntLevel},
1682 {'q',"silent","quiet",CommandLine::IntLevel},
1683 {'d',"download-only","APT::Get::Download-Only",0},
fb0ee66e
AL
1684 {'b',"compile","APT::Get::Compile",0},
1685 {'b',"build","APT::Get::Compile",0},
d150b09d
AL
1686 {'s',"simulate","APT::Get::Simulate",0},
1687 {'s',"just-print","APT::Get::Simulate",0},
1688 {'s',"recon","APT::Get::Simulate",0},
1689 {'s',"no-act","APT::Get::Simulate",0},
1690 {'y',"yes","APT::Get::Assume-Yes",0},
0a8e3465
AL
1691 {'y',"assume-yes","APT::Get::Assume-Yes",0},
1692 {'f',"fix-broken","APT::Get::Fix-Broken",0},
1693 {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
30e1eab5 1694 {'m',"ignore-missing","APT::Get::Fix-Missing",0},
ab559b35 1695 {0,"no-download","APT::Get::No-Download",0},
30e1eab5 1696 {0,"fix-missing","APT::Get::Fix-Missing",0},
c88edf1d 1697 {0,"ignore-hold","APT::Ingore-Hold",0},
83d89a9f
AL
1698 {0,"no-upgrade","APT::Get::no-upgrade",0},
1699 {0,"force-yes","APT::Get::force-yes",0},
f7a08e33 1700 {0,"print-uris","APT::Get::Print-URIs",0},
5fafc0ef
AL
1701 {0,"diff-only","APT::Get::Diff-Only",0},
1702 {0,"tar-only","APT::Get::tar-Only",0},
fc4b5c9f 1703 {0,"purge","APT::Get::Purge",0},
9df71a5b 1704 {0,"list-cleanup","APT::Get::List-Cleanup",0},
d0c59649 1705 {0,"reinstall","APT::Get::ReInstall",0},
d150b09d
AL
1706 {0,"trivial-only","APT::Get::Trivial-Only",0},
1707 {0,"no-remove","APT::Get::No-Remove",0},
0a8e3465
AL
1708 {'c',"config-file",0,CommandLine::ConfigFile},
1709 {'o',"option",0,CommandLine::ArbItem},
1710 {0,0,0,0}};
83d89a9f
AL
1711 CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
1712 {"upgrade",&DoUpgrade},
1713 {"install",&DoInstall},
1714 {"remove",&DoInstall},
1715 {"dist-upgrade",&DoDistUpgrade},
1716 {"dselect-upgrade",&DoDSelectUpgrade},
1717 {"clean",&DoClean},
1bc849af 1718 {"autoclean",&DoAutoClean},
83d89a9f 1719 {"check",&DoCheck},
36375005 1720 {"source",&DoSource},
3d615484 1721 {"help",&ShowHelp},
83d89a9f 1722 {0,0}};
0a8e3465
AL
1723
1724 // Parse the command line and initialize the package library
1725 CommandLine CmdL(Args,_config);
1726 if (pkgInitialize(*_config) == false ||
1727 CmdL.Parse(argc,argv) == false)
1728 {
1729 _error->DumpErrors();
1730 return 100;
1731 }
1732
1733 // See if the help should be shown
1734 if (_config->FindB("help") == true ||
04aa15a8 1735 _config->FindB("version") == true ||
0a8e3465 1736 CmdL.FileSize() == 0)
3d615484 1737 return ShowHelp(CmdL);
0a8e3465 1738
a9a5908d
AL
1739 // Deal with stdout not being a tty
1740 if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
1741 _config->Set("quiet","1");
1742
0a8e3465
AL
1743 // Setup the output streams
1744 c0out.rdbuf(cout.rdbuf());
1745 c1out.rdbuf(cout.rdbuf());
1746 c2out.rdbuf(cout.rdbuf());
1747 if (_config->FindI("quiet",0) > 0)
1748 c0out.rdbuf(devnull.rdbuf());
1749 if (_config->FindI("quiet",0) > 1)
1750 c1out.rdbuf(devnull.rdbuf());
d7827aca
AL
1751
1752 // Setup the signals
1753 signal(SIGPIPE,SIG_IGN);
1754 signal(SIGWINCH,SigWinch);
1755 SigWinch(0);
0a8e3465
AL
1756
1757 // Match the operation
83d89a9f 1758 CmdL.DispatchArg(Cmds);
0a8e3465
AL
1759
1760 // Print any errors or warnings found during parsing
1761 if (_error->empty() == false)
1762 {
1763 bool Errors = _error->PendingError();
1764 _error->DumpErrors();
0a8e3465
AL
1765 return Errors == true?100:0;
1766 }
1767
1768 return 0;
1769}