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