]> git.saurik.com Git - apt.git/blame - cmdline/apt-mark.cc
Provide "apt-get full-upgrade" to match "apt full-upgrade"
[apt.git] / cmdline / apt-mark.cc
CommitLineData
c98fb5e0
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* #####################################################################
4 apt-mark - show and change auto-installed bit information
5 ##################################################################### */
6 /*}}}*/
7// Include Files /*{{{*/
ea542140
DK
8#include <config.h>
9
c98fb5e0
DK
10#include <apt-pkg/cachefile.h>
11#include <apt-pkg/cacheset.h>
12#include <apt-pkg/cmndline.h>
13#include <apt-pkg/error.h>
453b82a3 14#include <apt-pkg/fileutl.h>
c98fb5e0 15#include <apt-pkg/init.h>
472ff00e 16#include <apt-pkg/pkgsystem.h>
453b82a3
DK
17#include <apt-pkg/strutl.h>
18#include <apt-pkg/cacheiterators.h>
19#include <apt-pkg/configuration.h>
20#include <apt-pkg/depcache.h>
21#include <apt-pkg/macros.h>
22#include <apt-pkg/pkgcache.h>
23
24#include <apt-private/private-cmndline.h>
d9e518c6 25#include <apt-private/private-output.h>
c98fb5e0 26
6fddb156 27#include <errno.h>
6fddb156 28#include <fcntl.h>
453b82a3
DK
29#include <stddef.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/wait.h>
34#include <unistd.h>
35#include <algorithm>
36#include <fstream>
37#include <iostream>
38#include <string>
39#include <vector>
b9179170 40
ea542140 41#include <apti18n.h>
c98fb5e0
DK
42 /*}}}*/
43using namespace std;
44
c98fb5e0 45/* DoAuto - mark packages as automatically/manually installed {{{*/
c3ccac92 46static bool DoAuto(CommandLine &CmdL)
c98fb5e0
DK
47{
48 pkgCacheFile CacheFile;
49 pkgCache *Cache = CacheFile.GetPkgCache();
50 pkgDepCache *DepCache = CacheFile.GetDepCache();
51 if (unlikely(Cache == NULL || DepCache == NULL))
52 return false;
53
c4cca791 54 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
c98fb5e0
DK
55 if (pkgset.empty() == true)
56 return _error->Error(_("No packages found"));
57
58 bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0;
59 int AutoMarkChanged = 0;
60
c4cca791 61 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
c98fb5e0
DK
62 {
63 if (Pkg->CurrentVer == 0)
64 {
65 ioprintf(c1out,_("%s can not be marked as it is not installed.\n"), Pkg.FullName(true).c_str());
66 continue;
67 }
68 else if ((((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
69 {
70 if (MarkAuto == false)
71 ioprintf(c1out,_("%s was already set to manually installed.\n"), Pkg.FullName(true).c_str());
72 else
73 ioprintf(c1out,_("%s was already set to automatically installed.\n"), Pkg.FullName(true).c_str());
74 continue;
75 }
76
77 if (MarkAuto == false)
78 ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.FullName(true).c_str());
79 else
80 ioprintf(c1out,_("%s set to automatically installed.\n"), Pkg.FullName(true).c_str());
81
82 DepCache->MarkAuto(Pkg, MarkAuto);
83 ++AutoMarkChanged;
84 }
85 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
86 return DepCache->writeStateFile(NULL);
87 return true;
88}
89 /*}}}*/
90/* DoMarkAuto - mark packages as automatically/manually installed {{{*/
91/* Does the same as DoAuto but tries to do it exactly the same why as
92 the python implementation did it so it can be a drop-in replacement */
c3ccac92 93static bool DoMarkAuto(CommandLine &CmdL)
c98fb5e0
DK
94{
95 pkgCacheFile CacheFile;
96 pkgCache *Cache = CacheFile.GetPkgCache();
97 pkgDepCache *DepCache = CacheFile.GetDepCache();
98 if (unlikely(Cache == NULL || DepCache == NULL))
99 return false;
100
c4cca791 101 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
c98fb5e0
DK
102 if (pkgset.empty() == true)
103 return _error->Error(_("No packages found"));
104
105 bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
106 bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
107 int AutoMarkChanged = 0;
108
c4cca791 109 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
c98fb5e0
DK
110 {
111 if (Pkg->CurrentVer == 0 ||
112 (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
113 continue;
114
115 if (Verbose == true)
116 ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
117
118 DepCache->MarkAuto(Pkg, MarkAuto);
119 ++AutoMarkChanged;
120 }
121 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
122 return DepCache->writeStateFile(NULL);
123
124 _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
125
126 return true;
127}
128 /*}}}*/
129/* ShowAuto - show automatically installed packages (sorted) {{{*/
c3ccac92 130static bool ShowAuto(CommandLine &CmdL)
c98fb5e0
DK
131{
132 pkgCacheFile CacheFile;
133 pkgCache *Cache = CacheFile.GetPkgCache();
134 pkgDepCache *DepCache = CacheFile.GetDepCache();
135 if (unlikely(Cache == NULL || DepCache == NULL))
136 return false;
137
138 std::vector<string> packages;
139
140 bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
141
142 if (CmdL.FileList[1] == 0)
143 {
144 packages.reserve(Cache->HeaderP->PackageCount / 3);
145 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
146 if (P->CurrentVer != 0 &&
147 (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
148 packages.push_back(P.FullName(true));
149 }
150 else
151 {
152 APT::CacheSetHelper helper(false); // do not show errors
153 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
154 packages.reserve(pkgset.size());
155 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
156 if (P->CurrentVer != 0 &&
157 (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
158 packages.push_back(P.FullName(true));
159 }
160
161 std::sort(packages.begin(), packages.end());
162
a09e4489
DK
163 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
164 std::cout << *I << std::endl;
165
166 return true;
167}
168 /*}}}*/
169/* DoHold - mark packages as hold by dpkg {{{*/
c3ccac92 170static bool DoHold(CommandLine &CmdL)
a09e4489
DK
171{
172 pkgCacheFile CacheFile;
173 pkgCache *Cache = CacheFile.GetPkgCache();
174 if (unlikely(Cache == NULL))
175 return false;
176
6fddb156
DK
177 // Generate the base argument list for dpkg
178 std::vector<const char *> Args;
179 string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
180 {
181 string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/");
182 size_t dpkgChrootLen = dpkgChrootDir.length();
183 if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0)
184 {
185 if (dpkgChrootDir[dpkgChrootLen - 1] == '/')
186 --dpkgChrootLen;
187 Tmp = Tmp.substr(dpkgChrootLen);
188 }
189 }
190 Args.push_back(Tmp.c_str());
191
192 // Stick in any custom dpkg options
193 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
194 if (Opts != 0)
195 {
196 Opts = Opts->Child;
197 for (; Opts != 0; Opts = Opts->Next)
198 {
199 if (Opts->Value.empty() == true)
200 continue;
201 Args.push_back(Opts->Value.c_str());
202 }
203 }
204
205 size_t const BaseArgs = Args.size();
206 // we need to detect if we can qualify packages with the architecture or not
207 Args.push_back("--assert-multi-arch");
208 Args.push_back(NULL);
209
210
211 pid_t dpkgAssertMultiArch = ExecFork();
212 if (dpkgAssertMultiArch == 0)
213 {
214 std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
6fddb156
DK
215 // redirect everything to the ultimate sink as we only need the exit-status
216 int const nullfd = open("/dev/null", O_RDONLY);
217 dup2(nullfd, STDIN_FILENO);
218 dup2(nullfd, STDOUT_FILENO);
219 dup2(nullfd, STDERR_FILENO);
2510eea4 220 if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
36a0c0f7 221 _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --assert-multi-arch", chrootDir.c_str());
6fddb156
DK
222 execvp(Args[0], (char**) &Args[0]);
223 _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
224 _exit(2);
225 }
226
c4cca791 227 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
a09e4489
DK
228 if (pkgset.empty() == true)
229 return _error->Error(_("No packages found"));
230
231 bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
232
5eb9a474 233 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end();)
a09e4489
DK
234 {
235 if ((Pkg->SelectedState == pkgCache::State::Hold) == MarkHold)
236 {
237 if (MarkHold == true)
238 ioprintf(c1out,_("%s was already set on hold.\n"), Pkg.FullName(true).c_str());
239 else
240 ioprintf(c1out,_("%s was already not hold.\n"), Pkg.FullName(true).c_str());
c687384b 241 Pkg = pkgset.erase(Pkg);
a09e4489 242 }
5eb9a474
DK
243 else
244 ++Pkg;
a09e4489
DK
245 }
246
6fddb156
DK
247 bool dpkgMultiArch = false;
248 if (dpkgAssertMultiArch > 0)
249 {
250 int Status = 0;
251 while (waitpid(dpkgAssertMultiArch, &Status, 0) != dpkgAssertMultiArch)
252 {
253 if (errno == EINTR)
254 continue;
255 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
256 break;
257 }
258 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
259 dpkgMultiArch = true;
260 }
261
a09e4489
DK
262 if (pkgset.empty() == true)
263 return true;
264
265 if (_config->FindB("APT::Mark::Simulate", false) == true)
266 {
c4cca791 267 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
a09e4489
DK
268 {
269 if (MarkHold == false)
270 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
271 else
272 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
273 }
274 return true;
275 }
276
1d838084 277 APT::PackageList keepoffset;
374f8492
DK
278 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
279 {
280 if (Pkg->CurrentVer != 0)
281 continue;
3bf4c93a 282 keepoffset.insert(*Pkg);
374f8492 283 }
374f8492 284
1d838084 285 if (keepoffset.empty() == false)
374f8492 286 {
1d838084
DK
287 Args.erase(Args.begin() + BaseArgs, Args.end());
288 Args.push_back("--merge-avail");
289 // FIXME: supported only since 1.17.7 in dpkg
290 Args.push_back("-");
291 Args.push_back(NULL);
292
293 int external[2] = {-1, -1};
294 if (pipe(external) != 0)
295 return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --merge-avail");
296
297 pid_t dpkgMergeAvail = ExecFork();
298 if (dpkgMergeAvail == 0)
374f8492 299 {
1d838084
DK
300 close(external[1]);
301 std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
302 if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
303 _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --merge-avail", chrootDir.c_str());
304 dup2(external[0], STDIN_FILENO);
305 int const nullfd = open("/dev/null", O_RDONLY);
306 dup2(nullfd, STDOUT_FILENO);
307 execvp(Args[0], (char**) &Args[0]);
308 _error->WarningE("dpkgGo", "Can't get dpkg --merge-avail running!");
309 _exit(2);
310 }
311
312 FILE* dpkg = fdopen(external[1], "w");
313 for (APT::PackageList::iterator Pkg = keepoffset.begin(); Pkg != keepoffset.end(); ++Pkg)
314 {
315 char const * Arch;
316 if (Pkg->VersionList != 0)
317 Arch = Pkg.VersionList().Arch();
318 else
319 Arch = Pkg.Arch();
320 fprintf(dpkg, "Package: %s\nVersion: 0~\nArchitecture: %s\nMaintainer: Dummy Example <dummy@example.org>\n"
321 "Description: dummy package record\n A record is needed to put a package on hold, so here it is.\n\n", Pkg.Name(), Arch);
322 }
323 fclose(dpkg);
324 keepoffset.clear();
325
326 if (dpkgMergeAvail > 0)
327 {
328 int Status = 0;
329 while (waitpid(dpkgMergeAvail, &Status, 0) != dpkgMergeAvail)
330 {
331 if (errno == EINTR)
332 continue;
333 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --merge-avail");
334 break;
335 }
336 if (WIFEXITED(Status) == false || WEXITSTATUS(Status) != 0)
337 return _error->Error(_("Executing dpkg failed. Are you root?"));
374f8492 338 }
374f8492
DK
339 }
340
341 Args.erase(Args.begin() + BaseArgs, Args.end());
342 Args.push_back("--set-selections");
343 Args.push_back(NULL);
344
1d838084 345 int external[2] = {-1, -1};
6fddb156
DK
346 if (pipe(external) != 0)
347 return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
348
349 pid_t dpkgSelection = ExecFork();
350 if (dpkgSelection == 0)
351 {
352 close(external[1]);
353 std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
2510eea4 354 if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
6fddb156 355 _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir.c_str());
5834d7a1 356 dup2(external[0], STDIN_FILENO);
6fddb156 357 execvp(Args[0], (char**) &Args[0]);
374f8492 358 _error->WarningE("dpkgGo", "Can't get dpkg --set-selections running!");
6fddb156
DK
359 _exit(2);
360 }
a09e4489 361
1d838084 362 FILE* dpkg = fdopen(external[1], "w");
c4cca791 363 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
a09e4489 364 {
b855a400
DK
365 if (dpkgMultiArch == false)
366 fprintf(dpkg, "%s", Pkg.FullName(true).c_str());
367 else
368 {
369 if (Pkg->CurrentVer != 0)
370 fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.CurrentVer().Arch());
371 else if (Pkg.VersionList().end() == false)
372 fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.VersionList().Arch());
373 else
374 fprintf(dpkg, "%s", Pkg.FullName(false).c_str());
375 }
376
a09e4489
DK
377 if (MarkHold == true)
378 {
b855a400 379 fprintf(dpkg, " hold\n");
a09e4489
DK
380 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
381 }
382 else
383 {
b855a400 384 fprintf(dpkg, " install\n");
a09e4489
DK
385 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
386 }
387 }
6fddb156 388 fclose(dpkg);
a09e4489 389
6fddb156
DK
390 if (dpkgSelection > 0)
391 {
392 int Status = 0;
393 while (waitpid(dpkgSelection, &Status, 0) != dpkgSelection)
394 {
395 if (errno == EINTR)
396 continue;
397 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
398 break;
399 }
400 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
401 return true;
402 }
403 return _error->Error(_("Executing dpkg failed. Are you root?"));
a09e4489
DK
404}
405 /*}}}*/
406/* ShowHold - show packages set on hold in dpkg status {{{*/
c3ccac92 407static bool ShowHold(CommandLine &CmdL)
a09e4489
DK
408{
409 pkgCacheFile CacheFile;
410 pkgCache *Cache = CacheFile.GetPkgCache();
411 if (unlikely(Cache == NULL))
412 return false;
413
414 std::vector<string> packages;
415
416 if (CmdL.FileList[1] == 0)
417 {
418 packages.reserve(50); // how many holds are realistic? I hope just a few…
419 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
420 if (P->SelectedState == pkgCache::State::Hold)
421 packages.push_back(P.FullName(true));
422 }
423 else
424 {
425 APT::CacheSetHelper helper(false); // do not show errors
426 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
427 packages.reserve(pkgset.size());
428 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
429 if (P->SelectedState == pkgCache::State::Hold)
430 packages.push_back(P.FullName(true));
431 }
432
433 std::sort(packages.begin(), packages.end());
434
c98fb5e0
DK
435 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
436 std::cout << *I << std::endl;
437
438 return true;
439}
440 /*}}}*/
441// ShowHelp - Show a help screen /*{{{*/
442// ---------------------------------------------------------------------
443/* */
65512241 444static bool ShowHelp(CommandLine &)
c98fb5e0 445{
249aec3b 446 ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
c98fb5e0
DK
447
448 cout <<
449 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
450 "\n"
451 "apt-mark is a simple command line interface for marking packages\n"
3999d158 452 "as manually or automatically installed. It can also list marks.\n"
c98fb5e0
DK
453 "\n"
454 "Commands:\n"
455 " auto - Mark the given packages as automatically installed\n"
456 " manual - Mark the given packages as manually installed\n"
1aef66d5
MV
457 " hold - Mark a package as held back\n"
458 " unhold - Unset a package set as held back\n"
459 " showauto - Print the list of automatically installed packages\n"
460 " showmanual - Print the list of manually installed packages\n"
461 " showhold - Print the list of package on hold\n"
c98fb5e0
DK
462 "\n"
463 "Options:\n"
464 " -h This help text.\n"
465 " -q Loggable output - no progress indicator\n"
466 " -qq No output except for errors\n"
467 " -s No-act. Just prints what would be done.\n"
468 " -f read/write auto/manual marking in the given file\n"
469 " -c=? Read this configuration file\n"
470 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
471 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
472 << std::endl;
473 return true;
474}
475 /*}}}*/
476int main(int argc,const char *argv[]) /*{{{*/
477{
c98fb5e0
DK
478 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
479 {"auto",&DoAuto},
480 {"manual",&DoAuto},
a09e4489
DK
481 {"hold",&DoHold},
482 {"unhold",&DoHold},
c98fb5e0
DK
483 {"showauto",&ShowAuto},
484 {"showmanual",&ShowAuto},
a09e4489
DK
485 {"showhold",&ShowHold},
486 // be nice and forgive the typo
487 {"showholds",&ShowHold},
488 // be nice and forgive it as it is technical right
489 {"install",&DoHold},
c98fb5e0
DK
490 // obsolete commands for compatibility
491 {"markauto", &DoMarkAuto},
492 {"unmarkauto", &DoMarkAuto},
493 {0,0}};
494
b9179170
MV
495 std::vector<CommandLine::Args> Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv));
496
c98fb5e0
DK
497 // Set up gettext support
498 setlocale(LC_ALL,"");
499 textdomain(PACKAGE);
500
ad7e0941
DK
501 CommandLine CmdL;
502 ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
c98fb5e0 503
d9e518c6 504 InitOutput();
c98fb5e0
DK
505
506 // Match the operation
507 CmdL.DispatchArg(Cmds);
508
509 // Print any errors or warnings found during parsing
510 bool const Errors = _error->PendingError();
511 if (_config->FindI("quiet",0) > 0)
512 _error->DumpErrors();
513 else
514 _error->DumpErrors(GlobalError::DEBUG);
515 return Errors == true ? 100 : 0;
516}
517 /*}}}*/