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
&) /*{{{*/
47 _("Usage: apt-internal-solver\n"
49 "apt-internal-solver is an interface to use the current internal\n"
50 "resolver for the APT family like an external one, for debugging or\n"
55 APT_NORETURN
static void DIE(std::string
const &message
) { /*{{{*/
56 std::cerr
<< "ERROR: " << message
<< std::endl
;
57 _error
->DumpErrors(std::cerr
);
61 std::vector
<aptDispatchWithHelp
> GetCommands() /*{{{*/
66 int main(int argc
,const char *argv
[]) /*{{{*/
70 // we really don't need anything
74 ParseCommandLine(CmdL
, APT_CMD::APT_INTERNAL_SOLVER
, &_config
, NULL
, argc
, argv
);
76 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
78 if (pkgInitSystem(*_config
,_system
) == false) {
79 std::cerr
<< "System could not be initialized!" << std::endl
;
82 pkgCacheFile CacheFile
;
83 CacheFile
.Open(NULL
, false);
84 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
85 FILE* output
= stdout
;
86 if (pkgset
.empty() == true)
87 EDSP::WriteScenario(CacheFile
, output
);
89 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgset
);
91 _error
->DumpErrors(std::cerr
);
95 // Deal with stdout not being a tty
96 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
97 _config
->Set("quiet","1");
99 if (_config
->FindI("quiet", 0) < 1)
100 _config
->Set("Debug::EDSP::WriteSolution", true);
102 _config
->Set("APT::System", "Debian APT solver interface");
103 _config
->Set("APT::Solver", "internal");
104 _config
->Set("edsp::scenario", "/nonexistent/stdin");
105 int input
= STDIN_FILENO
;
106 FILE* output
= stdout
;
107 SetNonBlock(input
, false);
109 EDSP::WriteProgress(0, "Start up solver…", output
);
111 if (pkgInitSystem(*_config
,_system
) == false)
112 DIE("System could not be initialized!");
114 EDSP::WriteProgress(1, "Read request…", output
);
116 if (WaitFd(input
, false, 5) == false)
117 DIE("WAIT timed out in the resolver");
119 std::list
<std::string
> install
, remove
;
120 bool upgrade
, distUpgrade
, autoRemove
;
121 if (EDSP::ReadRequest(input
, install
, remove
, upgrade
, distUpgrade
, autoRemove
) == false)
122 DIE("Parsing the request failed!");
124 EDSP::WriteProgress(5, "Read scenario…", output
);
126 pkgCacheFile CacheFile
;
127 if (CacheFile
.Open(NULL
, false) == false)
128 DIE("Failed to open CacheFile!");
130 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
132 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false)
133 DIE("Failed to apply request to depcache!");
135 pkgProblemResolver
Fix(CacheFile
);
136 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
137 i
!= remove
.end(); ++i
) {
138 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
144 for (std::list
<std::string
>::const_iterator i
= install
.begin();
145 i
!= install
.end(); ++i
) {
146 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
151 for (std::list
<std::string
>::const_iterator i
= install
.begin();
152 i
!= install
.end(); ++i
)
153 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
155 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
158 if (upgrade
== true) {
159 if (APT::Upgrade::Upgrade(CacheFile
, APT::Upgrade::FORBID_REMOVE_PACKAGES
| APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES
) == false)
160 failure
= "ERR_UNSOLVABLE_UPGRADE";
161 } else if (distUpgrade
== true) {
162 if (APT::Upgrade::Upgrade(CacheFile
, APT::Upgrade::ALLOW_EVERYTHING
) == false)
163 failure
= "ERR_UNSOLVABLE_DIST_UPGRADE";
164 } else if (Fix
.Resolve() == false)
165 failure
= "ERR_UNSOLVABLE";
167 if (failure
.empty() == false) {
168 std::ostringstream broken
;
169 ShowBroken(broken
, CacheFile
, false);
170 EDSP::WriteError(failure
.c_str(), broken
.str(), output
);
174 EDSP::WriteProgress(95, "Write solution…", output
);
176 if (EDSP::WriteSolution(CacheFile
, output
) == false)
177 DIE("Failed to output the solution!");
179 EDSP::WriteProgress(100, "Done", output
);
181 return DispatchCommandLine(CmdL
, {});