]>
git.saurik.com Git - apt.git/blob - cmdline/apt-mark.cc
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>
28 ofstream
devnull("/dev/null");
29 /* DoAuto - mark packages as automatically/manually installed {{{*/
30 bool DoAuto(CommandLine
&CmdL
)
32 pkgCacheFile CacheFile
;
33 pkgCache
*Cache
= CacheFile
.GetPkgCache();
34 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
35 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
38 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
39 if (pkgset
.empty() == true)
40 return _error
->Error(_("No packages found"));
42 bool MarkAuto
= strcasecmp(CmdL
.FileList
[0],"auto") == 0;
43 int AutoMarkChanged
= 0;
45 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
47 if (Pkg
->CurrentVer
== 0)
49 ioprintf(c1out
,_("%s can not be marked as it is not installed.\n"), Pkg
.FullName(true).c_str());
52 else if ((((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
54 if (MarkAuto
== false)
55 ioprintf(c1out
,_("%s was already set to manually installed.\n"), Pkg
.FullName(true).c_str());
57 ioprintf(c1out
,_("%s was already set to automatically installed.\n"), Pkg
.FullName(true).c_str());
61 if (MarkAuto
== false)
62 ioprintf(c1out
,_("%s set to manually installed.\n"), Pkg
.FullName(true).c_str());
64 ioprintf(c1out
,_("%s set to automatically installed.\n"), Pkg
.FullName(true).c_str());
66 DepCache
->MarkAuto(Pkg
, MarkAuto
);
69 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
70 return DepCache
->writeStateFile(NULL
);
74 /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
75 /* Does the same as DoAuto but tries to do it exactly the same why as
76 the python implementation did it so it can be a drop-in replacement */
77 bool DoMarkAuto(CommandLine
&CmdL
)
79 pkgCacheFile CacheFile
;
80 pkgCache
*Cache
= CacheFile
.GetPkgCache();
81 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
82 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
85 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
86 if (pkgset
.empty() == true)
87 return _error
->Error(_("No packages found"));
89 bool const MarkAuto
= strcasecmp(CmdL
.FileList
[0],"markauto") == 0;
90 bool const Verbose
= _config
->FindB("APT::MarkAuto::Verbose", false);
91 int AutoMarkChanged
= 0;
93 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
95 if (Pkg
->CurrentVer
== 0 ||
96 (((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
100 ioprintf(c1out
, "changing %s to %d\n", Pkg
.Name(), (MarkAuto
== false) ? 0 : 1);
102 DepCache
->MarkAuto(Pkg
, MarkAuto
);
105 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
106 return DepCache
->writeStateFile(NULL
);
108 _error
->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
113 /* ShowAuto - show automatically installed packages (sorted) {{{*/
114 bool ShowAuto(CommandLine
&CmdL
)
116 pkgCacheFile CacheFile
;
117 pkgCache
*Cache
= CacheFile
.GetPkgCache();
118 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
119 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
122 std::vector
<string
> packages
;
124 bool const ShowAuto
= strcasecmp(CmdL
.FileList
[0],"showauto") == 0;
126 if (CmdL
.FileList
[1] == 0)
128 packages
.reserve(Cache
->HeaderP
->PackageCount
/ 3);
129 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
130 if (P
->CurrentVer
!= 0 &&
131 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
132 packages
.push_back(P
.FullName(true));
136 APT::CacheSetHelper
helper(false); // do not show errors
137 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
138 packages
.reserve(pkgset
.size());
139 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
140 if (P
->CurrentVer
!= 0 &&
141 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
142 packages
.push_back(P
.FullName(true));
145 std::sort(packages
.begin(), packages
.end());
147 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
148 std::cout
<< *I
<< std::endl
;
153 /* DoHold - mark packages as hold by dpkg {{{*/
154 bool DoHold(CommandLine
&CmdL
)
156 pkgCacheFile CacheFile
;
157 pkgCache
*Cache
= CacheFile
.GetPkgCache();
158 if (unlikely(Cache
== NULL
))
161 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
162 if (pkgset
.empty() == true)
163 return _error
->Error(_("No packages found"));
165 bool const MarkHold
= strcasecmp(CmdL
.FileList
[0],"hold") == 0;
167 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
169 if ((Pkg
->SelectedState
== pkgCache::State::Hold
) == MarkHold
)
171 if (MarkHold
== true)
172 ioprintf(c1out
,_("%s was already set on hold.\n"), Pkg
.FullName(true).c_str());
174 ioprintf(c1out
,_("%s was already not hold.\n"), Pkg
.FullName(true).c_str());
180 if (pkgset
.empty() == true)
183 if (_config
->FindB("APT::Mark::Simulate", false) == true)
185 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
187 if (MarkHold
== false)
188 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
190 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
195 string dpkgcall
= _config
->Find("Dir::Bin::dpkg", "dpkg");
196 std::vector
<string
> const dpkgoptions
= _config
->FindVector("DPkg::options");
197 for (std::vector
<string
>::const_iterator o
= dpkgoptions
.begin();
198 o
!= dpkgoptions
.end(); ++o
)
199 dpkgcall
.append(" ").append(*o
);
200 dpkgcall
.append(" --set-selections");
201 FILE *dpkg
= popen(dpkgcall
.c_str(), "w");
203 return _error
->Errno("DoHold", "fdopen on dpkg stdin failed");
205 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
207 if (MarkHold
== true)
209 fprintf(dpkg
, "%s hold\n", Pkg
.FullName(true).c_str());
210 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
214 fprintf(dpkg
, "%s install\n", Pkg
.FullName(true).c_str());
215 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
219 int const status
= pclose(dpkg
);
221 return _error
->Errno("DoHold", "dpkg execution failed in the end");
222 if (WIFEXITED(status
) == false || WEXITSTATUS(status
) != 0)
223 return _error
->Error(_("Executing dpkg failed. Are you root?"));
227 /* ShowHold - show packages set on hold in dpkg status {{{*/
228 bool ShowHold(CommandLine
&CmdL
)
230 pkgCacheFile CacheFile
;
231 pkgCache
*Cache
= CacheFile
.GetPkgCache();
232 if (unlikely(Cache
== NULL
))
235 std::vector
<string
> packages
;
237 if (CmdL
.FileList
[1] == 0)
239 packages
.reserve(50); // how many holds are realistic? I hope just a few…
240 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
241 if (P
->SelectedState
== pkgCache::State::Hold
)
242 packages
.push_back(P
.FullName(true));
246 APT::CacheSetHelper
helper(false); // do not show errors
247 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
248 packages
.reserve(pkgset
.size());
249 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
250 if (P
->SelectedState
== pkgCache::State::Hold
)
251 packages
.push_back(P
.FullName(true));
254 std::sort(packages
.begin(), packages
.end());
256 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
257 std::cout
<< *I
<< std::endl
;
262 // ShowHelp - Show a help screen /*{{{*/
263 // ---------------------------------------------------------------------
265 bool ShowHelp(CommandLine
&CmdL
)
267 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,VERSION
,
268 COMMON_ARCH
,__DATE__
,__TIME__
);
271 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
273 "apt-mark is a simple command line interface for marking packages\n"
274 "as manual or automatical installed. It can also list marks.\n"
277 " auto - Mark the given packages as automatically installed\n"
278 " manual - Mark the given packages as manually installed\n"
281 " -h This help text.\n"
282 " -q Loggable output - no progress indicator\n"
283 " -qq No output except for errors\n"
284 " -s No-act. Just prints what would be done.\n"
285 " -f read/write auto/manual marking in the given file\n"
286 " -c=? Read this configuration file\n"
287 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
288 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
293 int main(int argc
,const char *argv
[]) /*{{{*/
295 CommandLine::Args Args
[] = {
296 {'h',"help","help",0},
297 {0,"version","version",0},
298 {'q',"quiet","quiet",CommandLine::IntLevel
},
299 {'q',"silent","quiet",CommandLine::IntLevel
},
300 {'v',"verbose","APT::MarkAuto::Verbose",0},
301 {'s',"simulate","APT::Mark::Simulate",0},
302 {'s',"just-print","APT::Mark::Simulate",0},
303 {'s',"recon","APT::Mark::Simulate",0},
304 {'s',"dry-run","APT::Mark::Simulate",0},
305 {'s',"no-act","APT::Mark::Simulate",0},
306 {'f',"file","Dir::State::extended_states",CommandLine::HasArg
},
307 {'c',"config-file",0,CommandLine::ConfigFile
},
308 {'o',"option",0,CommandLine::ArbItem
},
310 CommandLine::Dispatch Cmds
[] = {{"help",&ShowHelp
},
315 {"showauto",&ShowAuto
},
316 {"showmanual",&ShowAuto
},
317 {"showhold",&ShowHold
},
318 // be nice and forgive the typo
319 {"showholds",&ShowHold
},
320 // be nice and forgive it as it is technical right
322 // obsolete commands for compatibility
323 {"markauto", &DoMarkAuto
},
324 {"unmarkauto", &DoMarkAuto
},
327 // Set up gettext support
328 setlocale(LC_ALL
,"");
331 // Parse the command line and initialize the package library
332 CommandLine
CmdL(Args
,_config
);
333 if (pkgInitConfig(*_config
) == false ||
334 CmdL
.Parse(argc
,argv
) == false ||
335 pkgInitSystem(*_config
,_system
) == false)
337 if (_config
->FindB("version") == true)
339 _error
->DumpErrors();
343 // See if the help should be shown
344 if (_config
->FindB("help") == true ||
345 _config
->FindB("version") == true ||
346 CmdL
.FileSize() == 0)
352 // Deal with stdout not being a tty
353 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
354 _config
->Set("quiet","1");
356 // Setup the output streams
357 c0out
.rdbuf(cout
.rdbuf());
358 c1out
.rdbuf(cout
.rdbuf());
359 c2out
.rdbuf(cout
.rdbuf());
360 if (_config
->FindI("quiet",0) > 0)
361 c0out
.rdbuf(devnull
.rdbuf());
362 if (_config
->FindI("quiet",0) > 1)
363 c1out
.rdbuf(devnull
.rdbuf());
365 // Match the operation
366 CmdL
.DispatchArg(Cmds
);
368 // Print any errors or warnings found during parsing
369 bool const Errors
= _error
->PendingError();
370 if (_config
->FindI("quiet",0) > 0)
371 _error
->DumpErrors();
373 _error
->DumpErrors(GlobalError::DEBUG
);
374 return Errors
== true ? 100 : 0;