]>
Commit | Line | Data |
---|---|---|
b9179170 MV |
1 | // Include Files /*{{{*/ |
2 | #include <config.h> | |
3 | ||
453b82a3 | 4 | #include <apt-pkg/acquire.h> |
b9179170 | 5 | #include <apt-pkg/acquire-item.h> |
453b82a3 | 6 | #include <apt-pkg/algorithms.h> |
b9179170 MV |
7 | #include <apt-pkg/cachefile.h> |
8 | #include <apt-pkg/cacheset.h> | |
453b82a3 DK |
9 | #include <apt-pkg/cmndline.h> |
10 | #include <apt-pkg/depcache.h> | |
11 | #include <apt-pkg/error.h> | |
12 | #include <apt-pkg/fileutl.h> | |
b9179170 | 13 | #include <apt-pkg/pkgrecords.h> |
453b82a3 DK |
14 | #include <apt-pkg/pkgsystem.h> |
15 | #include <apt-pkg/sptr.h> | |
16 | #include <apt-pkg/strutl.h> | |
17 | #include <apt-pkg/cacheiterators.h> | |
18 | #include <apt-pkg/configuration.h> | |
19 | #include <apt-pkg/macros.h> | |
20 | #include <apt-pkg/packagemanager.h> | |
21 | #include <apt-pkg/pkgcache.h> | |
0d29b9d4 | 22 | #include <apt-pkg/sourcelist.h> |
b58f28d4 | 23 | #include <apt-pkg/install-progress.h> |
0d29b9d4 | 24 | |
453b82a3 DK |
25 | #include <errno.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
b9179170 MV |
28 | #include <sys/statfs.h> |
29 | #include <sys/statvfs.h> | |
453b82a3 DK |
30 | #include <algorithm> |
31 | #include <iostream> | |
32 | #include <set> | |
33 | #include <vector> | |
eafc5435 | 34 | #include <map> |
b9179170 | 35 | |
453b82a3 DK |
36 | #include <apt-private/acqprogress.h> |
37 | #include <apt-private/private-install.h> | |
38 | #include <apt-private/private-cachefile.h> | |
39 | #include <apt-private/private-cacheset.h> | |
40 | #include <apt-private/private-download.h> | |
41 | #include <apt-private/private-output.h> | |
b9179170 MV |
42 | |
43 | #include <apti18n.h> | |
44 | /*}}}*/ | |
453b82a3 | 45 | class pkgSourceList; |
b9179170 | 46 | |
b9179170 MV |
47 | // InstallPackages - Actually download and install the packages /*{{{*/ |
48 | // --------------------------------------------------------------------- | |
49 | /* This displays the informative messages describing what is going to | |
50 | happen and then calls the download routines */ | |
51 | bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask, bool Safety) | |
52 | { | |
53 | if (_config->FindB("APT::Get::Purge",false) == true) | |
54 | { | |
55 | pkgCache::PkgIterator I = Cache->PkgBegin(); | |
56 | for (; I.end() == false; ++I) | |
57 | { | |
58 | if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete) | |
59 | Cache->MarkDelete(I,true); | |
60 | } | |
61 | } | |
62 | ||
63 | bool Fail = false; | |
64 | bool Essential = false; | |
65 | ||
66 | // Show all the various warning indicators | |
67 | ShowDel(c1out,Cache); | |
68 | ShowNew(c1out,Cache); | |
69 | if (ShwKept == true) | |
70 | ShowKept(c1out,Cache); | |
71 | Fail |= !ShowHold(c1out,Cache); | |
72 | if (_config->FindB("APT::Get::Show-Upgraded",true) == true) | |
73 | ShowUpgraded(c1out,Cache); | |
74 | Fail |= !ShowDowngraded(c1out,Cache); | |
75 | if (_config->FindB("APT::Get::Download-Only",false) == false) | |
76 | Essential = !ShowEssential(c1out,Cache); | |
77 | Fail |= Essential; | |
78 | Stats(c1out,Cache); | |
79 | ||
80 | // Sanity check | |
81 | if (Cache->BrokenCount() != 0) | |
82 | { | |
83 | ShowBroken(c1out,Cache,false); | |
84 | return _error->Error(_("Internal error, InstallPackages was called with broken packages!")); | |
85 | } | |
86 | ||
87 | if (Cache->DelCount() == 0 && Cache->InstCount() == 0 && | |
88 | Cache->BadCount() == 0) | |
89 | return true; | |
90 | ||
91 | // No remove flag | |
92 | if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false) | |
93 | return _error->Error(_("Packages need to be removed but remove is disabled.")); | |
94 | ||
95 | // Run the simulator .. | |
96 | if (_config->FindB("APT::Get::Simulate") == true) | |
97 | { | |
98 | pkgSimulate PM(Cache); | |
5e9458e2 | 99 | |
bd5f39b3 MV |
100 | #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13) |
101 | APT::Progress::PackageManager *progress = APT::Progress::PackageManagerProgressFactory(); | |
e6ad8031 MV |
102 | pkgPackageManager::OrderResult Res = PM.DoInstall(progress); |
103 | delete progress; | |
bd5f39b3 MV |
104 | #else |
105 | int status_fd = _config->FindI("APT::Status-Fd",-1); | |
106 | pkgPackageManager::OrderResult Res = PM.DoInstall(status_fd); | |
107 | #endif | |
5e9458e2 | 108 | |
b9179170 MV |
109 | if (Res == pkgPackageManager::Failed) |
110 | return false; | |
111 | if (Res != pkgPackageManager::Completed) | |
112 | return _error->Error(_("Internal error, Ordering didn't finish")); | |
113 | return true; | |
114 | } | |
115 | ||
116 | // Create the text record parser | |
117 | pkgRecords Recs(Cache); | |
118 | if (_error->PendingError() == true) | |
119 | return false; | |
120 | ||
121 | // Create the download object | |
122 | pkgAcquire Fetcher; | |
123 | AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0)); | |
124 | if (_config->FindB("APT::Get::Print-URIs", false) == true) | |
125 | { | |
126 | // force a hashsum for compatibility reasons | |
127 | _config->CndSet("Acquire::ForceHash", "md5sum"); | |
128 | } | |
129 | else if (Fetcher.Setup(&Stat, _config->FindDir("Dir::Cache::Archives")) == false) | |
130 | return false; | |
131 | ||
132 | // Read the source list | |
133 | if (Cache.BuildSourceList() == false) | |
134 | return false; | |
135 | pkgSourceList *List = Cache.GetSourceList(); | |
136 | ||
137 | // Create the package manager and prepare to download | |
138 | SPtr<pkgPackageManager> PM= _system->CreatePM(Cache); | |
139 | if (PM->GetArchives(&Fetcher,List,&Recs) == false || | |
140 | _error->PendingError() == true) | |
141 | return false; | |
142 | ||
143 | // Display statistics | |
144 | unsigned long long FetchBytes = Fetcher.FetchNeeded(); | |
145 | unsigned long long FetchPBytes = Fetcher.PartialPresent(); | |
146 | unsigned long long DebBytes = Fetcher.TotalNeeded(); | |
147 | if (DebBytes != Cache->DebSize()) | |
148 | { | |
149 | c0out << DebBytes << ',' << Cache->DebSize() << std::endl; | |
1166ea79 | 150 | c0out << _("How odd... The sizes didn't match, email apt@packages.debian.org") << std::endl; |
b9179170 MV |
151 | } |
152 | ||
153 | // Number of bytes | |
154 | if (DebBytes != FetchBytes) | |
155 | //TRANSLATOR: The required space between number and unit is already included | |
156 | // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB | |
157 | ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"), | |
158 | SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str()); | |
159 | else if (DebBytes != 0) | |
160 | //TRANSLATOR: The required space between number and unit is already included | |
161 | // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB | |
162 | ioprintf(c1out,_("Need to get %sB of archives.\n"), | |
163 | SizeToStr(DebBytes).c_str()); | |
164 | ||
165 | // Size delta | |
166 | if (Cache->UsrSize() >= 0) | |
167 | //TRANSLATOR: The required space between number and unit is already included | |
168 | // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB | |
169 | ioprintf(c1out,_("After this operation, %sB of additional disk space will be used.\n"), | |
170 | SizeToStr(Cache->UsrSize()).c_str()); | |
171 | else | |
172 | //TRANSLATOR: The required space between number and unit is already included | |
173 | // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB | |
174 | ioprintf(c1out,_("After this operation, %sB disk space will be freed.\n"), | |
175 | SizeToStr(-1*Cache->UsrSize()).c_str()); | |
176 | ||
177 | if (_error->PendingError() == true) | |
178 | return false; | |
179 | ||
180 | /* Check for enough free space, but only if we are actually going to | |
181 | download */ | |
182 | if (_config->FindB("APT::Get::Print-URIs") == false && | |
183 | _config->FindB("APT::Get::Download",true) == true) | |
184 | { | |
185 | struct statvfs Buf; | |
186 | std::string OutputDir = _config->FindDir("Dir::Cache::Archives"); | |
187 | if (statvfs(OutputDir.c_str(),&Buf) != 0) { | |
188 | if (errno == EOVERFLOW) | |
189 | return _error->WarningE("statvfs",_("Couldn't determine free space in %s"), | |
190 | OutputDir.c_str()); | |
191 | else | |
192 | return _error->Errno("statvfs",_("Couldn't determine free space in %s"), | |
193 | OutputDir.c_str()); | |
194 | } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize) | |
195 | { | |
196 | struct statfs Stat; | |
197 | if (statfs(OutputDir.c_str(),&Stat) != 0 | |
198 | #if HAVE_STRUCT_STATFS_F_TYPE | |
199 | || unsigned(Stat.f_type) != RAMFS_MAGIC | |
200 | #endif | |
201 | ) | |
202 | return _error->Error(_("You don't have enough free space in %s."), | |
203 | OutputDir.c_str()); | |
204 | } | |
205 | } | |
206 | ||
207 | // Fail safe check | |
208 | if (_config->FindI("quiet",0) >= 2 || | |
209 | _config->FindB("APT::Get::Assume-Yes",false) == true) | |
210 | { | |
211 | if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false) | |
212 | return _error->Error(_("There are problems and -y was used without --force-yes")); | |
213 | } | |
214 | ||
215 | if (Essential == true && Safety == true) | |
216 | { | |
217 | if (_config->FindB("APT::Get::Trivial-Only",false) == true) | |
218 | return _error->Error(_("Trivial Only specified but this is not a trivial operation.")); | |
219 | ||
220 | // TRANSLATOR: This string needs to be typed by the user as a confirmation, so be | |
221 | // careful with hard to type or special characters (like non-breaking spaces) | |
222 | const char *Prompt = _("Yes, do as I say!"); | |
223 | ioprintf(c2out, | |
224 | _("You are about to do something potentially harmful.\n" | |
225 | "To continue type in the phrase '%s'\n" | |
226 | " ?] "),Prompt); | |
227 | c2out << std::flush; | |
228 | if (AnalPrompt(Prompt) == false) | |
229 | { | |
230 | c2out << _("Abort.") << std::endl; | |
231 | exit(1); | |
232 | } | |
233 | } | |
234 | else | |
235 | { | |
236 | // Prompt to continue | |
237 | if (Ask == true || Fail == true) | |
238 | { | |
239 | if (_config->FindB("APT::Get::Trivial-Only",false) == true) | |
240 | return _error->Error(_("Trivial Only specified but this is not a trivial operation.")); | |
241 | ||
242 | if (_config->FindI("quiet",0) < 2 && | |
243 | _config->FindB("APT::Get::Assume-Yes",false) == false) | |
244 | { | |
245 | c2out << _("Do you want to continue?") << std::flush; | |
246 | if (YnPrompt() == false) | |
247 | { | |
248 | c2out << _("Abort.") << std::endl; | |
249 | exit(1); | |
250 | } | |
251 | } | |
252 | } | |
253 | } | |
254 | ||
255 | // Just print out the uris an exit if the --print-uris flag was used | |
256 | if (_config->FindB("APT::Get::Print-URIs") == true) | |
257 | { | |
258 | pkgAcquire::UriIterator I = Fetcher.UriBegin(); | |
259 | for (; I != Fetcher.UriEnd(); ++I) | |
ac69a4d8 | 260 | std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' << |
b9179170 MV |
261 | I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl; |
262 | return true; | |
263 | } | |
264 | ||
866893a6 | 265 | if (!CheckAuth(Fetcher, true)) |
b9179170 MV |
266 | return false; |
267 | ||
268 | /* Unlock the dpkg lock if we are not going to be doing an install | |
269 | after. */ | |
270 | if (_config->FindB("APT::Get::Download-Only",false) == true) | |
271 | _system->UnLock(); | |
272 | ||
273 | // Run it | |
274 | while (1) | |
275 | { | |
276 | bool Transient = false; | |
277 | if (_config->FindB("APT::Get::Download",true) == false) | |
278 | { | |
279 | for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();) | |
280 | { | |
281 | if ((*I)->Local == true) | |
282 | { | |
283 | ++I; | |
284 | continue; | |
285 | } | |
286 | ||
287 | // Close the item and check if it was found in cache | |
288 | (*I)->Finished(); | |
289 | if ((*I)->Complete == false) | |
290 | Transient = true; | |
291 | ||
292 | // Clear it out of the fetch list | |
293 | delete *I; | |
294 | I = Fetcher.ItemsBegin(); | |
295 | } | |
296 | } | |
b9179170 | 297 | |
866893a6 DK |
298 | bool Failed = false; |
299 | if (AcquireRun(Fetcher, 0, &Failed, &Transient) == false) | |
300 | return false; | |
b9179170 MV |
301 | |
302 | /* If we are in no download mode and missing files and there were | |
303 | 'failures' then the user must specify -m. Furthermore, there | |
304 | is no such thing as a transient error in no-download mode! */ | |
305 | if (Transient == true && | |
306 | _config->FindB("APT::Get::Download",true) == false) | |
307 | { | |
308 | Transient = false; | |
309 | Failed = true; | |
310 | } | |
311 | ||
312 | if (_config->FindB("APT::Get::Download-Only",false) == true) | |
313 | { | |
314 | if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false) | |
315 | return _error->Error(_("Some files failed to download")); | |
316 | c1out << _("Download complete and in download only mode") << std::endl; | |
317 | return true; | |
318 | } | |
319 | ||
320 | if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false) | |
321 | { | |
322 | return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?")); | |
323 | } | |
324 | ||
325 | if (Transient == true && Failed == true) | |
326 | return _error->Error(_("--fix-missing and media swapping is not currently supported")); | |
327 | ||
328 | // Try to deal with missing package files | |
329 | if (Failed == true && PM->FixMissing() == false) | |
330 | { | |
331 | c2out << _("Unable to correct missing packages.") << std::endl; | |
332 | return _error->Error(_("Aborting install.")); | |
333 | } | |
334 | ||
335 | _system->UnLock(); | |
e6ad8031 | 336 | |
bd5f39b3 MV |
337 | #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13) |
338 | APT::Progress::PackageManager *progress = APT::Progress::PackageManagerProgressFactory(); | |
e6ad8031 MV |
339 | pkgPackageManager::OrderResult Res = PM->DoInstall(progress); |
340 | delete progress; | |
bd5f39b3 MV |
341 | #else |
342 | int status_fd = _config->FindI("APT::Status-Fd", -1); | |
343 | pkgPackageManager::OrderResult Res = PM->DoInstall(status_fd); | |
344 | #endif | |
e6ad8031 | 345 | |
b9179170 MV |
346 | if (Res == pkgPackageManager::Failed || _error->PendingError() == true) |
347 | return false; | |
348 | if (Res == pkgPackageManager::Completed) | |
349 | break; | |
350 | ||
351 | // Reload the fetcher object and loop again for media swapping | |
352 | Fetcher.Shutdown(); | |
353 | if (PM->GetArchives(&Fetcher,List,&Recs) == false) | |
354 | return false; | |
355 | ||
356 | _system->Lock(); | |
357 | } | |
358 | ||
359 | std::set<std::string> const disappearedPkgs = PM->GetDisappearedPackages(); | |
360 | if (disappearedPkgs.empty() == true) | |
361 | return true; | |
362 | ||
363 | std::string disappear; | |
364 | for (std::set<std::string>::const_iterator d = disappearedPkgs.begin(); | |
365 | d != disappearedPkgs.end(); ++d) | |
366 | disappear.append(*d).append(" "); | |
367 | ||
368 | ShowList(c1out, P_("The following package disappeared from your system as\n" | |
369 | "all files have been overwritten by other packages:", | |
370 | "The following packages disappeared from your system as\n" | |
371 | "all files have been overwritten by other packages:", disappearedPkgs.size()), disappear, ""); | |
372 | c0out << _("Note: This is done automatically and on purpose by dpkg.") << std::endl; | |
373 | ||
374 | return true; | |
375 | } | |
376 | /*}}}*/ | |
b9179170 MV |
377 | // DoAutomaticRemove - Remove all automatic unused packages /*{{{*/ |
378 | // --------------------------------------------------------------------- | |
379 | /* Remove unused automatic packages */ | |
c3ccac92 | 380 | static bool DoAutomaticRemove(CacheFile &Cache) |
b9179170 MV |
381 | { |
382 | bool Debug = _config->FindI("Debug::pkgAutoRemove",false); | |
383 | bool doAutoRemove = _config->FindB("APT::Get::AutomaticRemove", false); | |
384 | bool hideAutoRemove = _config->FindB("APT::Get::HideAutoRemove"); | |
385 | ||
386 | pkgDepCache::ActionGroup group(*Cache); | |
387 | if(Debug) | |
388 | std::cout << "DoAutomaticRemove()" << std::endl; | |
389 | ||
390 | if (doAutoRemove == true && | |
391 | _config->FindB("APT::Get::Remove",true) == false) | |
392 | { | |
393 | c1out << _("We are not supposed to delete stuff, can't start " | |
394 | "AutoRemover") << std::endl; | |
395 | return false; | |
396 | } | |
397 | ||
398 | bool purgePkgs = _config->FindB("APT::Get::Purge", false); | |
399 | bool smallList = (hideAutoRemove == false && | |
400 | strcasecmp(_config->Find("APT::Get::HideAutoRemove","").c_str(),"small") == 0); | |
401 | ||
402 | unsigned long autoRemoveCount = 0; | |
403 | APT::PackageSet tooMuch; | |
404 | APT::PackageList autoRemoveList; | |
405 | // look over the cache to see what can be removed | |
406 | for (unsigned J = 0; J < Cache->Head().PackageCount; ++J) | |
407 | { | |
408 | pkgCache::PkgIterator Pkg(Cache,Cache.List[J]); | |
409 | if (Cache[Pkg].Garbage) | |
410 | { | |
411 | if(Pkg.CurrentVer() != 0 || Cache[Pkg].Install()) | |
412 | if(Debug) | |
413 | std::cout << "We could delete %s" << Pkg.FullName(true).c_str() << std::endl; | |
414 | ||
415 | if (doAutoRemove) | |
416 | { | |
417 | if(Pkg.CurrentVer() != 0 && | |
418 | Pkg->CurrentState != pkgCache::State::ConfigFiles) | |
419 | Cache->MarkDelete(Pkg, purgePkgs, 0, false); | |
420 | else | |
421 | Cache->MarkKeep(Pkg, false, false); | |
422 | } | |
423 | else | |
424 | { | |
425 | if (hideAutoRemove == false && Cache[Pkg].Delete() == false) | |
426 | autoRemoveList.insert(Pkg); | |
427 | // if the package is a new install and already garbage we don't need to | |
428 | // install it in the first place, so nuke it instead of show it | |
429 | if (Cache[Pkg].Install() == true && Pkg.CurrentVer() == 0) | |
430 | { | |
431 | if (Pkg.CandVersion() != 0) | |
432 | tooMuch.insert(Pkg); | |
433 | Cache->MarkDelete(Pkg, false, 0, false); | |
434 | } | |
435 | // only show stuff in the list that is not yet marked for removal | |
436 | else if(hideAutoRemove == false && Cache[Pkg].Delete() == false) | |
437 | ++autoRemoveCount; | |
438 | } | |
439 | } | |
440 | } | |
441 | ||
442 | // we could have removed a new dependency of a garbage package, | |
443 | // so check if a reverse depends is broken and if so install it again. | |
444 | if (tooMuch.empty() == false && (Cache->BrokenCount() != 0 || Cache->PolicyBrokenCount() != 0)) | |
445 | { | |
446 | bool Changed; | |
447 | do { | |
448 | Changed = false; | |
449 | for (APT::PackageSet::const_iterator Pkg = tooMuch.begin(); | |
3a7a206f | 450 | Pkg != tooMuch.end(); ++Pkg) |
b9179170 MV |
451 | { |
452 | APT::PackageSet too; | |
453 | too.insert(*Pkg); | |
454 | for (pkgCache::PrvIterator Prv = Cache[Pkg].CandidateVerIter(Cache).ProvidesList(); | |
455 | Prv.end() == false; ++Prv) | |
456 | too.insert(Prv.ParentPkg()); | |
3a7a206f DK |
457 | for (APT::PackageSet::const_iterator P = too.begin(); P != too.end(); ++P) |
458 | { | |
b9179170 MV |
459 | for (pkgCache::DepIterator R = P.RevDependsList(); |
460 | R.end() == false; ++R) | |
461 | { | |
462 | if (R.IsNegative() == true || | |
463 | Cache->IsImportantDep(R) == false) | |
464 | continue; | |
465 | pkgCache::PkgIterator N = R.ParentPkg(); | |
466 | if (N.end() == true || (N->CurrentVer == 0 && (*Cache)[N].Install() == false)) | |
467 | continue; | |
468 | if (Debug == true) | |
469 | std::clog << "Save " << Pkg << " as another installed garbage package depends on it" << std::endl; | |
470 | Cache->MarkInstall(Pkg, false, 0, false); | |
471 | if (hideAutoRemove == false) | |
472 | ++autoRemoveCount; | |
473 | tooMuch.erase(Pkg); | |
474 | Changed = true; | |
475 | break; | |
476 | } | |
3a7a206f DK |
477 | if (Changed == true) |
478 | break; | |
b9179170 | 479 | } |
3a7a206f DK |
480 | if (Changed == true) |
481 | break; | |
b9179170 MV |
482 | } |
483 | } while (Changed == true); | |
484 | } | |
485 | ||
486 | std::string autoremovelist, autoremoveversions; | |
487 | if (smallList == false && autoRemoveCount != 0) | |
488 | { | |
489 | for (APT::PackageList::const_iterator Pkg = autoRemoveList.begin(); Pkg != autoRemoveList.end(); ++Pkg) | |
490 | { | |
491 | if (Cache[Pkg].Garbage == false) | |
492 | continue; | |
493 | autoremovelist += Pkg.FullName(true) + " "; | |
494 | autoremoveversions += std::string(Cache[Pkg].CandVersion) + "\n"; | |
495 | } | |
496 | } | |
497 | ||
498 | // Now see if we had destroyed anything (if we had done anything) | |
499 | if (Cache->BrokenCount() != 0) | |
500 | { | |
501 | c1out << _("Hmm, seems like the AutoRemover destroyed something which really\n" | |
502 | "shouldn't happen. Please file a bug report against apt.") << std::endl; | |
503 | c1out << std::endl; | |
504 | c1out << _("The following information may help to resolve the situation:") << std::endl; | |
505 | c1out << std::endl; | |
506 | ShowBroken(c1out,Cache,false); | |
507 | ||
508 | return _error->Error(_("Internal Error, AutoRemover broke stuff")); | |
509 | } | |
510 | ||
511 | // if we don't remove them, we should show them! | |
512 | if (doAutoRemove == false && (autoremovelist.empty() == false || autoRemoveCount != 0)) | |
513 | { | |
514 | if (smallList == false) | |
515 | ShowList(c1out, P_("The following package was automatically installed and is no longer required:", | |
516 | "The following packages were automatically installed and are no longer required:", | |
517 | autoRemoveCount), autoremovelist, autoremoveversions); | |
518 | else | |
519 | ioprintf(c1out, P_("%lu package was automatically installed and is no longer required.\n", | |
520 | "%lu packages were automatically installed and are no longer required.\n", autoRemoveCount), autoRemoveCount); | |
521 | c1out << P_("Use 'apt-get autoremove' to remove it.", "Use 'apt-get autoremove' to remove them.", autoRemoveCount) << std::endl; | |
522 | } | |
523 | return true; | |
524 | } | |
525 | /*}}}*/ | |
ee0167c4 | 526 | // DoCacheManipulationFromCommandLine /*{{{*/ |
d8a8f9d7 MV |
527 | static const unsigned short MOD_REMOVE = 1; |
528 | static const unsigned short MOD_INSTALL = 2; | |
b9179170 | 529 | |
d8a8f9d7 MV |
530 | bool DoCacheManipulationFromCommandLine(CommandLine &CmdL, CacheFile &Cache) |
531 | { | |
532 | std::map<unsigned short, APT::VersionSet> verset; | |
533 | return DoCacheManipulationFromCommandLine(CmdL, Cache, verset); | |
534 | } | |
d8a8f9d7 MV |
535 | bool DoCacheManipulationFromCommandLine(CommandLine &CmdL, CacheFile &Cache, |
536 | std::map<unsigned short, APT::VersionSet> &verset) | |
b9179170 | 537 | { |
d8a8f9d7 | 538 | |
b9179170 MV |
539 | // Enter the special broken fixing mode if the user specified arguments |
540 | bool BrokenFix = false; | |
541 | if (Cache->BrokenCount() != 0) | |
542 | BrokenFix = true; | |
543 | ||
be0270de | 544 | SPtr<pkgProblemResolver> Fix; |
b9179170 MV |
545 | if (_config->FindB("APT::Get::CallResolver", true) == true) |
546 | Fix = new pkgProblemResolver(Cache); | |
547 | ||
b9179170 MV |
548 | unsigned short fallback = MOD_INSTALL; |
549 | if (strcasecmp(CmdL.FileList[0],"remove") == 0) | |
550 | fallback = MOD_REMOVE; | |
551 | else if (strcasecmp(CmdL.FileList[0], "purge") == 0) | |
552 | { | |
553 | _config->Set("APT::Get::Purge", true); | |
554 | fallback = MOD_REMOVE; | |
555 | } | |
556 | else if (strcasecmp(CmdL.FileList[0], "autoremove") == 0) | |
557 | { | |
558 | _config->Set("APT::Get::AutomaticRemove", "true"); | |
559 | fallback = MOD_REMOVE; | |
560 | } | |
561 | ||
562 | std::list<APT::VersionSet::Modifier> mods; | |
563 | mods.push_back(APT::VersionSet::Modifier(MOD_INSTALL, "+", | |
564 | APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::CANDIDATE)); | |
565 | mods.push_back(APT::VersionSet::Modifier(MOD_REMOVE, "-", | |
566 | APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::NEWEST)); | |
567 | CacheSetHelperAPTGet helper(c0out); | |
d8a8f9d7 | 568 | verset = APT::VersionSet::GroupedFromCommandLine(Cache, |
b9179170 MV |
569 | CmdL.FileList + 1, mods, fallback, helper); |
570 | ||
571 | if (_error->PendingError() == true) | |
572 | { | |
573 | helper.showVirtualPackageErrors(Cache); | |
b9179170 MV |
574 | return false; |
575 | } | |
576 | ||
577 | ||
578 | TryToInstall InstallAction(Cache, Fix, BrokenFix); | |
579 | TryToRemove RemoveAction(Cache, Fix); | |
580 | ||
581 | // new scope for the ActionGroup | |
582 | { | |
583 | pkgDepCache::ActionGroup group(Cache); | |
584 | unsigned short const order[] = { MOD_REMOVE, MOD_INSTALL, 0 }; | |
585 | ||
586 | for (unsigned short i = 0; order[i] != 0; ++i) | |
587 | { | |
588 | if (order[i] == MOD_INSTALL) | |
589 | InstallAction = std::for_each(verset[MOD_INSTALL].begin(), verset[MOD_INSTALL].end(), InstallAction); | |
590 | else if (order[i] == MOD_REMOVE) | |
591 | RemoveAction = std::for_each(verset[MOD_REMOVE].begin(), verset[MOD_REMOVE].end(), RemoveAction); | |
592 | } | |
593 | ||
594 | if (Fix != NULL && _config->FindB("APT::Get::AutoSolving", true) == true) | |
595 | { | |
596 | for (unsigned short i = 0; order[i] != 0; ++i) | |
597 | { | |
598 | if (order[i] != MOD_INSTALL) | |
599 | continue; | |
600 | InstallAction.propergateReleaseCandiateSwitching(helper.selectedByRelease, c0out); | |
601 | InstallAction.doAutoInstall(); | |
602 | } | |
603 | } | |
604 | ||
605 | if (_error->PendingError() == true) | |
606 | { | |
b9179170 MV |
607 | return false; |
608 | } | |
609 | ||
610 | /* If we are in the Broken fixing mode we do not attempt to fix the | |
611 | problems. This is if the user invoked install without -f and gave | |
612 | packages */ | |
613 | if (BrokenFix == true && Cache->BrokenCount() != 0) | |
614 | { | |
615 | c1out << _("You might want to run 'apt-get -f install' to correct these:") << std::endl; | |
616 | ShowBroken(c1out,Cache,false); | |
b9179170 MV |
617 | return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).")); |
618 | } | |
619 | ||
620 | if (Fix != NULL) | |
621 | { | |
622 | // Call the scored problem resolver | |
58377ceb DK |
623 | if (Fix->Resolve(true) == false && Cache->BrokenCount() == 0) |
624 | return false; | |
b9179170 MV |
625 | } |
626 | ||
627 | // Now we check the state of the packages, | |
628 | if (Cache->BrokenCount() != 0) | |
629 | { | |
630 | c1out << | |
631 | _("Some packages could not be installed. This may mean that you have\n" | |
632 | "requested an impossible situation or if you are using the unstable\n" | |
633 | "distribution that some required packages have not yet been created\n" | |
634 | "or been moved out of Incoming.") << std::endl; | |
635 | /* | |
636 | if (Packages == 1) | |
637 | { | |
638 | c1out << std::endl; | |
639 | c1out << | |
640 | _("Since you only requested a single operation it is extremely likely that\n" | |
641 | "the package is simply not installable and a bug report against\n" | |
642 | "that package should be filed.") << std::endl; | |
643 | } | |
644 | */ | |
645 | ||
646 | c1out << _("The following information may help to resolve the situation:") << std::endl; | |
647 | c1out << std::endl; | |
648 | ShowBroken(c1out,Cache,false); | |
649 | if (_error->PendingError() == true) | |
650 | return false; | |
651 | else | |
652 | return _error->Error(_("Broken packages")); | |
653 | } | |
654 | } | |
655 | if (!DoAutomaticRemove(Cache)) | |
656 | return false; | |
657 | ||
d8a8f9d7 MV |
658 | // if nothing changed in the cache, but only the automark information |
659 | // we write the StateFile here, otherwise it will be written in | |
660 | // cache.commit() | |
661 | if (InstallAction.AutoMarkChanged > 0 && | |
662 | Cache->DelCount() == 0 && Cache->InstCount() == 0 && | |
663 | Cache->BadCount() == 0 && | |
664 | _config->FindB("APT::Get::Simulate",false) == false) | |
665 | Cache->writeStateFile(NULL); | |
666 | ||
667 | return true; | |
668 | } | |
ee0167c4 | 669 | /*}}}*/ |
d8a8f9d7 MV |
670 | // DoInstall - Install packages from the command line /*{{{*/ |
671 | // --------------------------------------------------------------------- | |
672 | /* Install named packages */ | |
673 | bool DoInstall(CommandLine &CmdL) | |
674 | { | |
675 | CacheFile Cache; | |
0d29b9d4 MV |
676 | // first check for local pkgs and add them to the cache |
677 | for (const char **I = CmdL.FileList; *I != 0; I++) | |
678 | { | |
679 | if(FileExists(*I)) | |
680 | { | |
eafc5435 MV |
681 | // FIXME: make this more elegant |
682 | std::string TypeStr = flExtension(*I) + "-file"; | |
683 | pkgSourceList::Type *Type = pkgSourceList::Type::GetType(TypeStr.c_str()); | |
684 | if(Type != 0) | |
685 | { | |
686 | std::vector<metaIndex *> List; | |
687 | std::map<std::string, std::string> Options; | |
688 | if(Type->CreateItem(List, *I, "", "", Options)) | |
689 | { | |
aaf677da MV |
690 | // we have our own CacheFile that gives us a SourceList |
691 | // with superpowerz | |
692 | SourceList *sources = (SourceList*)Cache.GetSourceList(); | |
94f66115 | 693 | sources->AddMetaIndex(List[0]); |
eafc5435 MV |
694 | } |
695 | } | |
0d29b9d4 MV |
696 | } |
697 | } | |
698 | ||
699 | // then open the cache | |
d8a8f9d7 MV |
700 | if (Cache.OpenForInstall() == false || |
701 | Cache.CheckDeps(CmdL.FileSize() != 1) == false) | |
702 | return false; | |
0d29b9d4 | 703 | |
d8a8f9d7 MV |
704 | std::map<unsigned short, APT::VersionSet> verset; |
705 | ||
706 | if(!DoCacheManipulationFromCommandLine(CmdL, Cache, verset)) | |
707 | return false; | |
708 | ||
b9179170 MV |
709 | /* Print out a list of packages that are going to be installed extra |
710 | to what the user asked */ | |
711 | if (Cache->InstCount() != verset[MOD_INSTALL].size()) | |
712 | { | |
713 | std::string List; | |
714 | std::string VersionsList; | |
715 | for (unsigned J = 0; J < Cache->Head().PackageCount; J++) | |
716 | { | |
717 | pkgCache::PkgIterator I(Cache,Cache.List[J]); | |
718 | if ((*Cache)[I].Install() == false) | |
719 | continue; | |
720 | pkgCache::VerIterator Cand = Cache[I].CandidateVerIter(Cache); | |
721 | ||
722 | if (verset[MOD_INSTALL].find(Cand) != verset[MOD_INSTALL].end()) | |
723 | continue; | |
724 | ||
725 | List += I.FullName(true) + " "; | |
726 | VersionsList += std::string(Cache[I].CandVersion) + "\n"; | |
727 | } | |
728 | ||
729 | ShowList(c1out,_("The following extra packages will be installed:"),List,VersionsList); | |
730 | } | |
731 | ||
732 | /* Print out a list of suggested and recommended packages */ | |
733 | { | |
734 | std::string SuggestsList, RecommendsList; | |
735 | std::string SuggestsVersions, RecommendsVersions; | |
736 | for (unsigned J = 0; J < Cache->Head().PackageCount; J++) | |
737 | { | |
738 | pkgCache::PkgIterator Pkg(Cache,Cache.List[J]); | |
739 | ||
740 | /* Just look at the ones we want to install */ | |
741 | if ((*Cache)[Pkg].Install() == false) | |
742 | continue; | |
743 | ||
744 | // get the recommends/suggests for the candidate ver | |
745 | pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache); | |
746 | for (pkgCache::DepIterator D = CV.DependsList(); D.end() == false; ) | |
747 | { | |
748 | pkgCache::DepIterator Start; | |
749 | pkgCache::DepIterator End; | |
750 | D.GlobOr(Start,End); // advances D | |
751 | ||
752 | // FIXME: we really should display a or-group as a or-group to the user | |
753 | // the problem is that ShowList is incapable of doing this | |
754 | std::string RecommendsOrList,RecommendsOrVersions; | |
755 | std::string SuggestsOrList,SuggestsOrVersions; | |
756 | bool foundInstalledInOrGroup = false; | |
757 | for(;;) | |
758 | { | |
759 | /* Skip if package is installed already, or is about to be */ | |
760 | std::string target = Start.TargetPkg().FullName(true) + " "; | |
761 | pkgCache::PkgIterator const TarPkg = Start.TargetPkg(); | |
762 | if (TarPkg->SelectedState == pkgCache::State::Install || | |
763 | TarPkg->SelectedState == pkgCache::State::Hold || | |
764 | Cache[Start.TargetPkg()].Install()) | |
765 | { | |
766 | foundInstalledInOrGroup=true; | |
767 | break; | |
768 | } | |
769 | ||
770 | /* Skip if we already saw it */ | |
771 | if (int(SuggestsList.find(target)) != -1 || int(RecommendsList.find(target)) != -1) | |
772 | { | |
773 | foundInstalledInOrGroup=true; | |
774 | break; | |
775 | } | |
776 | ||
777 | // this is a dep on a virtual pkg, check if any package that provides it | |
778 | // should be installed | |
779 | if(Start.TargetPkg().ProvidesList() != 0) | |
780 | { | |
781 | pkgCache::PrvIterator I = Start.TargetPkg().ProvidesList(); | |
782 | for (; I.end() == false; ++I) | |
783 | { | |
784 | pkgCache::PkgIterator Pkg = I.OwnerPkg(); | |
785 | if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer() && | |
786 | Pkg.CurrentVer() != 0) | |
787 | foundInstalledInOrGroup=true; | |
788 | } | |
789 | } | |
790 | ||
791 | if (Start->Type == pkgCache::Dep::Suggests) | |
792 | { | |
793 | SuggestsOrList += target; | |
794 | SuggestsOrVersions += std::string(Cache[Start.TargetPkg()].CandVersion) + "\n"; | |
795 | } | |
796 | ||
797 | if (Start->Type == pkgCache::Dep::Recommends) | |
798 | { | |
799 | RecommendsOrList += target; | |
800 | RecommendsOrVersions += std::string(Cache[Start.TargetPkg()].CandVersion) + "\n"; | |
801 | } | |
802 | ||
803 | if (Start >= End) | |
804 | break; | |
805 | ++Start; | |
806 | } | |
807 | ||
808 | if(foundInstalledInOrGroup == false) | |
809 | { | |
810 | RecommendsList += RecommendsOrList; | |
811 | RecommendsVersions += RecommendsOrVersions; | |
812 | SuggestsList += SuggestsOrList; | |
813 | SuggestsVersions += SuggestsOrVersions; | |
814 | } | |
815 | ||
816 | } | |
817 | } | |
818 | ||
819 | ShowList(c1out,_("Suggested packages:"),SuggestsList,SuggestsVersions); | |
820 | ShowList(c1out,_("Recommended packages:"),RecommendsList,RecommendsVersions); | |
821 | ||
822 | } | |
823 | ||
b9179170 MV |
824 | // See if we need to prompt |
825 | // FIXME: check if really the packages in the set are going to be installed | |
826 | if (Cache->InstCount() == verset[MOD_INSTALL].size() && Cache->DelCount() == 0) | |
827 | return InstallPackages(Cache,false,false); | |
828 | ||
829 | return InstallPackages(Cache,false); | |
830 | } | |
831 | /*}}}*/ | |
4d695011 DK |
832 | |
833 | // TryToInstall - Mark a package for installation /*{{{*/ | |
834 | void TryToInstall::operator() (pkgCache::VerIterator const &Ver) { | |
835 | pkgCache::PkgIterator Pkg = Ver.ParentPkg(); | |
836 | ||
837 | Cache->GetDepCache()->SetCandidateVersion(Ver); | |
838 | pkgDepCache::StateCache &State = (*Cache)[Pkg]; | |
839 | ||
840 | // Handle the no-upgrade case | |
841 | if (_config->FindB("APT::Get::upgrade",true) == false && Pkg->CurrentVer != 0) | |
842 | ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"), | |
843 | Pkg.FullName(true).c_str()); | |
844 | // Ignore request for install if package would be new | |
845 | else if (_config->FindB("APT::Get::Only-Upgrade", false) == true && Pkg->CurrentVer == 0) | |
846 | ioprintf(c1out,_("Skipping %s, it is not installed and only upgrades are requested.\n"), | |
847 | Pkg.FullName(true).c_str()); | |
848 | else { | |
849 | if (Fix != NULL) { | |
850 | Fix->Clear(Pkg); | |
851 | Fix->Protect(Pkg); | |
852 | } | |
853 | Cache->GetDepCache()->MarkInstall(Pkg,false); | |
854 | ||
855 | if (State.Install() == false) { | |
856 | if (_config->FindB("APT::Get::ReInstall",false) == true) { | |
857 | if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false) | |
858 | ioprintf(c1out,_("Reinstallation of %s is not possible, it cannot be downloaded.\n"), | |
859 | Pkg.FullName(true).c_str()); | |
860 | else | |
861 | Cache->GetDepCache()->SetReInstall(Pkg, true); | |
862 | } else | |
863 | ioprintf(c1out,_("%s is already the newest version.\n"), | |
864 | Pkg.FullName(true).c_str()); | |
865 | } | |
866 | ||
867 | // Install it with autoinstalling enabled (if we not respect the minial | |
868 | // required deps or the policy) | |
869 | if (FixBroken == false) | |
870 | doAutoInstallLater.insert(Pkg); | |
871 | } | |
872 | ||
873 | // see if we need to fix the auto-mark flag | |
874 | // e.g. apt-get install foo | |
875 | // where foo is marked automatic | |
876 | if (State.Install() == false && | |
877 | (State.Flags & pkgCache::Flag::Auto) && | |
878 | _config->FindB("APT::Get::ReInstall",false) == false && | |
879 | _config->FindB("APT::Get::Only-Upgrade",false) == false && | |
880 | _config->FindB("APT::Get::Download-Only",false) == false) | |
881 | { | |
882 | ioprintf(c1out,_("%s set to manually installed.\n"), | |
883 | Pkg.FullName(true).c_str()); | |
884 | Cache->GetDepCache()->MarkAuto(Pkg,false); | |
885 | AutoMarkChanged++; | |
886 | } | |
887 | } | |
888 | /*}}}*/ | |
889 | bool TryToInstall::propergateReleaseCandiateSwitching(std::list<std::pair<pkgCache::VerIterator, std::string> > const &start, std::ostream &out)/*{{{*/ | |
890 | { | |
891 | for (std::list<std::pair<pkgCache::VerIterator, std::string> >::const_iterator s = start.begin(); | |
892 | s != start.end(); ++s) | |
893 | Cache->GetDepCache()->SetCandidateVersion(s->first); | |
894 | ||
895 | bool Success = true; | |
896 | // the Changed list contains: | |
897 | // first: "new version" | |
898 | // second: "what-caused the change" | |
899 | std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed; | |
900 | for (std::list<std::pair<pkgCache::VerIterator, std::string> >::const_iterator s = start.begin(); | |
901 | s != start.end(); ++s) | |
902 | { | |
903 | Changed.push_back(std::make_pair(s->first, pkgCache::VerIterator(*Cache))); | |
904 | // We continue here even if it failed to enhance the ShowBroken output | |
905 | Success &= Cache->GetDepCache()->SetCandidateRelease(s->first, s->second, Changed); | |
906 | } | |
907 | for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin(); | |
908 | c != Changed.end(); ++c) | |
909 | { | |
910 | if (c->second.end() == true) | |
911 | ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"), | |
912 | c->first.VerStr(), c->first.RelStr().c_str(), c->first.ParentPkg().FullName(true).c_str()); | |
913 | else if (c->first.ParentPkg()->Group != c->second.ParentPkg()->Group) | |
914 | { | |
915 | pkgCache::VerIterator V = (*Cache)[c->first.ParentPkg()].CandidateVerIter(*Cache); | |
916 | ioprintf(out, _("Selected version '%s' (%s) for '%s' because of '%s'\n"), V.VerStr(), | |
917 | V.RelStr().c_str(), V.ParentPkg().FullName(true).c_str(), c->second.ParentPkg().FullName(true).c_str()); | |
918 | } | |
919 | } | |
920 | return Success; | |
921 | } | |
922 | /*}}}*/ | |
923 | void TryToInstall::doAutoInstall() { /*{{{*/ | |
924 | for (APT::PackageSet::const_iterator P = doAutoInstallLater.begin(); | |
925 | P != doAutoInstallLater.end(); ++P) { | |
926 | pkgDepCache::StateCache &State = (*Cache)[P]; | |
927 | if (State.InstBroken() == false && State.InstPolicyBroken() == false) | |
928 | continue; | |
929 | Cache->GetDepCache()->MarkInstall(P, true); | |
930 | } | |
931 | doAutoInstallLater.clear(); | |
932 | } | |
933 | /*}}}*/ | |
934 | // TryToRemove - Mark a package for removal /*{{{*/ | |
935 | void TryToRemove::operator() (pkgCache::VerIterator const &Ver) | |
936 | { | |
937 | pkgCache::PkgIterator Pkg = Ver.ParentPkg(); | |
938 | ||
939 | if (Fix != NULL) | |
940 | { | |
941 | Fix->Clear(Pkg); | |
942 | Fix->Protect(Pkg); | |
943 | Fix->Remove(Pkg); | |
944 | } | |
945 | ||
946 | if ((Pkg->CurrentVer == 0 && PurgePkgs == false) || | |
947 | (PurgePkgs == true && Pkg->CurrentState == pkgCache::State::NotInstalled)) | |
948 | { | |
949 | pkgCache::GrpIterator Grp = Pkg.Group(); | |
950 | pkgCache::PkgIterator P = Grp.PackageList(); | |
951 | for (; P.end() != true; P = Grp.NextPkg(P)) | |
952 | { | |
953 | if (P == Pkg) | |
954 | continue; | |
955 | if (P->CurrentVer != 0 || (PurgePkgs == true && P->CurrentState != pkgCache::State::NotInstalled)) | |
956 | { | |
957 | // TRANSLATORS: Note, this is not an interactive question | |
958 | ioprintf(c1out,_("Package '%s' is not installed, so not removed. Did you mean '%s'?\n"), | |
959 | Pkg.FullName(true).c_str(), P.FullName(true).c_str()); | |
960 | break; | |
961 | } | |
962 | } | |
963 | if (P.end() == true) | |
964 | ioprintf(c1out,_("Package '%s' is not installed, so not removed\n"),Pkg.FullName(true).c_str()); | |
965 | ||
966 | // MarkInstall refuses to install packages on hold | |
967 | Pkg->SelectedState = pkgCache::State::Hold; | |
968 | } | |
969 | else | |
970 | Cache->GetDepCache()->MarkDelete(Pkg, PurgePkgs); | |
971 | } | |
972 | /*}}}*/ |