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