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/init.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/pkgsystem.h>
17 #include <apt-pkg/fileutl.h>
22 #include <sys/types.h>
34 ofstream
devnull("/dev/null");
35 /* DoAuto - mark packages as automatically/manually installed {{{*/
36 bool DoAuto(CommandLine
&CmdL
)
38 pkgCacheFile CacheFile
;
39 pkgCache
*Cache
= CacheFile
.GetPkgCache();
40 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
41 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
44 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
45 if (pkgset
.empty() == true)
46 return _error
->Error(_("No packages found"));
48 bool MarkAuto
= strcasecmp(CmdL
.FileList
[0],"auto") == 0;
49 int AutoMarkChanged
= 0;
51 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
53 if (Pkg
->CurrentVer
== 0)
55 ioprintf(c1out
,_("%s can not be marked as it is not installed.\n"), Pkg
.FullName(true).c_str());
58 else if ((((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
60 if (MarkAuto
== false)
61 ioprintf(c1out
,_("%s was already set to manually installed.\n"), Pkg
.FullName(true).c_str());
63 ioprintf(c1out
,_("%s was already set to automatically installed.\n"), Pkg
.FullName(true).c_str());
67 if (MarkAuto
== false)
68 ioprintf(c1out
,_("%s set to manually installed.\n"), Pkg
.FullName(true).c_str());
70 ioprintf(c1out
,_("%s set to automatically installed.\n"), Pkg
.FullName(true).c_str());
72 DepCache
->MarkAuto(Pkg
, MarkAuto
);
75 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
76 return DepCache
->writeStateFile(NULL
);
80 /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
81 /* Does the same as DoAuto but tries to do it exactly the same why as
82 the python implementation did it so it can be a drop-in replacement */
83 bool DoMarkAuto(CommandLine
&CmdL
)
85 pkgCacheFile CacheFile
;
86 pkgCache
*Cache
= CacheFile
.GetPkgCache();
87 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
88 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
91 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
92 if (pkgset
.empty() == true)
93 return _error
->Error(_("No packages found"));
95 bool const MarkAuto
= strcasecmp(CmdL
.FileList
[0],"markauto") == 0;
96 bool const Verbose
= _config
->FindB("APT::MarkAuto::Verbose", false);
97 int AutoMarkChanged
= 0;
99 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
101 if (Pkg
->CurrentVer
== 0 ||
102 (((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
106 ioprintf(c1out
, "changing %s to %d\n", Pkg
.Name(), (MarkAuto
== false) ? 0 : 1);
108 DepCache
->MarkAuto(Pkg
, MarkAuto
);
111 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
112 return DepCache
->writeStateFile(NULL
);
114 _error
->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
119 /* ShowAuto - show automatically installed packages (sorted) {{{*/
120 bool ShowAuto(CommandLine
&CmdL
)
122 pkgCacheFile CacheFile
;
123 pkgCache
*Cache
= CacheFile
.GetPkgCache();
124 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
125 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
128 std::vector
<string
> packages
;
130 bool const ShowAuto
= strcasecmp(CmdL
.FileList
[0],"showauto") == 0;
132 if (CmdL
.FileList
[1] == 0)
134 packages
.reserve(Cache
->HeaderP
->PackageCount
/ 3);
135 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
136 if (P
->CurrentVer
!= 0 &&
137 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
138 packages
.push_back(P
.FullName(true));
142 APT::CacheSetHelper
helper(false); // do not show errors
143 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
144 packages
.reserve(pkgset
.size());
145 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
146 if (P
->CurrentVer
!= 0 &&
147 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
148 packages
.push_back(P
.FullName(true));
151 std::sort(packages
.begin(), packages
.end());
153 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
154 std::cout
<< *I
<< std::endl
;
159 /* DoHold - mark packages as hold by dpkg {{{*/
160 bool DoHold(CommandLine
&CmdL
)
162 pkgCacheFile CacheFile
;
163 pkgCache
*Cache
= CacheFile
.GetPkgCache();
164 if (unlikely(Cache
== NULL
))
167 // Generate the base argument list for dpkg
168 std::vector
<const char *> Args
;
169 string Tmp
= _config
->Find("Dir::Bin::dpkg","dpkg");
171 string
const dpkgChrootDir
= _config
->FindDir("DPkg::Chroot-Directory", "/");
172 size_t dpkgChrootLen
= dpkgChrootDir
.length();
173 if (dpkgChrootDir
!= "/" && Tmp
.find(dpkgChrootDir
) == 0)
175 if (dpkgChrootDir
[dpkgChrootLen
- 1] == '/')
177 Tmp
= Tmp
.substr(dpkgChrootLen
);
180 Args
.push_back(Tmp
.c_str());
182 // Stick in any custom dpkg options
183 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
187 for (; Opts
!= 0; Opts
= Opts
->Next
)
189 if (Opts
->Value
.empty() == true)
191 Args
.push_back(Opts
->Value
.c_str());
195 size_t const BaseArgs
= Args
.size();
196 // we need to detect if we can qualify packages with the architecture or not
197 Args
.push_back("--assert-multi-arch");
198 Args
.push_back(NULL
);
201 pid_t dpkgAssertMultiArch
= ExecFork();
202 if (dpkgAssertMultiArch
== 0)
204 std::string
const chrootDir
= _config
->FindDir("DPkg::Chroot-Directory");
205 if (chrootDir
!= "/" && chroot(chrootDir
.c_str()) != 0)
206 _error
->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --assert-multi-arch", chrootDir
.c_str());
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
);
212 execvp(Args
[0], (char**) &Args
[0]);
213 _error
->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
217 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
218 if (pkgset
.empty() == true)
219 return _error
->Error(_("No packages found"));
221 bool const MarkHold
= strcasecmp(CmdL
.FileList
[0],"hold") == 0;
223 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end();)
225 if ((Pkg
->SelectedState
== pkgCache::State::Hold
) == MarkHold
)
227 if (MarkHold
== true)
228 ioprintf(c1out
,_("%s was already set on hold.\n"), Pkg
.FullName(true).c_str());
230 ioprintf(c1out
,_("%s was already not hold.\n"), Pkg
.FullName(true).c_str());
231 Pkg
= pkgset
.erase(Pkg
, true);
237 bool dpkgMultiArch
= false;
238 if (dpkgAssertMultiArch
> 0)
241 while (waitpid(dpkgAssertMultiArch
, &Status
, 0) != dpkgAssertMultiArch
)
245 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
248 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
249 dpkgMultiArch
= true;
252 if (pkgset
.empty() == true)
255 if (_config
->FindB("APT::Mark::Simulate", false) == true)
257 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
259 if (MarkHold
== false)
260 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
262 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
267 Args
.erase(Args
.begin() + BaseArgs
, Args
.end());
268 Args
.push_back("--set-selections");
269 Args
.push_back(NULL
);
271 int external
[2] = {-1, -1};
272 if (pipe(external
) != 0)
273 return _error
->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
275 pid_t dpkgSelection
= ExecFork();
276 if (dpkgSelection
== 0)
279 std::string
const chrootDir
= _config
->FindDir("DPkg::Chroot-Directory");
280 if (chrootDir
!= "/" && chroot(chrootDir
.c_str()) != 0)
281 _error
->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir
.c_str());
282 int const nullfd
= open("/dev/null", O_RDONLY
);
283 dup2(external
[0], STDIN_FILENO
);
284 dup2(nullfd
, STDOUT_FILENO
);
285 dup2(nullfd
, STDERR_FILENO
);
286 execvp(Args
[0], (char**) &Args
[0]);
287 _error
->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
291 FILE* dpkg
= fdopen(external
[1], "w");
292 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
294 if (dpkgMultiArch
== false)
295 fprintf(dpkg
, "%s", Pkg
.FullName(true).c_str());
298 if (Pkg
->CurrentVer
!= 0)
299 fprintf(dpkg
, "%s:%s", Pkg
.Name(), Pkg
.CurrentVer().Arch());
300 else if (Pkg
.VersionList().end() == false)
301 fprintf(dpkg
, "%s:%s", Pkg
.Name(), Pkg
.VersionList().Arch());
303 fprintf(dpkg
, "%s", Pkg
.FullName(false).c_str());
306 if (MarkHold
== true)
308 fprintf(dpkg
, " hold\n");
309 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
313 fprintf(dpkg
, " install\n");
314 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
319 if (dpkgSelection
> 0)
322 while (waitpid(dpkgSelection
, &Status
, 0) != dpkgSelection
)
326 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
329 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
332 return _error
->Error(_("Executing dpkg failed. Are you root?"));
335 /* ShowHold - show packages set on hold in dpkg status {{{*/
336 bool ShowHold(CommandLine
&CmdL
)
338 pkgCacheFile CacheFile
;
339 pkgCache
*Cache
= CacheFile
.GetPkgCache();
340 if (unlikely(Cache
== NULL
))
343 std::vector
<string
> packages
;
345 if (CmdL
.FileList
[1] == 0)
347 packages
.reserve(50); // how many holds are realistic? I hope just a few…
348 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
349 if (P
->SelectedState
== pkgCache::State::Hold
)
350 packages
.push_back(P
.FullName(true));
354 APT::CacheSetHelper
helper(false); // do not show errors
355 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
356 packages
.reserve(pkgset
.size());
357 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
358 if (P
->SelectedState
== pkgCache::State::Hold
)
359 packages
.push_back(P
.FullName(true));
362 std::sort(packages
.begin(), packages
.end());
364 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
365 std::cout
<< *I
<< std::endl
;
370 // ShowHelp - Show a help screen /*{{{*/
371 // ---------------------------------------------------------------------
373 bool ShowHelp(CommandLine
&CmdL
)
375 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
376 COMMON_ARCH
,__DATE__
,__TIME__
);
379 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
381 "apt-mark is a simple command line interface for marking packages\n"
382 "as manually or automatically installed. It can also list marks.\n"
385 " auto - Mark the given packages as automatically installed\n"
386 " manual - Mark the given packages as manually installed\n"
389 " -h This help text.\n"
390 " -q Loggable output - no progress indicator\n"
391 " -qq No output except for errors\n"
392 " -s No-act. Just prints what would be done.\n"
393 " -f read/write auto/manual marking in the given file\n"
394 " -c=? Read this configuration file\n"
395 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
396 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
401 int main(int argc
,const char *argv
[]) /*{{{*/
403 CommandLine::Args Args
[] = {
404 {'h',"help","help",0},
405 {0,"version","version",0},
406 {'q',"quiet","quiet",CommandLine::IntLevel
},
407 {'q',"silent","quiet",CommandLine::IntLevel
},
408 {'v',"verbose","APT::MarkAuto::Verbose",0},
409 {'s',"simulate","APT::Mark::Simulate",0},
410 {'s',"just-print","APT::Mark::Simulate",0},
411 {'s',"recon","APT::Mark::Simulate",0},
412 {'s',"dry-run","APT::Mark::Simulate",0},
413 {'s',"no-act","APT::Mark::Simulate",0},
414 {'f',"file","Dir::State::extended_states",CommandLine::HasArg
},
415 {'c',"config-file",0,CommandLine::ConfigFile
},
416 {'o',"option",0,CommandLine::ArbItem
},
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 // Set up gettext support
436 setlocale(LC_ALL
,"");
439 // Parse the command line and initialize the package library
440 CommandLine
CmdL(Args
,_config
);
441 if (pkgInitConfig(*_config
) == false ||
442 CmdL
.Parse(argc
,argv
) == false ||
443 pkgInitSystem(*_config
,_system
) == false)
445 if (_config
->FindB("version") == true)
447 _error
->DumpErrors();
451 // See if the help should be shown
452 if (_config
->FindB("help") == true ||
453 _config
->FindB("version") == true ||
454 CmdL
.FileSize() == 0)
460 // Deal with stdout not being a tty
461 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
462 _config
->Set("quiet","1");
464 // Setup the output streams
465 c0out
.rdbuf(cout
.rdbuf());
466 c1out
.rdbuf(cout
.rdbuf());
467 c2out
.rdbuf(cout
.rdbuf());
468 if (_config
->FindI("quiet",0) > 0)
469 c0out
.rdbuf(devnull
.rdbuf());
470 if (_config
->FindI("quiet",0) > 1)
471 c1out
.rdbuf(devnull
.rdbuf());
473 // Match the operation
474 CmdL
.DispatchArg(Cmds
);
476 // Print any errors or warnings found during parsing
477 bool const Errors
= _error
->PendingError();
478 if (_config
->FindI("quiet",0) > 0)
479 _error
->DumpErrors();
481 _error
->DumpErrors(GlobalError::DEBUG
);
482 return Errors
== true ? 100 : 0;