* cmdline/apt-mark.cc:
[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(); ++Pkg)
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 pkgset.erase(Pkg);
232 continue;
233 }
234 }
235
236 bool dpkgMultiArch = false;
237 if (dpkgAssertMultiArch > 0)
238 {
239 int Status = 0;
240 while (waitpid(dpkgAssertMultiArch, &Status, 0) != dpkgAssertMultiArch)
241 {
242 if (errno == EINTR)
243 continue;
244 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
245 break;
246 }
247 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
248 dpkgMultiArch = true;
249 }
250
251 if (pkgset.empty() == true)
252 return true;
253
254 if (_config->FindB("APT::Mark::Simulate", false) == true)
255 {
256 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
257 {
258 if (MarkHold == false)
259 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
260 else
261 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
262 }
263 return true;
264 }
265
266 Args.erase(Args.begin() + BaseArgs, Args.end());
267 Args.push_back("--set-selections");
268 Args.push_back(NULL);
269
270 int external[2] = {-1, -1};
271 if (pipe(external) != 0)
272 return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
273
274 pid_t dpkgSelection = ExecFork();
275 if (dpkgSelection == 0)
276 {
277 close(external[1]);
278 std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
279 if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0)
280 _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir.c_str());
281 int const nullfd = open("/dev/null", O_RDONLY);
282 dup2(nullfd, STDIN_FILENO);
283 dup2(external[0], STDOUT_FILENO);
284 dup2(nullfd, STDERR_FILENO);
285 execvp(Args[0], (char**) &Args[0]);
286 _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
287 _exit(2);
288 }
289
290 FILE* dpkg = fdopen(external[1], "w");
291 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
292 {
293 if (MarkHold == true)
294 {
295 fprintf(dpkg, "%s hold\n", Pkg.FullName(!dpkgMultiArch).c_str());
296 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
297 }
298 else
299 {
300 fprintf(dpkg, "%s install\n", Pkg.FullName(!dpkgMultiArch).c_str());
301 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
302 }
303 }
304 fclose(dpkg);
305
306 if (dpkgSelection > 0)
307 {
308 int Status = 0;
309 while (waitpid(dpkgSelection, &Status, 0) != dpkgSelection)
310 {
311 if (errno == EINTR)
312 continue;
313 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
314 break;
315 }
316 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
317 return true;
318 }
319 return _error->Error(_("Executing dpkg failed. Are you root?"));
320 }
321 /*}}}*/
322 /* ShowHold - show packages set on hold in dpkg status {{{*/
323 bool ShowHold(CommandLine &CmdL)
324 {
325 pkgCacheFile CacheFile;
326 pkgCache *Cache = CacheFile.GetPkgCache();
327 if (unlikely(Cache == NULL))
328 return false;
329
330 std::vector<string> packages;
331
332 if (CmdL.FileList[1] == 0)
333 {
334 packages.reserve(50); // how many holds are realistic? I hope just a few…
335 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
336 if (P->SelectedState == pkgCache::State::Hold)
337 packages.push_back(P.FullName(true));
338 }
339 else
340 {
341 APT::CacheSetHelper helper(false); // do not show errors
342 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
343 packages.reserve(pkgset.size());
344 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
345 if (P->SelectedState == pkgCache::State::Hold)
346 packages.push_back(P.FullName(true));
347 }
348
349 std::sort(packages.begin(), packages.end());
350
351 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
352 std::cout << *I << std::endl;
353
354 return true;
355 }
356 /*}}}*/
357 // ShowHelp - Show a help screen /*{{{*/
358 // ---------------------------------------------------------------------
359 /* */
360 bool ShowHelp(CommandLine &CmdL)
361 {
362 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
363 COMMON_ARCH,__DATE__,__TIME__);
364
365 cout <<
366 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
367 "\n"
368 "apt-mark is a simple command line interface for marking packages\n"
369 "as manual or automatical installed. It can also list marks.\n"
370 "\n"
371 "Commands:\n"
372 " auto - Mark the given packages as automatically installed\n"
373 " manual - Mark the given packages as manually installed\n"
374 "\n"
375 "Options:\n"
376 " -h This help text.\n"
377 " -q Loggable output - no progress indicator\n"
378 " -qq No output except for errors\n"
379 " -s No-act. Just prints what would be done.\n"
380 " -f read/write auto/manual marking in the given file\n"
381 " -c=? Read this configuration file\n"
382 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
383 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
384 << std::endl;
385 return true;
386 }
387 /*}}}*/
388 int main(int argc,const char *argv[]) /*{{{*/
389 {
390 CommandLine::Args Args[] = {
391 {'h',"help","help",0},
392 {0,"version","version",0},
393 {'q',"quiet","quiet",CommandLine::IntLevel},
394 {'q',"silent","quiet",CommandLine::IntLevel},
395 {'v',"verbose","APT::MarkAuto::Verbose",0},
396 {'s',"simulate","APT::Mark::Simulate",0},
397 {'s',"just-print","APT::Mark::Simulate",0},
398 {'s',"recon","APT::Mark::Simulate",0},
399 {'s',"dry-run","APT::Mark::Simulate",0},
400 {'s',"no-act","APT::Mark::Simulate",0},
401 {'f',"file","Dir::State::extended_states",CommandLine::HasArg},
402 {'c',"config-file",0,CommandLine::ConfigFile},
403 {'o',"option",0,CommandLine::ArbItem},
404 {0,0,0,0}};
405 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
406 {"auto",&DoAuto},
407 {"manual",&DoAuto},
408 {"hold",&DoHold},
409 {"unhold",&DoHold},
410 {"showauto",&ShowAuto},
411 {"showmanual",&ShowAuto},
412 {"showhold",&ShowHold},
413 // be nice and forgive the typo
414 {"showholds",&ShowHold},
415 // be nice and forgive it as it is technical right
416 {"install",&DoHold},
417 // obsolete commands for compatibility
418 {"markauto", &DoMarkAuto},
419 {"unmarkauto", &DoMarkAuto},
420 {0,0}};
421
422 // Set up gettext support
423 setlocale(LC_ALL,"");
424 textdomain(PACKAGE);
425
426 // Parse the command line and initialize the package library
427 CommandLine CmdL(Args,_config);
428 if (pkgInitConfig(*_config) == false ||
429 CmdL.Parse(argc,argv) == false ||
430 pkgInitSystem(*_config,_system) == false)
431 {
432 if (_config->FindB("version") == true)
433 ShowHelp(CmdL);
434 _error->DumpErrors();
435 return 100;
436 }
437
438 // See if the help should be shown
439 if (_config->FindB("help") == true ||
440 _config->FindB("version") == true ||
441 CmdL.FileSize() == 0)
442 {
443 ShowHelp(CmdL);
444 return 0;
445 }
446
447 // Deal with stdout not being a tty
448 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
449 _config->Set("quiet","1");
450
451 // Setup the output streams
452 c0out.rdbuf(cout.rdbuf());
453 c1out.rdbuf(cout.rdbuf());
454 c2out.rdbuf(cout.rdbuf());
455 if (_config->FindI("quiet",0) > 0)
456 c0out.rdbuf(devnull.rdbuf());
457 if (_config->FindI("quiet",0) > 1)
458 c1out.rdbuf(devnull.rdbuf());
459
460 // Match the operation
461 CmdL.DispatchArg(Cmds);
462
463 // Print any errors or warnings found during parsing
464 bool const Errors = _error->PendingError();
465 if (_config->FindI("quiet",0) > 0)
466 _error->DumpErrors();
467 else
468 _error->DumpErrors(GlobalError::DEBUG);
469 return Errors == true ? 100 : 0;
470 }
471 /*}}}*/