]>
git.saurik.com Git - apt.git/blob - cmdline/apt-mark.cc
a5dc45a10555d46043535c9a517c63b1d7e946f0
1 // -*- mode: cpp; mode: fold -*-
3 /* #####################################################################
4 apt-mark - show and change auto-installed bit information
5 ##################################################################### */
7 // Include Files /*{{{*/
8 #include <apt-pkg/cachefile.h>
9 #include <apt-pkg/cacheset.h>
10 #include <apt-pkg/cmndline.h>
11 #include <apt-pkg/error.h>
12 #include <apt-pkg/init.h>
13 #include <apt-pkg/strutl.h>
25 ofstream
devnull("/dev/null");
26 /* DoAuto - mark packages as automatically/manually installed {{{*/
27 bool DoAuto(CommandLine
&CmdL
)
29 pkgCacheFile CacheFile
;
30 pkgCache
*Cache
= CacheFile
.GetPkgCache();
31 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
32 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
35 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
36 if (pkgset
.empty() == true)
37 return _error
->Error(_("No packages found"));
39 bool MarkAuto
= strcasecmp(CmdL
.FileList
[0],"auto") == 0;
40 int AutoMarkChanged
= 0;
42 for (APT::PackageSet::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
44 if (Pkg
->CurrentVer
== 0)
46 ioprintf(c1out
,_("%s can not be marked as it is not installed.\n"), Pkg
.FullName(true).c_str());
49 else if ((((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
51 if (MarkAuto
== false)
52 ioprintf(c1out
,_("%s was already set to manually installed.\n"), Pkg
.FullName(true).c_str());
54 ioprintf(c1out
,_("%s was already set to automatically installed.\n"), Pkg
.FullName(true).c_str());
58 if (MarkAuto
== false)
59 ioprintf(c1out
,_("%s set to manually installed.\n"), Pkg
.FullName(true).c_str());
61 ioprintf(c1out
,_("%s set to automatically installed.\n"), Pkg
.FullName(true).c_str());
63 DepCache
->MarkAuto(Pkg
, MarkAuto
);
66 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
67 return DepCache
->writeStateFile(NULL
);
71 /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
72 /* Does the same as DoAuto but tries to do it exactly the same why as
73 the python implementation did it so it can be a drop-in replacement */
74 bool DoMarkAuto(CommandLine
&CmdL
)
76 pkgCacheFile CacheFile
;
77 pkgCache
*Cache
= CacheFile
.GetPkgCache();
78 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
79 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
82 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
83 if (pkgset
.empty() == true)
84 return _error
->Error(_("No packages found"));
86 bool const MarkAuto
= strcasecmp(CmdL
.FileList
[0],"markauto") == 0;
87 bool const Verbose
= _config
->FindB("APT::MarkAuto::Verbose", false);
88 int AutoMarkChanged
= 0;
90 for (APT::PackageSet::const_iterator Pkg
= pkgset
.begin(); Pkg
!= pkgset
.end(); ++Pkg
)
92 if (Pkg
->CurrentVer
== 0 ||
93 (((*DepCache
)[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == MarkAuto
)
97 ioprintf(c1out
, "changing %s to %d\n", Pkg
.Name(), (MarkAuto
== false) ? 0 : 1);
99 DepCache
->MarkAuto(Pkg
, MarkAuto
);
102 if (AutoMarkChanged
> 0 && _config
->FindB("APT::Mark::Simulate", false) == false)
103 return DepCache
->writeStateFile(NULL
);
105 _error
->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
110 /* ShowAuto - show automatically installed packages (sorted) {{{*/
111 bool ShowAuto(CommandLine
&CmdL
)
113 pkgCacheFile CacheFile
;
114 pkgCache
*Cache
= CacheFile
.GetPkgCache();
115 pkgDepCache
*DepCache
= CacheFile
.GetDepCache();
116 if (unlikely(Cache
== NULL
|| DepCache
== NULL
))
119 std::vector
<string
> packages
;
121 bool const ShowAuto
= strcasecmp(CmdL
.FileList
[0],"showauto") == 0;
123 if (CmdL
.FileList
[1] == 0)
125 packages
.reserve(Cache
->HeaderP
->PackageCount
/ 3);
126 for (pkgCache::PkgIterator P
= Cache
->PkgBegin(); P
.end() == false; ++P
)
127 if (P
->CurrentVer
!= 0 &&
128 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
129 packages
.push_back(P
.FullName(true));
133 APT::CacheSetHelper
helper(false); // do not show errors
134 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1, helper
);
135 packages
.reserve(pkgset
.size());
136 for (APT::PackageSet::const_iterator P
= pkgset
.begin(); P
!= pkgset
.end(); ++P
)
137 if (P
->CurrentVer
!= 0 &&
138 (((*DepCache
)[P
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
) == ShowAuto
)
139 packages
.push_back(P
.FullName(true));
142 std::sort(packages
.begin(), packages
.end());
144 for (vector
<string
>::const_iterator I
= packages
.begin(); I
!= packages
.end(); ++I
)
145 std::cout
<< *I
<< std::endl
;
150 // ShowHelp - Show a help screen /*{{{*/
151 // ---------------------------------------------------------------------
153 bool ShowHelp(CommandLine
&CmdL
)
155 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,VERSION
,
156 COMMON_ARCH
,__DATE__
,__TIME__
);
159 _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
161 "apt-mark is a simple command line interface for marking packages\n"
162 "as manual or automatical installed. It can also list marks.\n"
165 " auto - Mark the given packages as automatically installed\n"
166 " manual - Mark the given packages as manually installed\n"
169 " -h This help text.\n"
170 " -q Loggable output - no progress indicator\n"
171 " -qq No output except for errors\n"
172 " -s No-act. Just prints what would be done.\n"
173 " -f read/write auto/manual marking in the given file\n"
174 " -c=? Read this configuration file\n"
175 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
176 "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
181 int main(int argc
,const char *argv
[]) /*{{{*/
183 CommandLine::Args Args
[] = {
184 {'h',"help","help",0},
185 {0,"version","version",0},
186 {'q',"quiet","quiet",CommandLine::IntLevel
},
187 {'q',"silent","quiet",CommandLine::IntLevel
},
188 {'v',"verbose","APT::MarkAuto::Verbose",0},
189 {'s',"simulate","APT::Mark::Simulate",0},
190 {'s',"just-print","APT::Mark::Simulate",0},
191 {'s',"recon","APT::Mark::Simulate",0},
192 {'s',"dry-run","APT::Mark::Simulate",0},
193 {'s',"no-act","APT::Mark::Simulate",0},
194 {'f',"file","Dir::State::extended_states",CommandLine::HasArg
},
195 {'c',"config-file",0,CommandLine::ConfigFile
},
196 {'o',"option",0,CommandLine::ArbItem
},
198 CommandLine::Dispatch Cmds
[] = {{"help",&ShowHelp
},
201 {"showauto",&ShowAuto
},
202 {"showmanual",&ShowAuto
},
203 // obsolete commands for compatibility
204 {"markauto", &DoMarkAuto
},
205 {"unmarkauto", &DoMarkAuto
},
208 // Set up gettext support
209 setlocale(LC_ALL
,"");
212 // Parse the command line and initialize the package library
213 CommandLine
CmdL(Args
,_config
);
214 if (pkgInitConfig(*_config
) == false ||
215 CmdL
.Parse(argc
,argv
) == false ||
216 pkgInitSystem(*_config
,_system
) == false)
218 if (_config
->FindB("version") == true)
220 _error
->DumpErrors();
224 // See if the help should be shown
225 if (_config
->FindB("help") == true ||
226 _config
->FindB("version") == true ||
227 CmdL
.FileSize() == 0)
233 // Deal with stdout not being a tty
234 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
235 _config
->Set("quiet","1");
237 // Setup the output streams
238 c0out
.rdbuf(cout
.rdbuf());
239 c1out
.rdbuf(cout
.rdbuf());
240 c2out
.rdbuf(cout
.rdbuf());
241 if (_config
->FindI("quiet",0) > 0)
242 c0out
.rdbuf(devnull
.rdbuf());
243 if (_config
->FindI("quiet",0) > 1)
244 c1out
.rdbuf(devnull
.rdbuf());
246 // Match the operation
247 CmdL
.DispatchArg(Cmds
);
249 // Print any errors or warnings found during parsing
250 bool const Errors
= _error
->PendingError();
251 if (_config
->FindI("quiet",0) > 0)
252 _error
->DumpErrors();
254 _error
->DumpErrors(GlobalError::DEBUG
);
255 return Errors
== true ? 100 : 0;