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>
25 #include <apt-private/private-output.h>
45 /* DoAuto - mark packages as automatically/manually installed {{{*/
46 static bool DoAuto(CommandLine
&CmdL
)
48 pkgCacheFile CacheFile
;
49 pkgCache
*Cache
= CacheFile
.GetPkgCache();
50 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
51 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
54 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
55 if (pkgset
.empty() == true)
56 return _error
->Error(_("No packages found"));
58 bool MarkAuto
= strcasecmp(CmdL
.FileList
[0],"auto") == 0;
59 int AutoMarkChanged
= 0;
61 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
63 if (Pkg
->CurrentVer
== 0)
65 ioprintf(c1out
,_("%s can not be marked as it is not installed.\n"), Pkg
.FullName(true).c_str());
68 else if ((((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
70 if (MarkAuto
== false)
71 ioprintf(c1out
,_("%s was already set to manually installed.\n"), Pkg
.FullName(true).c_str());
73 ioprintf(c1out
,_("%s was already set to automatically installed.\n"), Pkg
.FullName(true).c_str());
77 if (MarkAuto
== false)
78 ioprintf(c1out
,_("%s set to manually installed.\n"), Pkg
.FullName(true).c_str());
80 ioprintf(c1out
,_("%s set to automatically installed.\n"), Pkg
.FullName(true).c_str());
82 DepCache
->MarkAuto(Pkg
, MarkAuto
);
85 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
86 return DepCache
->writeStateFile(NULL
);
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
)
95 pkgCacheFile CacheFile
;
96 pkgCache
*Cache
= CacheFile
.GetPkgCache();
97 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
98 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
101 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
102 if (pkgset
.empty() == true)
103 return _error
->Error(_("No packages found"));
105 bool const MarkAuto
= strcasecmp(CmdL
.FileList
[0],"markauto") == 0;
106 bool const Verbose
= _config
->FindB("APT::MarkAuto::Verbose", false);
107 int AutoMarkChanged
= 0;
109 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
111 if (Pkg
->CurrentVer
== 0 ||
112 (((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
116 ioprintf(c1out
, "changing %s to %d\n", Pkg
.Name(), (MarkAuto
== false) ? 0 : 1);
118 DepCache
->MarkAuto(Pkg
, MarkAuto
);
121 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
122 return DepCache
->writeStateFile(NULL
);
124 _error
->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
129 /* ShowAuto - show automatically installed packages (sorted) {{{*/
130 static bool ShowAuto(CommandLine
&CmdL
)
132 pkgCacheFile CacheFile
;
133 pkgCache
*Cache
= CacheFile
.GetPkgCache();
134 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
135 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
138 std::vector
<string
> packages
;
140 bool const ShowAuto
= strcasecmp(CmdL
.FileList
[0],"showauto") == 0;
142 if (CmdL
.FileList
[1] == 0)
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));
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));
161 std::sort(packages
.begin(), packages
.end());
163 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
164 std::cout
<< *I
<< std::endl
;
169 /* DoHold - mark packages as hold by dpkg {{{*/
170 static bool DoHold(CommandLine
&CmdL
)
172 pkgCacheFile CacheFile
;
173 pkgCache
*Cache
= CacheFile
.GetPkgCache();
174 if (unlikely(Cache
== NULL
))
177 // Generate the base argument list for dpkg
178 std::vector
<const char *> Args
;
179 string Tmp
= _config
->Find("Dir::Bin::dpkg","dpkg");
181 string
const dpkgChrootDir
= _config
->FindDir("DPkg::Chroot-Directory", "/");
182 size_t dpkgChrootLen
= dpkgChrootDir
.length();
183 if (dpkgChrootDir
!= "/" && Tmp
.find(dpkgChrootDir
) == 0)
185 if (dpkgChrootDir
[dpkgChrootLen
- 1] == '/')
187 Tmp
= Tmp
.substr(dpkgChrootLen
);
190 Args
.push_back(Tmp
.c_str());
192 // Stick in any custom dpkg options
193 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
197 for (; Opts
!= 0; Opts
= Opts
->Next
)
199 if (Opts
->Value
.empty() == true)
201 Args
.push_back(Opts
->Value
.c_str());
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
);
211 pid_t dpkgAssertMultiArch
= ExecFork();
212 if (dpkgAssertMultiArch
== 0)
214 std::string
const chrootDir
= _config
->FindDir("DPkg::Chroot-Directory");
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
);
220 if (chrootDir
!= "/" && chroot(chrootDir
.c_str()) != 0 && chdir("/") != 0)
221 _error
->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --assert-multi-arch", chrootDir
.c_str());
222 execvp(Args
[0], (char**) &Args
[0]);
223 _error
->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
227 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
228 if (pkgset
.empty() == true)
229 return _error
->Error(_("No packages found"));
231 bool const MarkHold
= strcasecmp(CmdL
.FileList
[0],"hold") == 0;
233 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end();)
235 if ((Pkg
->SelectedState
== pkgCache::State::Hold
) == MarkHold
)
237 if (MarkHold
== true)
238 ioprintf(c1out
,_("%s was already set on hold.\n"), Pkg
.FullName(true).c_str());
240 ioprintf(c1out
,_("%s was already not hold.\n"), Pkg
.FullName(true).c_str());
241 Pkg
= pkgset
.erase(Pkg
, true);
247 bool dpkgMultiArch
= false;
248 if (dpkgAssertMultiArch
> 0)
251 while (waitpid(dpkgAssertMultiArch
, &Status
, 0) != dpkgAssertMultiArch
)
255 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
258 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
259 dpkgMultiArch
= true;
262 if (pkgset
.empty() == true)
265 if (_config
->FindB("APT::Mark::Simulate", false) == true)
267 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
269 if (MarkHold
== false)
270 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
272 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
277 Args
.erase(Args
.begin() + BaseArgs
, Args
.end());
278 Args
.push_back("--set-selections");
279 Args
.push_back(NULL
);
281 int external
[2] = {-1, -1};
282 if (pipe(external
) != 0)
283 return _error
->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
285 pid_t dpkgSelection
= ExecFork();
286 if (dpkgSelection
== 0)
289 std::string
const chrootDir
= _config
->FindDir("DPkg::Chroot-Directory");
290 if (chrootDir
!= "/" && chroot(chrootDir
.c_str()) != 0 && chdir("/") != 0)
291 _error
->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir
.c_str());
292 int const nullfd
= open("/dev/null", O_RDONLY
);
293 dup2(external
[0], STDIN_FILENO
);
294 dup2(nullfd
, STDOUT_FILENO
);
295 dup2(nullfd
, STDERR_FILENO
);
296 execvp(Args
[0], (char**) &Args
[0]);
297 _error
->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
301 FILE* dpkg
= fdopen(external
[1], "w");
302 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
304 if (dpkgMultiArch
== false)
305 fprintf(dpkg
, "%s", Pkg
.FullName(true).c_str());
308 if (Pkg
->CurrentVer
!= 0)
309 fprintf(dpkg
, "%s:%s", Pkg
.Name(), Pkg
.CurrentVer().Arch());
310 else if (Pkg
.VersionList().end() == false)
311 fprintf(dpkg
, "%s:%s", Pkg
.Name(), Pkg
.VersionList().Arch());
313 fprintf(dpkg
, "%s", Pkg
.FullName(false).c_str());
316 if (MarkHold
== true)
318 fprintf(dpkg
, " hold\n");
319 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
323 fprintf(dpkg
, " install\n");
324 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
329 if (dpkgSelection
> 0)
332 while (waitpid(dpkgSelection
, &Status
, 0) != dpkgSelection
)
336 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
339 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
342 return _error
->Error(_("Executing dpkg failed. Are you root?"));
345 /* ShowHold - show packages set on hold in dpkg status {{{*/
346 static bool ShowHold(CommandLine
&CmdL
)
348 pkgCacheFile CacheFile
;
349 pkgCache
*Cache
= CacheFile
.GetPkgCache();
350 if (unlikely(Cache
== NULL
))
353 std::vector
<string
> packages
;
355 if (CmdL
.FileList
[1] == 0)
357 packages
.reserve(50); // how many holds are realistic? I hope just a few…
358 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
359 if (P
->SelectedState
== pkgCache::State::Hold
)
360 packages
.push_back(P
.FullName(true));
364 APT::CacheSetHelper
helper(false); // do not show errors
365 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
366 packages
.reserve(pkgset
.size());
367 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
368 if (P
->SelectedState
== pkgCache::State::Hold
)
369 packages
.push_back(P
.FullName(true));
372 std::sort(packages
.begin(), packages
.end());
374 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
375 std::cout
<< *I
<< std::endl
;
380 // ShowHelp - Show a help screen /*{{{*/
381 // ---------------------------------------------------------------------
383 static bool ShowHelp(CommandLine
&)
385 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
386 COMMON_ARCH
,__DATE__
,__TIME__
);
389 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
391 "apt-mark is a simple command line interface for marking packages\n"
392 "as manually or automatically installed. It can also list marks.\n"
395 " auto - Mark the given packages as automatically installed\n"
396 " manual - Mark the given packages as manually installed\n"
397 " hold - Mark a package as held back\n"
398 " unhold - Unset a package set as held back\n"
399 " showauto - Print the list of automatically installed packages\n"
400 " showmanual - Print the list of manually installed packages\n"
401 " showhold - Print the list of package on hold\n"
404 " -h This help text.\n"
405 " -q Loggable output - no progress indicator\n"
406 " -qq No output except for errors\n"
407 " -s No-act. Just prints what would be done.\n"
408 " -f read/write auto/manual marking in the given file\n"
409 " -c=? Read this configuration file\n"
410 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
411 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
416 int main(int argc
,const char *argv
[]) /*{{{*/
418 CommandLine::Dispatch Cmds
[] = {{"help",&ShowHelp
},
423 {"showauto",&ShowAuto
},
424 {"showmanual",&ShowAuto
},
425 {"showhold",&ShowHold
},
426 // be nice and forgive the typo
427 {"showholds",&ShowHold
},
428 // be nice and forgive it as it is technical right
430 // obsolete commands for compatibility
431 {"markauto", &DoMarkAuto
},
432 {"unmarkauto", &DoMarkAuto
},
435 std::vector
<CommandLine::Args
> Args
= getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds
, argc
, argv
));
437 // Set up gettext support
438 setlocale(LC_ALL
,"");
442 ParseCommandLine(CmdL
, Cmds
, Args
.data(), &_config
, &_system
, argc
, argv
, ShowHelp
);
446 // Match the operation
447 CmdL
.DispatchArg(Cmds
);
449 // Print any errors or warnings found during parsing
450 bool const Errors
= _error
->PendingError();
451 if (_config
->FindI("quiet",0) > 0)
452 _error
->DumpErrors();
454 _error
->DumpErrors(GlobalError::DEBUG
);
455 return Errors
== true ? 100 : 0;