merged from donkults experimental branch
[ntk/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/init.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/pkgsystem.h>
17 #include <apt-pkg/fileutl.h>
18
19 #include <algorithm>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <fcntl.h>
26
27 #include <apti18n.h>
28 /*}}}*/
29 using namespace std;
30
31 ostream c0out(0);
32 ostream c1out(0);
33 ostream c2out(0);
34 ofstream devnull("/dev/null");
35 /* DoAuto - mark packages as automatically/manually installed {{{*/
36 bool DoAuto(CommandLine &CmdL)
37 {
38 pkgCacheFile CacheFile;
39 pkgCache *Cache = CacheFile.GetPkgCache();
40 pkgDepCache *DepCache = CacheFile.GetDepCache();
41 if (unlikely(Cache == NULL || DepCache == NULL))
42 return false;
43
44 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
45 if (pkgset.empty() == true)
46 return _error->Error(_("No packages found"));
47
48 bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0;
49 int AutoMarkChanged = 0;
50
51 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
52 {
53 if (Pkg->CurrentVer == 0)
54 {
55 ioprintf(c1out,_("%s can not be marked as it is not installed.\n"), Pkg.FullName(true).c_str());
56 continue;
57 }
58 else if ((((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
59 {
60 if (MarkAuto == false)
61 ioprintf(c1out,_("%s was already set to manually installed.\n"), Pkg.FullName(true).c_str());
62 else
63 ioprintf(c1out,_("%s was already set to automatically installed.\n"), Pkg.FullName(true).c_str());
64 continue;
65 }
66
67 if (MarkAuto == false)
68 ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.FullName(true).c_str());
69 else
70 ioprintf(c1out,_("%s set to automatically installed.\n"), Pkg.FullName(true).c_str());
71
72 DepCache->MarkAuto(Pkg, MarkAuto);
73 ++AutoMarkChanged;
74 }
75 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
76 return DepCache->writeStateFile(NULL);
77 return true;
78 }
79 /*}}}*/
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)
84 {
85 pkgCacheFile CacheFile;
86 pkgCache *Cache = CacheFile.GetPkgCache();
87 pkgDepCache *DepCache = CacheFile.GetDepCache();
88 if (unlikely(Cache == NULL || DepCache == NULL))
89 return false;
90
91 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
92 if (pkgset.empty() == true)
93 return _error->Error(_("No packages found"));
94
95 bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
96 bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
97 int AutoMarkChanged = 0;
98
99 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
100 {
101 if (Pkg->CurrentVer == 0 ||
102 (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
103 continue;
104
105 if (Verbose == true)
106 ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
107
108 DepCache->MarkAuto(Pkg, MarkAuto);
109 ++AutoMarkChanged;
110 }
111 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
112 return DepCache->writeStateFile(NULL);
113
114 _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
115
116 return true;
117 }
118 /*}}}*/
119 /* ShowAuto - show automatically installed packages (sorted) {{{*/
120 bool ShowAuto(CommandLine &CmdL)
121 {
122 pkgCacheFile CacheFile;
123 pkgCache *Cache = CacheFile.GetPkgCache();
124 pkgDepCache *DepCache = CacheFile.GetDepCache();
125 if (unlikely(Cache == NULL || DepCache == NULL))
126 return false;
127
128 std::vector<string> packages;
129
130 bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
131
132 if (CmdL.FileList[1] == 0)
133 {
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));
139 }
140 else
141 {
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));
149 }
150
151 std::sort(packages.begin(), packages.end());
152
153 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
154 std::cout << *I << std::endl;
155
156 return true;
157 }
158 /*}}}*/
159 /* DoHold - mark packages as hold by dpkg {{{*/
160 bool DoHold(CommandLine &CmdL)
161 {
162 pkgCacheFile CacheFile;
163 pkgCache *Cache = CacheFile.GetPkgCache();
164 if (unlikely(Cache == NULL))
165 return false;
166
167 // Generate the base argument list for dpkg
168 std::vector<const char *> Args;
169 string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
170 {
171 string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/");
172 size_t dpkgChrootLen = dpkgChrootDir.length();
173 if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0)
174 {
175 if (dpkgChrootDir[dpkgChrootLen - 1] == '/')
176 --dpkgChrootLen;
177 Tmp = Tmp.substr(dpkgChrootLen);
178 }
179 }
180 Args.push_back(Tmp.c_str());
181
182 // Stick in any custom dpkg options
183 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
184 if (Opts != 0)
185 {
186 Opts = Opts->Child;
187 for (; Opts != 0; Opts = Opts->Next)
188 {
189 if (Opts->Value.empty() == true)
190 continue;
191 Args.push_back(Opts->Value.c_str());
192 }
193 }
194
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);
199
200
201 pid_t dpkgAssertMultiArch = ExecFork();
202 if (dpkgAssertMultiArch == 0)
203 {
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!");
214 _exit(2);
215 }
216
217 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
218 if (pkgset.empty() == true)
219 return _error->Error(_("No packages found"));
220
221 bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
222
223 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end();)
224 {
225 if ((Pkg->SelectedState == pkgCache::State::Hold) == MarkHold)
226 {
227 if (MarkHold == true)
228 ioprintf(c1out,_("%s was already set on hold.\n"), Pkg.FullName(true).c_str());
229 else
230 ioprintf(c1out,_("%s was already not hold.\n"), Pkg.FullName(true).c_str());
231 Pkg = pkgset.erase(Pkg, true);
232 }
233 else
234 ++Pkg;
235 }
236
237 bool dpkgMultiArch = false;
238 if (dpkgAssertMultiArch > 0)
239 {
240 int Status = 0;
241 while (waitpid(dpkgAssertMultiArch, &Status, 0) != dpkgAssertMultiArch)
242 {
243 if (errno == EINTR)
244 continue;
245 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
246 break;
247 }
248 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
249 dpkgMultiArch = true;
250 }
251
252 if (pkgset.empty() == true)
253 return true;
254
255 if (_config->FindB("APT::Mark::Simulate", false) == true)
256 {
257 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
258 {
259 if (MarkHold == false)
260 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
261 else
262 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
263 }
264 return true;
265 }
266
267 Args.erase(Args.begin() + BaseArgs, Args.end());
268 Args.push_back("--set-selections");
269 Args.push_back(NULL);
270
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");
274
275 pid_t dpkgSelection = ExecFork();
276 if (dpkgSelection == 0)
277 {
278 close(external[1]);
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!");
288 _exit(2);
289 }
290
291 FILE* dpkg = fdopen(external[1], "w");
292 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
293 {
294 if (MarkHold == true)
295 {
296 fprintf(dpkg, "%s hold\n", Pkg.FullName(!dpkgMultiArch).c_str());
297 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
298 }
299 else
300 {
301 fprintf(dpkg, "%s install\n", Pkg.FullName(!dpkgMultiArch).c_str());
302 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
303 }
304 }
305 fclose(dpkg);
306
307 if (dpkgSelection > 0)
308 {
309 int Status = 0;
310 while (waitpid(dpkgSelection, &Status, 0) != dpkgSelection)
311 {
312 if (errno == EINTR)
313 continue;
314 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
315 break;
316 }
317 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
318 return true;
319 }
320 return _error->Error(_("Executing dpkg failed. Are you root?"));
321 }
322 /*}}}*/
323 /* ShowHold - show packages set on hold in dpkg status {{{*/
324 bool ShowHold(CommandLine &CmdL)
325 {
326 pkgCacheFile CacheFile;
327 pkgCache *Cache = CacheFile.GetPkgCache();
328 if (unlikely(Cache == NULL))
329 return false;
330
331 std::vector<string> packages;
332
333 if (CmdL.FileList[1] == 0)
334 {
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));
339 }
340 else
341 {
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));
348 }
349
350 std::sort(packages.begin(), packages.end());
351
352 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
353 std::cout << *I << std::endl;
354
355 return true;
356 }
357 /*}}}*/
358 // ShowHelp - Show a help screen /*{{{*/
359 // ---------------------------------------------------------------------
360 /* */
361 bool ShowHelp(CommandLine &CmdL)
362 {
363 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
364 COMMON_ARCH,__DATE__,__TIME__);
365
366 cout <<
367 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
368 "\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"
371 "\n"
372 "Commands:\n"
373 " auto - Mark the given packages as automatically installed\n"
374 " manual - Mark the given packages as manually installed\n"
375 "\n"
376 "Options:\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.")
385 << std::endl;
386 return true;
387 }
388 /*}}}*/
389 int main(int argc,const char *argv[]) /*{{{*/
390 {
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},
405 {0,0,0,0}};
406 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
407 {"auto",&DoAuto},
408 {"manual",&DoAuto},
409 {"hold",&DoHold},
410 {"unhold",&DoHold},
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
417 {"install",&DoHold},
418 // obsolete commands for compatibility
419 {"markauto", &DoMarkAuto},
420 {"unmarkauto", &DoMarkAuto},
421 {0,0}};
422
423 // Set up gettext support
424 setlocale(LC_ALL,"");
425 textdomain(PACKAGE);
426
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)
432 {
433 if (_config->FindB("version") == true)
434 ShowHelp(CmdL);
435 _error->DumpErrors();
436 return 100;
437 }
438
439 // See if the help should be shown
440 if (_config->FindB("help") == true ||
441 _config->FindB("version") == true ||
442 CmdL.FileSize() == 0)
443 {
444 ShowHelp(CmdL);
445 return 0;
446 }
447
448 // Deal with stdout not being a tty
449 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
450 _config->Set("quiet","1");
451
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());
460
461 // Match the operation
462 CmdL.DispatchArg(Cmds);
463
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();
468 else
469 _error->DumpErrors(GlobalError::DEBUG);
470 return Errors == true ? 100 : 0;
471 }
472 /*}}}*/