]> git.saurik.com Git - apt.git/blob - cmdline/apt-mark.cc
add test for Suite with path
[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 <apt-private/private-cmndline.h>
28
29 #include <apti18n.h>
30 /*}}}*/
31 using namespace std;
32
33 ostream c0out(0);
34 ostream c1out(0);
35 ostream c2out(0);
36 ofstream devnull("/dev/null");
37 /* DoAuto - mark packages as automatically/manually installed {{{*/
38 bool DoAuto(CommandLine &CmdL)
39 {
40 pkgCacheFile CacheFile;
41 pkgCache *Cache = CacheFile.GetPkgCache();
42 pkgDepCache *DepCache = CacheFile.GetDepCache();
43 if (unlikely(Cache == NULL || DepCache == NULL))
44 return false;
45
46 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
47 if (pkgset.empty() == true)
48 return _error->Error(_("No packages found"));
49
50 bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0;
51 int AutoMarkChanged = 0;
52
53 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
54 {
55 if (Pkg->CurrentVer == 0)
56 {
57 ioprintf(c1out,_("%s can not be marked as it is not installed.\n"), Pkg.FullName(true).c_str());
58 continue;
59 }
60 else if ((((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
61 {
62 if (MarkAuto == false)
63 ioprintf(c1out,_("%s was already set to manually installed.\n"), Pkg.FullName(true).c_str());
64 else
65 ioprintf(c1out,_("%s was already set to automatically installed.\n"), Pkg.FullName(true).c_str());
66 continue;
67 }
68
69 if (MarkAuto == false)
70 ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.FullName(true).c_str());
71 else
72 ioprintf(c1out,_("%s set to automatically installed.\n"), Pkg.FullName(true).c_str());
73
74 DepCache->MarkAuto(Pkg, MarkAuto);
75 ++AutoMarkChanged;
76 }
77 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
78 return DepCache->writeStateFile(NULL);
79 return true;
80 }
81 /*}}}*/
82 /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
83 /* Does the same as DoAuto but tries to do it exactly the same why as
84 the python implementation did it so it can be a drop-in replacement */
85 bool DoMarkAuto(CommandLine &CmdL)
86 {
87 pkgCacheFile CacheFile;
88 pkgCache *Cache = CacheFile.GetPkgCache();
89 pkgDepCache *DepCache = CacheFile.GetDepCache();
90 if (unlikely(Cache == NULL || DepCache == NULL))
91 return false;
92
93 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
94 if (pkgset.empty() == true)
95 return _error->Error(_("No packages found"));
96
97 bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
98 bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
99 int AutoMarkChanged = 0;
100
101 for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
102 {
103 if (Pkg->CurrentVer == 0 ||
104 (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
105 continue;
106
107 if (Verbose == true)
108 ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
109
110 DepCache->MarkAuto(Pkg, MarkAuto);
111 ++AutoMarkChanged;
112 }
113 if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
114 return DepCache->writeStateFile(NULL);
115
116 _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
117
118 return true;
119 }
120 /*}}}*/
121 /* ShowAuto - show automatically installed packages (sorted) {{{*/
122 bool ShowAuto(CommandLine &CmdL)
123 {
124 pkgCacheFile CacheFile;
125 pkgCache *Cache = CacheFile.GetPkgCache();
126 pkgDepCache *DepCache = CacheFile.GetDepCache();
127 if (unlikely(Cache == NULL || DepCache == NULL))
128 return false;
129
130 std::vector<string> packages;
131
132 bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
133
134 if (CmdL.FileList[1] == 0)
135 {
136 packages.reserve(Cache->HeaderP->PackageCount / 3);
137 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
138 if (P->CurrentVer != 0 &&
139 (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
140 packages.push_back(P.FullName(true));
141 }
142 else
143 {
144 APT::CacheSetHelper helper(false); // do not show errors
145 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
146 packages.reserve(pkgset.size());
147 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
148 if (P->CurrentVer != 0 &&
149 (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
150 packages.push_back(P.FullName(true));
151 }
152
153 std::sort(packages.begin(), packages.end());
154
155 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
156 std::cout << *I << std::endl;
157
158 return true;
159 }
160 /*}}}*/
161 /* DoHold - mark packages as hold by dpkg {{{*/
162 bool DoHold(CommandLine &CmdL)
163 {
164 pkgCacheFile CacheFile;
165 pkgCache *Cache = CacheFile.GetPkgCache();
166 if (unlikely(Cache == NULL))
167 return false;
168
169 // Generate the base argument list for dpkg
170 std::vector<const char *> Args;
171 string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
172 {
173 string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/");
174 size_t dpkgChrootLen = dpkgChrootDir.length();
175 if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0)
176 {
177 if (dpkgChrootDir[dpkgChrootLen - 1] == '/')
178 --dpkgChrootLen;
179 Tmp = Tmp.substr(dpkgChrootLen);
180 }
181 }
182 Args.push_back(Tmp.c_str());
183
184 // Stick in any custom dpkg options
185 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
186 if (Opts != 0)
187 {
188 Opts = Opts->Child;
189 for (; Opts != 0; Opts = Opts->Next)
190 {
191 if (Opts->Value.empty() == true)
192 continue;
193 Args.push_back(Opts->Value.c_str());
194 }
195 }
196
197 size_t const BaseArgs = Args.size();
198 // we need to detect if we can qualify packages with the architecture or not
199 Args.push_back("--assert-multi-arch");
200 Args.push_back(NULL);
201
202
203 pid_t dpkgAssertMultiArch = ExecFork();
204 if (dpkgAssertMultiArch == 0)
205 {
206 std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
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 if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
213 _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --assert-multi-arch", chrootDir.c_str());
214 execvp(Args[0], (char**) &Args[0]);
215 _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
216 _exit(2);
217 }
218
219 APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
220 if (pkgset.empty() == true)
221 return _error->Error(_("No packages found"));
222
223 bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
224
225 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end();)
226 {
227 if ((Pkg->SelectedState == pkgCache::State::Hold) == MarkHold)
228 {
229 if (MarkHold == true)
230 ioprintf(c1out,_("%s was already set on hold.\n"), Pkg.FullName(true).c_str());
231 else
232 ioprintf(c1out,_("%s was already not hold.\n"), Pkg.FullName(true).c_str());
233 Pkg = pkgset.erase(Pkg, true);
234 }
235 else
236 ++Pkg;
237 }
238
239 bool dpkgMultiArch = false;
240 if (dpkgAssertMultiArch > 0)
241 {
242 int Status = 0;
243 while (waitpid(dpkgAssertMultiArch, &Status, 0) != dpkgAssertMultiArch)
244 {
245 if (errno == EINTR)
246 continue;
247 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
248 break;
249 }
250 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
251 dpkgMultiArch = true;
252 }
253
254 if (pkgset.empty() == true)
255 return true;
256
257 if (_config->FindB("APT::Mark::Simulate", false) == true)
258 {
259 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
260 {
261 if (MarkHold == false)
262 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
263 else
264 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
265 }
266 return true;
267 }
268
269 Args.erase(Args.begin() + BaseArgs, Args.end());
270 Args.push_back("--set-selections");
271 Args.push_back(NULL);
272
273 int external[2] = {-1, -1};
274 if (pipe(external) != 0)
275 return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
276
277 pid_t dpkgSelection = ExecFork();
278 if (dpkgSelection == 0)
279 {
280 close(external[1]);
281 std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
282 if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
283 _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir.c_str());
284 int const nullfd = open("/dev/null", O_RDONLY);
285 dup2(external[0], STDIN_FILENO);
286 dup2(nullfd, STDOUT_FILENO);
287 dup2(nullfd, STDERR_FILENO);
288 execvp(Args[0], (char**) &Args[0]);
289 _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
290 _exit(2);
291 }
292
293 FILE* dpkg = fdopen(external[1], "w");
294 for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
295 {
296 if (dpkgMultiArch == false)
297 fprintf(dpkg, "%s", Pkg.FullName(true).c_str());
298 else
299 {
300 if (Pkg->CurrentVer != 0)
301 fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.CurrentVer().Arch());
302 else if (Pkg.VersionList().end() == false)
303 fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.VersionList().Arch());
304 else
305 fprintf(dpkg, "%s", Pkg.FullName(false).c_str());
306 }
307
308 if (MarkHold == true)
309 {
310 fprintf(dpkg, " hold\n");
311 ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
312 }
313 else
314 {
315 fprintf(dpkg, " install\n");
316 ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
317 }
318 }
319 fclose(dpkg);
320
321 if (dpkgSelection > 0)
322 {
323 int Status = 0;
324 while (waitpid(dpkgSelection, &Status, 0) != dpkgSelection)
325 {
326 if (errno == EINTR)
327 continue;
328 _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
329 break;
330 }
331 if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
332 return true;
333 }
334 return _error->Error(_("Executing dpkg failed. Are you root?"));
335 }
336 /*}}}*/
337 /* ShowHold - show packages set on hold in dpkg status {{{*/
338 bool ShowHold(CommandLine &CmdL)
339 {
340 pkgCacheFile CacheFile;
341 pkgCache *Cache = CacheFile.GetPkgCache();
342 if (unlikely(Cache == NULL))
343 return false;
344
345 std::vector<string> packages;
346
347 if (CmdL.FileList[1] == 0)
348 {
349 packages.reserve(50); // how many holds are realistic? I hope just a few…
350 for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
351 if (P->SelectedState == pkgCache::State::Hold)
352 packages.push_back(P.FullName(true));
353 }
354 else
355 {
356 APT::CacheSetHelper helper(false); // do not show errors
357 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
358 packages.reserve(pkgset.size());
359 for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
360 if (P->SelectedState == pkgCache::State::Hold)
361 packages.push_back(P.FullName(true));
362 }
363
364 std::sort(packages.begin(), packages.end());
365
366 for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
367 std::cout << *I << std::endl;
368
369 return true;
370 }
371 /*}}}*/
372 // ShowHelp - Show a help screen /*{{{*/
373 // ---------------------------------------------------------------------
374 /* */
375 bool ShowHelp(CommandLine &CmdL)
376 {
377 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
378 COMMON_ARCH,__DATE__,__TIME__);
379
380 cout <<
381 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
382 "\n"
383 "apt-mark is a simple command line interface for marking packages\n"
384 "as manually or automatically installed. It can also list marks.\n"
385 "\n"
386 "Commands:\n"
387 " auto - Mark the given packages as automatically installed\n"
388 " manual - Mark the given packages as manually installed\n"
389 " hold - Mark a package as held back\n"
390 " unhold - Unset a package set as held back\n"
391 " showauto - Print the list of automatically installed packages\n"
392 " showmanual - Print the list of manually installed packages\n"
393 " showhold - Print the list of package on hold\n"
394 "\n"
395 "Options:\n"
396 " -h This help text.\n"
397 " -q Loggable output - no progress indicator\n"
398 " -qq No output except for errors\n"
399 " -s No-act. Just prints what would be done.\n"
400 " -f read/write auto/manual marking in the given file\n"
401 " -c=? Read this configuration file\n"
402 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
403 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
404 << std::endl;
405 return true;
406 }
407 /*}}}*/
408 int main(int argc,const char *argv[]) /*{{{*/
409 {
410 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
411 {"auto",&DoAuto},
412 {"manual",&DoAuto},
413 {"hold",&DoHold},
414 {"unhold",&DoHold},
415 {"showauto",&ShowAuto},
416 {"showmanual",&ShowAuto},
417 {"showhold",&ShowHold},
418 // be nice and forgive the typo
419 {"showholds",&ShowHold},
420 // be nice and forgive it as it is technical right
421 {"install",&DoHold},
422 // obsolete commands for compatibility
423 {"markauto", &DoMarkAuto},
424 {"unmarkauto", &DoMarkAuto},
425 {0,0}};
426
427 std::vector<CommandLine::Args> Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv));
428
429 // Set up gettext support
430 setlocale(LC_ALL,"");
431 textdomain(PACKAGE);
432
433 // Parse the command line and initialize the package library
434 CommandLine CmdL(Args.data(),_config);
435 if (pkgInitConfig(*_config) == false ||
436 CmdL.Parse(argc,argv) == false ||
437 pkgInitSystem(*_config,_system) == false)
438 {
439 if (_config->FindB("version") == true)
440 ShowHelp(CmdL);
441 _error->DumpErrors();
442 return 100;
443 }
444
445 // See if the help should be shown
446 if (_config->FindB("help") == true ||
447 _config->FindB("version") == true ||
448 CmdL.FileSize() == 0)
449 {
450 ShowHelp(CmdL);
451 return 0;
452 }
453
454 // Deal with stdout not being a tty
455 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
456 _config->Set("quiet","1");
457
458 // Setup the output streams
459 c0out.rdbuf(cout.rdbuf());
460 c1out.rdbuf(cout.rdbuf());
461 c2out.rdbuf(cout.rdbuf());
462 if (_config->FindI("quiet",0) > 0)
463 c0out.rdbuf(devnull.rdbuf());
464 if (_config->FindI("quiet",0) > 1)
465 c1out.rdbuf(devnull.rdbuf());
466
467 // Match the operation
468 CmdL.DispatchArg(Cmds);
469
470 // Print any errors or warnings found during parsing
471 bool const Errors = _error->PendingError();
472 if (_config->FindI("quiet",0) > 0)
473 _error->DumpErrors();
474 else
475 _error->DumpErrors(GlobalError::DEBUG);
476 return Errors == true ? 100 : 0;
477 }
478 /*}}}*/