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 (MarkHold
== true)
296 fprintf(dpkg
, "%s hold\n", Pkg
.FullName(!dpkgMultiArch
).c_str());
297 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
301 fprintf(dpkg
, "%s install\n", Pkg
.FullName(!dpkgMultiArch
).c_str());
302 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
307 if (dpkgSelection
> 0)
310 while (waitpid(dpkgSelection
, &Status
, 0) != dpkgSelection
)
314 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
317 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
320 return _error
->Error(_("Executing dpkg failed. Are you root?"));
323 /* ShowHold - show packages set on hold in dpkg status {{{*/
324 bool ShowHold(CommandLine
&CmdL
)
326 pkgCacheFile CacheFile
;
327 pkgCache
*Cache
= CacheFile
.GetPkgCache();
328 if (unlikely(Cache
== NULL
))
331 std::vector
<string
> packages
;
333 if (CmdL
.FileList
[1] == 0)
335 packages
.reserve(50); // how many holds are realistic? I hope just a few…
336 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
337 if (P
->SelectedState
== pkgCache::State::Hold
)
338 packages
.push_back(P
.FullName(true));
342 APT::CacheSetHelper
helper(false); // do not show errors
343 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
344 packages
.reserve(pkgset
.size());
345 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
346 if (P
->SelectedState
== pkgCache::State::Hold
)
347 packages
.push_back(P
.FullName(true));
350 std::sort(packages
.begin(), packages
.end());
352 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
353 std::cout
<< *I
<< std::endl
;
358 // ShowHelp - Show a help screen /*{{{*/
359 // ---------------------------------------------------------------------
361 bool ShowHelp(CommandLine
&CmdL
)
363 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
364 COMMON_ARCH
,__DATE__
,__TIME__
);
367 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
369 "apt-mark is a simple command line interface for marking packages\n"
370 "as manual or automatical installed. It can also list marks.\n"
373 " auto - Mark the given packages as automatically installed\n"
374 " manual - Mark the given packages as manually installed\n"
377 " -h This help text.\n"
378 " -q Loggable output - no progress indicator\n"
379 " -qq No output except for errors\n"
380 " -s No-act. Just prints what would be done.\n"
381 " -f read/write auto/manual marking in the given file\n"
382 " -c=? Read this configuration file\n"
383 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
384 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
389 int main(int argc
,const char *argv
[]) /*{{{*/
391 CommandLine::Args Args
[] = {
392 {'h',"help","help",0},
393 {0,"version","version",0},
394 {'q',"quiet","quiet",CommandLine::IntLevel
},
395 {'q',"silent","quiet",CommandLine::IntLevel
},
396 {'v',"verbose","APT::MarkAuto::Verbose",0},
397 {'s',"simulate","APT::Mark::Simulate",0},
398 {'s',"just-print","APT::Mark::Simulate",0},
399 {'s',"recon","APT::Mark::Simulate",0},
400 {'s',"dry-run","APT::Mark::Simulate",0},
401 {'s',"no-act","APT::Mark::Simulate",0},
402 {'f',"file","Dir::State::extended_states",CommandLine::HasArg
},
403 {'c',"config-file",0,CommandLine::ConfigFile
},
404 {'o',"option",0,CommandLine::ArbItem
},
406 CommandLine::Dispatch Cmds
[] = {{"help",&ShowHelp
},
411 {"showauto",&ShowAuto
},
412 {"showmanual",&ShowAuto
},
413 {"showhold",&ShowHold
},
414 // be nice and forgive the typo
415 {"showholds",&ShowHold
},
416 // be nice and forgive it as it is technical right
418 // obsolete commands for compatibility
419 {"markauto", &DoMarkAuto
},
420 {"unmarkauto", &DoMarkAuto
},
423 // Set up gettext support
424 setlocale(LC_ALL
,"");
427 // Parse the command line and initialize the package library
428 CommandLine
CmdL(Args
,_config
);
429 if (pkgInitConfig(*_config
) == false ||
430 CmdL
.Parse(argc
,argv
) == false ||
431 pkgInitSystem(*_config
,_system
) == false)
433 if (_config
->FindB("version") == true)
435 _error
->DumpErrors();
439 // See if the help should be shown
440 if (_config
->FindB("help") == true ||
441 _config
->FindB("version") == true ||
442 CmdL
.FileSize() == 0)
448 // Deal with stdout not being a tty
449 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
450 _config
->Set("quiet","1");
452 // Setup the output streams
453 c0out
.rdbuf(cout
.rdbuf());
454 c1out
.rdbuf(cout
.rdbuf());
455 c2out
.rdbuf(cout
.rdbuf());
456 if (_config
->FindI("quiet",0) > 0)
457 c0out
.rdbuf(devnull
.rdbuf());
458 if (_config
->FindI("quiet",0) > 1)
459 c1out
.rdbuf(devnull
.rdbuf());
461 // Match the operation
462 CmdL
.DispatchArg(Cmds
);
464 // Print any errors or warnings found during parsing
465 bool const Errors
= _error
->PendingError();
466 if (_config
->FindI("quiet",0) > 0)
467 _error
->DumpErrors();
469 _error
->DumpErrors(GlobalError::DEBUG
);
470 return Errors
== true ? 100 : 0;