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-internal-planner.cc
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* #####################################################################
4 
5  cover around the internal solver to be able to run it like an external
6 
7  ##################################################################### */
8  /*}}}*/
9 // Include Files /*{{{*/
10 #include <config.h>
11 
12 #include <apt-pkg/cachefile.h>
13 #include <apt-pkg/cacheset.h>
14 #include <apt-pkg/cmndline.h>
15 #include <apt-pkg/configuration.h>
16 #include <apt-pkg/depcache.h>
17 #include <apt-pkg/edsp.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/fileutl.h>
20 #include <apt-pkg/init.h>
21 #include <apt-pkg/packagemanager.h>
22 #include <apt-pkg/pkgcache.h>
23 #include <apt-pkg/pkgsystem.h>
24 #include <apt-pkg/prettyprinters.h>
25 #include <apt-pkg/strutl.h>
26 
30 
31 #include <cstdio>
32 #include <iostream>
33 #include <list>
34 #include <sstream>
35 #include <string>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <apti18n.h>
41  /*}}}*/
42 
43 static bool ShowHelp(CommandLine &) /*{{{*/
44 {
45  std::cout <<
46  _("Usage: apt-internal-planner\n"
47  "\n"
48  "apt-internal-planner is an interface to use the current internal\n"
49  "installation planner for the APT family like an external one,\n"
50  "for debugging or the like.\n");
51  return true;
52 }
53  /*}}}*/
54 APT_NORETURN static void DIE(std::string const &message) { /*{{{*/
55  std::cerr << "ERROR: " << message << std::endl;
56  _error->DumpErrors(std::cerr);
57  exit(EXIT_FAILURE);
58 }
59  /*}}}*/
60 static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
61 {
62  return {};
63 }
64  /*}}}*/
65 class PMOutput: public pkgPackageManager /*{{{*/
66 {
68  bool const Debug;
69 
70 protected:
71  virtual bool Install(PkgIterator Pkg,std::string) APT_OVERRIDE
72  {
73  //std::cerr << "INSTALL: " << APT::PrettyPkg(&Cache, Pkg) << std::endl;
74  return EDSP::WriteSolutionStanza(output, "Unpack", Cache[Pkg].InstVerIter(Cache));
75  }
77  {
78  //std::cerr << "CONFIGURE: " << APT::PrettyPkg(&Cache, Pkg) << " " << std::endl;
79  return EDSP::WriteSolutionStanza(output, "Configure", Cache[Pkg].InstVerIter(Cache));
80  }
81  virtual bool Remove(PkgIterator Pkg,bool) APT_OVERRIDE
82  {
83  //std::cerr << "REMOVE: " << APT::PrettyPkg(&Cache, Pkg) << " " << std::endl;
84  return EDSP::WriteSolutionStanza(output, "Remove", Pkg.CurrentVer());
85  }
86 public:
88  Debug(_config->FindB("Debug::EDSP::WriteSolution", false))
89  {}
90 
91  bool ApplyRequest(std::list<std::pair<std::string,EIPP::PKG_ACTION>> const &actions)
92  {
93  for (auto && a: actions)
94  {
95  auto const Pkg = Cache.FindPkg(a.first);
96  if (unlikely(Pkg.end() == true))
97  continue;
98  switch (a.second)
99  {
101  break;
104  FileNames[Pkg->ID] = "EIPP";
105  break;
107  break;
108  }
109  }
110  return true;
111  }
112 };
113  /*}}}*/
114 int main(int argc,const char *argv[]) /*{{{*/
115 {
116  // we really don't need anything
117  DropPrivileges();
118 
119  CommandLine CmdL;
121 
122  // Deal with stdout not being a tty
123  if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
124  _config->Set("quiet","1");
125 
126  if (_config->FindI("quiet", 0) < 1)
127  _config->Set("Debug::EIPP::WriteSolution", true);
128 
129  _config->Set("APT::System", "Debian APT planner interface");
130  _config->Set("APT::Planner", "internal");
131  _config->Set("eipp::scenario", "/nonexistent/stdin");
132  FileFd output;
133  if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
134  DIE("stdout couldn't be opened");
135  int const input = STDIN_FILENO;
136  SetNonBlock(input, false);
137 
138  EDSP::WriteProgress(0, "Start up planner…", output);
139 
140  if (pkgInitSystem(*_config,_system) == false)
141  DIE("System could not be initialized!");
142 
143  EDSP::WriteProgress(1, "Read request…", output);
144 
145  if (WaitFd(input, false, 5) == false)
146  DIE("WAIT timed out in the planner");
147 
148  std::list<std::pair<std::string,EIPP::PKG_ACTION>> actions;
149  unsigned int flags;
150  if (EIPP::ReadRequest(input, actions, flags) == false)
151  DIE("Parsing the request failed!");
152  _config->Set("APT::Immediate-Configure", (flags & EIPP::Request::NO_IMMEDIATE_CONFIGURATION) == 0);
153  _config->Set("APT::Immediate-Configure-All", (flags & EIPP::Request::IMMEDIATE_CONFIGURATION_ALL) != 0);
154  _config->Set("APT::Force-LoopBreak", (flags & EIPP::Request::ALLOW_TEMPORARY_REMOVE_OF_ESSENTIALS) != 0);
155 
156  EDSP::WriteProgress(5, "Read scenario…", output);
157 
159  if (CacheFile.Open(NULL, false) == false)
160  DIE("Failed to open CacheFile!");
161 
162  EDSP::WriteProgress(50, "Apply request on scenario…", output);
163 
164  if (EIPP::ApplyRequest(actions, CacheFile) == false)
165  DIE("Failed to apply request to depcache!");
166 
167  EDSP::WriteProgress(60, "Call orderinstall on current scenario…", output);
168 
169  //_config->Set("Debug::pkgOrderList", true);
170  //_config->Set("Debug::pkgPackageManager", true);
171  PMOutput PM(CacheFile, output);
172  if (PM.ApplyRequest(actions) == false)
173  DIE("Failed to apply request to packagemanager!");
175  std::ostringstream broken;
176  switch (Res)
177  {
179  EDSP::WriteProgress(100, "Done", output);
180  break;
182  broken << "Planner could only incompletely plan an installation order!" << std::endl;
183  _error->DumpErrors(broken, GlobalError::DEBUG);
184  EDSP::WriteError("pm-incomplete", broken.str(), output);
185  break;
187  broken << "Planner failed to find an installation order!" << std::endl;
188  _error->DumpErrors(broken, GlobalError::DEBUG);
189  EDSP::WriteError("pm-failed", broken.str(), output);
190  break;
191  }
192 
193  return DispatchCommandLine(CmdL, {});
194 }
195  /*}}}*/
return false
static bool ShowHelp(CommandLine &)
static std::vector< aptDispatchWithHelp > GetCommands()
static APT_NORETURN void DIE(std::string const &message)
int main(int argc, const char *argv[])
bool Open(bool WithLock=true)
int FindI(const char *Name, int const &Default=0) const
void Set(const std::string &Name, const std::string &Value)
Definition: configuration.h:92
Definition: fileutl.h:39
bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false)
Definition: fileutl.cc:2572
@ WriteOnly
Definition: fileutl.h:60
@ BufferedWrite
Definition: fileutl.h:67
@ DEBUG
for developers only in areas it is hard to print something directly
Definition: error.h:66
bool const Debug
bool ApplyRequest(std::list< std::pair< std::string, EIPP::PKG_ACTION >> const &actions)
virtual bool Remove(PkgIterator Pkg, bool) APT_OVERRIDE
virtual bool Install(PkgIterator Pkg, std::string) APT_OVERRIDE
virtual bool Configure(PkgIterator Pkg) APT_OVERRIDE
PMOutput(pkgDepCache *Cache, FileFd &file)
pkgCache::PkgIterator PkgIterator
Definition: pkgcache.h:829
PkgIterator FindPkg(APT::StringView Name)
Definition: depcache.h:358
std::string * FileNames
pkgDepCache & Cache
OrderResult DoInstallPreFork()
Configuration * _config
void SetNonBlock(int Fd, bool Block)
Definition: fileutl.cc:804
bool WaitFd(int Fd, bool write, unsigned long timeout)
Definition: fileutl.cc:819
bool DropPrivileges()
Drop privileges.
Definition: fileutl.cc:3260
bool pkgInitSystem(Configuration &Cnf, pkgSystem *&Sys)
Definition: init.cc:257
#define APT_OVERRIDE
Definition: macros.h:111
#define APT_NORETURN
Definition: macros.h:57
APT_PUBLIC bool WriteSolutionStanza(FileFd &output, char const *const Type, pkgCache::VerIterator const &Ver)
formats a solution stanza for the given version
Definition: edsp.cc:620
APT_PUBLIC bool WriteProgress(unsigned short const percent, const char *const message, FileFd &output)
sends a progress report
Definition: edsp.cc:630
APT_PUBLIC bool WriteError(char const *const uuid, std::string const &message, FileFd &output)
sends an error report
Definition: edsp.cc:641
@ NO_IMMEDIATE_CONFIGURATION
Definition: edsp.h:223
@ ALLOW_TEMPORARY_REMOVE_OF_ESSENTIALS
Definition: edsp.h:224
@ IMMEDIATE_CONFIGURATION_ALL
Definition: edsp.h:222
APT_PUBLIC bool ReadRequest(int const input, std::list< std::pair< std::string, PKG_ACTION >> &actions, unsigned int &flags)
APT_PUBLIC bool ApplyRequest(std::list< std::pair< std::string, PKG_ACTION >> &actions, pkgDepCache &Cache)
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)
@ APT_INTERNAL_PLANNER