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