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 static 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 static std::vector
<aptDispatchWithHelp
> GetCommands() /*{{{*/
66 static bool WriteSolution(pkgDepCache
&Cache
, FileFd
&output
) /*{{{*/
68 bool Okay
= output
.Failed() == false;
69 for (pkgCache::PkgIterator Pkg
= Cache
.PkgBegin(); Pkg
.end() == false && likely(Okay
); ++Pkg
)
72 if (Cache
[Pkg
].Delete() == true)
73 Okay
&= EDSP::WriteSolutionStanza(output
, "Remove", Pkg
.CurrentVer());
74 else if (Cache
[Pkg
].NewInstall() == true || Cache
[Pkg
].Upgrade() == true)
75 Okay
&= EDSP::WriteSolutionStanza(output
, "Install", Cache
.GetCandidateVersion(Pkg
));
76 else if (Cache
[Pkg
].Garbage
== true)
77 Okay
&= EDSP::WriteSolutionStanza(output
, "Autoremove", Pkg
.CurrentVer());
82 int main(int argc
,const char *argv
[]) /*{{{*/
84 // we really don't need anything
88 ParseCommandLine(CmdL
, APT_CMD::APT_INTERNAL_SOLVER
, &_config
, NULL
, argc
, argv
, &ShowHelp
, &GetCommands
);
90 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
92 if (pkgInitSystem(*_config
,_system
) == false) {
93 std::cerr
<< "System could not be initialized!" << std::endl
;
96 pkgCacheFile CacheFile
;
97 CacheFile
.Open(NULL
, false);
98 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
100 if (output
.OpenDescriptor(STDOUT_FILENO
, FileFd::WriteOnly
| FileFd::BufferedWrite
, true) == false)
102 if (pkgset
.empty() == true)
103 EDSP::WriteScenario(CacheFile
, output
);
106 std::vector
<bool> pkgvec(CacheFile
->Head().PackageCount
, false);
107 for (auto const &p
: pkgset
)
108 pkgvec
[p
->ID
] = true;
109 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgvec
);
112 _error
->DumpErrors(std::cerr
);
116 // Deal with stdout not being a tty
117 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
118 _config
->Set("quiet","1");
120 if (_config
->FindI("quiet", 0) < 1)
121 _config
->Set("Debug::EDSP::WriteSolution", true);
123 _config
->Set("APT::System", "Debian APT solver interface");
124 _config
->Set("APT::Solver", "internal");
125 _config
->Set("edsp::scenario", "/nonexistent/stdin");
127 if (output
.OpenDescriptor(STDOUT_FILENO
, FileFd::WriteOnly
| FileFd::BufferedWrite
, true) == false)
128 DIE("stdout couldn't be opened");
129 int const input
= STDIN_FILENO
;
130 SetNonBlock(input
, false);
132 EDSP::WriteProgress(0, "Start up solver…", output
);
134 if (pkgInitSystem(*_config
,_system
) == false)
135 DIE("System could not be initialized!");
137 EDSP::WriteProgress(1, "Read request…", output
);
139 if (WaitFd(input
, false, 5) == false)
140 DIE("WAIT timed out in the resolver");
142 std::list
<std::string
> install
, remove
;
144 if (EDSP::ReadRequest(input
, install
, remove
, flags
) == false)
145 DIE("Parsing the request failed!");
147 EDSP::WriteProgress(5, "Read scenario…", output
);
149 pkgCacheFile CacheFile
;
150 if (CacheFile
.Open(NULL
, false) == false)
151 DIE("Failed to open CacheFile!");
153 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
155 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false)
156 DIE("Failed to apply request to depcache!");
158 pkgProblemResolver
Fix(CacheFile
);
159 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
160 i
!= remove
.end(); ++i
) {
161 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
167 for (std::list
<std::string
>::const_iterator i
= install
.begin();
168 i
!= install
.end(); ++i
) {
169 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
174 for (std::list
<std::string
>::const_iterator i
= install
.begin();
175 i
!= install
.end(); ++i
)
176 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
178 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
181 if (flags
& EDSP::Request::UPGRADE_ALL
) {
182 int upgrade_flags
= APT::Upgrade::ALLOW_EVERYTHING
;
183 if (flags
& EDSP::Request::FORBID_NEW_INSTALL
)
184 upgrade_flags
|= APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES
;
185 if (flags
& EDSP::Request::FORBID_REMOVE
)
186 upgrade_flags
|= APT::Upgrade::FORBID_REMOVE_PACKAGES
;
188 if (APT::Upgrade::Upgrade(CacheFile
, upgrade_flags
))
190 else if (upgrade_flags
== APT::Upgrade::ALLOW_EVERYTHING
)
191 failure
= "ERR_UNSOLVABLE_FULL_UPGRADE";
193 failure
= "ERR_UNSOLVABLE_UPGRADE";
194 } else if (Fix
.Resolve() == false)
195 failure
= "ERR_UNSOLVABLE";
197 if (failure
.empty() == false) {
198 std::ostringstream broken
;
199 ShowBroken(broken
, CacheFile
, false);
200 EDSP::WriteError(failure
.c_str(), broken
.str(), output
);
204 EDSP::WriteProgress(95, "Write solution…", output
);
206 if (WriteSolution(CacheFile
, output
) == false)
207 DIE("Failed to output the solution!");
209 EDSP::WriteProgress(100, "Done", output
);
211 return DispatchCommandLine(CmdL
, {});