* apt-pkg/pkgcache.cc:
[ntk/apt.git] / cmdline / apt-mark.cc
CommitLineData
c98fb5e0
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* #####################################################################
4 apt-mark - show and change auto-installed bit information
5 ##################################################################### */
6 /*}}}*/
7// Include Files /*{{{*/
8#include <apt-pkg/cachefile.h>
9#include <apt-pkg/cacheset.h>
10#include <apt-pkg/cmndline.h>
11#include <apt-pkg/error.h>
12#include <apt-pkg/init.h>
13#include <apt-pkg/strutl.h>
14
15#include <config.h>
16#include <apti18n.h>
17
18#include <algorithm>
19 /*}}}*/
20using namespace std;
21
22ostream c0out(0);
23ostream c1out(0);
24ostream c2out(0);
25ofstream devnull("/dev/null");
26/* DoAuto - mark packages as automatically/manually installed {{{*/
27bool DoAuto(CommandLine &CmdL)
28{
29 pkgCacheFile CacheFile;
30 pkgCache *Cache = CacheFile.GetPkgCache();
31 pkgDepCache *DepCache = CacheFile.GetDepCache();
32 if (unlikely(Cache == NULL || DepCache == NULL))
33 return false;
34
35 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
36 if (pkgset.empty() == true)
37 return _error->Error(_("No packages found"));
38
39 bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0;
40 int AutoMarkChanged = 0;
41
42 for (APT::PackageSet::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
43 {
44 if (Pkg->CurrentVer == 0)
45 {
46 ioprintf(c1out,_("%s can not be marked as it is not installed.\n"), Pkg.FullName(true).c_str());
47 continue;
48 }
49 else if ((((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
50 {
51 if (MarkAuto == false)
52 ioprintf(c1out,_("%s was already set to manually installed.\n"), Pkg.FullName(true).c_str());
53 else
54 ioprintf(c1out,_("%s was already set to automatically installed.\n"), Pkg.FullName(true).c_str());
55 continue;
56 }
57
58 if (MarkAuto == false)
59 ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.FullName(true).c_str());
60 else
61 ioprintf(c1out,_("%s set to automatically installed.\n"), Pkg.FullName(true).c_str());
62
63 DepCache->MarkAuto(Pkg, MarkAuto);
64 ++AutoMarkChanged;
65 }
66 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
67 return DepCache->writeStateFile(NULL);
68 return true;
69}
70 /*}}}*/
71/* DoMarkAuto - mark packages as automatically/manually installed {{{*/
72/* Does the same as DoAuto but tries to do it exactly the same why as
73 the python implementation did it so it can be a drop-in replacement */
74bool DoMarkAuto(CommandLine &CmdL)
75{
76 pkgCacheFile CacheFile;
77 pkgCache *Cache = CacheFile.GetPkgCache();
78 pkgDepCache *DepCache = CacheFile.GetDepCache();
79 if (unlikely(Cache == NULL || DepCache == NULL))
80 return false;
81
82 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
83 if (pkgset.empty() == true)
84 return _error->Error(_("No packages found"));
85
86 bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
87 bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
88 int AutoMarkChanged = 0;
89
90 for (APT::PackageSet::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
91 {
92 if (Pkg->CurrentVer == 0 ||
93 (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
94 continue;
95
96 if (Verbose == true)
97 ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
98
99 DepCache->MarkAuto(Pkg, MarkAuto);
100 ++AutoMarkChanged;
101 }
102 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
103 return DepCache->writeStateFile(NULL);
104
105 _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
106
107 return true;
108}
109 /*}}}*/
110/* ShowAuto - show automatically installed packages (sorted) {{{*/
111bool ShowAuto(CommandLine &CmdL)
112{
113 pkgCacheFile CacheFile;
114 pkgCache *Cache = CacheFile.GetPkgCache();
115 pkgDepCache *DepCache = CacheFile.GetDepCache();
116 if (unlikely(Cache == NULL || DepCache == NULL))
117 return false;
118
119 std::vector<string> packages;
120
121 bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
122
123 if (CmdL.FileList[1] == 0)
124 {
125 packages.reserve(Cache->HeaderP->PackageCount / 3);
126 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
127 if (P->CurrentVer != 0 &&
128 (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
129 packages.push_back(P.FullName(true));
130 }
131 else
132 {
133 APT::CacheSetHelper helper(false); // do not show errors
134 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
135 packages.reserve(pkgset.size());
136 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
137 if (P->CurrentVer != 0 &&
138 (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
139 packages.push_back(P.FullName(true));
140 }
141
142 std::sort(packages.begin(), packages.end());
143
144 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
145 std::cout << *I << std::endl;
146
147 return true;
148}
149 /*}}}*/
150// ShowHelp - Show a help screen /*{{{*/
151// ---------------------------------------------------------------------
152/* */
153bool ShowHelp(CommandLine &CmdL)
154{
155 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
156 COMMON_ARCH,__DATE__,__TIME__);
157
158 cout <<
159 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
160 "\n"
161 "apt-mark is a simple command line interface for marking packages\n"
162 "as manual or automatical installed. It can also list marks.\n"
163 "\n"
164 "Commands:\n"
165 " auto - Mark the given packages as automatically installed\n"
166 " manual - Mark the given packages as manually installed\n"
167 "\n"
168 "Options:\n"
169 " -h This help text.\n"
170 " -q Loggable output - no progress indicator\n"
171 " -qq No output except for errors\n"
172 " -s No-act. Just prints what would be done.\n"
173 " -f read/write auto/manual marking in the given file\n"
174 " -c=? Read this configuration file\n"
175 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
176 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
177 << std::endl;
178 return true;
179}
180 /*}}}*/
181int main(int argc,const char *argv[]) /*{{{*/
182{
183 CommandLine::Args Args[] = {
184 {'h',"help","help",0},
185 {0,"version","version",0},
186 {'q',"quiet","quiet",CommandLine::IntLevel},
187 {'q',"silent","quiet",CommandLine::IntLevel},
188 {'v',"verbose","APT::MarkAuto::Verbose",0},
189 {'s',"simulate","APT::Mark::Simulate",0},
190 {'s',"just-print","APT::Mark::Simulate",0},
191 {'s',"recon","APT::Mark::Simulate",0},
192 {'s',"dry-run","APT::Mark::Simulate",0},
193 {'s',"no-act","APT::Mark::Simulate",0},
194 {'f',"file","Dir::State::extended_states",CommandLine::HasArg},
195 {'c',"config-file",0,CommandLine::ConfigFile},
196 {'o',"option",0,CommandLine::ArbItem},
197 {0,0,0,0}};
198 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
199 {"auto",&DoAuto},
200 {"manual",&DoAuto},
201 {"showauto",&ShowAuto},
202 {"showmanual",&ShowAuto},
203 // obsolete commands for compatibility
204 {"markauto", &DoMarkAuto},
205 {"unmarkauto", &DoMarkAuto},
206 {0,0}};
207
208 // Set up gettext support
209 setlocale(LC_ALL,"");
210 textdomain(PACKAGE);
211
212 // Parse the command line and initialize the package library
213 CommandLine CmdL(Args,_config);
214 if (pkgInitConfig(*_config) == false ||
215 CmdL.Parse(argc,argv) == false ||
216 pkgInitSystem(*_config,_system) == false)
217 {
218 if (_config->FindB("version") == true)
219 ShowHelp(CmdL);
220 _error->DumpErrors();
221 return 100;
222 }
223
224 // See if the help should be shown
225 if (_config->FindB("help") == true ||
226 _config->FindB("version") == true ||
227 CmdL.FileSize() == 0)
228 {
229 ShowHelp(CmdL);
230 return 0;
231 }
232
233 // Deal with stdout not being a tty
234 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
235 _config->Set("quiet","1");
236
237 // Setup the output streams
238 c0out.rdbuf(cout.rdbuf());
239 c1out.rdbuf(cout.rdbuf());
240 c2out.rdbuf(cout.rdbuf());
241 if (_config->FindI("quiet",0) > 0)
242 c0out.rdbuf(devnull.rdbuf());
243 if (_config->FindI("quiet",0) > 1)
244 c1out.rdbuf(devnull.rdbuf());
245
246 // Match the operation
247 CmdL.DispatchArg(Cmds);
248
249 // Print any errors or warnings found during parsing
250 bool const Errors = _error->PendingError();
251 if (_config->FindI("quiet",0) > 0)
252 _error->DumpErrors();
253 else
254 _error->DumpErrors(GlobalError::DEBUG);
255 return Errors == true ? 100 : 0;
256}
257 /*}}}*/