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