1 // -*- mode: cpp; mode: fold -*-
3 /* #####################################################################
4 apt-mark - show and change auto-installed bit information
5 ##################################################################### */
7 // Include Files /*{{{*/
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>
24 #include <apt-private/private-cmndline.h>
47 ofstream
devnull("/dev/null");
48 /* DoAuto - mark packages as automatically/manually installed {{{*/
49 static bool DoAuto(CommandLine
&CmdL
)
51 pkgCacheFile CacheFile
;
52 pkgCache
*Cache
= CacheFile
.GetPkgCache();
53 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
54 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
57 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
58 if (pkgset
.empty() == true)
59 return _error
->Error(_("No packages found"));
61 bool MarkAuto
= strcasecmp(CmdL
.FileList
[0],"auto") == 0;
62 int AutoMarkChanged
= 0;
64 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
66 if (Pkg
->CurrentVer
== 0)
68 ioprintf(c1out
,_("%s can not be marked as it is not installed.\n"), Pkg
.FullName(true).c_str());
71 else if ((((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
73 if (MarkAuto
== false)
74 ioprintf(c1out
,_("%s was already set to manually installed.\n"), Pkg
.FullName(true).c_str());
76 ioprintf(c1out
,_("%s was already set to automatically installed.\n"), Pkg
.FullName(true).c_str());
80 if (MarkAuto
== false)
81 ioprintf(c1out
,_("%s set to manually installed.\n"), Pkg
.FullName(true).c_str());
83 ioprintf(c1out
,_("%s set to automatically installed.\n"), Pkg
.FullName(true).c_str());
85 DepCache
->MarkAuto(Pkg
, MarkAuto
);
88 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
89 return DepCache
->writeStateFile(NULL
);
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
)
98 pkgCacheFile CacheFile
;
99 pkgCache
*Cache
= CacheFile
.GetPkgCache();
100 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
101 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
104 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
105 if (pkgset
.empty() == true)
106 return _error
->Error(_("No packages found"));
108 bool const MarkAuto
= strcasecmp(CmdL
.FileList
[0],"markauto") == 0;
109 bool const Verbose
= _config
->FindB("APT::MarkAuto::Verbose", false);
110 int AutoMarkChanged
= 0;
112 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
114 if (Pkg
->CurrentVer
== 0 ||
115 (((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
119 ioprintf(c1out
, "changing %s to %d\n", Pkg
.Name(), (MarkAuto
== false) ? 0 : 1);
121 DepCache
->MarkAuto(Pkg
, MarkAuto
);
124 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
125 return DepCache
->writeStateFile(NULL
);
127 _error
->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
132 /* ShowAuto - show automatically installed packages (sorted) {{{*/
133 static bool ShowAuto(CommandLine
&CmdL
)
135 pkgCacheFile CacheFile
;
136 pkgCache
*Cache
= CacheFile
.GetPkgCache();
137 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
138 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
141 std::vector
<string
> packages
;
143 bool const ShowAuto
= strcasecmp(CmdL
.FileList
[0],"showauto") == 0;
145 if (CmdL
.FileList
[1] == 0)
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));
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));
164 std::sort(packages
.begin(), packages
.end());
166 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
167 std::cout
<< *I
<< std::endl
;
172 /* DoHold - mark packages as hold by dpkg {{{*/
173 static bool DoHold(CommandLine
&CmdL
)
175 pkgCacheFile CacheFile
;
176 pkgCache
*Cache
= CacheFile
.GetPkgCache();
177 if (unlikely(Cache
== NULL
))
180 // Generate the base argument list for dpkg
181 std::vector
<const char *> Args
;
182 string Tmp
= _config
->Find("Dir::Bin::dpkg","dpkg");
184 string
const dpkgChrootDir
= _config
->FindDir("DPkg::Chroot-Directory", "/");
185 size_t dpkgChrootLen
= dpkgChrootDir
.length();
186 if (dpkgChrootDir
!= "/" && Tmp
.find(dpkgChrootDir
) == 0)
188 if (dpkgChrootDir
[dpkgChrootLen
- 1] == '/')
190 Tmp
= Tmp
.substr(dpkgChrootLen
);
193 Args
.push_back(Tmp
.c_str());
195 // Stick in any custom dpkg options
196 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
200 for (; Opts
!= 0; Opts
= Opts
->Next
)
202 if (Opts
->Value
.empty() == true)
204 Args
.push_back(Opts
->Value
.c_str());
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
);
214 pid_t dpkgAssertMultiArch
= ExecFork();
215 if (dpkgAssertMultiArch
== 0)
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!");
230 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
231 if (pkgset
.empty() == true)
232 return _error
->Error(_("No packages found"));
234 bool const MarkHold
= strcasecmp(CmdL
.FileList
[0],"hold") == 0;
236 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end();)
238 if ((Pkg
->SelectedState
== pkgCache::State::Hold
) == MarkHold
)
240 if (MarkHold
== true)
241 ioprintf(c1out
,_("%s was already set on hold.\n"), Pkg
.FullName(true).c_str());
243 ioprintf(c1out
,_("%s was already not hold.\n"), Pkg
.FullName(true).c_str());
244 Pkg
= pkgset
.erase(Pkg
, true);
250 bool dpkgMultiArch
= false;
251 if (dpkgAssertMultiArch
> 0)
254 while (waitpid(dpkgAssertMultiArch
, &Status
, 0) != dpkgAssertMultiArch
)
258 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
261 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
262 dpkgMultiArch
= true;
265 if (pkgset
.empty() == true)
268 if (_config
->FindB("APT::Mark::Simulate", false) == true)
270 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
272 if (MarkHold
== false)
273 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
275 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
280 Args
.erase(Args
.begin() + BaseArgs
, Args
.end());
281 Args
.push_back("--set-selections");
282 Args
.push_back(NULL
);
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");
288 pid_t dpkgSelection
= ExecFork();
289 if (dpkgSelection
== 0)
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!");
304 FILE* dpkg
= fdopen(external
[1], "w");
305 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
307 if (dpkgMultiArch
== false)
308 fprintf(dpkg
, "%s", Pkg
.FullName(true).c_str());
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());
316 fprintf(dpkg
, "%s", Pkg
.FullName(false).c_str());
319 if (MarkHold
== true)
321 fprintf(dpkg
, " hold\n");
322 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
326 fprintf(dpkg
, " install\n");
327 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
332 if (dpkgSelection
> 0)
335 while (waitpid(dpkgSelection
, &Status
, 0) != dpkgSelection
)
339 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
342 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
345 return _error
->Error(_("Executing dpkg failed. Are you root?"));
348 /* ShowHold - show packages set on hold in dpkg status {{{*/
349 static bool ShowHold(CommandLine
&CmdL
)
351 pkgCacheFile CacheFile
;
352 pkgCache
*Cache
= CacheFile
.GetPkgCache();
353 if (unlikely(Cache
== NULL
))
356 std::vector
<string
> packages
;
358 if (CmdL
.FileList
[1] == 0)
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));
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));
375 std::sort(packages
.begin(), packages
.end());
377 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
378 std::cout
<< *I
<< std::endl
;
383 // ShowHelp - Show a help screen /*{{{*/
384 // ---------------------------------------------------------------------
386 static bool ShowHelp(CommandLine
&)
388 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
389 COMMON_ARCH
,__DATE__
,__TIME__
);
392 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\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"
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"
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.")
419 int main(int argc
,const char *argv
[]) /*{{{*/
421 CommandLine::Dispatch Cmds
[] = {{"help",&ShowHelp
},
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
433 // obsolete commands for compatibility
434 {"markauto", &DoMarkAuto
},
435 {"unmarkauto", &DoMarkAuto
},
438 std::vector
<CommandLine::Args
> Args
= getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds
, argc
, argv
));
440 // Set up gettext support
441 setlocale(LC_ALL
,"");
445 ParseCommandLine(CmdL
, Cmds
, Args
.data(), &_config
, &_system
, argc
, argv
, ShowHelp
);
447 // Deal with stdout not being a tty
448 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
449 _config
->Set("quiet","1");
451 // Setup the output streams
452 c0out
.rdbuf(cout
.rdbuf());
453 c1out
.rdbuf(cout
.rdbuf());
454 c2out
.rdbuf(cout
.rdbuf());
455 if (_config
->FindI("quiet",0) > 0)
456 c0out
.rdbuf(devnull
.rdbuf());
457 if (_config
->FindI("quiet",0) > 1)
458 c1out
.rdbuf(devnull
.rdbuf());
460 // Match the operation
461 CmdL
.DispatchArg(Cmds
);
463 // Print any errors or warnings found during parsing
464 bool const Errors
= _error
->PendingError();
465 if (_config
->FindI("quiet",0) > 0)
466 _error
->DumpErrors();
468 _error
->DumpErrors(GlobalError::DEBUG
);
469 return Errors
== true ? 100 : 0;