]> git.saurik.com Git - apt.git/blob - cmdline/apt-mark.cc
trigger the usage of the fallback code for kfreebsd also in the
[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 (dpkgMultiArch == false)
295 fprintf(dpkg, "%s", Pkg.FullName(true).c_str());
296 else
297 {
298 if (Pkg->CurrentVer != 0)
299 fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.CurrentVer().Arch());
300 else if (Pkg.VersionList().end() == false)
301 fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.VersionList().Arch());
302 else
303 fprintf(dpkg, "%s", Pkg.FullName(false).c_str());
304 }
305
306 if (MarkHold == true)
307 {
308 fprintf(dpkg, " hold\n");
309 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
310 }
311 else
312 {
313 fprintf(dpkg, " install\n");
314 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
315 }
316 }
317 fclose(dpkg);
318
319 if (dpkgSelection > 0)
320 {
321 int Status = 0;
322 while (waitpid(dpkgSelection, &Status, 0) != dpkgSelection)
323 {
324 if (errno == EINTR)
325 continue;
326 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
327 break;
328 }
329 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
330 return true;
331 }
332 return _error->Error(_("Executing dpkg failed. Are you root?"));
333 }
334 /*}}}*/
335 /* ShowHold - show packages set on hold in dpkg status {{{*/
336 bool ShowHold(CommandLine &CmdL)
337 {
338 pkgCacheFile CacheFile;
339 pkgCache *Cache = CacheFile.GetPkgCache();
340 if (unlikely(Cache == NULL))
341 return false;
342
343 std::vector<string> packages;
344
345 if (CmdL.FileList[1] == 0)
346 {
347 packages.reserve(50); // how many holds are realistic? I hope just a few…
348 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
349 if (P->SelectedState == pkgCache::State::Hold)
350 packages.push_back(P.FullName(true));
351 }
352 else
353 {
354 APT::CacheSetHelper helper(false); // do not show errors
355 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
356 packages.reserve(pkgset.size());
357 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
358 if (P->SelectedState == pkgCache::State::Hold)
359 packages.push_back(P.FullName(true));
360 }
361
362 std::sort(packages.begin(), packages.end());
363
364 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
365 std::cout << *I << std::endl;
366
367 return true;
368 }
369 /*}}}*/
370 // ShowHelp - Show a help screen /*{{{*/
371 // ---------------------------------------------------------------------
372 /* */
373 bool ShowHelp(CommandLine &CmdL)
374 {
375 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
376 COMMON_ARCH,__DATE__,__TIME__);
377
378 cout <<
379 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
380 "\n"
381 "apt-mark is a simple command line interface for marking packages\n"
382 "as manually or automatically installed. It can also list marks.\n"
383 "\n"
384 "Commands:\n"
385 " auto - Mark the given packages as automatically installed\n"
386 " manual - Mark the given packages as manually installed\n"
387 "\n"
388 "Options:\n"
389 " -h This help text.\n"
390 " -q Loggable output - no progress indicator\n"
391 " -qq No output except for errors\n"
392 " -s No-act. Just prints what would be done.\n"
393 " -f read/write auto/manual marking in the given file\n"
394 " -c=? Read this configuration file\n"
395 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
396 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
397 << std::endl;
398 return true;
399 }
400 /*}}}*/
401 int main(int argc,const char *argv[]) /*{{{*/
402 {
403 CommandLine::Args Args[] = {
404 {'h',"help","help",0},
405 {0,"version","version",0},
406 {'q',"quiet","quiet",CommandLine::IntLevel},
407 {'q',"silent","quiet",CommandLine::IntLevel},
408 {'v',"verbose","APT::MarkAuto::Verbose",0},
409 {'s',"simulate","APT::Mark::Simulate",0},
410 {'s',"just-print","APT::Mark::Simulate",0},
411 {'s',"recon","APT::Mark::Simulate",0},
412 {'s',"dry-run","APT::Mark::Simulate",0},
413 {'s',"no-act","APT::Mark::Simulate",0},
414 {'f',"file","Dir::State::extended_states",CommandLine::HasArg},
415 {'c',"config-file",0,CommandLine::ConfigFile},
416 {'o',"option",0,CommandLine::ArbItem},
417 {0,0,0,0}};
418 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
419 {"auto",&DoAuto},
420 {"manual",&DoAuto},
421 {"hold",&DoHold},
422 {"unhold",&DoHold},
423 {"showauto",&ShowAuto},
424 {"showmanual",&ShowAuto},
425 {"showhold",&ShowHold},
426 // be nice and forgive the typo
427 {"showholds",&ShowHold},
428 // be nice and forgive it as it is technical right
429 {"install",&DoHold},
430 // obsolete commands for compatibility
431 {"markauto", &DoMarkAuto},
432 {"unmarkauto", &DoMarkAuto},
433 {0,0}};
434
435 // Set up gettext support
436 setlocale(LC_ALL,"");
437 textdomain(PACKAGE);
438
439 // Parse the command line and initialize the package library
440 CommandLine CmdL(Args,_config);
441 if (pkgInitConfig(*_config) == false ||
442 CmdL.Parse(argc,argv) == false ||
443 pkgInitSystem(*_config,_system) == false)
444 {
445 if (_config->FindB("version") == true)
446 ShowHelp(CmdL);
447 _error->DumpErrors();
448 return 100;
449 }
450
451 // See if the help should be shown
452 if (_config->FindB("help") == true ||
453 _config->FindB("version") == true ||
454 CmdL.FileSize() == 0)
455 {
456 ShowHelp(CmdL);
457 return 0;
458 }
459
460 // Deal with stdout not being a tty
461 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
462 _config->Set("quiet","1");
463
464 // Setup the output streams
465 c0out.rdbuf(cout.rdbuf());
466 c1out.rdbuf(cout.rdbuf());
467 c2out.rdbuf(cout.rdbuf());
468 if (_config->FindI("quiet",0) > 0)
469 c0out.rdbuf(devnull.rdbuf());
470 if (_config->FindI("quiet",0) > 1)
471 c1out.rdbuf(devnull.rdbuf());
472
473 // Match the operation
474 CmdL.DispatchArg(Cmds);
475
476 // Print any errors or warnings found during parsing
477 bool const Errors = _error->PendingError();
478 if (_config->FindI("quiet",0) > 0)
479 _error->DumpErrors();
480 else
481 _error->DumpErrors(GlobalError::DEBUG);
482 return Errors == true ? 100 : 0;
483 }
484 /*}}}*/