1 // -*- mode: cpp; mode: fold -*-
3 /* #####################################################################
5 cover around the internal solver to be able to run it like an external
7 ##################################################################### */
9 // Include Files /*{{{*/
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/cmndline.h>
14 #include <apt-pkg/init.h>
15 #include <apt-pkg/cachefile.h>
16 #include <apt-pkg/cacheset.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/edsp.h>
19 #include <apt-pkg/algorithms.h>
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/pkgsystem.h>
22 #include <apt-pkg/upgrade.h>
23 #include <apt-pkg/configuration.h>
24 #include <apt-pkg/depcache.h>
25 #include <apt-pkg/pkgcache.h>
26 #include <apt-pkg/cacheiterators.h>
28 #include <apt-private/private-output.h>
29 #include <apt-private/private-cmndline.h>
30 #include <apt-private/private-main.h>
44 bool ShowHelp(CommandLine
&, aptDispatchWithHelp
const *) /*{{{*/
47 _("Usage: apt-internal-solver\n"
49 "apt-internal-solver is an interface to use the current internal\n"
50 "like an external resolver for the APT family for debugging or alike\n"
53 " -h This help text.\n"
54 " -q Loggable output - no progress indicator\n"
55 " -c=? Read this configuration file\n"
56 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
60 APT_NORETURN
static void DIE(std::string
const &message
) { /*{{{*/
61 std::cerr
<< "ERROR: " << message
<< std::endl
;
62 _error
->DumpErrors(std::cerr
);
66 std::vector
<aptDispatchWithHelp
> GetCommands() /*{{{*/
71 int main(int argc
,const char *argv
[]) /*{{{*/
75 // we really don't need anything
79 ParseCommandLine(CmdL
, APT_CMD::APT_INTERNAL_SOLVER
, &_config
, NULL
, argc
, argv
);
81 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
83 if (pkgInitSystem(*_config
,_system
) == false) {
84 std::cerr
<< "System could not be initialized!" << std::endl
;
87 pkgCacheFile CacheFile
;
88 CacheFile
.Open(NULL
, false);
89 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
90 FILE* output
= stdout
;
91 if (pkgset
.empty() == true)
92 EDSP::WriteScenario(CacheFile
, output
);
94 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgset
);
96 _error
->DumpErrors(std::cerr
);
100 // Deal with stdout not being a tty
101 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
102 _config
->Set("quiet","1");
104 if (_config
->FindI("quiet", 0) < 1)
105 _config
->Set("Debug::EDSP::WriteSolution", true);
107 _config
->Set("APT::System", "Debian APT solver interface");
108 _config
->Set("APT::Solver", "internal");
109 _config
->Set("edsp::scenario", "/nonexistent/stdin");
110 int input
= STDIN_FILENO
;
111 FILE* output
= stdout
;
112 SetNonBlock(input
, false);
114 EDSP::WriteProgress(0, "Start up solver…", output
);
116 if (pkgInitSystem(*_config
,_system
) == false)
117 DIE("System could not be initialized!");
119 EDSP::WriteProgress(1, "Read request…", output
);
121 if (WaitFd(input
, false, 5) == false)
122 DIE("WAIT timed out in the resolver");
124 std::list
<std::string
> install
, remove
;
125 bool upgrade
, distUpgrade
, autoRemove
;
126 if (EDSP::ReadRequest(input
, install
, remove
, upgrade
, distUpgrade
, autoRemove
) == false)
127 DIE("Parsing the request failed!");
129 EDSP::WriteProgress(5, "Read scenario…", output
);
131 pkgCacheFile CacheFile
;
132 if (CacheFile
.Open(NULL
, false) == false)
133 DIE("Failed to open CacheFile!");
135 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
137 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false)
138 DIE("Failed to apply request to depcache!");
140 pkgProblemResolver
Fix(CacheFile
);
141 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
142 i
!= remove
.end(); ++i
) {
143 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
149 for (std::list
<std::string
>::const_iterator i
= install
.begin();
150 i
!= install
.end(); ++i
) {
151 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
156 for (std::list
<std::string
>::const_iterator i
= install
.begin();
157 i
!= install
.end(); ++i
)
158 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
160 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
163 if (upgrade
== true) {
164 if (APT::Upgrade::Upgrade(CacheFile
, APT::Upgrade::FORBID_REMOVE_PACKAGES
| APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES
) == false)
165 failure
= "ERR_UNSOLVABLE_UPGRADE";
166 } else if (distUpgrade
== true) {
167 if (APT::Upgrade::Upgrade(CacheFile
, APT::Upgrade::ALLOW_EVERYTHING
) == false)
168 failure
= "ERR_UNSOLVABLE_DIST_UPGRADE";
169 } else if (Fix
.Resolve() == false)
170 failure
= "ERR_UNSOLVABLE";
172 if (failure
.empty() == false) {
173 std::ostringstream broken
;
174 ShowBroken(broken
, CacheFile
, false);
175 EDSP::WriteError(failure
.c_str(), broken
.str(), output
);
179 EDSP::WriteProgress(95, "Write solution…", output
);
181 if (EDSP::WriteSolution(CacheFile
, output
) == false)
182 DIE("Failed to output the solution!");
184 EDSP::WriteProgress(100, "Done", output
);
186 return DispatchCommandLine(CmdL
, {});