]> git.saurik.com Git - apt.git/blob - cmdline/apt-mark.cc
d9a93fa054a85316585896672e54f73c4d37a2d6
[apt.git] / cmdline / apt-mark.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* #####################################################################
4 apt-mark - show and change auto-installed bit information
5 ##################################################################### */
6 /*}}}*/
7 // Include Files /*{{{*/
8 #include <config.h>
9
10 #include <apt-pkg/cachefile.h>
11 #include <apt-pkg/cacheset.h>
12 #include <apt-pkg/cmndline.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/fileutl.h>
15 #include <apt-pkg/init.h>
16 #include <apt-pkg/pkgsystem.h>
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>
25 #include <apt-private/private-output.h>
26
27 #include <errno.h>
28 #include <fcntl.h>
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>
40
41 #include <apti18n.h>
42 /*}}}*/
43 using namespace std;
44
45 /* DoAuto - mark packages as automatically/manually installed {{{*/
46 static bool DoAuto(CommandLine &CmdL)
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
54 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
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
61 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
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 */
93 static bool DoMarkAuto(CommandLine &CmdL)
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
101 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
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
109 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
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) {{{*/
130 static bool ShowAuto(CommandLine &CmdL)
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
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 {{{*/
170 static bool DoHold(CommandLine &CmdL)
171 {
172 pkgCacheFile CacheFile;
173 pkgCache *Cache = CacheFile.GetPkgCache();
174 if (unlikely(Cache == NULL))
175 return false;
176
177 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
178 if (pkgset.empty() == true)
179 return _error->Error(_("No packages found"));
180
181 bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
182
183 auto const part = std::stable_partition(pkgset.begin(), pkgset.end(),
184 [](pkgCache::PkgIterator const &P) { return P->SelectedState == pkgCache::State::Hold; });
185
186 auto const doneBegin = MarkHold ? pkgset.begin() : part;
187 auto const doneEnd = MarkHold ? part : pkgset.end();
188 auto const changeBegin = MarkHold ? part : pkgset.begin();
189 auto const changeEnd = MarkHold ? pkgset.end() : part;
190
191 std::for_each(doneBegin, doneEnd, [&MarkHold](pkgCache::PkgIterator const &P) {
192 if (MarkHold == true)
193 ioprintf(c1out, _("%s was already set on hold.\n"), P.FullName(true).c_str());
194 else
195 ioprintf(c1out, _("%s was already not hold.\n"), P.FullName(true).c_str());
196 });
197
198 if (doneBegin == pkgset.begin() && doneEnd == pkgset.end())
199 return true;
200
201 if (_config->FindB("APT::Mark::Simulate", false) == true)
202 {
203 std::for_each(changeBegin, changeEnd, [&MarkHold](pkgCache::PkgIterator const &P) {
204 if (MarkHold == false)
205 ioprintf(c1out, _("%s set on hold.\n"), P.FullName(true).c_str());
206 else
207 ioprintf(c1out, _("Canceled hold on %s.\n"), P.FullName(true).c_str());
208 });
209 return true;
210 }
211
212 // Generate the base argument list for dpkg
213 std::vector<const char *> Args;
214 string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
215 {
216 string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/");
217 size_t dpkgChrootLen = dpkgChrootDir.length();
218 if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0)
219 {
220 if (dpkgChrootDir[dpkgChrootLen - 1] == '/')
221 --dpkgChrootLen;
222 Tmp = Tmp.substr(dpkgChrootLen);
223 }
224 }
225 Args.push_back(Tmp.c_str());
226
227 // Stick in any custom dpkg options
228 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
229 if (Opts != 0)
230 {
231 Opts = Opts->Child;
232 for (; Opts != 0; Opts = Opts->Next)
233 {
234 if (Opts->Value.empty() == true)
235 continue;
236 Args.push_back(Opts->Value.c_str());
237 }
238 }
239
240 APT::PackageList keepoffset;
241 std::copy_if(changeBegin, changeEnd, std::back_inserter(keepoffset),
242 [](pkgCache::PkgIterator const &P) { return P->CurrentVer == 0; });
243
244 if (keepoffset.empty() == false)
245 {
246 size_t const BaseArgs = Args.size();
247 Args.push_back("--merge-avail");
248 // FIXME: supported only since 1.17.7 in dpkg
249 Args.push_back("-");
250 Args.push_back(NULL);
251
252 int external[2] = {-1, -1};
253 if (pipe(external) != 0)
254 return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --merge-avail");
255
256 pid_t dpkgMergeAvail = ExecFork();
257 if (dpkgMergeAvail == 0)
258 {
259 close(external[1]);
260 std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
261 if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
262 _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --merge-avail", chrootDir.c_str());
263 dup2(external[0], STDIN_FILENO);
264 int const nullfd = open("/dev/null", O_RDONLY);
265 dup2(nullfd, STDOUT_FILENO);
266 execvp(Args[0], (char**) &Args[0]);
267 _error->WarningE("dpkgGo", "Can't get dpkg --merge-avail running!");
268 _exit(2);
269 }
270
271 FILE* dpkg = fdopen(external[1], "w");
272 for (APT::PackageList::iterator Pkg = keepoffset.begin(); Pkg != keepoffset.end(); ++Pkg)
273 {
274 char const * Arch;
275 if (Pkg->VersionList != 0)
276 Arch = Pkg.VersionList().Arch();
277 else
278 Arch = Pkg.Arch();
279 fprintf(dpkg, "Package: %s\nVersion: 0~\nArchitecture: %s\nMaintainer: Dummy Example <dummy@example.org>\n"
280 "Description: dummy package record\n A record is needed to put a package on hold, so here it is.\n\n", Pkg.Name(), Arch);
281 }
282 fclose(dpkg);
283 keepoffset.clear();
284
285 if (dpkgMergeAvail > 0)
286 {
287 int Status = 0;
288 while (waitpid(dpkgMergeAvail, &Status, 0) != dpkgMergeAvail)
289 {
290 if (errno == EINTR)
291 continue;
292 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --merge-avail");
293 break;
294 }
295 if (WIFEXITED(Status) == false || WEXITSTATUS(Status) != 0)
296 return _error->Error(_("Executing dpkg failed. Are you root?"));
297 }
298 Args.erase(Args.begin() + BaseArgs, Args.end());
299 }
300
301 Args.push_back("--set-selections");
302 Args.push_back(NULL);
303
304 int external[2] = {-1, -1};
305 if (pipe(external) != 0)
306 return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
307
308 pid_t dpkgSelection = ExecFork();
309 if (dpkgSelection == 0)
310 {
311 close(external[1]);
312 std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
313 if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
314 _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir.c_str());
315 dup2(external[0], STDIN_FILENO);
316 execvp(Args[0], (char**) &Args[0]);
317 _error->WarningE("dpkgGo", "Can't get dpkg --set-selections running!");
318 _exit(2);
319 }
320
321 bool const dpkgMultiArch = _system->MultiArchSupported();
322 FILE* dpkg = fdopen(external[1], "w");
323 for (auto Pkg = changeBegin; Pkg != changeEnd; ++Pkg)
324 {
325 if (dpkgMultiArch == false)
326 fprintf(dpkg, "%s", Pkg.FullName(true).c_str());
327 else
328 {
329 if (Pkg->CurrentVer != 0)
330 fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.CurrentVer().Arch());
331 else if (Pkg.VersionList().end() == false)
332 fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.VersionList().Arch());
333 else
334 fprintf(dpkg, "%s", Pkg.FullName(false).c_str());
335 }
336
337 if (MarkHold == true)
338 {
339 fprintf(dpkg, " hold\n");
340 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
341 }
342 else
343 {
344 fprintf(dpkg, " install\n");
345 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
346 }
347 }
348 fclose(dpkg);
349
350 if (dpkgSelection > 0)
351 {
352 int Status = 0;
353 while (waitpid(dpkgSelection, &Status, 0) != dpkgSelection)
354 {
355 if (errno == EINTR)
356 continue;
357 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
358 break;
359 }
360 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
361 return true;
362 }
363 return _error->Error(_("Executing dpkg failed. Are you root?"));
364 }
365 /*}}}*/
366 /* ShowHold - show packages set on hold in dpkg status {{{*/
367 static bool ShowHold(CommandLine &CmdL)
368 {
369 pkgCacheFile CacheFile;
370 pkgCache *Cache = CacheFile.GetPkgCache();
371 if (unlikely(Cache == NULL))
372 return false;
373
374 std::vector<string> packages;
375
376 if (CmdL.FileList[1] == 0)
377 {
378 packages.reserve(50); // how many holds are realistic? I hope just a few…
379 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
380 if (P->SelectedState == pkgCache::State::Hold)
381 packages.push_back(P.FullName(true));
382 }
383 else
384 {
385 APT::CacheSetHelper helper(false); // do not show errors
386 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
387 packages.reserve(pkgset.size());
388 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
389 if (P->SelectedState == pkgCache::State::Hold)
390 packages.push_back(P.FullName(true));
391 }
392
393 std::sort(packages.begin(), packages.end());
394
395 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
396 std::cout << *I << std::endl;
397
398 return true;
399 }
400 /*}}}*/
401 // ShowHelp - Show a help screen /*{{{*/
402 // ---------------------------------------------------------------------
403 /* */
404 static bool ShowHelp(CommandLine &)
405 {
406 ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
407
408 cout <<
409 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
410 "\n"
411 "apt-mark is a simple command line interface for marking packages\n"
412 "as manually or automatically installed. It can also list marks.\n"
413 "\n"
414 "Commands:\n"
415 " auto - Mark the given packages as automatically installed\n"
416 " manual - Mark the given packages as manually installed\n"
417 " hold - Mark a package as held back\n"
418 " unhold - Unset a package set as held back\n"
419 " showauto - Print the list of automatically installed packages\n"
420 " showmanual - Print the list of manually installed packages\n"
421 " showhold - Print the list of package on hold\n"
422 "\n"
423 "Options:\n"
424 " -h This help text.\n"
425 " -q Loggable output - no progress indicator\n"
426 " -qq No output except for errors\n"
427 " -s No-act. Just prints what would be done.\n"
428 " -f read/write auto/manual marking in the given file\n"
429 " -c=? Read this configuration file\n"
430 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
431 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
432 << std::endl;
433 return true;
434 }
435 /*}}}*/
436 int main(int argc,const char *argv[]) /*{{{*/
437 {
438 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
439 {"auto",&DoAuto},
440 {"manual",&DoAuto},
441 {"hold",&DoHold},
442 {"unhold",&DoHold},
443 {"showauto",&ShowAuto},
444 {"showmanual",&ShowAuto},
445 {"showhold",&ShowHold},
446 // be nice and forgive the typo
447 {"showholds",&ShowHold},
448 // be nice and forgive it as it is technical right
449 {"install",&DoHold},
450 // obsolete commands for compatibility
451 {"markauto", &DoMarkAuto},
452 {"unmarkauto", &DoMarkAuto},
453 {0,0}};
454
455 std::vector<CommandLine::Args> Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv));
456
457 // Set up gettext support
458 setlocale(LC_ALL,"");
459 textdomain(PACKAGE);
460
461 CommandLine CmdL;
462 ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
463
464 InitOutput();
465
466 // Match the operation
467 CmdL.DispatchArg(Cmds);
468
469 // Print any errors or warnings found during parsing
470 bool const Errors = _error->PendingError();
471 if (_config->FindI("quiet",0) > 0)
472 _error->DumpErrors();
473 else
474 _error->DumpErrors(GlobalError::DEBUG);
475 return Errors == true ? 100 : 0;
476 }
477 /*}}}*/