]>
git.saurik.com Git - apt.git/blob - cmdline/apt-internal-solver.cc
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>
39 // ShowHelp - Show a help screen /*{{{*/
40 // ---------------------------------------------------------------------
42 static bool ShowHelp(CommandLine
&) {
43 ioprintf(std::cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
44 COMMON_ARCH
,__DATE__
,__TIME__
);
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 int main(int argc
,const char *argv
[]) /*{{{*/
68 CommandLine::Args Args
[] = {
69 {'h',"help","help",0},
70 {'v',"version","version",0},
71 {'q',"quiet","quiet",CommandLine::IntLevel
},
72 {'q',"silent","quiet",CommandLine::IntLevel
},
73 {'c',"config-file",0,CommandLine::ConfigFile
},
74 {'o',"option",0,CommandLine::ArbItem
},
77 CommandLine
CmdL(Args
,_config
);
78 if (pkgInitConfig(*_config
) == false ||
79 CmdL
.Parse(argc
,argv
) == false) {
84 // See if the help should be shown
85 if (_config
->FindB("help") == true ||
86 _config
->FindB("version") == true) {
91 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
93 if (pkgInitSystem(*_config
,_system
) == false) {
94 std::cerr
<< "System could not be initialized!" << std::endl
;
97 pkgCacheFile CacheFile
;
98 CacheFile
.Open(NULL
, false);
99 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
100 FILE* output
= stdout
;
101 if (pkgset
.empty() == true)
102 EDSP::WriteScenario(CacheFile
, output
);
104 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgset
);
106 _error
->DumpErrors(std::cerr
);
110 // Deal with stdout not being a tty
111 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
112 _config
->Set("quiet","1");
114 if (_config
->FindI("quiet", 0) < 1)
115 _config
->Set("Debug::EDSP::WriteSolution", true);
117 _config
->Set("APT::Solver", "internal");
118 _config
->Set("edsp::scenario", "stdin");
119 int input
= STDIN_FILENO
;
120 FILE* output
= stdout
;
121 SetNonBlock(input
, false);
123 EDSP::WriteProgress(0, "Start up solver…", output
);
125 if (pkgInitSystem(*_config
,_system
) == false)
126 DIE("System could not be initialized!");
128 EDSP::WriteProgress(1, "Read request…", output
);
130 if (WaitFd(input
, false, 5) == false)
131 DIE("WAIT timed out in the resolver");
133 std::list
<std::string
> install
, remove
;
134 bool upgrade
, distUpgrade
, autoRemove
;
135 if (EDSP::ReadRequest(input
, install
, remove
, upgrade
, distUpgrade
, autoRemove
) == false)
136 DIE("Parsing the request failed!");
138 EDSP::WriteProgress(5, "Read scenario…", output
);
140 pkgCacheFile CacheFile
;
141 if (CacheFile
.Open(NULL
, false) == false)
142 DIE("Failed to open CacheFile!");
144 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
146 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false)
147 DIE("Failed to apply request to depcache!");
149 pkgProblemResolver
Fix(CacheFile
);
150 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
151 i
!= remove
.end(); ++i
) {
152 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
158 for (std::list
<std::string
>::const_iterator i
= install
.begin();
159 i
!= install
.end(); ++i
) {
160 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
165 for (std::list
<std::string
>::const_iterator i
= install
.begin();
166 i
!= install
.end(); ++i
)
167 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
169 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
171 if (upgrade
== true) {
172 if (pkgAllUpgrade(CacheFile
) == false) {
173 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occurred", output
);
176 } else if (distUpgrade
== true) {
177 if (pkgDistUpgrade(CacheFile
) == false) {
178 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occurred", output
);
181 } else if (Fix
.Resolve() == false) {
182 EDSP::WriteError("ERR_UNSOLVABLE", "An error occurred", output
);
186 EDSP::WriteProgress(95, "Write solution…", output
);
188 if (EDSP::WriteSolution(CacheFile
, output
) == false)
189 DIE("Failed to output the solution!");
191 EDSP::WriteProgress(100, "Done", output
);
193 bool const Errors
= _error
->PendingError();
194 if (_config
->FindI("quiet",0) > 0)
195 _error
->DumpErrors(std::cerr
);
197 _error
->DumpErrors(std::cerr
, GlobalError::DEBUG
);
198 return Errors
== true ? 100 : 0;