]> git.saurik.com Git - apt.git/blob - cmdline/apt-mark.cc
9d1d0863e4b6cfb730ec7271946fc470f4f72441
[apt.git] / cmdline / apt-mark.cc
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 <config.h>
9
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/statechanges.h>
19 #include <apt-pkg/cacheiterators.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/depcache.h>
22 #include <apt-pkg/macros.h>
23 #include <apt-pkg/pkgcache.h>
24
25 #include <apt-private/private-cmndline.h>
26 #include <apt-private/private-output.h>
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36 #include <algorithm>
37 #include <fstream>
38 #include <iostream>
39 #include <string>
40 #include <vector>
41
42 #include <apti18n.h>
43 /*}}}*/
44 using namespace std;
45
46 /* DoAuto - mark packages as automatically/manually installed {{{*/
47 static bool DoAuto(CommandLine &CmdL)
48 {
49 pkgCacheFile CacheFile;
50 pkgCache *Cache = CacheFile.GetPkgCache();
51 pkgDepCache *DepCache = CacheFile.GetDepCache();
52 if (unlikely(Cache == NULL || DepCache == NULL))
53 return false;
54
55 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
56 if (pkgset.empty() == true)
57 return _error->Error(_("No packages found"));
58
59 bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0;
60 int AutoMarkChanged = 0;
61
62 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
63 {
64 if (Pkg->CurrentVer == 0)
65 {
66 ioprintf(c1out,_("%s can not be marked as it is not installed.\n"), Pkg.FullName(true).c_str());
67 continue;
68 }
69 else if ((((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
70 {
71 if (MarkAuto == false)
72 ioprintf(c1out,_("%s was already set to manually installed.\n"), Pkg.FullName(true).c_str());
73 else
74 ioprintf(c1out,_("%s was already set to automatically installed.\n"), Pkg.FullName(true).c_str());
75 continue;
76 }
77
78 if (MarkAuto == false)
79 ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.FullName(true).c_str());
80 else
81 ioprintf(c1out,_("%s set to automatically installed.\n"), Pkg.FullName(true).c_str());
82
83 DepCache->MarkAuto(Pkg, MarkAuto);
84 ++AutoMarkChanged;
85 }
86 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
87 return DepCache->writeStateFile(NULL);
88 return true;
89 }
90 /*}}}*/
91 /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
92 /* Does the same as DoAuto but tries to do it exactly the same why as
93 the python implementation did it so it can be a drop-in replacement */
94 static bool DoMarkAuto(CommandLine &CmdL)
95 {
96 pkgCacheFile CacheFile;
97 pkgCache *Cache = CacheFile.GetPkgCache();
98 pkgDepCache *DepCache = CacheFile.GetDepCache();
99 if (unlikely(Cache == NULL || DepCache == NULL))
100 return false;
101
102 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
103 if (pkgset.empty() == true)
104 return _error->Error(_("No packages found"));
105
106 bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
107 bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
108 int AutoMarkChanged = 0;
109
110 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
111 {
112 if (Pkg->CurrentVer == 0 ||
113 (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
114 continue;
115
116 if (Verbose == true)
117 ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
118
119 DepCache->MarkAuto(Pkg, MarkAuto);
120 ++AutoMarkChanged;
121 }
122 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
123 return DepCache->writeStateFile(NULL);
124
125 _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
126
127 return true;
128 }
129 /*}}}*/
130 /* ShowAuto - show automatically installed packages (sorted) {{{*/
131 static bool ShowAuto(CommandLine &CmdL)
132 {
133 pkgCacheFile CacheFile;
134 pkgCache *Cache = CacheFile.GetPkgCache();
135 pkgDepCache *DepCache = CacheFile.GetDepCache();
136 if (unlikely(Cache == NULL || DepCache == NULL))
137 return false;
138
139 std::vector<string> packages;
140
141 bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
142
143 if (CmdL.FileList[1] == 0)
144 {
145 packages.reserve(Cache->HeaderP->PackageCount / 3);
146 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
147 if (P->CurrentVer != 0 &&
148 (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
149 packages.push_back(P.FullName(true));
150 }
151 else
152 {
153 APT::CacheSetHelper helper(false); // do not show errors
154 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
155 packages.reserve(pkgset.size());
156 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
157 if (P->CurrentVer != 0 &&
158 (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
159 packages.push_back(P.FullName(true));
160 }
161
162 std::sort(packages.begin(), packages.end());
163
164 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
165 std::cout << *I << std::endl;
166
167 return true;
168 }
169 /*}}}*/
170 /* DoHold - mark packages as hold by dpkg {{{*/
171 static bool DoHold(CommandLine &CmdL)
172 {
173 pkgCacheFile CacheFile;
174 pkgCache *Cache = CacheFile.GetPkgCache();
175 if (unlikely(Cache == NULL))
176 return false;
177
178 APT::VersionVector pkgset = APT::VersionVector::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::INSTCAND);
179 if (pkgset.empty() == true)
180 return _error->Error(_("No packages found"));
181
182 bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
183
184 auto const part = std::stable_partition(pkgset.begin(), pkgset.end(),
185 [](pkgCache::VerIterator const &V) { return V.ParentPkg()->SelectedState == pkgCache::State::Hold; });
186
187 auto const doneBegin = MarkHold ? pkgset.begin() : part;
188 auto const doneEnd = MarkHold ? part : pkgset.end();
189
190 std::for_each(doneBegin, doneEnd, [&MarkHold](pkgCache::VerIterator const &V) {
191 if (MarkHold == true)
192 ioprintf(c1out, _("%s was already set on hold.\n"), V.ParentPkg().FullName(true).c_str());
193 else
194 ioprintf(c1out, _("%s was already not hold.\n"), V.ParentPkg().FullName(true).c_str());
195 });
196
197 if (doneBegin == pkgset.begin() && doneEnd == pkgset.end())
198 return true;
199
200 auto const changeBegin = MarkHold ? part : pkgset.begin();
201 auto const changeEnd = MarkHold ? pkgset.end() : part;
202
203 APT::StateChanges marks;
204 std::move(changeBegin, changeEnd, std::back_inserter(MarkHold ? marks.Hold() : marks.Unhold()));
205 pkgset.clear();
206
207 bool success = true;
208 if (_config->FindB("APT::Mark::Simulate", false) == false)
209 {
210 success = marks.Save();
211 if (success == false)
212 _error->Error(_("Executing dpkg failed. Are you root?"));
213 }
214
215 for (auto Ver : marks.Hold())
216 ioprintf(c1out,_("%s set on hold.\n"), Ver.ParentPkg().FullName(true).c_str());
217 for (auto Ver : marks.Unhold())
218 ioprintf(c1out,_("Canceled hold on %s.\n"), Ver.ParentPkg().FullName(true).c_str());
219
220 return success;
221 }
222 /*}}}*/
223 /* ShowHold - show packages set on hold in dpkg status {{{*/
224 static bool ShowHold(CommandLine &CmdL)
225 {
226 pkgCacheFile CacheFile;
227 pkgCache *Cache = CacheFile.GetPkgCache();
228 if (unlikely(Cache == NULL))
229 return false;
230
231 std::vector<string> packages;
232
233 if (CmdL.FileList[1] == 0)
234 {
235 packages.reserve(50); // how many holds are realistic? I hope just a few…
236 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
237 if (P->SelectedState == pkgCache::State::Hold)
238 packages.push_back(P.FullName(true));
239 }
240 else
241 {
242 APT::CacheSetHelper helper(false); // do not show errors
243 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
244 packages.reserve(pkgset.size());
245 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
246 if (P->SelectedState == pkgCache::State::Hold)
247 packages.push_back(P.FullName(true));
248 }
249
250 std::sort(packages.begin(), packages.end());
251
252 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
253 std::cout << *I << std::endl;
254
255 return true;
256 }
257 /*}}}*/
258 // ShowHelp - Show a help screen /*{{{*/
259 // ---------------------------------------------------------------------
260 /* */
261 static bool ShowHelp(CommandLine &)
262 {
263 ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
264
265 cout <<
266 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
267 "\n"
268 "apt-mark is a simple command line interface for marking packages\n"
269 "as manually or automatically installed. It can also list marks.\n"
270 "\n"
271 "Commands:\n"
272 " auto - Mark the given packages as automatically installed\n"
273 " manual - Mark the given packages as manually installed\n"
274 " hold - Mark a package as held back\n"
275 " unhold - Unset a package set as held back\n"
276 " showauto - Print the list of automatically installed packages\n"
277 " showmanual - Print the list of manually installed packages\n"
278 " showhold - Print the list of package on hold\n"
279 "\n"
280 "Options:\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.")
289 << std::endl;
290 return true;
291 }
292 /*}}}*/
293 int main(int argc,const char *argv[]) /*{{{*/
294 {
295 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
296 {"auto",&DoAuto},
297 {"manual",&DoAuto},
298 {"hold",&DoHold},
299 {"unhold",&DoHold},
300 {"showauto",&ShowAuto},
301 {"showmanual",&ShowAuto},
302 {"showhold",&ShowHold},
303 // be nice and forgive the typo
304 {"showholds",&ShowHold},
305 // be nice and forgive it as it is technical right
306 {"install",&DoHold},
307 // obsolete commands for compatibility
308 {"markauto", &DoMarkAuto},
309 {"unmarkauto", &DoMarkAuto},
310 {0,0}};
311
312 std::vector<CommandLine::Args> Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv));
313
314 // Set up gettext support
315 setlocale(LC_ALL,"");
316 textdomain(PACKAGE);
317
318 CommandLine CmdL;
319 ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
320
321 InitOutput();
322
323 // Match the operation
324 CmdL.DispatchArg(Cmds);
325
326 // Print any errors or warnings found during parsing
327 bool const Errors = _error->PendingError();
328 if (_config->FindI("quiet",0) > 0)
329 _error->DumpErrors();
330 else
331 _error->DumpErrors(GlobalError::DEBUG);
332 return Errors == true ? 100 : 0;
333 }
334 /*}}}*/