apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

apt-get.cc
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  apt-get - Cover for dpkg
6 
7  This is an allout cover for dpkg implementing a safer front end. It is
8  based largely on libapt-pkg.
9 
10  The syntax is different,
11  apt-get [opt] command [things]
12  Where command is:
13  update - Resyncronize the package files from their sources
14  upgrade - Smart-Download the newest versions of all packages
15  dselect-upgrade - Follows dselect's changes to the Status: field
16  and installs new and removes old packages
17  dist-upgrade - Powerful upgrader designed to handle the issues with
18  a new distribution.
19  install - Download and install a given package (by name, not by .deb)
20  check - Update the package cache and check for broken packages
21  clean - Erase the .debs downloaded to /var/cache/apt/archives and
22  the partial dir too
23 
24  ##################################################################### */
25  /*}}}*/
26 // Include Files /*{{{*/
27 #include <config.h>
28 
29 #include <apt-pkg/acquire-item.h>
30 #include <apt-pkg/acquire.h>
31 #include <apt-pkg/algorithms.h>
33 #include <apt-pkg/cachefile.h>
34 #include <apt-pkg/cacheset.h>
35 #include <apt-pkg/clean.h>
36 #include <apt-pkg/cmndline.h>
37 #include <apt-pkg/configuration.h>
38 #include <apt-pkg/debmetaindex.h>
39 #include <apt-pkg/depcache.h>
40 #include <apt-pkg/error.h>
41 #include <apt-pkg/fileutl.h>
42 #include <apt-pkg/indexfile.h>
43 #include <apt-pkg/init.h>
44 #include <apt-pkg/macros.h>
45 #include <apt-pkg/metaindex.h>
46 #include <apt-pkg/pkgcache.h>
47 #include <apt-pkg/pkgrecords.h>
48 #include <apt-pkg/pkgsystem.h>
49 #include <apt-pkg/progress.h>
50 #include <apt-pkg/sourcelist.h>
51 #include <apt-pkg/srcrecords.h>
52 #include <apt-pkg/strutl.h>
53 #include <apt-pkg/upgrade.h>
54 #include <apt-pkg/version.h>
55 
69 
70 #include <errno.h>
71 #include <grp.h>
72 #include <pwd.h>
73 #include <signal.h>
74 #include <stddef.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <sys/ioctl.h>
79 #include <sys/stat.h>
80 #include <unistd.h>
81 
82 #include <algorithm>
83 #include <fstream>
84 #include <iostream>
85 #include <set>
86 #include <sstream>
87 #include <string>
88 #include <vector>
89 
90 #include <apti18n.h>
91  /*}}}*/
92 
93 using namespace std;
94 
95 /* mark packages as automatically/manually installed. {{{*/
96 static bool DoMarkAuto(CommandLine &CmdL)
97 {
98  bool Action = true;
99  int AutoMarkChanged = 0;
100  OpTextProgress progress;
101  CacheFile Cache;
102  if (Cache.Open() == false)
103  return false;
104 
105  if (strcasecmp(CmdL.FileList[0],"markauto") == 0)
106  Action = true;
107  else if (strcasecmp(CmdL.FileList[0],"unmarkauto") == 0)
108  Action = false;
109 
110  for (const char **I = CmdL.FileList + 1; *I != 0; I++)
111  {
112  const char *S = *I;
113  // Locate the package
114  pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
115  if (Pkg.end() == true) {
116  return _error->Error(_("Couldn't find package %s"),S);
117  }
118  else
119  {
120  if (!Action)
121  ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.Name());
122  else
123  ioprintf(c1out,_("%s set to automatically installed.\n"),
124  Pkg.Name());
125 
126  Cache->MarkAuto(Pkg,Action);
127  AutoMarkChanged++;
128  }
129  }
130 
131  _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
132 
133  if (AutoMarkChanged && ! _config->FindB("APT::Get::Simulate",false))
134  return Cache->writeStateFile(NULL);
135  return false;
136 }
137  /*}}}*/
138 // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
139 // ---------------------------------------------------------------------
140 /* Follows dselect's selections */
142 {
143  CacheFile Cache;
144  if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
145  return false;
146 
147  pkgDepCache::ActionGroup group(Cache);
148 
149  // Install everything with the install flag set
150  for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() != true; ++I)
151  {
152  /* Install the package only if it is a new install, the autoupgrader
153  will deal with the rest */
154  if (I->SelectedState == pkgCache::State::Install)
155  Cache->MarkInstall(I,false);
156  }
157 
158  /* Now install their deps too, if we do this above then order of
159  the status file is significant for | groups */
160  for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() != true; ++I)
161  {
162  /* Install the package only if it is a new install, the autoupgrader
163  will deal with the rest */
164  if (I->SelectedState == pkgCache::State::Install)
165  Cache->MarkInstall(I,true);
166  }
167 
168  // Apply erasures now, they override everything else.
169  for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() != true; ++I)
170  {
171  // Remove packages
172  if (I->SelectedState == pkgCache::State::DeInstall ||
173  I->SelectedState == pkgCache::State::Purge)
174  Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
175  }
176 
177  /* Resolve any problems that dselect created, allupgrade cannot handle
178  such things. We do so quite aggressively too.. */
179  if (Cache->BrokenCount() != 0)
180  {
181  pkgProblemResolver Fix(Cache);
182 
183  // Hold back held packages.
184  if (_config->FindB("APT::Ignore-Hold",false) == false)
185  {
186  for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; ++I)
187  {
188  if (I->SelectedState == pkgCache::State::Hold)
189  {
190  Fix.Protect(I);
191  Cache->MarkKeep(I);
192  }
193  }
194  }
195 
196  if (Fix.Resolve() == false)
197  {
198  ShowBroken(c1out,Cache,false);
199  return _error->Error(_("Internal error, problem resolver broke stuff"));
200  }
201  }
202 
203  // Now upgrade everything
205  {
206  ShowBroken(c1out,Cache,false);
207  return _error->Error(_("Internal error, problem resolver broke stuff"));
208  }
209 
210  return InstallPackages(Cache,false);
211 }
212  /*}}}*/
213 // DoCheck - Perform the check operation /*{{{*/
214 // ---------------------------------------------------------------------
215 /* Opening automatically checks the system, this command is mostly used
216  for debugging */
217 static bool DoCheck(CommandLine &)
218 {
219  CacheFile Cache;
220  Cache.Open();
221  Cache.CheckDeps();
222 
223  return true;
224 }
225  /*}}}*/
226 // DoIndexTargets - Lists all IndexTargets /*{{{*/
227 static std::string format_key(std::string key)
228 {
229  // deb822 is case-insensitive, but the human eye prefers candy
230  std::transform(key.begin(), key.end(), key.begin(), ::tolower);
231  key[0] = ::toupper(key[0]);
232  size_t found = key.find("_uri");
233  if (found != std::string::npos)
234  key.replace(found, 4, "-URI");
235  while ((found = key.find('_')) != std::string::npos)
236  {
237  key[found] = '-';
238  key[found + 1] = ::toupper(key[found + 1]);
239  }
240  return key;
241 }
242 static bool DoIndexTargets(CommandLine &CmdL)
243 {
246  pkgCache *Cache = CacheFile.GetPkgCache();
247 
248  if (SrcList == nullptr || Cache == nullptr)
249  return false;
250 
251  std::string const Format = _config->Find("APT::Get::IndexTargets::Format");
252  bool const ReleaseInfo = _config->FindB("APT::Get::IndexTargets::ReleaseInfo", true);
253  bool Filtered = CmdL.FileSize() > 1;
254  for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); ++S)
255  {
256  std::vector<IndexTarget> const targets = (*S)->GetIndexTargets();
257  std::map<std::string, string> AddOptions;
258  if (ReleaseInfo)
259  {
260  AddOptions.insert(std::make_pair("TRUSTED", ((*S)->IsTrusted() ? "yes" : "no")));
261  pkgCache::RlsFileIterator const RlsFile = (*S)->FindInCache(*Cache, false);
262  if (RlsFile.end())
263  continue;
264 #define APT_RELEASE(X,Y) if (RlsFile.Y() != NULL) AddOptions.insert(std::make_pair(X, RlsFile.Y()))
265  APT_RELEASE("CODENAME", Codename);
266  APT_RELEASE("SUITE", Archive);
267  APT_RELEASE("VERSION", Version);
268  APT_RELEASE("ORIGIN", Origin);
269  APT_RELEASE("LABEL", Label);
270 #undef APT_RELEASE
271  }
272 
273  for (std::vector<IndexTarget>::const_iterator T = targets.begin(); T != targets.end(); ++T)
274  {
275  std::string filename = T->Option(ReleaseInfo ? IndexTarget::EXISTING_FILENAME : IndexTarget::FILENAME);
276  if (filename.empty())
277  continue;
278 
279  std::ostringstream stanza;
280  if (Filtered || Format.empty())
281  {
282  stanza << "MetaKey: " << T->MetaKey << "\n"
283  << "ShortDesc: " << T->ShortDesc << "\n"
284  << "Description: " << T->Description << "\n"
285  << "URI: " << T->URI << "\n"
286  << "Filename: " << filename << "\n"
287  << "Optional: " << (T->IsOptional ? "yes" : "no") << "\n"
288  << "KeepCompressed: " << (T->KeepCompressed ? "yes" : "no") << "\n";
289  for (std::map<std::string,std::string>::const_iterator O = AddOptions.begin(); O != AddOptions.end(); ++O)
290  stanza << format_key(O->first) << ": " << O->second << "\n";
291  for (std::map<std::string,std::string>::const_iterator O = T->Options.begin(); O != T->Options.end(); ++O)
292  {
293  if (O->first == "PDIFFS")
294  stanza << "PDiffs: " << O->second << "\n";
295  else if (O->first == "COMPRESSIONTYPES")
296  stanza << "CompressionTypes: " << O->second << "\n";
297  else if (O->first == "KEEPCOMPRESSEDAS")
298  stanza << "KeepCompressedAs: " << O->second << "\n";
299  else if (O->first == "DEFAULTENABLED")
300  stanza << "DefaultEnabled: " << O->second << "\n";
301  else
302  stanza << format_key(O->first) << ": " << O->second << "\n";
303  }
304  stanza << "\n";
305 
306  if (Filtered)
307  {
308  // that is a bit crude, but good enough for now
309  bool found = true;
310  std::string haystack = std::string("\n") + stanza.str() + "\n";
311  std::transform(haystack.begin(), haystack.end(), haystack.begin(), ::tolower);
312  size_t const filesize = CmdL.FileSize() - 1;
313  for (size_t i = 0; i != filesize; ++i)
314  {
315  std::string needle = std::string("\n") + CmdL.FileList[i + 1] + "\n";
316  std::transform(needle.begin(), needle.end(), needle.begin(), ::tolower);
317  if (haystack.find(needle) != std::string::npos)
318  continue;
319  found = false;
320  break;
321  }
322  if (found == false)
323  continue;
324  }
325  }
326 
327  if (Format.empty())
328  cout << stanza.str();
329  else
330  {
331  std::string out = SubstVar(Format, "$(FILENAME)", filename);
332  out = T->Format(out);
333  for (std::map<std::string,std::string>::const_iterator O = AddOptions.begin(); O != AddOptions.end(); ++O)
334  out = SubstVar(out, std::string("$(") + O->first + ")", O->second);
335  cout << out << std::endl;
336  }
337  }
338  }
339 
340  return true;
341 }
342  /*}}}*/
343 static bool ShowHelp(CommandLine &) /*{{{*/
344 {
345  if (_config->FindB("version") == true)
346  {
347  cout << _("Supported modules:") << endl;
348 
349  for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
350  {
352  if (_system != 0 && _system->VS == VS)
353  cout << '*';
354  else
355  cout << ' ';
356  cout << "Ver: " << VS->Label << endl;
357 
358  /* Print out all the packaging systems that will work with
359  this VS */
360  for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
361  {
363  if (_system == Sys)
364  cout << '*';
365  else
366  cout << ' ';
367  if (Sys->VS->TestCompatibility(*VS) == true)
368  cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
369  }
370  }
371 
372  for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
373  {
375  cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
376  }
377 
378  for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
379  {
381  cout << " Idx: " << Type->Label << endl;
382  }
383 
384  return true;
385  }
386 
387  std::cout <<
388  _("Usage: apt-get [options] command\n"
389  " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
390  " apt-get [options] source pkg1 [pkg2 ...]\n"
391  "\n"
392  "apt-get is a command line interface for retrieval of packages\n"
393  "and information about them from authenticated sources and\n"
394  "for installation, upgrade and removal of packages together\n"
395  "with their dependencies.\n");
396  return true;
397 }
398  /*}}}*/
399 static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
400 {
401  return {
402  {"update", &DoUpdate, _("Retrieve new lists of packages")},
403  {"upgrade", &DoUpgrade, _("Perform an upgrade")},
404  {"install", &DoInstall, _("Install new packages (pkg is libc6 not libc6.deb)")},
405  {"reinstall", &DoInstall, _("Reinstall packages (pkg is libc6 not libc6.deb)")},
406  {"remove", &DoInstall, _("Remove packages")},
407  {"purge", &DoInstall, _("Remove packages and config files")},
408  {"autoremove", &DoInstall, _("Remove automatically all unused packages")},
409  {"auto-remove", &DoInstall, nullptr},
410  {"autopurge",&DoInstall, nullptr},
411  {"markauto", &DoMarkAuto, nullptr},
412  {"unmarkauto", &DoMarkAuto, nullptr},
413  {"dist-upgrade", &DoDistUpgrade, _("Distribution upgrade, see apt-get(8)")},
414  {"full-upgrade", &DoDistUpgrade, nullptr},
415  {"dselect-upgrade", &DoDSelectUpgrade, _("Follow dselect selections")},
416  {"build-dep", &DoBuildDep, _("Configure build-dependencies for source packages")},
417  {"satisfy", &DoBuildDep, _("Satisfy dependency strings")},
418  {"clean", &DoClean, _("Erase downloaded archive files")},
419  {"autoclean", &DoAutoClean, _("Erase old downloaded archive files")},
420  {"auto-clean", &DoAutoClean, nullptr},
421  {"check", &DoCheck, _("Verify that there are no broken dependencies")},
422  {"source", &DoSource, _("Download source archives")},
423  {"download", &DoDownload, _("Download the binary package into the current directory")},
424  {"changelog", &DoChangelog, _("Download and display the changelog for the given package")},
425  {"indextargets", &DoIndexTargets, nullptr},
426  {"moo", &DoMoo, nullptr},
427  {nullptr, nullptr, nullptr}
428  };
429 }
430  /*}}}*/
431 int main(int argc,const char *argv[]) /*{{{*/
432 {
433  // Parse the command line and initialize the package library
434  CommandLine CmdL;
435  auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_GET, &_config, &_system, argc, argv, &ShowHelp, &GetCommands);
436 
437  InitSignals();
438  InitOutput();
439 
440  CheckIfSimulateMode(CmdL);
441 
442  return DispatchCommandLine(CmdL, Cmds);
443 }
444  /*}}}*/
static bool std::string const metaIndex const *const pkgAcqMetaClearSig *const pkgAcquire::Item *const I
static bool ShowHelp(CommandLine &)
Definition: apt-get.cc:343
static std::vector< aptDispatchWithHelp > GetCommands()
Definition: apt-get.cc:399
static std::string format_key(std::string key)
Definition: apt-get.cc:227
#define APT_RELEASE(X, Y)
static bool DoMarkAuto(CommandLine &CmdL)
Definition: apt-get.cc:96
static bool DoIndexTargets(CommandLine &CmdL)
Definition: apt-get.cc:242
int main(int argc, const char *argv[])
Definition: apt-get.cc:431
static bool DoDSelectUpgrade(CommandLine &)
Definition: apt-get.cc:141
static bool DoCheck(CommandLine &)
Definition: apt-get.cc:217
bool OpenForInstall()
bool Open(bool WithLock=true)
bool CheckDeps(bool AllowBroken=false)
const char ** FileList
Definition: cmndline.h:78
unsigned int FileSize() const APT_PURE
Definition: cmndline.cc:353
@ EXISTING_FILENAME
Definition: indexfile.h:81
pkgSourceList * GetSourceList()
Definition: cachefile.h:76
pkgCache * GetPkgCache()
Definition: cachefile.h:73
Represents an active action group.
Definition: depcache.h:166
const char * Label
Definition: indexfile.h:118
static Type ** GlobalList
Definition: indexfile.h:114
static unsigned long GlobalListLen
Definition: indexfile.h:115
bool Resolve(bool BrokenFix=false, OpProgress *const Progress=NULL)
Definition: algorithms.cc:741
void Protect(pkgCache::PkgIterator Pkg)
Definition: algorithms.h:130
static Type ** GlobalList
Definition: sourcelist.h:54
char const *const Name
Definition: sourcelist.h:58
static unsigned long GlobalListLen
Definition: sourcelist.h:55
char const *const Label
Definition: sourcelist.h:59
const_iterator begin() const
Definition: sourcelist.h:97
const_iterator end() const
Definition: sourcelist.h:98
std::vector< metaIndex * >::const_iterator const_iterator
Definition: sourcelist.h:76
static pkgSystem ** GlobalList
Definition: pkgsystem.h:57
pkgVersioningSystem *const VS
Definition: pkgsystem.h:62
const char *const Label
Definition: pkgsystem.h:61
static unsigned long GlobalListLen
Definition: pkgsystem.h:58
virtual signed Score(Configuration const &)
Definition: pkgsystem.h:89
static pkgVersioningSystem ** GlobalList
Definition: version.h:30
const char * Label
Definition: version.h:34
virtual bool TestCompatibility(pkgVersioningSystem const &Against)
Definition: version.h:46
static unsigned long GlobalListLen
Definition: version.h:31
Configuration * _config
@ FORBID_REMOVE_PACKAGES
Definition: upgrade.h:23
@ FORBID_INSTALL_NEW_PACKAGES
Definition: upgrade.h:24
APT_PUBLIC bool Upgrade(pkgDepCache &Cache, int UpgradeMode, OpProgress *const Progress=NULL)
Definition: upgrade.cc:268
pkgCache - Structure definitions for the cache file
pkgSystem * _system
Definition: pkgsystem.cc:24
std::vector< CommandLine::Dispatch > ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary, Configuration *const *const Cnf, pkgSystem **const Sys, int const argc, const char *argv[], bool(*ShowHelp)(CommandLine &), std::vector< aptDispatchWithHelp >(*GetCommands)(void))
unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector< CommandLine::Dispatch > const &Cmds)
bool DoChangelog(CommandLine &CmdL)
bool DoDownload(CommandLine &CmdL)
bool DoClean(CommandLine &)
bool DoAutoClean(CommandLine &)
bool DoInstall(CommandLine &CmdL)
bool InstallPackages(CacheFile &Cache, bool ShwKept, bool Ask, bool Safety)
void CheckIfSimulateMode(CommandLine &CmdL)
Definition: private-main.cc:54
void InitSignals()
Definition: private-main.cc:49
bool DoMoo(CommandLine &CmdL)
Definition: private-moo.cc:166
bool InitOutput(std::basic_streambuf< char > *const out)
void ShowBroken(ostream &out, CacheFile &Cache, bool const Now)
APT_PUBLIC std::ostream c1out
bool DoSource(CommandLine &CmdL)
bool DoBuildDep(CommandLine &CmdL)
bool DoUpdate(CommandLine &CmdL)
bool DoUpgrade(CommandLine &CmdL)
bool DoDistUpgrade(CommandLine &CmdL)
string SubstVar(const string &Str, const string &Subst, const string &Contents)
Definition: strutl.cc:502
void ioprintf(ostream &out, const char *format,...)
Definition: strutl.cc:1433