]>
git.saurik.com Git - apt.git/blob - apt-pkg/edsp.cc
ae5c7a3733d88a6ffbf6a1dca6f51026b3372ae5
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
4 Set of methods to help writing and reading everything needed for EDSP
5 ##################################################################### */
7 // Include Files /*{{{*/
10 #include <apt-pkg/error.h>
11 #include <apt-pkg/cacheset.h>
12 #include <apt-pkg/depcache.h>
13 #include <apt-pkg/pkgcache.h>
14 #include <apt-pkg/cacheiterators.h>
15 #include <apt-pkg/prettyprinters.h>
16 #include <apt-pkg/packagemanager.h>
17 #include <apt-pkg/progress.h>
18 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/edsp.h>
20 #include <apt-pkg/tagfile.h>
21 #include <apt-pkg/strutl.h>
22 #include <apt-pkg/string_view.h>
23 #include <apt-pkg/pkgsystem.h>
41 // we could use pkgCache::DepType and ::Priority, but these would be localized strings…
42 constexpr char const * const PrioMap
[] = {
43 nullptr , "important" , "required" , "standard" ,
46 constexpr char const * const DepMap
[] = {
47 nullptr , "Depends" , "Pre-Depends" , "Suggests" ,
48 "Recommends" , "Conflicts" , "Replaces" ,
49 "Obsoletes" , "Breaks" , "Enhances"
52 // WriteOkay - varaidic helper to easily Write to a FileFd /*{{{*/
53 static bool WriteOkay_fn ( FileFd
&) { return true ; }
54 template < typename
... Tail
> static bool WriteOkay_fn ( FileFd
& output
, APT :: StringView data
, Tail
... more_data
)
56 return likely ( output
. Write ( data
. data (), data
. length ()) && WriteOkay_fn ( output
, more_data
...));
58 template < typename
... Tail
> static bool WriteOkay_fn ( FileFd
& output
, unsigned int data
, Tail
... more_data
)
61 strprintf ( number
, " %d " , data
);
62 return likely ( output
. Write ( number
. data (), number
. length ()) && WriteOkay_fn ( output
, more_data
...));
64 template < typename
... Data
> static bool WriteOkay ( bool & Okay
, FileFd
& output
, Data
&&... data
)
66 Okay
= likely ( Okay
&& WriteOkay_fn ( output
, std :: forward
< Data
>( data
)...));
69 template < typename
... Data
> static bool WriteOkay ( FileFd
& output
, Data
&&... data
)
71 bool Okay
= likely ( output
. Failed () == false );
72 return WriteOkay ( Okay
, output
, std :: forward
< Data
>( data
)...);
75 // WriteScenarioVersion /*{{{*/
76 static void WriteScenarioVersion ( pkgDepCache
& Cache
, FILE * output
, pkgCache :: PkgIterator
const & Pkg
,
77 pkgCache :: VerIterator
const & Ver
)
79 fprintf ( output
, "Package: %s \n " , Pkg
. Name ());
80 fprintf ( output
, "Source: %s \n " , Ver
. SourcePkgName ());
81 fprintf ( output
, "Architecture: %s \n " , Ver
. Arch ());
82 fprintf ( output
, "Version: %s \n " , Ver
. VerStr ());
83 fprintf ( output
, "Source-Version: %s \n " , Ver
. SourceVerStr ());
84 if ( Pkg
. CurrentVer () == Ver
)
85 fprintf ( output
, "Installed: yes \n " );
86 if ( Pkg
-> SelectedState
== pkgCache :: State :: Hold
||
87 ( Cache
[ Pkg
]. Keep () == true && Cache
[ Pkg
]. Protect () == true ))
88 fprintf ( output
, "Hold: yes \n " );
89 fprintf ( output
, "APT-ID: %d \n " , Ver
-> ID
);
90 if ( PrioMap
[ Ver
-> Priority
] != nullptr )
91 fprintf ( output
, "Priority: %s \n " , PrioMap
[ Ver
-> Priority
]);
92 if (( Pkg
-> Flags
& pkgCache :: Flag :: Essential
) == pkgCache :: Flag :: Essential
)
93 fprintf ( output
, "Essential: yes \n " );
94 if ( Ver
-> Section
!= 0 )
95 fprintf ( output
, "Section: %s \n " , Ver
. Section ());
96 if (( Ver
-> MultiArch
& pkgCache :: Version :: Allowed
) == pkgCache :: Version :: Allowed
)
97 fprintf ( output
, "Multi-Arch: allowed \n " );
98 else if (( Ver
-> MultiArch
& pkgCache :: Version :: Foreign
) == pkgCache :: Version :: Foreign
)
99 fprintf ( output
, "Multi-Arch: foreign \n " );
100 else if (( Ver
-> MultiArch
& pkgCache :: Version :: Same
) == pkgCache :: Version :: Same
)
101 fprintf ( output
, "Multi-Arch: same \n " );
102 std :: set
< string
> Releases
;
103 for ( pkgCache :: VerFileIterator I
= Ver
. FileList (); I
. end () == false ; ++ I
) {
104 pkgCache :: PkgFileIterator File
= I
. File ();
105 if ( File
. Flagged ( pkgCache :: Flag :: NotSource
) == false ) {
106 string Release
= File
. RelStr ();
107 if (! Release
. empty ())
108 Releases
. insert ( Release
);
111 if (! Releases
. empty ()) {
112 fprintf ( output
, "APT-Release: \n " );
113 for ( std :: set
< string
>:: iterator R
= Releases
. begin (); R
!= Releases
. end (); ++ R
)
114 fprintf ( output
, " %s \n " , R
-> c_str ());
116 fprintf ( output
, "APT-Pin: %d \n " , Cache
. GetPolicy (). GetPriority ( Ver
));
117 if ( Cache
. GetCandidateVersion ( Pkg
) == Ver
)
118 fprintf ( output
, "APT-Candidate: yes \n " );
119 if (( Cache
[ Pkg
]. Flags
& pkgCache :: Flag :: Auto
) == pkgCache :: Flag :: Auto
)
120 fprintf ( output
, "APT-Automatic: yes \n " );
122 static bool WriteScenarioVersion ( FileFd
& output
, pkgCache :: PkgIterator
const & Pkg
,
123 pkgCache :: VerIterator
const & Ver
)
125 bool Okay
= WriteOkay ( output
, "Package: " , Pkg
. Name (),
126 " \n Architecture: " , Ver
. Arch (),
127 " \n Version: " , Ver
. VerStr ());
128 WriteOkay ( Okay
, output
, " \n APT-ID: " , Ver
-> ID
);
129 if (( Pkg
-> Flags
& pkgCache :: Flag :: Essential
) == pkgCache :: Flag :: Essential
)
130 WriteOkay ( Okay
, output
, " \n Essential: yes" );
131 if (( Ver
-> MultiArch
& pkgCache :: Version :: Allowed
) == pkgCache :: Version :: Allowed
)
132 WriteOkay ( Okay
, output
, " \n Multi-Arch: allowed" );
133 else if (( Ver
-> MultiArch
& pkgCache :: Version :: Foreign
) == pkgCache :: Version :: Foreign
)
134 WriteOkay ( Okay
, output
, " \n Multi-Arch: foreign" );
135 else if (( Ver
-> MultiArch
& pkgCache :: Version :: Same
) == pkgCache :: Version :: Same
)
136 WriteOkay ( Okay
, output
, " \n Multi-Arch: same" );
140 // WriteScenarioDependency /*{{{*/
141 static void WriteScenarioDependency ( FILE * output
, pkgCache :: VerIterator
const & Ver
)
143 std :: array
< std :: string
, _count ( DepMap
)> dependencies
;
144 bool orGroup
= false ;
145 for ( pkgCache :: DepIterator Dep
= Ver
. DependsList (); Dep
. end () == false ; ++ Dep
)
147 if ( Dep
. IsImplicit () == true )
149 if ( orGroup
== false )
150 dependencies
[ Dep
-> Type
]. append ( ", " );
151 dependencies
[ Dep
-> Type
]. append ( Dep
. TargetPkg (). Name ());
152 if ( Dep
-> Version
!= 0 )
153 dependencies
[ Dep
-> Type
]. append ( " (" ). append ( pkgCache :: CompTypeDeb ( Dep
-> CompareOp
)). append ( " " ). append ( Dep
. TargetVer ()). append ( ")" );
154 if (( Dep
-> CompareOp
& pkgCache :: Dep :: Or
) == pkgCache :: Dep :: Or
)
156 dependencies
[ Dep
-> Type
]. append ( " | " );
162 for ( size_t i
= 1 ; i
< dependencies
. size (); ++ i
)
163 if ( dependencies
[ i
]. empty () == false )
164 fprintf ( output
, " %s : %s \n " , DepMap
[ i
], dependencies
[ i
]. c_str ()+ 2 );
166 for ( pkgCache :: PrvIterator Prv
= Ver
. ProvidesList (); Prv
. end () == false ; ++ Prv
)
168 if ( Prv
. IsMultiArchImplicit () == true )
170 if ( provides
. empty () == false )
171 provides
. append ( ", " );
172 provides
. append ( Prv
. Name ());
173 if ( Prv
-> ProvideVersion
!= 0 )
174 provides
. append ( " (= " ). append ( Prv
. ProvideVersion ()). append ( ")" );
176 if ( provides
. empty () == false )
177 fprintf ( output
, "Provides: %s \n " , provides
. c_str ());
179 static bool WriteScenarioDependency ( FileFd
& output
, pkgCache :: VerIterator
const & Ver
, bool const OnlyCritical
)
181 std :: array
< std :: string
, _count ( DepMap
)> dependencies
;
182 bool orGroup
= false ;
183 for ( pkgCache :: DepIterator Dep
= Ver
. DependsList (); Dep
. end () == false ; ++ Dep
)
185 if ( Dep
. IsImplicit () == true )
187 if ( OnlyCritical
&& Dep
. IsCritical () == false )
189 if ( orGroup
== false && dependencies
[ Dep
-> Type
]. empty () == false )
190 dependencies
[ Dep
-> Type
]. append ( ", " );
191 dependencies
[ Dep
-> Type
]. append ( Dep
. TargetPkg (). Name ());
192 if ( Dep
-> Version
!= 0 )
193 dependencies
[ Dep
-> Type
]. append ( " (" ). append ( pkgCache :: CompTypeDeb ( Dep
-> CompareOp
)). append ( " " ). append ( Dep
. TargetVer ()). append ( ")" );
194 if (( Dep
-> CompareOp
& pkgCache :: Dep :: Or
) == pkgCache :: Dep :: Or
)
196 dependencies
[ Dep
-> Type
]. append ( " | " );
202 bool Okay
= output
. Failed () == false ;
203 for ( size_t i
= 1 ; i
< dependencies
. size (); ++ i
)
204 if ( dependencies
[ i
]. empty () == false )
205 WriteOkay ( Okay
, output
, " \n " , DepMap
[ i
], ": " , dependencies
[ i
]);
207 for ( pkgCache :: PrvIterator Prv
= Ver
. ProvidesList (); Prv
. end () == false ; ++ Prv
)
209 if ( Prv
. IsMultiArchImplicit () == true )
211 if ( provides
. empty () == false )
212 provides
. append ( ", " );
213 provides
. append ( Prv
. Name ());
214 if ( Prv
-> ProvideVersion
!= 0 )
215 provides
. append ( " (= " ). append ( Prv
. ProvideVersion ()). append ( ")" );
217 if ( provides
. empty () == false )
218 WriteOkay ( Okay
, output
, " \n Provides: " , provides
);
219 return WriteOkay ( Okay
, output
, " \n " );
222 // WriteScenarioLimitedDependency /*{{{*/
223 static void WriteScenarioLimitedDependency ( FILE * output
,
224 pkgCache :: VerIterator
const & Ver
,
225 APT :: PackageSet
const & pkgset
)
227 std :: array
< std :: string
, _count ( DepMap
)> dependencies
;
228 bool orGroup
= false ;
229 for ( pkgCache :: DepIterator Dep
= Ver
. DependsList (); Dep
. end () == false ; ++ Dep
)
231 if ( Dep
. IsImplicit () == true )
233 if ( orGroup
== false )
235 if ( pkgset
. find ( Dep
. TargetPkg ()) == pkgset
. end ())
237 if ( dependencies
[ Dep
-> Type
]. empty () == false )
238 dependencies
[ Dep
-> Type
]. append ( ", " );
240 else if ( pkgset
. find ( Dep
. TargetPkg ()) == pkgset
. end ())
242 if (( Dep
-> CompareOp
& pkgCache :: Dep :: Or
) == pkgCache :: Dep :: Or
)
244 dependencies
[ Dep
-> Type
]. erase ( dependencies
[ Dep
-> Type
]. end ()- 3 , dependencies
[ Dep
-> Type
]. end ());
248 dependencies
[ Dep
-> Type
]. append ( Dep
. TargetPkg (). Name ());
249 if ( Dep
-> Version
!= 0 )
250 dependencies
[ Dep
-> Type
]. append ( " (" ). append ( pkgCache :: CompTypeDeb ( Dep
-> CompareOp
)). append ( " " ). append ( Dep
. TargetVer ()). append ( ")" );
251 if (( Dep
-> CompareOp
& pkgCache :: Dep :: Or
) == pkgCache :: Dep :: Or
)
253 dependencies
[ Dep
-> Type
]. append ( " | " );
259 for ( size_t i
= 1 ; i
< dependencies
. size (); ++ i
)
260 if ( dependencies
[ i
]. empty () == false )
261 fprintf ( output
, " %s : %s \n " , DepMap
[ i
], dependencies
[ i
]. c_str ());
263 for ( pkgCache :: PrvIterator Prv
= Ver
. ProvidesList (); Prv
. end () == false ; ++ Prv
)
265 if ( Prv
. IsMultiArchImplicit () == true )
267 if ( pkgset
. find ( Prv
. ParentPkg ()) == pkgset
. end ())
269 if ( provides
. empty () == false )
270 provides
. append ( ", " );
271 provides
. append ( Prv
. Name ());
272 if ( Prv
-> ProvideVersion
!= 0 )
273 provides
. append ( " (= " ). append ( Prv
. ProvideVersion ()). append ( ")" );
275 if ( provides
. empty () == false )
276 fprintf ( output
, "Provides: %s \n " , provides
. c_str ());
278 static bool WriteScenarioLimitedDependency ( FileFd
& output
,
279 pkgCache :: VerIterator
const & Ver
,
280 std :: vector
< bool > const & pkgset
,
281 bool const OnlyCritical
)
283 std :: array
< std :: string
, _count ( DepMap
)> dependencies
;
284 bool orGroup
= false ;
285 for ( pkgCache :: DepIterator Dep
= Ver
. DependsList (); Dep
. end () == false ; ++ Dep
)
287 if ( Dep
. IsImplicit () == true )
289 if ( OnlyCritical
&& Dep
. IsCritical () == false )
291 if ( orGroup
== false )
293 if ( pkgset
[ Dep
. TargetPkg ()-> ID
] == false )
295 if ( dependencies
[ Dep
-> Type
]. empty () == false )
296 dependencies
[ Dep
-> Type
]. append ( ", " );
298 else if ( pkgset
[ Dep
. TargetPkg ()-> ID
] == false )
300 if (( Dep
-> CompareOp
& pkgCache :: Dep :: Or
) == pkgCache :: Dep :: Or
)
302 dependencies
[ Dep
-> Type
]. erase ( dependencies
[ Dep
-> Type
]. end ()- 3 , dependencies
[ Dep
-> Type
]. end ());
306 dependencies
[ Dep
-> Type
]. append ( Dep
. TargetPkg (). Name ());
307 if ( Dep
-> Version
!= 0 )
308 dependencies
[ Dep
-> Type
]. append ( " (" ). append ( pkgCache :: CompTypeDeb ( Dep
-> CompareOp
)). append ( " " ). append ( Dep
. TargetVer ()). append ( ")" );
309 if (( Dep
-> CompareOp
& pkgCache :: Dep :: Or
) == pkgCache :: Dep :: Or
)
311 dependencies
[ Dep
-> Type
]. append ( " | " );
317 bool Okay
= output
. Failed () == false ;
318 for ( size_t i
= 1 ; i
< dependencies
. size (); ++ i
)
319 if ( dependencies
[ i
]. empty () == false )
320 WriteOkay ( Okay
, output
, " \n " , DepMap
[ i
], ": " , dependencies
[ i
]);
322 for ( pkgCache :: PrvIterator Prv
= Ver
. ProvidesList (); Prv
. end () == false ; ++ Prv
)
324 if ( Prv
. IsMultiArchImplicit () == true )
326 if ( pkgset
[ Prv
. ParentPkg ()-> ID
] == false )
328 if ( provides
. empty () == false )
329 provides
. append ( ", " );
330 provides
. append ( Prv
. Name ());
331 if ( Prv
-> ProvideVersion
!= 0 )
332 provides
. append ( " (= " ). append ( Prv
. ProvideVersion ()). append ( ")" );
334 if ( provides
. empty () == false )
335 WriteOkay ( Okay
, output
, " \n Provides: " , provides
);
336 return WriteOkay ( Okay
, output
, " \n " );
339 static bool SkipUnavailableVersions ( pkgDepCache
& Cache
, pkgCache :: PkgIterator
const & Pkg
, pkgCache :: VerIterator
const & Ver
) /*{{{*/
341 /* versions which aren't current and aren't available in
342 any "online" source file are bad, expect if they are the choosen
343 candidate: The exception is for build-dep implementation as it creates
344 such pseudo (package) versions and removes them later on again.
345 We filter out versions at all so packages in 'rc' state only available
346 in dpkg/status aren't passed to solvers as they can't be installed. */
347 if ( Pkg
-> CurrentVer
!= 0 )
349 if ( Cache
. GetCandidateVersion ( Pkg
) == Ver
)
351 for ( pkgCache :: VerFileIterator I
= Ver
. FileList (); I
. end () == false ; ++ I
)
352 if ( I
. File (). Flagged ( pkgCache :: Flag :: NotSource
) == false )
357 static bool WriteScenarioEDSPVersion ( pkgDepCache
& Cache
, FileFd
& output
, pkgCache :: PkgIterator
const & Pkg
, /*{{{*/
358 pkgCache :: VerIterator
const & Ver
)
360 bool Okay
= WriteOkay ( output
, " \n Source: " , Ver
. SourcePkgName (),
361 " \n Source-Version: " , Ver
. SourceVerStr ());
362 if ( PrioMap
[ Ver
-> Priority
] != nullptr )
363 WriteOkay ( Okay
, output
, " \n Priority: " , PrioMap
[ Ver
-> Priority
]);
364 if ( Ver
-> Section
!= 0 )
365 WriteOkay ( Okay
, output
, " \n Section: " , Ver
. Section ());
366 if ( Pkg
. CurrentVer () == Ver
)
367 WriteOkay ( Okay
, output
, " \n Installed: yes" );
368 if ( Pkg
-> SelectedState
== pkgCache :: State :: Hold
||
369 ( Cache
[ Pkg
]. Keep () == true && Cache
[ Pkg
]. Protect () == true ))
370 WriteOkay ( Okay
, output
, " \n Hold: yes" );
371 std :: set
< string
> Releases
;
372 for ( pkgCache :: VerFileIterator I
= Ver
. FileList (); I
. end () == false ; ++ I
) {
373 pkgCache :: PkgFileIterator File
= I
. File ();
374 if ( File
. Flagged ( pkgCache :: Flag :: NotSource
) == false ) {
375 string Release
= File
. RelStr ();
376 if (! Release
. empty ())
377 Releases
. insert ( Release
);
380 if (! Releases
. empty ()) {
381 WriteOkay ( Okay
, output
, " \n APT-Release:" );
382 for ( std :: set
< string
>:: iterator R
= Releases
. begin (); R
!= Releases
. end (); ++ R
)
383 WriteOkay ( Okay
, output
, " \n " , * R
);
385 WriteOkay ( Okay
, output
, " \n APT-Pin: " , Cache
. GetPolicy (). GetPriority ( Ver
));
386 if ( Cache
. GetCandidateVersion ( Pkg
) == Ver
)
387 WriteOkay ( Okay
, output
, " \n APT-Candidate: yes" );
388 if (( Cache
[ Pkg
]. Flags
& pkgCache :: Flag :: Auto
) == pkgCache :: Flag :: Auto
)
389 WriteOkay ( Okay
, output
, " \n APT-Automatic: yes" );
393 // EDSP::WriteScenario - to the given file descriptor /*{{{*/
394 bool EDSP :: WriteScenario ( pkgDepCache
& Cache
, FILE * output
, OpProgress
* Progress
)
396 if ( Progress
!= NULL
)
397 Progress
-> SubProgress ( Cache
. Head (). VersionCount
, _ ( "Send scenario to solver" ));
399 std :: vector
< std :: string
> archs
= APT :: Configuration :: getArchitectures ();
400 for ( pkgCache :: PkgIterator Pkg
= Cache
. PkgBegin (); Pkg
. end () == false ; ++ Pkg
)
402 std :: string
const arch
= Pkg
. Arch ();
403 if ( std :: find ( archs
. begin (), archs
. end (), arch
) == archs
. end ())
405 for ( pkgCache :: VerIterator Ver
= Pkg
. VersionList (); Ver
. end () == false ; ++ Ver
, ++ p
)
407 if ( SkipUnavailableVersions ( Cache
, Pkg
, Ver
))
409 WriteScenarioVersion ( Cache
, output
, Pkg
, Ver
);
410 WriteScenarioDependency ( output
, Ver
);
411 fprintf ( output
, " \n " );
412 if ( Progress
!= NULL
&& p
% 100 == 0 )
413 Progress
-> Progress ( p
);
418 bool EDSP :: WriteScenario ( pkgDepCache
& Cache
, FileFd
& output
, OpProgress
* Progress
)
420 if ( Progress
!= NULL
)
421 Progress
-> SubProgress ( Cache
. Head (). VersionCount
, _ ( "Send scenario to solver" ));
423 bool Okay
= output
. Failed () == false ;
424 std :: vector
< std :: string
> archs
= APT :: Configuration :: getArchitectures ();
425 for ( pkgCache :: PkgIterator Pkg
= Cache
. PkgBegin (); Pkg
. end () == false && likely ( Okay
); ++ Pkg
)
427 std :: string
const arch
= Pkg
. Arch ();
428 if ( std :: find ( archs
. begin (), archs
. end (), arch
) == archs
. end ())
430 for ( pkgCache :: VerIterator Ver
= Pkg
. VersionList (); Ver
. end () == false && likely ( Okay
); ++ Ver
, ++ p
)
432 if ( SkipUnavailableVersions ( Cache
, Pkg
, Ver
))
434 Okay
&= WriteScenarioVersion ( output
, Pkg
, Ver
);
435 Okay
&= WriteScenarioEDSPVersion ( Cache
, output
, Pkg
, Ver
);
436 Okay
&= WriteScenarioDependency ( output
, Ver
, false );
437 WriteOkay ( Okay
, output
, " \n " );
438 if ( Progress
!= NULL
&& p
% 100 == 0 )
439 Progress
-> Progress ( p
);
445 // EDSP::WriteLimitedScenario - to the given file descriptor /*{{{*/
446 bool EDSP :: WriteLimitedScenario ( pkgDepCache
& Cache
, FILE * output
,
447 APT :: PackageSet
const & pkgset
,
448 OpProgress
* Progress
)
450 if ( Progress
!= NULL
)
451 Progress
-> SubProgress ( Cache
. Head (). VersionCount
, _ ( "Send scenario to solver" ));
453 for ( APT :: PackageSet :: const_iterator Pkg
= pkgset
. begin (); Pkg
!= pkgset
. end (); ++ Pkg
, ++ p
)
454 for ( pkgCache :: VerIterator Ver
= Pkg
. VersionList (); Ver
. end () == false ; ++ Ver
)
456 if ( SkipUnavailableVersions ( Cache
, Pkg
, Ver
))
458 WriteScenarioVersion ( Cache
, output
, Pkg
, Ver
);
459 WriteScenarioLimitedDependency ( output
, Ver
, pkgset
);
460 fprintf ( output
, " \n " );
461 if ( Progress
!= NULL
&& p
% 100 == 0 )
462 Progress
-> Progress ( p
);
464 if ( Progress
!= NULL
)
468 bool EDSP :: WriteLimitedScenario ( pkgDepCache
& Cache
, FileFd
& output
,
469 std :: vector
< bool > const & pkgset
,
470 OpProgress
* Progress
)
472 if ( Progress
!= NULL
)
473 Progress
-> SubProgress ( Cache
. Head (). VersionCount
, _ ( "Send scenario to solver" ));
475 bool Okay
= output
. Failed () == false ;
476 for ( auto Pkg
= Cache
. PkgBegin (); Pkg
. end () == false && likely ( Okay
); ++ Pkg
, ++ p
)
478 if ( pkgset
[ Pkg
-> ID
] == false )
480 for ( pkgCache :: VerIterator Ver
= Pkg
. VersionList (); Ver
. end () == false && likely ( Okay
); ++ Ver
)
482 if ( SkipUnavailableVersions ( Cache
, Pkg
, Ver
))
484 Okay
&= WriteScenarioVersion ( output
, Pkg
, Ver
);
485 Okay
&= WriteScenarioEDSPVersion ( Cache
, output
, Pkg
, Ver
);
486 Okay
&= WriteScenarioLimitedDependency ( output
, Ver
, pkgset
, false );
487 WriteOkay ( Okay
, output
, " \n " );
488 if ( Progress
!= NULL
&& p
% 100 == 0 )
489 Progress
-> Progress ( p
);
492 if ( Progress
!= NULL
)
497 // EDSP::WriteRequest - to the given file descriptor /*{{{*/
498 bool EDSP :: WriteRequest ( pkgDepCache
& Cache
, FILE * output
, bool const Upgrade
,
499 bool const DistUpgrade
, bool const AutoRemove
,
500 OpProgress
* Progress
)
502 if ( Progress
!= NULL
)
503 Progress
-> SubProgress ( Cache
. Head (). PackageCount
, _ ( "Send request to solver" ));
506 for ( pkgCache :: PkgIterator Pkg
= Cache
. PkgBegin (); Pkg
. end () == false ; ++ Pkg
, ++ p
)
508 if ( Progress
!= NULL
&& p
% 100 == 0 )
509 Progress
-> Progress ( p
);
511 pkgDepCache :: StateCache
& P
= Cache
[ Pkg
];
512 if ( P
. Delete () == true )
514 else if ( P
. NewInstall () == true || P
. Upgrade () == true || P
. ReInstall () == true ||
515 ( P
. Mode
== pkgDepCache :: ModeKeep
&& ( P
. iFlags
& pkgDepCache :: Protected
) == pkgDepCache :: Protected
))
519 req
-> append ( " " ). append ( Pkg
. FullName ());
521 fprintf ( output
, "Request: EDSP 0.5 \n " );
523 const char * arch
= _config
-> Find ( "APT::Architecture" ). c_str ();
524 std :: vector
< string
> archs
= APT :: Configuration :: getArchitectures ();
525 fprintf ( output
, "Architecture: %s \n " , arch
);
526 fprintf ( output
, "Architectures:" );
527 for ( std :: vector
< string
>:: const_iterator a
= archs
. begin (); a
!= archs
. end (); ++ a
)
528 fprintf ( output
, " %s " , a
-> c_str ());
529 fprintf ( output
, " \n " );
531 if ( del
. empty () == false )
532 fprintf ( output
, "Remove: %s \n " , del
. c_str ()+ 1 );
533 if ( inst
. empty () == false )
534 fprintf ( output
, "Install: %s \n " , inst
. c_str ()+ 1 );
536 fprintf ( output
, "Upgrade: yes \n " );
537 if ( DistUpgrade
== true )
538 fprintf ( output
, "Dist-Upgrade: yes \n " );
539 if ( AutoRemove
== true )
540 fprintf ( output
, "Autoremove: yes \n " );
541 auto const solver
= _config
-> Find ( "APT::Solver" , "internal" );
542 fprintf ( output
, "Solver: %s \n " , solver
. c_str ());
543 auto const solverconf
= std :: string ( "APT::Solver::" ) + solver
+ "::" ;
544 if ( _config
-> FindB ( solverconf
+ "Strict-Pinning" , _config
-> FindB ( "APT::Solver::Strict-Pinning" , true )) == false )
545 fprintf ( output
, "Strict-Pinning: no \n " );
546 auto const solverpref
= _config
-> Find ( solverconf
+ "Preferences" , _config
-> Find ( "APT::Solver::Preferences" , "" ));
547 if ( solverpref
. empty () == false )
548 fprintf ( output
, "Preferences: %s \n " , solverpref
. c_str ());
549 fprintf ( output
, " \n " );
552 bool EDSP :: WriteRequest ( pkgDepCache
& Cache
, FileFd
& output
,
553 unsigned int const flags
,
554 OpProgress
* Progress
)
556 if ( Progress
!= NULL
)
557 Progress
-> SubProgress ( Cache
. Head (). PackageCount
, _ ( "Send request to solver" ));
560 for ( pkgCache :: PkgIterator Pkg
= Cache
. PkgBegin (); Pkg
. end () == false ; ++ Pkg
, ++ p
)
562 if ( Progress
!= NULL
&& p
% 100 == 0 )
563 Progress
-> Progress ( p
);
565 pkgDepCache :: StateCache
& P
= Cache
[ Pkg
];
566 if ( P
. Delete () == true )
568 else if ( P
. NewInstall () == true || P
. Upgrade () == true || P
. ReInstall () == true ||
569 ( P
. Mode
== pkgDepCache :: ModeKeep
&& ( P
. iFlags
& pkgDepCache :: Protected
) == pkgDepCache :: Protected
))
573 req
-> append ( " " ). append ( Pkg
. FullName ());
575 bool Okay
= WriteOkay ( output
, "Request: EDSP 0.5 \n " );
577 const char * arch
= _config
-> Find ( "APT::Architecture" ). c_str ();
578 std :: vector
< string
> archs
= APT :: Configuration :: getArchitectures ();
579 WriteOkay ( Okay
, output
, "Architecture: " , arch
, " \n " ,
581 for ( std :: vector
< string
>:: const_iterator a
= archs
. begin (); a
!= archs
. end (); ++ a
)
582 WriteOkay ( Okay
, output
, " " , * a
);
583 WriteOkay ( Okay
, output
, " \n " );
585 if ( del
. empty () == false )
586 WriteOkay ( Okay
, output
, "Remove:" , del
, " \n " );
587 if ( inst
. empty () == false )
588 WriteOkay ( Okay
, output
, "Install:" , inst
, " \n " );
589 if ( flags
& Request :: AUTOREMOVE
)
590 WriteOkay ( Okay
, output
, "Autoremove: yes \n " );
591 if ( flags
& Request :: UPGRADE_ALL
)
593 WriteOkay ( Okay
, output
, "Upgrade-All: yes \n " );
594 if ( flags
& ( Request :: FORBID_NEW_INSTALL
| Request :: FORBID_REMOVE
))
595 WriteOkay ( Okay
, output
, "Upgrade: yes \n " );
597 WriteOkay ( Okay
, output
, "Dist-Upgrade: yes \n " );
599 if ( flags
& Request :: FORBID_NEW_INSTALL
)
600 WriteOkay ( Okay
, output
, "Forbid-New-Install: yes \n " );
601 if ( flags
& Request :: FORBID_REMOVE
)
602 WriteOkay ( Okay
, output
, "Forbid-Remove: yes \n " );
603 auto const solver
= _config
-> Find ( "APT::Solver" , "internal" );
604 WriteOkay ( Okay
, output
, "Solver: " , solver
, " \n " );
605 if ( _config
-> FindB ( "APT::Solver::Strict-Pinning" , true ) == false )
606 WriteOkay ( Okay
, output
, "Strict-Pinning: no \n " );
607 string
solverpref ( "APT::Solver::" );
608 solverpref
. append ( solver
). append ( "::Preferences" );
609 if ( _config
-> Exists ( solverpref
) == true )
610 WriteOkay ( Okay
, output
, "Preferences: " , _config
-> Find ( solverpref
, "" ), " \n " );
611 return WriteOkay ( Okay
, output
, " \n " );
614 // EDSP::ReadResponse - from the given file descriptor /*{{{*/
615 bool EDSP :: ReadResponse ( int const input
, pkgDepCache
& Cache
, OpProgress
* Progress
) {
616 /* We build an map id to mmap offset here
617 In theory we could use the offset as ID, but then VersionCount
618 couldn't be used to create other versionmappings anymore and it
619 would be too easy for a (buggy) solver to segfault APT… */
620 unsigned long long const VersionCount
= Cache
. Head (). VersionCount
;
621 unsigned long VerIdx
[ VersionCount
];
622 for ( pkgCache :: PkgIterator P
= Cache
. PkgBegin (); P
. end () == false ; ++ P
) {
623 for ( pkgCache :: VerIterator V
= P
. VersionList (); V
. end () == false ; ++ V
)
624 VerIdx
[ V
-> ID
] = V
. Index ();
625 Cache
[ P
]. Marked
= true ;
626 Cache
[ P
]. Garbage
= false ;
630 in
. OpenDescriptor ( input
, FileFd :: ReadOnly
, true );
631 pkgTagFile
response (& in
, 100 );
632 pkgTagSection section
;
634 std :: set
< decltype ( Cache
. PkgBegin ()-> ID
)> seenOnce
;
635 while ( response
. Step ( section
) == true ) {
637 if ( section
. Exists ( "Install" ) == true )
639 else if ( section
. Exists ( "Remove" ) == true )
641 else if ( section
. Exists ( "Progress" ) == true ) {
642 if ( Progress
!= NULL
) {
643 string msg
= section
. FindS ( "Message" );
644 if ( msg
. empty () == true )
645 msg
= _ ( "Prepare for receiving solution" );
646 Progress
-> SubProgress ( 100 , msg
, section
. FindI ( "Percentage" , 0 ));
649 } else if ( section
. Exists ( "Error" ) == true ) {
650 std :: string msg
= SubstVar ( SubstVar ( section
. FindS ( "Message" ), " \n . \n " , " \n\n " ), " \n " , " \n " );
651 if ( msg
. empty () == true ) {
652 msg
= _ ( "External solver failed without a proper error message" );
653 _error
-> Error ( " %s " , msg
. c_str ());
655 _error
-> Error ( "External solver failed with: %s " , msg
. substr ( 0 , msg
. find ( ' \n ' )). c_str ());
656 if ( Progress
!= NULL
)
658 std :: cerr
<< "The solver encountered an error of type: " << section
. FindS ( "Error" ) << std :: endl
;
659 std :: cerr
<< "The following information might help you to understand what is wrong:" << std :: endl
;
660 std :: cerr
<< msg
<< std :: endl
<< std :: endl
;
662 } else if ( section
. Exists ( "Autoremove" ) == true )
665 char const * Start
, * End
;
666 section
. GetSection ( Start
, End
);
667 _error
-> Warning ( "Encountered an unexpected section with %d fields: %s " , section
. Count (), std :: string ( Start
, End
). c_str ());
671 size_t const id
= section
. FindULL ( type
. c_str (), VersionCount
);
672 if ( id
== VersionCount
) {
673 _error
-> Warning ( "Unable to parse %s request with id value ' %s '!" , type
. c_str (), section
. FindS ( type
. c_str ()). c_str ());
675 } else if ( id
> Cache
. Head (). VersionCount
) {
676 _error
-> Warning ( "ID value ' %s ' in %s request stanza is to high to refer to a known version!" , section
. FindS ( type
. c_str ()). c_str (), type
. c_str ());
680 pkgCache :: VerIterator
Ver ( Cache
. GetCache (), Cache
. GetCache (). VerP
+ VerIdx
[ id
]);
681 auto const Pkg
= Ver
. ParentPkg ();
682 if ( type
== "Autoremove" ) {
683 Cache
[ Pkg
]. Marked
= false ;
684 Cache
[ Pkg
]. Garbage
= true ;
685 } else if ( seenOnce
. emplace ( Pkg
-> ID
). second
== false ) {
686 _error
-> Warning ( "Ignoring %s stanza received for package %s which already had a previous stanza effecting it!" , type
. c_str (), Pkg
. FullName ( false ). c_str ());
687 } else if ( type
== "Install" ) {
688 if ( Pkg
. CurrentVer () == Ver
) {
689 _error
-> Warning ( "Ignoring Install stanza received for version %s of package %s which is already installed!" ,
690 Ver
. VerStr (), Pkg
. FullName ( false ). c_str ());
692 Cache
. SetCandidateVersion ( Ver
);
693 Cache
. MarkInstall ( Pkg
, false , 0 , false );
695 } else if ( type
== "Remove" ) {
696 if ( Pkg
-> CurrentVer
== 0 )
697 _error
-> Warning ( "Ignoring Remove stanza received for version %s of package %s which isn't installed!" ,
698 Ver
. VerStr (), Pkg
. FullName ( false ). c_str ());
699 else if ( Pkg
. CurrentVer () != Ver
)
700 _error
-> Warning ( "Ignoring Remove stanza received for version %s of package %s which isn't the installed version %s !" ,
701 Ver
. VerStr (), Pkg
. FullName ( false ). c_str (), Pkg
. CurrentVer (). VerStr ());
703 Cache
. MarkDelete ( Ver
. ParentPkg (), false );
709 // ReadLine - first line from the given file descriptor /*{{{*/
710 // ---------------------------------------------------------------------
711 /* Little helper method to read a complete line into a string. Similar to
712 fgets but we need to use the low-level read() here as otherwise the
713 listparser will be confused later on as mixing of fgets and read isn't
714 a supported action according to the manpages and results are undefined */
715 static bool ReadLine ( int const input
, std :: string
& line
) {
720 while (( data
= read ( input
, & one
, sizeof ( one
))) != - 1 ) {
727 if ( line
. empty () == true && isblank ( one
) != 0 )
734 // StringToBool - convert yes/no to bool /*{{{*/
735 // ---------------------------------------------------------------------
736 /* we are not as lazy as we are in the global StringToBool as we really
737 only accept yes/no here */
738 static bool localStringToBool ( std :: string answer
, bool const defValue
) {
739 std :: transform ( answer
. begin (), answer
. end (), answer
. begin (), :: tolower
);
742 else if ( answer
== "no" )
745 _error
-> Warning ( "Value ' %s ' is not a boolean 'yes' or 'no'!" , answer
. c_str ());
749 static bool LineStartsWithAndStrip ( std :: string
& line
, APT :: StringView
const with
) /*{{{*/
751 if ( line
. compare ( 0 , with
. size (), with
. data ()) != 0 )
753 line
= APT :: String :: Strip ( line
. substr ( with
. length ()));
757 static bool ReadFlag ( unsigned int & flags
, std :: string
& line
, APT :: StringView
const name
, unsigned int const setflag
) /*{{{*/
759 if ( LineStartsWithAndStrip ( line
, name
) == false )
761 if ( localStringToBool ( line
, false ))
768 // EDSP::ReadRequest - first stanza from the given file descriptor /*{{{*/
769 bool EDSP :: ReadRequest ( int const input
, std :: list
< std :: string
> & install
,
770 std :: list
< std :: string
> & remove
, unsigned int & flags
)
776 while ( ReadLine ( input
, line
) == true )
778 // Skip empty lines before request
779 if ( line
. empty () == true )
781 // The first Tag must be a request, so search for it
782 if ( LineStartsWithAndStrip ( line
, "Request:" ))
785 while ( ReadLine ( input
, line
) == true )
787 // empty lines are the end of the request
788 if ( line
. empty () == true )
791 std :: list
< std :: string
> * request
= NULL
;
792 if ( LineStartsWithAndStrip ( line
, "Install:" ))
794 else if ( LineStartsWithAndStrip ( line
, "Remove:" ))
796 else if ( ReadFlag ( flags
, line
, "Upgrade:" , ( Request :: UPGRADE_ALL
| Request :: FORBID_REMOVE
| Request :: FORBID_NEW_INSTALL
)) ||
797 ReadFlag ( flags
, line
, "Dist-Upgrade:" , Request :: UPGRADE_ALL
) ||
798 ReadFlag ( flags
, line
, "Upgrade-All:" , Request :: UPGRADE_ALL
) ||
799 ReadFlag ( flags
, line
, "Forbid-New-Install:" , Request :: FORBID_NEW_INSTALL
) ||
800 ReadFlag ( flags
, line
, "Forbid-Remove:" , Request :: FORBID_REMOVE
) ||
801 ReadFlag ( flags
, line
, "Autoremove:" , Request :: AUTOREMOVE
))
803 else if ( LineStartsWithAndStrip ( line
, "Architecture:" ))
804 _config
-> Set ( "APT::Architecture" , line
);
805 else if ( LineStartsWithAndStrip ( line
, "Architectures:" ))
806 _config
-> Set ( "APT::Architectures" , SubstVar ( line
, " " , "," ));
807 else if ( LineStartsWithAndStrip ( line
, "Solver:" ))
808 ; // purely informational line
810 _error
-> Warning ( "Unknown line in EDSP Request stanza: %s " , line
. c_str ());
814 auto const pkgs
= VectorizeString ( line
, ' ' );
815 std :: move ( pkgs
. begin (), pkgs
. end (), std :: back_inserter (* request
));
820 bool EDSP :: ReadRequest ( int const input
, std :: list
< std :: string
> & install
,
821 std :: list
< std :: string
> & remove
, bool & upgrade
,
822 bool & distUpgrade
, bool & autoRemove
)
825 auto const ret
= ReadRequest ( input
, install
, remove
, flags
);
826 autoRemove
= ( flags
& Request :: AUTOREMOVE
);
827 if ( flags
& Request :: UPGRADE_ALL
)
829 if ( flags
& ( Request :: FORBID_NEW_INSTALL
| Request :: FORBID_REMOVE
))
846 // EDSP::ApplyRequest - first stanza from the given file descriptor /*{{{*/
847 bool EDSP :: ApplyRequest ( std :: list
< std :: string
> const & install
,
848 std :: list
< std :: string
> const & remove
,
851 for ( std :: list
< std :: string
>:: const_iterator i
= install
. begin ();
852 i
!= install
. end (); ++ i
) {
853 pkgCache :: PkgIterator P
= Cache
. FindPkg (* i
);
855 _error
-> Warning ( "Package %s is not known, so can't be installed" , i
-> c_str ());
857 Cache
. MarkInstall ( P
, false );
860 for ( std :: list
< std :: string
>:: const_iterator i
= remove
. begin ();
861 i
!= remove
. end (); ++ i
) {
862 pkgCache :: PkgIterator P
= Cache
. FindPkg (* i
);
864 _error
-> Warning ( "Package %s is not known, so can't be installed" , i
-> c_str ());
871 // EDSP::WriteSolutionStanza - to the given file descriptor /*{{{*/
872 bool EDSP :: WriteSolution ( pkgDepCache
& Cache
, FILE * output
)
874 bool const Debug
= _config
-> FindB ( "Debug::EDSP::WriteSolution" , false );
875 for ( pkgCache :: PkgIterator Pkg
= Cache
. PkgBegin (); Pkg
. end () == false ; ++ Pkg
)
877 if ( Cache
[ Pkg
]. Delete () == true )
879 fprintf ( output
, "Remove: %d \n " , _system
-> GetVersionMapping ( Pkg
. CurrentVer ()-> ID
));
881 fprintf ( output
, "Package: %s \n Version: %s \n " , Pkg
. FullName (). c_str (), Pkg
. CurrentVer (). VerStr ());
883 else if ( Cache
[ Pkg
]. NewInstall () == true || Cache
[ Pkg
]. Upgrade () == true )
885 pkgCache :: VerIterator
const CandVer
= Cache
. GetCandidateVersion ( Pkg
);
886 fprintf ( output
, "Install: %d \n " , _system
-> GetVersionMapping ( CandVer
-> ID
));
888 fprintf ( output
, "Package: %s \n Version: %s \n " , Pkg
. FullName (). c_str (), CandVer
. VerStr ());
890 else if ( Cache
[ Pkg
]. Garbage
== true )
892 fprintf ( output
, "Autoremove: %d \n " , _system
-> GetVersionMapping ( Pkg
. CurrentVer ()-> ID
));
894 fprintf ( output
, "Package: %s \n Version: %s \n " , Pkg
. FullName (). c_str (), Pkg
. CurrentVer (). VerStr ());
898 fprintf ( output
, " \n " );
903 bool EDSP :: WriteSolutionStanza ( FileFd
& output
, char const * const Type
, pkgCache :: VerIterator
const & Ver
)
905 bool Okay
= output
. Failed () == false ;
906 WriteOkay ( Okay
, output
, Type
, ": " , _system
-> GetVersionMapping ( Ver
-> ID
));
907 if ( _config
-> FindB ( "Debug::EDSP::WriteSolution" , false ) == true )
908 WriteOkay ( Okay
, output
, " \n Package: " , Ver
. ParentPkg (). FullName (), " \n Version: " , Ver
. VerStr ());
909 return WriteOkay ( Okay
, output
, " \n\n " );
912 // EDSP::WriteProgess - pulse to the given file descriptor /*{{{*/
913 bool EDSP :: WriteProgress ( unsigned short const percent
, const char * const message
, FILE * output
) {
914 fprintf ( output
, "Progress: %s \n " , TimeRFC1123 ( time ( NULL
)). c_str ());
915 fprintf ( output
, "Percentage: %d \n " , percent
);
916 fprintf ( output
, "Message: %s \n\n " , message
);
920 bool EDSP :: WriteProgress ( unsigned short const percent
, const char * const message
, FileFd
& output
) {
921 return WriteOkay ( output
, "Progress: " , TimeRFC1123 ( time ( NULL
)), " \n " ,
922 "Percentage: " , percent
, " \n " ,
923 "Message: " , message
, " \n\n " ) && output
. Flush ();
926 // EDSP::WriteError - format an error message to be send to file descriptor /*{{{*/
927 bool EDSP :: WriteError ( char const * const uuid
, std :: string
const & message
, FILE * output
) {
928 fprintf ( output
, "Error: %s \n " , uuid
);
929 fprintf ( output
, "Message: %s \n\n " , SubstVar ( SubstVar ( message
, " \n\n " , " \n . \n " ), " \n " , " \n " ). c_str ());
932 bool EDSP :: WriteError ( char const * const uuid
, std :: string
const & message
, FileFd
& output
) {
933 return WriteOkay ( output
, "Error: " , uuid
, " \n " ,
934 "Message: " , SubstVar ( SubstVar ( message
, " \n\n " , " \n . \n " ), " \n " , " \n " ),
938 static std :: string
findExecutable ( std :: vector
< std :: string
> const & dirs
, char const * const binary
) { /*{{{*/
939 for ( auto && dir
: dirs
) {
940 std :: string
const file
= flCombine ( dir
, binary
);
941 if ( RealFileExists ( file
) == true )
947 static pid_t
ExecuteExternal ( char const * const type
, char const * const binary
, char const * const configdir
, int * const solver_in
, int * const solver_out
) { /*{{{*/
948 auto const solverDirs
= _config
-> FindVector ( configdir
);
949 auto const file
= findExecutable ( solverDirs
, binary
);
952 dumper
= findExecutable ( solverDirs
, "apt-dump-solver" );
954 dumper
= findExecutable ( solverDirs
, "dump" );
957 if ( file
. empty () == true )
959 _error
-> Error ( "Can't call external %s ' %s ' as it is not in a configured directory!" , type
, binary
);
962 int external
[ 4 ] = {- 1 , - 1 , - 1 , - 1 };
963 if ( pipe ( external
) != 0 || pipe ( external
+ 2 ) != 0 )
965 _error
-> Errno ( "Resolve" , "Can't create needed IPC pipes for EDSP" );
968 for ( int i
= 0 ; i
< 4 ; ++ i
)
969 SetCloseExec ( external
[ i
], true );
971 pid_t Solver
= ExecFork ();
973 dup2 ( external
[ 0 ], STDIN_FILENO
);
974 dup2 ( external
[ 3 ], STDOUT_FILENO
);
975 auto const dumpfile
= _config
-> FindFile (( std :: string ( "Dir::Log::" ) + type
). c_str ());
976 auto const dumpdir
= flNotFile ( dumpfile
);
977 auto const runasuser
= _config
-> Find ( std :: string ( "APT::" ) + type
+ "::" + binary
+ "::RunAsUser" ,
978 _config
-> Find ( std :: string ( "APT::" ) + type
+ "::RunAsUser" ,
979 _config
-> Find ( "APT::Sandbox::User" )));
980 if ( dumper
. empty () || dumpfile
. empty () || dumper
== file
|| CreateAPTDirectoryIfNeeded ( dumpdir
, dumpdir
) == false )
982 _config
-> Set ( "APT::Sandbox::User" , runasuser
);
984 char const * const calling
[] = { file
. c_str (), nullptr };
985 execv ( calling
[ 0 ], const_cast < char **>( calling
));
989 char const * const calling
[] = { dumper
. c_str (), "--user" , runasuser
. c_str (), dumpfile
. c_str (), file
. c_str (), nullptr };
990 execv ( calling
[ 0 ], const_cast < char **>( calling
));
992 std :: cerr
<< "Failed to execute " << type
<< " '" << binary
<< "'!" << std :: endl
;
998 if ( WaitFd ( external
[ 1 ], true , 5 ) == false )
1000 _error
-> Errno ( "Resolve" , "Timed out while Waiting on availability of %s stdin" , type
);
1004 * solver_in
= external
[ 1 ];
1005 * solver_out
= external
[ 2 ];
1009 // EDSP::ExecuteSolver - fork requested solver and setup ipc pipes {{{*/
1010 pid_t
EDSP :: ExecuteSolver ( const char * const solver
, int * const solver_in
, int * const solver_out
, bool ) {
1011 return ExecuteExternal ( "solver" , solver
, "Dir::Bin::Solvers" , solver_in
, solver_out
);
1013 bool EDSP :: ExecuteSolver ( const char * const solver
, int * solver_in
, int * solver_out
) {
1014 if ( ExecuteSolver ( solver
, solver_in
, solver_out
, true ) == 0 )
1019 // EDSP::ResolveExternal - resolve problems by asking external for help {{{*/
1020 bool EDSP :: ResolveExternal ( const char * const solver
, pkgDepCache
& Cache
,
1021 unsigned int const flags
, OpProgress
* Progress
) {
1022 if ( strcmp ( solver
, "internal" ) == 0 )
1024 auto const dumpfile
= _config
-> FindFile ( "Dir::Log::Solver" );
1025 if ( dumpfile
. empty ())
1027 auto const dumpdir
= flNotFile ( dumpfile
);
1029 if ( CreateAPTDirectoryIfNeeded ( dumpdir
, dumpdir
) == false ||
1030 output
. Open ( dumpfile
, FileFd :: WriteOnly
| FileFd :: Exclusive
| FileFd :: Create
, FileFd :: Extension
, 0644 ) == false )
1031 return _error
-> WarningE ( "EDSP::Resolve" , _ ( "Could not open file ' %s '" ), dumpfile
. c_str ());
1032 bool Okay
= EDSP :: WriteRequest ( Cache
, output
, flags
, nullptr );
1033 return Okay
&& EDSP :: WriteScenario ( Cache
, output
, nullptr );
1035 int solver_in
, solver_out
;
1036 pid_t
const solver_pid
= EDSP :: ExecuteSolver ( solver
, & solver_in
, & solver_out
, true );
1037 if ( solver_pid
== 0 )
1041 if ( output
. OpenDescriptor ( solver_in
, FileFd :: WriteOnly
| FileFd :: BufferedWrite
, true ) == false )
1042 return _error
-> Errno ( "ResolveExternal" , "Opening solver %s stdin on fd %d for writing failed" , solver
, solver_in
);
1044 bool Okay
= output
. Failed () == false ;
1045 if ( Progress
!= NULL
)
1046 Progress
-> OverallProgress ( 0 , 100 , 5 , _ ( "Execute external solver" ));
1047 Okay
&= EDSP :: WriteRequest ( Cache
, output
, flags
, Progress
);
1048 if ( Progress
!= NULL
)
1049 Progress
-> OverallProgress ( 5 , 100 , 20 , _ ( "Execute external solver" ));
1050 Okay
&= EDSP :: WriteScenario ( Cache
, output
, Progress
);
1053 if ( Progress
!= NULL
)
1054 Progress
-> OverallProgress ( 25 , 100 , 75 , _ ( "Execute external solver" ));
1055 if ( Okay
&& EDSP :: ReadResponse ( solver_out
, Cache
, Progress
) == false )
1058 return ExecWait ( solver_pid
, solver
);
1060 bool EDSP :: ResolveExternal ( const char * const solver
, pkgDepCache
& Cache
,
1061 bool const upgrade
, bool const distUpgrade
,
1062 bool const autoRemove
, OpProgress
* Progress
) {
1063 unsigned int flags
= 0 ;
1065 flags
|= Request :: AUTOREMOVE
;
1067 flags
|= Request :: UPGRADE_ALL
| Request :: FORBID_REMOVE
| Request :: FORBID_NEW_INSTALL
;
1069 flags
|= Request :: UPGRADE_ALL
;
1070 return ResolveExternal ( solver
, Cache
, flags
, Progress
);
1074 bool EIPP :: OrderInstall ( char const * const solver
, pkgPackageManager
* const PM
, /*{{{*/
1075 unsigned int const flags
, OpProgress
* const Progress
)
1077 int solver_in
, solver_out
;
1078 pid_t
const solver_pid
= ExecuteExternal ( "planer" , solver
, "Dir::Bin::Planers" , & solver_in
, & solver_out
);
1079 if ( solver_pid
== 0 )
1083 if ( output
. OpenDescriptor ( solver_in
, FileFd :: WriteOnly
| FileFd :: BufferedWrite
, true ) == false )
1084 return _error
-> Errno ( "OrderInstall" , "Opening planer %s stdin on fd %d for writing failed" , solver
, solver_in
);
1086 bool Okay
= output
. Failed () == false ;
1087 if ( Progress
!= NULL
)
1088 Progress
-> OverallProgress ( 0 , 100 , 5 , _ ( "Execute external planer" ));
1089 Okay
&= EIPP :: WriteRequest ( PM
-> Cache
, output
, flags
, Progress
);
1090 if ( Progress
!= NULL
)
1091 Progress
-> OverallProgress ( 5 , 100 , 20 , _ ( "Execute external planer" ));
1092 Okay
&= EIPP :: WriteScenario ( PM
-> Cache
, output
, Progress
);
1095 if ( Progress
!= NULL
)
1096 Progress
-> OverallProgress ( 25 , 100 , 75 , _ ( "Execute external planer" ));
1097 if ( Okay
&& EIPP :: ReadResponse ( solver_out
, PM
, Progress
) == false )
1100 return ExecWait ( solver_pid
, solver
);
1103 bool EIPP :: WriteRequest ( pkgDepCache
& Cache
, FileFd
& output
, /*{{{*/
1104 unsigned int const flags
,
1105 OpProgress
* const Progress
)
1108 if ( Progress
!= NULL
)
1109 Progress
-> SubProgress ( Cache
. Head (). PackageCount
, _ ( "Send request to planer" ));
1110 unsigned long p
= 0 ;
1111 string del
, purge
, inst
, reinst
;
1112 for ( pkgCache :: PkgIterator Pkg
= Cache
. PkgBegin (); Pkg
. end () == false ; ++ Pkg
, ++ p
)
1114 if ( Progress
!= NULL
&& p
% 100 == 0 )
1115 Progress
-> Progress ( p
);
1117 pkgDepCache :: StateCache
& P
= Cache
[ Pkg
];
1118 if ( P
. Purge () == true )
1120 if ( P
. Delete () == true )
1122 else if ( P
. NewInstall () == true || P
. Upgrade () == true || P
. Downgrade () == true )
1124 else if ( P
. ReInstall () == true )
1128 req
-> append ( " " ). append ( Pkg
. FullName ());
1130 bool Okay
= WriteOkay ( output
, "Request: EIPP 0.1 \n " );
1132 const char * arch
= _config
-> Find ( "APT::Architecture" ). c_str ();
1133 std :: vector
< string
> archs
= APT :: Configuration :: getArchitectures ();
1134 WriteOkay ( Okay
, output
, "Architecture: " , arch
, " \n " ,
1136 for ( std :: vector
< string
>:: const_iterator a
= archs
. begin (); a
!= archs
. end (); ++ a
)
1137 WriteOkay ( Okay
, output
, " " , * a
);
1138 WriteOkay ( Okay
, output
, " \n " );
1140 if ( purge
. empty () == false )
1141 WriteOkay ( Okay
, output
, "Purge:" , purge
, " \n " );
1142 if ( del
. empty () == false )
1143 WriteOkay ( Okay
, output
, "Remove:" , del
, " \n " );
1144 if ( inst
. empty () == false )
1145 WriteOkay ( Okay
, output
, "Install:" , inst
, " \n " );
1146 if ( reinst
. empty () == false )
1147 WriteOkay ( Okay
, output
, "ReInstall:" , reinst
, " \n " );
1148 WriteOkay ( Okay
, output
, "Planer: " , _config
-> Find ( "APT::Planer" , "internal" ), " \n " );
1149 return WriteOkay ( Okay
, output
, " \n " );
1152 static bool WriteScenarioEIPPVersion ( pkgDepCache
&, FileFd
& output
, pkgCache :: PkgIterator
const & Pkg
, /*{{{*/
1153 pkgCache :: VerIterator
const & Ver
)
1156 if ( Pkg
. CurrentVer () == Ver
)
1157 switch ( Pkg
-> CurrentState
)
1159 case pkgCache :: State :: NotInstalled
: WriteOkay ( Okay
, output
, " \n Status: not-installed" ); break ;
1160 case pkgCache :: State :: ConfigFiles
: WriteOkay ( Okay
, output
, " \n Status: config-files" ); break ;
1161 case pkgCache :: State :: HalfInstalled
: WriteOkay ( Okay
, output
, " \n Status: half-installed" ); break ;
1162 case pkgCache :: State :: UnPacked
: WriteOkay ( Okay
, output
, " \n Status: unpacked" ); break ;
1163 case pkgCache :: State :: HalfConfigured
: WriteOkay ( Okay
, output
, " \n Status: half-configured" ); break ;
1164 case pkgCache :: State :: TriggersAwaited
: WriteOkay ( Okay
, output
, " \n Status: triggers-awaited" ); break ;
1165 case pkgCache :: State :: TriggersPending
: WriteOkay ( Okay
, output
, " \n Status: triggers-pending" ); break ;
1166 case pkgCache :: State :: Installed
: WriteOkay ( Okay
, output
, " \n Status: installed" ); break ;
1171 // EIPP::WriteScenario - to the given file descriptor /*{{{*/
1172 template < typename forVersion
> void forAllInterestingVersions ( pkgDepCache
& Cache
, pkgCache :: PkgIterator
const & Pkg
, forVersion
const & func
)
1174 if ( Pkg
-> CurrentState
== pkgCache :: State :: NotInstalled
)
1176 auto P
= Cache
[ Pkg
];
1177 if ( P
. Install () == false )
1179 func ( Pkg
, P
. InstVerIter ( Cache
));
1183 if ( Pkg
-> CurrentVer
!= 0 )
1184 func ( Pkg
, Pkg
. CurrentVer ());
1185 auto P
= Cache
[ Pkg
];
1186 auto const V
= P
. InstVerIter ( Cache
);
1187 if ( P
. Delete () == false && Pkg
. CurrentVer () != V
)
1192 bool EIPP :: WriteScenario ( pkgDepCache
& Cache
, FileFd
& output
, OpProgress
* const Progress
)
1194 if ( Progress
!= NULL
)
1195 Progress
-> SubProgress ( Cache
. Head (). PackageCount
, _ ( "Send scenario to planer" ));
1196 unsigned long p
= 0 ;
1197 bool Okay
= output
. Failed () == false ;
1198 std :: vector
< std :: string
> archs
= APT :: Configuration :: getArchitectures ();
1199 std :: vector
< bool > pkgset ( Cache
. Head (). PackageCount
, false );
1200 auto const MarkVersion
= [&]( pkgCache :: PkgIterator
const & Pkg
, pkgCache :: VerIterator
const & Ver
) {
1201 pkgset
[ Pkg
-> ID
] = true ;
1202 for ( auto D
= Ver
. DependsList (); D
. end () == false ; ++ D
)
1204 if ( D
. IsCritical () == false )
1206 auto const P
= D
. TargetPkg ();
1207 for ( auto Prv
= P
. ProvidesList (); Prv
. end () == false ; ++ Prv
)
1209 auto const V
= Prv
. OwnerVer ();
1210 auto const PV
= V
. ParentPkg ();
1211 if ( V
== PV
. CurrentVer () || V
== Cache
[ PV
]. InstVerIter ( Cache
))
1212 pkgset
[ PV
-> ID
] = true ;
1214 pkgset
[ P
-> ID
] = true ;
1217 for ( pkgCache :: PkgIterator Pkg
= Cache
. PkgBegin (); Pkg
. end () == false ; ++ Pkg
)
1218 forAllInterestingVersions ( Cache
, Pkg
, MarkVersion
);
1219 auto const WriteVersion
= [&]( pkgCache :: PkgIterator
const & Pkg
, pkgCache :: VerIterator
const & Ver
) {
1220 Okay
&= WriteScenarioVersion ( output
, Pkg
, Ver
);
1221 Okay
&= WriteScenarioEIPPVersion ( Cache
, output
, Pkg
, Ver
);
1222 Okay
&= WriteScenarioLimitedDependency ( output
, Ver
, pkgset
, true );
1223 WriteOkay ( Okay
, output
, " \n " );
1224 if ( Progress
!= NULL
&& p
% 100 == 0 )
1225 Progress
-> Progress ( p
);
1227 for ( pkgCache :: PkgIterator Pkg
= Cache
. PkgBegin (); Pkg
. end () == false && likely ( Okay
); ++ Pkg
, ++ p
)
1229 if ( pkgset
[ Pkg
-> ID
] == false || Pkg
-> VersionList
== 0 )
1231 forAllInterestingVersions ( Cache
, Pkg
, WriteVersion
);
1236 // EIPP::ReadResponse - from the given file descriptor /*{{{*/
1237 bool EIPP :: ReadResponse ( int const input
, pkgPackageManager
* const PM
, OpProgress
* Progress
) {
1238 /* We build an map id to mmap offset here
1239 In theory we could use the offset as ID, but then VersionCount
1240 couldn't be used to create other versionmappings anymore and it
1241 would be too easy for a (buggy) solver to segfault APT… */
1242 unsigned long long const VersionCount
= PM
-> Cache
. Head (). VersionCount
;
1243 unsigned long VerIdx
[ VersionCount
];
1244 for ( pkgCache :: PkgIterator P
= PM
-> Cache
. PkgBegin (); P
. end () == false ; ++ P
) {
1245 for ( pkgCache :: VerIterator V
= P
. VersionList (); V
. end () == false ; ++ V
)
1246 VerIdx
[ V
-> ID
] = V
. Index ();
1250 in
. OpenDescriptor ( input
, FileFd :: ReadOnly
);
1251 pkgTagFile
response (& in
, 100 );
1252 pkgTagSection section
;
1254 while ( response
. Step ( section
) == true ) {
1255 char const * type
= nullptr ;
1256 if ( section
. Exists ( "Progress" ) == true ) {
1257 if ( Progress
!= NULL
) {
1258 string msg
= section
. FindS ( "Message" );
1259 if ( msg
. empty () == true )
1260 msg
= _ ( "Prepare for receiving solution" );
1261 Progress
-> SubProgress ( 100 , msg
, section
. FindI ( "Percentage" , 0 ));
1264 } else if ( section
. Exists ( "Error" ) == true ) {
1265 std :: string msg
= SubstVar ( SubstVar ( section
. FindS ( "Message" ), " \n . \n " , " \n\n " ), " \n " , " \n " );
1266 if ( msg
. empty () == true ) {
1267 msg
= _ ( "External planer failed without a proper error message" );
1268 _error
-> Error ( " %s " , msg
. c_str ());
1270 _error
-> Error ( "External planer failed with: %s " , msg
. substr ( 0 , msg
. find ( ' \n ' )). c_str ());
1271 if ( Progress
!= NULL
)
1273 std :: cerr
<< "The planer encountered an error of type: " << section
. FindS ( "Error" ) << std :: endl
;
1274 std :: cerr
<< "The following information might help you to understand what is wrong:" << std :: endl
;
1275 std :: cerr
<< msg
<< std :: endl
<< std :: endl
;
1277 } else if ( section
. Exists ( "Install" ) == true )
1279 else if ( section
. Exists ( "Configure" ) == true )
1281 else if ( section
. Exists ( "Remove" ) == true )
1284 char const * Start
, * End
;
1285 section
. GetSection ( Start
, End
);
1286 _error
-> Warning ( "Encountered an unexpected section with %d fields: %s " , section
. Count (), std :: string ( Start
, End
). c_str ());
1290 if ( type
== nullptr )
1292 size_t const id
= section
. FindULL ( type
, VersionCount
);
1293 if ( id
== VersionCount
) {
1294 _error
-> Warning ( "Unable to parse %s request with id value ' %s '!" , type
, section
. FindS ( type
). c_str ());
1296 } else if ( id
> PM
-> Cache
. Head (). VersionCount
) {
1297 _error
-> Warning ( "ID value ' %s ' in %s request stanza is to high to refer to a known version!" , section
. FindS ( type
). c_str (), type
);
1301 pkgCache :: VerIterator
Ver ( PM
-> Cache
. GetCache (), PM
-> Cache
. GetCache (). VerP
+ VerIdx
[ id
]);
1302 auto const Pkg
= Ver
. ParentPkg ();
1303 if ( strcmp ( type
, "Install" ) == 0 )
1304 PM
-> Install ( Pkg
, PM
-> FileNames
[ Pkg
-> ID
]);
1305 else if ( strcmp ( type
, "Configure" ) == 0 )
1307 else if ( strcmp ( type
, "Remove" ) == 0 )
1308 PM
-> Remove ( Pkg
, PM
-> Cache
[ Pkg
]. Purge ());
1313 bool EIPP :: ReadRequest ( int const input
, std :: list
< std :: pair
< std :: string
, PKG_ACTION
>> & actions
, /*{{{*/
1314 unsigned int & flags
)
1319 while ( ReadLine ( input
, line
) == true )
1321 // Skip empty lines before request
1322 if ( line
. empty () == true )
1324 // The first Tag must be a request, so search for it
1325 if ( line
. compare ( 0 , 8 , "Request:" ) != 0 )
1328 while ( ReadLine ( input
, line
) == true )
1330 // empty lines are the end of the request
1331 if ( line
. empty () == true )
1334 PKG_ACTION pkgact
= PKG_ACTION :: NOOP
;
1335 if ( LineStartsWithAndStrip ( line
, "Install:" ))
1336 pkgact
= PKG_ACTION :: INSTALL
;
1337 else if ( LineStartsWithAndStrip ( line
, "ReInstall:" ))
1338 pkgact
= PKG_ACTION :: REINSTALL
;
1339 else if ( LineStartsWithAndStrip ( line
, "Remove:" ))
1340 pkgact
= PKG_ACTION :: REMOVE
;
1341 else if ( LineStartsWithAndStrip ( line
, "Architecture:" ))
1342 _config
-> Set ( "APT::Architecture" , line
);
1343 else if ( LineStartsWithAndStrip ( line
, "Architectures:" ))
1344 _config
-> Set ( "APT::Architectures" , SubstVar ( line
, " " , "," ));
1345 else if ( LineStartsWithAndStrip ( line
, "Planer:" ))
1346 ; // purely informational line
1348 _error
-> Warning ( "Unknown line in EIPP Request stanza: %s " , line
. c_str ());
1350 if ( pkgact
== PKG_ACTION :: NOOP
)
1352 for ( auto && p
: VectorizeString ( line
, ' ' ))
1353 actions
. emplace_back ( std :: move ( p
), pkgact
);
1362 bool EIPP :: ApplyRequest ( std :: list
< std :: pair
< std :: string
, PKG_ACTION
>> & actions
, /*{{{*/
1365 for ( auto Pkg
= Cache
. PkgBegin (); Pkg
. end () == false ; ++ Pkg
)
1368 for ( auto Ver
= Pkg
. VersionList (); Ver
. end () == false ; ++ Ver
)
1371 if ( Pkg
. CurrentVer () == Ver
)
1373 Cache
. SetCandidateVersion ( Ver
);
1375 if ( unlikely ( versions
> 2 ))
1376 _error
-> Warning ( "Package %s has %d versions, but should have at most 2!" , Pkg
. FullName (). c_str (), versions
);
1378 for ( auto && a
: actions
)
1380 pkgCache :: PkgIterator P
= Cache
. FindPkg ( a
. first
);
1381 if ( P
. end () == true )
1383 _error
-> Warning ( "Package %s is not known, so can't be acted on" , a
. first
. c_str ());
1388 case PKG_ACTION :: NOOP
:
1389 _error
-> Warning ( "Package %s has NOOP as action?!?" , a
. first
. c_str ());
1391 case PKG_ACTION :: INSTALL
:
1392 Cache
. MarkInstall ( P
, false );
1394 case PKG_ACTION :: REINSTALL
:
1395 Cache
. MarkInstall ( P
, false );
1396 Cache
. SetReInstall ( P
, true );
1398 case PKG_ACTION :: REMOVE
:
1399 Cache
. MarkDelete ( P
);