]>
Commit | Line | Data |
---|---|---|
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 /*{{{*/ | |
ea542140 DK |
8 | #include <config.h> |
9 | ||
c98fb5e0 DK |
10 | #include <apt-pkg/cachefile.h> |
11 | #include <apt-pkg/cacheset.h> | |
12 | #include <apt-pkg/cmndline.h> | |
13 | #include <apt-pkg/error.h> | |
453b82a3 | 14 | #include <apt-pkg/fileutl.h> |
c98fb5e0 | 15 | #include <apt-pkg/init.h> |
472ff00e | 16 | #include <apt-pkg/pkgsystem.h> |
453b82a3 | 17 | #include <apt-pkg/strutl.h> |
b49068c5 | 18 | #include <apt-pkg/statechanges.h> |
453b82a3 DK |
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> | |
d9e518c6 | 26 | #include <apt-private/private-output.h> |
c98fb5e0 | 27 | |
6fddb156 | 28 | #include <errno.h> |
6fddb156 | 29 | #include <fcntl.h> |
453b82a3 DK |
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> | |
b9179170 | 41 | |
ea542140 | 42 | #include <apti18n.h> |
c98fb5e0 DK |
43 | /*}}}*/ |
44 | using namespace std; | |
45 | ||
c98fb5e0 | 46 | /* DoAuto - mark packages as automatically/manually installed {{{*/ |
c3ccac92 | 47 | static bool DoAuto(CommandLine &CmdL) |
c98fb5e0 DK |
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 | ||
c4cca791 | 55 | APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1); |
c98fb5e0 DK |
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 | ||
c4cca791 | 62 | for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg) |
c98fb5e0 DK |
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 */ | |
c3ccac92 | 94 | static bool DoMarkAuto(CommandLine &CmdL) |
c98fb5e0 DK |
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 | ||
c4cca791 | 102 | APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1); |
c98fb5e0 DK |
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 | ||
c4cca791 | 110 | for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg) |
c98fb5e0 DK |
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) {{{*/ | |
c3ccac92 | 131 | static bool ShowAuto(CommandLine &CmdL) |
c98fb5e0 DK |
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 | ||
a09e4489 DK |
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 | /*}}}*/ | |
64e3414e DK |
170 | // DoSelection - wrapping around dpkg selections /*{{{*/ |
171 | static bool DoSelection(CommandLine &CmdL) | |
a09e4489 DK |
172 | { |
173 | pkgCacheFile CacheFile; | |
174 | pkgCache *Cache = CacheFile.GetPkgCache(); | |
175 | if (unlikely(Cache == NULL)) | |
176 | return false; | |
177 | ||
c1553e72 | 178 | APT::VersionVector pkgset = APT::VersionVector::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::INSTCAND); |
b2a893dd DK |
179 | if (pkgset.empty() == true) |
180 | return _error->Error(_("No packages found")); | |
181 | ||
b49068c5 | 182 | APT::StateChanges marks; |
64e3414e DK |
183 | if (strcasecmp(CmdL.FileList[0], "hold") == 0 || strcasecmp(CmdL.FileList[0], "unhold") == 0) |
184 | { | |
185 | auto const part = std::stable_partition(pkgset.begin(), pkgset.end(), | |
186 | [](pkgCache::VerIterator const &V) { return V.ParentPkg()->SelectedState == pkgCache::State::Hold; }); | |
187 | ||
188 | bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0; | |
189 | auto const doneBegin = MarkHold ? pkgset.begin() : part; | |
190 | auto const doneEnd = MarkHold ? part : pkgset.end(); | |
191 | std::for_each(doneBegin, doneEnd, [&MarkHold](pkgCache::VerIterator const &V) { | |
192 | if (MarkHold == true) | |
193 | ioprintf(c1out, _("%s was already set on hold.\n"), V.ParentPkg().FullName(true).c_str()); | |
194 | else | |
195 | ioprintf(c1out, _("%s was already not hold.\n"), V.ParentPkg().FullName(true).c_str()); | |
196 | }); | |
197 | ||
198 | if (doneBegin == pkgset.begin() && doneEnd == pkgset.end()) | |
199 | return true; | |
200 | ||
201 | auto const changeBegin = MarkHold ? part : pkgset.begin(); | |
202 | auto const changeEnd = MarkHold ? pkgset.end() : part; | |
203 | std::move(changeBegin, changeEnd, std::back_inserter(MarkHold ? marks.Hold() : marks.Unhold())); | |
204 | } | |
205 | else | |
206 | { | |
207 | // FIXME: Maybe show a message for unchanged states here as well? | |
208 | if (strcasecmp(CmdL.FileList[0], "purge") == 0) | |
209 | std::swap(marks.Purge(), pkgset); | |
210 | else if (strcasecmp(CmdL.FileList[0], "deinstall") == 0 || strcasecmp(CmdL.FileList[0], "remove") == 0) | |
211 | std::swap(marks.Remove(), pkgset); | |
212 | else //if (strcasecmp(CmdL.FileList[0], "install") == 0) | |
213 | std::swap(marks.Install(), pkgset); | |
214 | } | |
b49068c5 | 215 | pkgset.clear(); |
6fddb156 | 216 | |
b49068c5 DK |
217 | bool success = true; |
218 | if (_config->FindB("APT::Mark::Simulate", false) == false) | |
6fddb156 | 219 | { |
b49068c5 DK |
220 | success = marks.Save(); |
221 | if (success == false) | |
222 | _error->Error(_("Executing dpkg failed. Are you root?")); | |
6fddb156 | 223 | } |
b49068c5 DK |
224 | for (auto Ver : marks.Hold()) |
225 | ioprintf(c1out,_("%s set on hold.\n"), Ver.ParentPkg().FullName(true).c_str()); | |
226 | for (auto Ver : marks.Unhold()) | |
227 | ioprintf(c1out,_("Canceled hold on %s.\n"), Ver.ParentPkg().FullName(true).c_str()); | |
64e3414e DK |
228 | for (auto Ver : marks.Purge()) |
229 | ioprintf(c1out,_("Selected %s for purge.\n"), Ver.ParentPkg().FullName(true).c_str()); | |
230 | for (auto Ver : marks.Remove()) | |
231 | ioprintf(c1out,_("Selected %s for removal.\n"), Ver.ParentPkg().FullName(true).c_str()); | |
232 | for (auto Ver : marks.Install()) | |
233 | ioprintf(c1out,_("Selected %s for installation.\n"), Ver.ParentPkg().FullName(true).c_str()); | |
b49068c5 | 234 | return success; |
a09e4489 DK |
235 | } |
236 | /*}}}*/ | |
64e3414e | 237 | static bool ShowSelection(CommandLine &CmdL) /*{{{*/ |
a09e4489 DK |
238 | { |
239 | pkgCacheFile CacheFile; | |
240 | pkgCache *Cache = CacheFile.GetPkgCache(); | |
241 | if (unlikely(Cache == NULL)) | |
242 | return false; | |
243 | ||
64e3414e DK |
244 | pkgCache::State::PkgSelectedState selector; |
245 | if (strncasecmp(CmdL.FileList[0], "showpurge", strlen("showpurge")) == 0) | |
246 | selector = pkgCache::State::Purge; | |
247 | else if (strncasecmp(CmdL.FileList[0], "showdeinstall", strlen("showdeinstall")) == 0 || | |
248 | strncasecmp(CmdL.FileList[0], "showremove", strlen("showremove")) == 0) | |
249 | selector = pkgCache::State::DeInstall; | |
250 | else if (strncasecmp(CmdL.FileList[0], "showhold", strlen("showhold")) == 0) | |
251 | selector = pkgCache::State::Hold; | |
252 | else //if (strcasecmp(CmdL.FileList[0], "showinstall", strlen("showinstall")) == 0) | |
253 | selector = pkgCache::State::Install; | |
254 | ||
a09e4489 DK |
255 | std::vector<string> packages; |
256 | ||
257 | if (CmdL.FileList[1] == 0) | |
258 | { | |
259 | packages.reserve(50); // how many holds are realistic? I hope just a few… | |
260 | for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P) | |
64e3414e | 261 | if (P->SelectedState == selector) |
a09e4489 DK |
262 | packages.push_back(P.FullName(true)); |
263 | } | |
264 | else | |
265 | { | |
266 | APT::CacheSetHelper helper(false); // do not show errors | |
267 | APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper); | |
268 | packages.reserve(pkgset.size()); | |
269 | for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P) | |
64e3414e | 270 | if (P->SelectedState == selector) |
a09e4489 DK |
271 | packages.push_back(P.FullName(true)); |
272 | } | |
273 | ||
274 | std::sort(packages.begin(), packages.end()); | |
275 | ||
c98fb5e0 DK |
276 | for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I) |
277 | std::cout << *I << std::endl; | |
278 | ||
279 | return true; | |
280 | } | |
281 | /*}}}*/ | |
282 | // ShowHelp - Show a help screen /*{{{*/ | |
283 | // --------------------------------------------------------------------- | |
284 | /* */ | |
65512241 | 285 | static bool ShowHelp(CommandLine &) |
c98fb5e0 | 286 | { |
249aec3b | 287 | ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); |
c98fb5e0 DK |
288 | |
289 | cout << | |
290 | _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n" | |
291 | "\n" | |
292 | "apt-mark is a simple command line interface for marking packages\n" | |
3999d158 | 293 | "as manually or automatically installed. It can also list marks.\n" |
c98fb5e0 DK |
294 | "\n" |
295 | "Commands:\n" | |
296 | " auto - Mark the given packages as automatically installed\n" | |
297 | " manual - Mark the given packages as manually installed\n" | |
1aef66d5 MV |
298 | " hold - Mark a package as held back\n" |
299 | " unhold - Unset a package set as held back\n" | |
300 | " showauto - Print the list of automatically installed packages\n" | |
301 | " showmanual - Print the list of manually installed packages\n" | |
302 | " showhold - Print the list of package on hold\n" | |
c98fb5e0 DK |
303 | "\n" |
304 | "Options:\n" | |
305 | " -h This help text.\n" | |
306 | " -q Loggable output - no progress indicator\n" | |
307 | " -qq No output except for errors\n" | |
308 | " -s No-act. Just prints what would be done.\n" | |
309 | " -f read/write auto/manual marking in the given file\n" | |
310 | " -c=? Read this configuration file\n" | |
311 | " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n" | |
312 | "See the apt-mark(8) and apt.conf(5) manual pages for more information.") | |
313 | << std::endl; | |
314 | return true; | |
315 | } | |
316 | /*}}}*/ | |
317 | int main(int argc,const char *argv[]) /*{{{*/ | |
318 | { | |
c98fb5e0 DK |
319 | CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp}, |
320 | {"auto",&DoAuto}, | |
321 | {"manual",&DoAuto}, | |
64e3414e DK |
322 | {"hold",&DoSelection}, |
323 | {"unhold",&DoSelection}, | |
324 | {"install",&DoSelection}, | |
325 | {"remove",&DoSelection}, // dpkg uses deinstall, but we use remove everywhere else | |
326 | {"deinstall",&DoSelection}, | |
327 | {"purge",&DoSelection}, | |
c98fb5e0 DK |
328 | {"showauto",&ShowAuto}, |
329 | {"showmanual",&ShowAuto}, | |
64e3414e DK |
330 | {"showhold",&ShowSelection}, {"showholds",&ShowSelection}, |
331 | {"showinstall",&ShowSelection}, {"showinstalls",&ShowSelection}, | |
332 | {"showdeinstall",&ShowSelection}, {"showdeinstalls",&ShowSelection}, | |
333 | {"showremove",&ShowSelection}, {"showremoves",&ShowSelection}, | |
334 | {"showpurge",&ShowSelection}, {"showpurges",&ShowSelection}, | |
c98fb5e0 DK |
335 | // obsolete commands for compatibility |
336 | {"markauto", &DoMarkAuto}, | |
337 | {"unmarkauto", &DoMarkAuto}, | |
338 | {0,0}}; | |
339 | ||
b9179170 MV |
340 | std::vector<CommandLine::Args> Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv)); |
341 | ||
c98fb5e0 DK |
342 | // Set up gettext support |
343 | setlocale(LC_ALL,""); | |
344 | textdomain(PACKAGE); | |
345 | ||
ad7e0941 DK |
346 | CommandLine CmdL; |
347 | ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp); | |
c98fb5e0 | 348 | |
d9e518c6 | 349 | InitOutput(); |
c98fb5e0 DK |
350 | |
351 | // Match the operation | |
352 | CmdL.DispatchArg(Cmds); | |
353 | ||
354 | // Print any errors or warnings found during parsing | |
355 | bool const Errors = _error->PendingError(); | |
356 | if (_config->FindI("quiet",0) > 0) | |
357 | _error->DumpErrors(); | |
358 | else | |
359 | _error->DumpErrors(GlobalError::DEBUG); | |
360 | return Errors == true ? 100 : 0; | |
361 | } | |
362 | /*}}}*/ |