]>
git.saurik.com Git - apt.git/blob - cmdline/apt-mark.cc
9b51345a39b88a100f92037179ba3f4b3e2f8b1d
1 // -*- mode: cpp; mode: fold -*-
3 /* #####################################################################
4 apt-mark - show and change auto-installed bit information
5 ##################################################################### */
7 // Include Files /*{{{*/
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/fileutl.h>
15 #include <apt-pkg/init.h>
16 #include <apt-pkg/pkgsystem.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/cacheiterators.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/depcache.h>
21 #include <apt-pkg/macros.h>
22 #include <apt-pkg/pkgcache.h>
24 #include <apt-private/private-cmndline.h>
25 #include <apt-private/private-output.h>
45 /* DoAuto - mark packages as automatically/manually installed {{{*/
46 static bool DoAuto(CommandLine
&CmdL
)
48 pkgCacheFile CacheFile
;
49 pkgCache
*Cache
= CacheFile
.GetPkgCache();
50 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
51 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
54 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
55 if (pkgset
.empty() == true)
56 return _error
->Error(_("No packages found"));
58 bool MarkAuto
= strcasecmp(CmdL
.FileList
[0],"auto") == 0;
59 int AutoMarkChanged
= 0;
61 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
63 if (Pkg
->CurrentVer
== 0)
65 ioprintf(c1out
,_("%s can not be marked as it is not installed.\n"), Pkg
.FullName(true).c_str());
68 else if ((((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
70 if (MarkAuto
== false)
71 ioprintf(c1out
,_("%s was already set to manually installed.\n"), Pkg
.FullName(true).c_str());
73 ioprintf(c1out
,_("%s was already set to automatically installed.\n"), Pkg
.FullName(true).c_str());
77 if (MarkAuto
== false)
78 ioprintf(c1out
,_("%s set to manually installed.\n"), Pkg
.FullName(true).c_str());
80 ioprintf(c1out
,_("%s set to automatically installed.\n"), Pkg
.FullName(true).c_str());
82 DepCache
->MarkAuto(Pkg
, MarkAuto
);
85 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
86 return DepCache
->writeStateFile(NULL
);
90 /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
91 /* Does the same as DoAuto but tries to do it exactly the same why as
92 the python implementation did it so it can be a drop-in replacement */
93 static bool DoMarkAuto(CommandLine
&CmdL
)
95 pkgCacheFile CacheFile
;
96 pkgCache
*Cache
= CacheFile
.GetPkgCache();
97 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
98 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
101 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
102 if (pkgset
.empty() == true)
103 return _error
->Error(_("No packages found"));
105 bool const MarkAuto
= strcasecmp(CmdL
.FileList
[0],"markauto") == 0;
106 bool const Verbose
= _config
->FindB("APT::MarkAuto::Verbose", false);
107 int AutoMarkChanged
= 0;
109 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
111 if (Pkg
->CurrentVer
== 0 ||
112 (((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
116 ioprintf(c1out
, "changing %s to %d\n", Pkg
.Name(), (MarkAuto
== false) ? 0 : 1);
118 DepCache
->MarkAuto(Pkg
, MarkAuto
);
121 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
122 return DepCache
->writeStateFile(NULL
);
124 _error
->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
129 /* ShowAuto - show automatically installed packages (sorted) {{{*/
130 static bool ShowAuto(CommandLine
&CmdL
)
132 pkgCacheFile CacheFile
;
133 pkgCache
*Cache
= CacheFile
.GetPkgCache();
134 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
135 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
138 std::vector
<string
> packages
;
140 bool const ShowAuto
= strcasecmp(CmdL
.FileList
[0],"showauto") == 0;
142 if (CmdL
.FileList
[1] == 0)
144 packages
.reserve(Cache
->HeaderP
->PackageCount
/ 3);
145 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
146 if (P
->CurrentVer
!= 0 &&
147 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
148 packages
.push_back(P
.FullName(true));
152 APT::CacheSetHelper
helper(false); // do not show errors
153 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
154 packages
.reserve(pkgset
.size());
155 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
156 if (P
->CurrentVer
!= 0 &&
157 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
158 packages
.push_back(P
.FullName(true));
161 std::sort(packages
.begin(), packages
.end());
163 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
164 std::cout
<< *I
<< std::endl
;
169 /* DoHold - mark packages as hold by dpkg {{{*/
170 static bool DoHold(CommandLine
&CmdL
)
172 pkgCacheFile CacheFile
;
173 pkgCache
*Cache
= CacheFile
.GetPkgCache();
174 if (unlikely(Cache
== NULL
))
177 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
178 if (pkgset
.empty() == true)
179 return _error
->Error(_("No packages found"));
181 bool const MarkHold
= strcasecmp(CmdL
.FileList
[0],"hold") == 0;
183 auto part
= std::stable_partition(pkgset
.begin(), pkgset
.end(),
184 [](pkgCache::PkgIterator
const &P
) { return P
->SelectedState
== pkgCache::State::Hold
; });
186 auto doneBegin
= MarkHold
? pkgset
.begin() : part
;
187 auto doneEnd
= MarkHold
? part
: pkgset
.end();
188 auto changeBegin
= MarkHold
? part
: pkgset
.begin();
189 auto changeEnd
= MarkHold
? pkgset
.end() : part
;
191 std::for_each(doneBegin
, doneEnd
, [&MarkHold
](pkgCache::PkgIterator
const &P
) {
192 if (MarkHold
== true)
193 ioprintf(c1out
, _("%s was already set on hold.\n"), P
.FullName(true).c_str());
195 ioprintf(c1out
, _("%s was already not hold.\n"), P
.FullName(true).c_str());
198 if (doneBegin
== pkgset
.begin() && doneEnd
== pkgset
.end())
201 if (_config
->FindB("APT::Mark::Simulate", false) == true)
203 std::for_each(changeBegin
, changeEnd
, [&MarkHold
](pkgCache::PkgIterator
const &P
) {
204 if (MarkHold
== false)
205 ioprintf(c1out
, _("%s set on hold.\n"), P
.FullName(true).c_str());
207 ioprintf(c1out
, _("Canceled hold on %s.\n"), P
.FullName(true).c_str());
212 // Generate the base argument list for dpkg
213 std::vector
<const char *> Args
;
214 string Tmp
= _config
->Find("Dir::Bin::dpkg","dpkg");
216 string
const dpkgChrootDir
= _config
->FindDir("DPkg::Chroot-Directory", "/");
217 size_t dpkgChrootLen
= dpkgChrootDir
.length();
218 if (dpkgChrootDir
!= "/" && Tmp
.find(dpkgChrootDir
) == 0)
220 if (dpkgChrootDir
[dpkgChrootLen
- 1] == '/')
222 Tmp
= Tmp
.substr(dpkgChrootLen
);
225 Args
.push_back(Tmp
.c_str());
227 // Stick in any custom dpkg options
228 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
232 for (; Opts
!= 0; Opts
= Opts
->Next
)
234 if (Opts
->Value
.empty() == true)
236 Args
.push_back(Opts
->Value
.c_str());
240 APT::PackageList keepoffset
;
241 for (APT::PackageList::iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
243 if (Pkg
->CurrentVer
!= 0)
245 keepoffset
.insert(*Pkg
);
248 if (keepoffset
.empty() == false)
250 size_t const BaseArgs
= Args
.size();
251 Args
.push_back("--merge-avail");
252 // FIXME: supported only since 1.17.7 in dpkg
254 Args
.push_back(NULL
);
256 int external
[2] = {-1, -1};
257 if (pipe(external
) != 0)
258 return _error
->WarningE("DoHold", "Can't create IPC pipe for dpkg --merge-avail");
260 pid_t dpkgMergeAvail
= ExecFork();
261 if (dpkgMergeAvail
== 0)
264 std::string
const chrootDir
= _config
->FindDir("DPkg::Chroot-Directory");
265 if (chrootDir
!= "/" && chroot(chrootDir
.c_str()) != 0 && chdir("/") != 0)
266 _error
->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --merge-avail", chrootDir
.c_str());
267 dup2(external
[0], STDIN_FILENO
);
268 int const nullfd
= open("/dev/null", O_RDONLY
);
269 dup2(nullfd
, STDOUT_FILENO
);
270 execvp(Args
[0], (char**) &Args
[0]);
271 _error
->WarningE("dpkgGo", "Can't get dpkg --merge-avail running!");
275 FILE* dpkg
= fdopen(external
[1], "w");
276 for (APT::PackageList::iterator Pkg
= keepoffset
.begin(); Pkg
!= keepoffset
.end(); ++Pkg
)
279 if (Pkg
->VersionList
!= 0)
280 Arch
= Pkg
.VersionList().Arch();
283 fprintf(dpkg
, "Package: %s\nVersion: 0~\nArchitecture: %s\nMaintainer: Dummy Example <dummy@example.org>\n"
284 "Description: dummy package record\n A record is needed to put a package on hold, so here it is.\n\n", Pkg
.Name(), Arch
);
289 if (dpkgMergeAvail
> 0)
292 while (waitpid(dpkgMergeAvail
, &Status
, 0) != dpkgMergeAvail
)
296 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --merge-avail");
299 if (WIFEXITED(Status
) == false || WEXITSTATUS(Status
) != 0)
300 return _error
->Error(_("Executing dpkg failed. Are you root?"));
302 Args
.erase(Args
.begin() + BaseArgs
, Args
.end());
305 Args
.push_back("--set-selections");
306 Args
.push_back(NULL
);
308 int external
[2] = {-1, -1};
309 if (pipe(external
) != 0)
310 return _error
->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
312 pid_t dpkgSelection
= ExecFork();
313 if (dpkgSelection
== 0)
316 std::string
const chrootDir
= _config
->FindDir("DPkg::Chroot-Directory");
317 if (chrootDir
!= "/" && chroot(chrootDir
.c_str()) != 0 && chdir("/") != 0)
318 _error
->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir
.c_str());
319 dup2(external
[0], STDIN_FILENO
);
320 execvp(Args
[0], (char**) &Args
[0]);
321 _error
->WarningE("dpkgGo", "Can't get dpkg --set-selections running!");
325 bool const dpkgMultiArch
= _system
->MultiArchSupported();
326 FILE* dpkg
= fdopen(external
[1], "w");
327 for (auto Pkg
= changeBegin
; Pkg
!= changeEnd
; ++Pkg
)
329 if (dpkgMultiArch
== false)
330 fprintf(dpkg
, "%s", Pkg
.FullName(true).c_str());
333 if (Pkg
->CurrentVer
!= 0)
334 fprintf(dpkg
, "%s:%s", Pkg
.Name(), Pkg
.CurrentVer().Arch());
335 else if (Pkg
.VersionList().end() == false)
336 fprintf(dpkg
, "%s:%s", Pkg
.Name(), Pkg
.VersionList().Arch());
338 fprintf(dpkg
, "%s", Pkg
.FullName(false).c_str());
341 if (MarkHold
== true)
343 fprintf(dpkg
, " hold\n");
344 ioprintf(c1out
,_("%s set on hold.\n"), Pkg
.FullName(true).c_str());
348 fprintf(dpkg
, " install\n");
349 ioprintf(c1out
,_("Canceled hold on %s.\n"), Pkg
.FullName(true).c_str());
354 if (dpkgSelection
> 0)
357 while (waitpid(dpkgSelection
, &Status
, 0) != dpkgSelection
)
361 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
364 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
367 return _error
->Error(_("Executing dpkg failed. Are you root?"));
370 /* ShowHold - show packages set on hold in dpkg status {{{*/
371 static bool ShowHold(CommandLine
&CmdL
)
373 pkgCacheFile CacheFile
;
374 pkgCache
*Cache
= CacheFile
.GetPkgCache();
375 if (unlikely(Cache
== NULL
))
378 std::vector
<string
> packages
;
380 if (CmdL
.FileList
[1] == 0)
382 packages
.reserve(50); // how many holds are realistic? I hope just a few…
383 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
384 if (P
->SelectedState
== pkgCache::State::Hold
)
385 packages
.push_back(P
.FullName(true));
389 APT::CacheSetHelper
helper(false); // do not show errors
390 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
391 packages
.reserve(pkgset
.size());
392 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
393 if (P
->SelectedState
== pkgCache::State::Hold
)
394 packages
.push_back(P
.FullName(true));
397 std::sort(packages
.begin(), packages
.end());
399 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
400 std::cout
<< *I
<< std::endl
;
405 // ShowHelp - Show a help screen /*{{{*/
406 // ---------------------------------------------------------------------
408 static bool ShowHelp(CommandLine
&)
410 ioprintf(std::cout
, "%s %s (%s)\n", PACKAGE
, PACKAGE_VERSION
, COMMON_ARCH
);
413 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
415 "apt-mark is a simple command line interface for marking packages\n"
416 "as manually or automatically installed. It can also list marks.\n"
419 " auto - Mark the given packages as automatically installed\n"
420 " manual - Mark the given packages as manually installed\n"
421 " hold - Mark a package as held back\n"
422 " unhold - Unset a package set as held back\n"
423 " showauto - Print the list of automatically installed packages\n"
424 " showmanual - Print the list of manually installed packages\n"
425 " showhold - Print the list of package on hold\n"
428 " -h This help text.\n"
429 " -q Loggable output - no progress indicator\n"
430 " -qq No output except for errors\n"
431 " -s No-act. Just prints what would be done.\n"
432 " -f read/write auto/manual marking in the given file\n"
433 " -c=? Read this configuration file\n"
434 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
435 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
440 int main(int argc
,const char *argv
[]) /*{{{*/
442 CommandLine::Dispatch Cmds
[] = {{"help",&ShowHelp
},
447 {"showauto",&ShowAuto
},
448 {"showmanual",&ShowAuto
},
449 {"showhold",&ShowHold
},
450 // be nice and forgive the typo
451 {"showholds",&ShowHold
},
452 // be nice and forgive it as it is technical right
454 // obsolete commands for compatibility
455 {"markauto", &DoMarkAuto
},
456 {"unmarkauto", &DoMarkAuto
},
459 std::vector
<CommandLine::Args
> Args
= getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds
, argc
, argv
));
461 // Set up gettext support
462 setlocale(LC_ALL
,"");
466 ParseCommandLine(CmdL
, Cmds
, Args
.data(), &_config
, &_system
, argc
, argv
, ShowHelp
);
470 // Match the operation
471 CmdL
.DispatchArg(Cmds
);
473 // Print any errors or warnings found during parsing
474 bool const Errors
= _error
->PendingError();
475 if (_config
->FindI("quiet",0) > 0)
476 _error
->DumpErrors();
478 _error
->DumpErrors(GlobalError::DEBUG
);
479 return Errors
== true ? 100 : 0;