]>
git.saurik.com Git - apt.git/blob - cmdline/apt-mark.cc
9d1d0863e4b6cfb730ec7271946fc470f4f72441
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/statechanges.h>
19 #include <apt-pkg/cacheiterators.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/depcache.h>
22 #include <apt-pkg/macros.h>
23 #include <apt-pkg/pkgcache.h>
25 #include <apt-private/private-cmndline.h>
26 #include <apt-private/private-output.h>
46 /* DoAuto - mark packages as automatically/manually installed {{{*/
47 static bool DoAuto(CommandLine
&CmdL
)
49 pkgCacheFile CacheFile
;
50 pkgCache
*Cache
= CacheFile
.GetPkgCache();
51 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
52 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
55 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
56 if (pkgset
.empty() == true)
57 return _error
->Error(_("No packages found"));
59 bool MarkAuto
= strcasecmp(CmdL
.FileList
[0],"auto") == 0;
60 int AutoMarkChanged
= 0;
62 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
64 if (Pkg
->CurrentVer
== 0)
66 ioprintf(c1out
,_("%s can not be marked as it is not installed.\n"), Pkg
.FullName(true).c_str());
69 else if ((((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
71 if (MarkAuto
== false)
72 ioprintf(c1out
,_("%s was already set to manually installed.\n"), Pkg
.FullName(true).c_str());
74 ioprintf(c1out
,_("%s was already set to automatically installed.\n"), Pkg
.FullName(true).c_str());
78 if (MarkAuto
== false)
79 ioprintf(c1out
,_("%s set to manually installed.\n"), Pkg
.FullName(true).c_str());
81 ioprintf(c1out
,_("%s set to automatically installed.\n"), Pkg
.FullName(true).c_str());
83 DepCache
->MarkAuto(Pkg
, MarkAuto
);
86 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
87 return DepCache
->writeStateFile(NULL
);
91 /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
92 /* Does the same as DoAuto but tries to do it exactly the same why as
93 the python implementation did it so it can be a drop-in replacement */
94 static bool DoMarkAuto(CommandLine
&CmdL
)
96 pkgCacheFile CacheFile
;
97 pkgCache
*Cache
= CacheFile
.GetPkgCache();
98 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
99 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
102 APT::PackageList pkgset
= APT::PackageList::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
103 if (pkgset
.empty() == true)
104 return _error
->Error(_("No packages found"));
106 bool const MarkAuto
= strcasecmp(CmdL
.FileList
[0],"markauto") == 0;
107 bool const Verbose
= _config
->FindB("APT::MarkAuto::Verbose", false);
108 int AutoMarkChanged
= 0;
110 for (APT::PackageList::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
112 if (Pkg
->CurrentVer
== 0 ||
113 (((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
117 ioprintf(c1out
, "changing %s to %d\n", Pkg
.Name(), (MarkAuto
== false) ? 0 : 1);
119 DepCache
->MarkAuto(Pkg
, MarkAuto
);
122 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
123 return DepCache
->writeStateFile(NULL
);
125 _error
->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
130 /* ShowAuto - show automatically installed packages (sorted) {{{*/
131 static bool ShowAuto(CommandLine
&CmdL
)
133 pkgCacheFile CacheFile
;
134 pkgCache
*Cache
= CacheFile
.GetPkgCache();
135 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
136 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
139 std::vector
<string
> packages
;
141 bool const ShowAuto
= strcasecmp(CmdL
.FileList
[0],"showauto") == 0;
143 if (CmdL
.FileList
[1] == 0)
145 packages
.reserve(Cache
->HeaderP
->PackageCount
/ 3);
146 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
147 if (P
->CurrentVer
!= 0 &&
148 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
149 packages
.push_back(P
.FullName(true));
153 APT::CacheSetHelper
helper(false); // do not show errors
154 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
155 packages
.reserve(pkgset
.size());
156 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
157 if (P
->CurrentVer
!= 0 &&
158 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
159 packages
.push_back(P
.FullName(true));
162 std::sort(packages
.begin(), packages
.end());
164 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
165 std::cout
<< *I
<< std::endl
;
170 /* DoHold - mark packages as hold by dpkg {{{*/
171 static bool DoHold(CommandLine
&CmdL
)
173 pkgCacheFile CacheFile
;
174 pkgCache
*Cache
= CacheFile
.GetPkgCache();
175 if (unlikely(Cache
== NULL
))
178 APT::VersionVector pkgset
= APT::VersionVector::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, APT::CacheSetHelper::INSTCAND
);
179 if (pkgset
.empty() == true)
180 return _error
->Error(_("No packages found"));
182 bool const MarkHold
= strcasecmp(CmdL
.FileList
[0],"hold") == 0;
184 auto const part
= std::stable_partition(pkgset
.begin(), pkgset
.end(),
185 [](pkgCache::VerIterator
const &V
) { return V
.ParentPkg()->SelectedState
== pkgCache::State::Hold
; });
187 auto const doneBegin
= MarkHold
? pkgset
.begin() : part
;
188 auto const doneEnd
= MarkHold
? part
: pkgset
.end();
190 std::for_each(doneBegin
, doneEnd
, [&MarkHold
](pkgCache::VerIterator
const &V
) {
191 if (MarkHold
== true)
192 ioprintf(c1out
, _("%s was already set on hold.\n"), V
.ParentPkg().FullName(true).c_str());
194 ioprintf(c1out
, _("%s was already not hold.\n"), V
.ParentPkg().FullName(true).c_str());
197 if (doneBegin
== pkgset
.begin() && doneEnd
== pkgset
.end())
200 auto const changeBegin
= MarkHold
? part
: pkgset
.begin();
201 auto const changeEnd
= MarkHold
? pkgset
.end() : part
;
203 APT::StateChanges marks
;
204 std::move(changeBegin
, changeEnd
, std::back_inserter(MarkHold
? marks
.Hold() : marks
.Unhold()));
208 if (_config
->FindB("APT::Mark::Simulate", false) == false)
210 success
= marks
.Save();
211 if (success
== false)
212 _error
->Error(_("Executing dpkg failed. Are you root?"));
215 for (auto Ver
: marks
.Hold())
216 ioprintf(c1out
,_("%s set on hold.\n"), Ver
.ParentPkg().FullName(true).c_str());
217 for (auto Ver
: marks
.Unhold())
218 ioprintf(c1out
,_("Canceled hold on %s.\n"), Ver
.ParentPkg().FullName(true).c_str());
223 /* ShowHold - show packages set on hold in dpkg status {{{*/
224 static bool ShowHold(CommandLine
&CmdL
)
226 pkgCacheFile CacheFile
;
227 pkgCache
*Cache
= CacheFile
.GetPkgCache();
228 if (unlikely(Cache
== NULL
))
231 std::vector
<string
> packages
;
233 if (CmdL
.FileList
[1] == 0)
235 packages
.reserve(50); // how many holds are realistic? I hope just a few…
236 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
237 if (P
->SelectedState
== pkgCache::State::Hold
)
238 packages
.push_back(P
.FullName(true));
242 APT::CacheSetHelper
helper(false); // do not show errors
243 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
244 packages
.reserve(pkgset
.size());
245 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
246 if (P
->SelectedState
== pkgCache::State::Hold
)
247 packages
.push_back(P
.FullName(true));
250 std::sort(packages
.begin(), packages
.end());
252 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
253 std::cout
<< *I
<< std::endl
;
258 // ShowHelp - Show a help screen /*{{{*/
259 // ---------------------------------------------------------------------
261 static bool ShowHelp(CommandLine
&)
263 ioprintf(std::cout
, "%s %s (%s)\n", PACKAGE
, PACKAGE_VERSION
, COMMON_ARCH
);
266 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
268 "apt-mark is a simple command line interface for marking packages\n"
269 "as manually or automatically installed. It can also list marks.\n"
272 " auto - Mark the given packages as automatically installed\n"
273 " manual - Mark the given packages as manually installed\n"
274 " hold - Mark a package as held back\n"
275 " unhold - Unset a package set as held back\n"
276 " showauto - Print the list of automatically installed packages\n"
277 " showmanual - Print the list of manually installed packages\n"
278 " showhold - Print the list of package on hold\n"
281 " -h This help text.\n"
282 " -q Loggable output - no progress indicator\n"
283 " -qq No output except for errors\n"
284 " -s No-act. Just prints what would be done.\n"
285 " -f read/write auto/manual marking in the given file\n"
286 " -c=? Read this configuration file\n"
287 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
288 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
293 int main(int argc
,const char *argv
[]) /*{{{*/
295 CommandLine::Dispatch Cmds
[] = {{"help",&ShowHelp
},
300 {"showauto",&ShowAuto
},
301 {"showmanual",&ShowAuto
},
302 {"showhold",&ShowHold
},
303 // be nice and forgive the typo
304 {"showholds",&ShowHold
},
305 // be nice and forgive it as it is technical right
307 // obsolete commands for compatibility
308 {"markauto", &DoMarkAuto
},
309 {"unmarkauto", &DoMarkAuto
},
312 std::vector
<CommandLine::Args
> Args
= getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds
, argc
, argv
));
314 // Set up gettext support
315 setlocale(LC_ALL
,"");
319 ParseCommandLine(CmdL
, Cmds
, Args
.data(), &_config
, &_system
, argc
, argv
, ShowHelp
);
323 // Match the operation
324 CmdL
.DispatchArg(Cmds
);
326 // Print any errors or warnings found during parsing
327 bool const Errors
= _error
->PendingError();
328 if (_config
->FindI("quiet",0) > 0)
329 _error
->DumpErrors();
331 _error
->DumpErrors(GlobalError::DEBUG
);
332 return Errors
== true ? 100 : 0;