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