]> git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
ebf67b5ee9b184686726cff85be91cac89b523b9
[apt.git] / apt-pkg / packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: packagemanager.cc,v 1.1 1998/07/07 04:17:01 jgg Exp $
4 /* ######################################################################
5
6 Package Manager - Abstacts the package manager
7
8 More work is needed in the area of transitioning provides, ie exim
9 replacing smail. This can cause interesing side effects.
10
11 Other cases involving conflicts+replaces should be tested.
12
13 ##################################################################### */
14 /*}}}*/
15 // Include Files /*{{{*/
16 #ifdef __GNUG__
17 #pragma implementation "pkglib/packagemanager.h"
18 #endif
19 #include <pkglib/packagemanager.h>
20 #include <pkglib/orderlist.h>
21 #include <pkglib/depcache.h>
22 #include <pkglib/sourcelist.h>
23 #include <pkglib/aquire.h>
24 #include <pkglib/controlcache.h>
25 #include <pkglib/error.h>
26 #include <options.h>
27 /*}}}*/
28
29 // PM::PackageManager - Constructor /*{{{*/
30 // ---------------------------------------------------------------------
31 /* */
32 pkgPackageManager::pkgPackageManager(pkgDepCache &Cache) : Cache(Cache)
33 {
34 FileNames = new string[Cache.Head().PackageCount];
35 List = 0;
36 }
37 /*}}}*/
38 // PM::PackageManager - Destructor /*{{{*/
39 // ---------------------------------------------------------------------
40 /* */
41 pkgPackageManager::~pkgPackageManager()
42 {
43 delete List;
44 delete [] FileNames;
45 }
46 /*}}}*/
47 // PM::GetArchives - Puts the install archives in the aquire list /*{{{*/
48 // ---------------------------------------------------------------------
49 /* The Source list is used to convert the packages to install into
50 URNs which are then passed to Aquire to convert to FileNames. */
51 bool pkgPackageManager::GetArchives(pkgSourceList &List,pkgAquire &Engine)
52 {
53 pkgControlCache CCache(Cache);
54 if (_error->PendingError() == true)
55 return false;
56
57 Engine.OutputDir(PKG_DEB_CA_ARCHIVES);
58 pkgCache::PkgIterator I = Cache.PkgBegin();
59 for (;I.end() != true; I++)
60 {
61 // Not interesting
62 if ((Cache[I].InstallVer == (pkgCache::Version *)I.CurrentVer() &&
63 I.State() != pkgCache::PkgIterator::NeedsUnpack) ||
64 Cache[I].Delete() == true)
65 continue;
66
67 // Cross match with the source list.
68 pkgSourceList::const_iterator Dist = List.MatchPkgFile(Cache[I].InstVerIter(Cache));
69 if (Dist == List.end())
70 {
71 _error->Warning("Couldn't locate an archive source for package %s",I.Name());
72 continue;
73 }
74
75 // Read in the info record
76 pkgSPkgCtrlInfo Inf = CCache[Cache[I].InstVerIter(Cache)];
77 if (Inf.isNull() == true)
78 {
79 _error->Warning("Couldn't locate info for package %s",I.Name());
80 continue;
81 }
82
83 // Isolate the filename
84 string File = Inf->Find("Filename")->Value();
85 if (File.empty() == true)
86 {
87 _error->Warning("Couldn't locate an archive for package %s",I.Name());
88 continue;
89 }
90
91 // Generate the get request.
92 string URI = Dist->ArchiveURI(File);
93
94 // Output file
95 unsigned int Pos = File.rfind('/');
96 if (Pos == File.length())
97 return _error->Error("Malformed file line in package %s",I.Name());
98
99 // Null pos isnt used in present package files
100 if (Pos == string::npos)
101 Pos = 0;
102 else
103 Pos++;
104
105 if (Engine.Get(URI,string(File,Pos),
106 Dist->ArchiveInfo(Cache[I].InstVerIter(Cache)),
107 Cache[I].InstVerIter(Cache)->Size,
108 Inf->Find("MD5sum")->Value(),
109 &FileNames[I->ID]) == false)
110 return false;
111 }
112
113 return true;
114 }
115 /*}}}*/
116 // PM::FixMissing - Keep all missing packages /*{{{*/
117 // ---------------------------------------------------------------------
118 /* This is called to correct the installation when packages could not
119 be downloaded. */
120 bool pkgPackageManager::FixMissing()
121 {
122 unsigned char *Touch = new unsigned char[Cache.Head().PackageCount];
123 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
124 {
125 // Create the status list that ResolveConflicts needs
126 if ((Cache[I].DepState & pkgDepCache::DepNowMin) == pkgDepCache::DepNowMin)
127 Touch[I->ID] = (1 << 0) | (1 << 1);
128 else
129 Touch[I->ID] = 1 << 1;
130
131 if (Cache[I].Keep() == true)
132 continue;
133 if (FileNames[I->ID].empty() == false || Cache[I].Delete() == true)
134 continue;
135 Cache.MarkKeep(I);
136 }
137
138 // Now downgrade everything that is broken
139 Cache.ResolveConflicts(Touch);
140 delete [] Touch;
141
142 return Cache.BrokenCount() == 0;
143 }
144 /*}}}*/
145
146 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
147 // ---------------------------------------------------------------------
148 /* The restriction on provides is to eliminate the case when provides
149 are transitioning between valid states [ie exim to smail] */
150 bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
151 {
152 if (D.TargetPkg()->ProvidesList != 0)
153 return false;
154
155 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
156 (Cache[D] & pkgDepCache::DepNow) != 0)
157 return true;
158 return false;
159 }
160 /*}}}*/
161 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
162 // ---------------------------------------------------------------------
163 /* This looks over the reverses for a conflicts line that needs early
164 removal. */
165 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
166 const char *Ver)
167 {
168 for (;D.end() == false; D++)
169 {
170 if (D->Type != pkgDEP_Conflicts)
171 continue;
172
173 if (D.ParentPkg() == Pkg)
174 continue;
175
176 if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
177 continue;
178
179 if (List->IsNow(Pkg) == false)
180 continue;
181
182 if (EarlyRemove(D.ParentPkg()) == false)
183 return false;
184 }
185 return true;
186 }
187 /*}}}*/
188 // PM::ConfigureAll - Run the all out configuration /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This configures every package. It is assumed they are all unpacked and
191 that the final configuration is valid. */
192 bool pkgPackageManager::ConfigureAll()
193 {
194 pkgOrderList OList(Cache);
195
196 // Populate the order list
197 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
198 if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
199 pkgOrderList::UnPacked) == true)
200 OList.push_back(*I);
201
202 if (OList.OrderConfigure() == false)
203 return false;
204
205 // Perform the configuring
206 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
207 {
208 PkgIterator Pkg(Cache,*I);
209
210 if (Configure(Pkg) == false)
211 return false;
212
213 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
214 }
215
216 return true;
217 }
218 /*}}}*/
219 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
220 // ---------------------------------------------------------------------
221 /* This routine scheduals the configuration of the given package and all
222 of it's dependents. */
223 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
224 {
225 pkgOrderList OList(Cache);
226
227 if (DepAdd(OList,Pkg) == false)
228 return false;
229
230 if (OList.OrderConfigure() == false)
231 return false;
232
233 // Perform the configuring
234 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
235 {
236 PkgIterator Pkg(Cache,*I);
237
238 if (Configure(Pkg) == false)
239 return false;
240
241 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
242 }
243
244 // Sanity Check
245 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
246 return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
247
248 return true;
249 }
250 /*}}}*/
251 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
252 // ---------------------------------------------------------------------
253 /* This recursively adds all dependents to the order list */
254 bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
255 {
256 if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
257 return true;
258 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
259 return true;
260 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
261 return false;
262
263
264 // Put the package on the list
265 OList.push_back(Pkg);
266 OList.Flag(Pkg,pkgOrderList::Added);
267 Depth++;
268
269 // Check the dependencies to see if they are all satisfied.
270 bool Bad = false;
271 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
272 {
273 if (D->Type != pkgDEP_Depends && D->Type != pkgDEP_PreDepends)
274 {
275 D++;
276 continue;
277 }
278
279 // Grok or groups
280 Bad = true;
281 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
282 {
283 LastOR = (D->CompareOp & pkgOP_OR) == pkgOP_OR;
284
285 if (Bad == false)
286 continue;
287
288 Version **VList = D.AllTargets();
289 for (Version **I = VList; *I != 0 && Bad == true; I++)
290 {
291 VerIterator Ver(Cache,*I);
292 PkgIterator Pkg = Ver.ParentPkg();
293
294 // See if the current version is ok
295 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
296 Pkg.State() == PkgIterator::NeedsNothing)
297 {
298 Bad = false;
299 continue;
300 }
301
302 // Not the install version
303 if (Cache[Pkg].InstallVer != *I ||
304 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
305 continue;
306 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
307 Bad = !DepAdd(OList,Pkg,Depth);
308 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
309 Bad = false;
310 }
311 delete [] VList;
312 }
313
314 if (Bad == true)
315 {
316 OList.Flag(Pkg,0,pkgOrderList::Added);
317 OList.pop_back();
318 Depth--;
319 return false;
320 }
321 }
322
323 Depth--;
324 return true;
325 }
326 /*}}}*/
327 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
328 // ---------------------------------------------------------------------
329 /* This is called to deal with conflicts arising from unpacking */
330 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
331 {
332 if (List->IsNow(Pkg) == false)
333 return true;
334
335 // Already removed it
336 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
337 return true;
338
339 // Woops, it will not be re-installed!
340 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
341 return false;
342
343 bool Res = SmartRemove(Pkg);
344 if (Cache[Pkg].Delete() == false)
345 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
346
347 return Res;
348 }
349 /*}}}*/
350 // PM::SmartRemove - Removal Helper /*{{{*/
351 // ---------------------------------------------------------------------
352 /* */
353 bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
354 {
355 if (List->IsNow(Pkg) == false)
356 return true;
357
358 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
359 return Remove(Pkg);
360 }
361 /*}}}*/
362 // PM::SmartUnPack - Install helper /*{{{*/
363 // ---------------------------------------------------------------------
364 /* This performs the task of handling pre-depends. */
365 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
366 {
367 // Check if it is already unpacked
368 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
369 Cache[Pkg].Keep() == true)
370 {
371 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
372 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
373 if (SmartConfigure(Pkg) == false)
374 return _error->Error("Internal Error, Could not perform immediate configuraton");
375 return true;
376 }
377
378 /* See if this packages install version has any predependencies
379 that are not met by 'now' packages. */
380 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
381 D.end() == false; D++)
382 {
383 if (D->Type == pkgDEP_PreDepends)
384 {
385 // Look for possible ok targets.
386 Version **VList = D.AllTargets();
387 bool Bad = true;
388 for (Version **I = VList; *I != 0 && Bad == true; I++)
389 {
390 VerIterator Ver(Cache,*I);
391 PkgIterator Pkg = Ver.ParentPkg();
392
393 // See if the current version is ok
394 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
395 Pkg.State() == PkgIterator::NeedsNothing)
396 {
397 Bad = false;
398 continue;
399 }
400 }
401
402 // Look for something that could be configured.
403 for (Version **I = VList; *I != 0 && Bad == true; I++)
404 {
405 VerIterator Ver(Cache,*I);
406 PkgIterator Pkg = Ver.ParentPkg();
407
408 // Not the install version
409 if (Cache[Pkg].InstallVer != *I ||
410 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
411 continue;
412
413 Bad = !SmartConfigure(Pkg);
414 }
415
416 delete [] VList;
417
418 if (Bad == true)
419 return _error->Error("Internal Error, Couldn't configure a pre-depend");
420
421 continue;
422 }
423
424 if (D->Type == pkgDEP_Conflicts)
425 {
426 /* Look for conflicts. Two packages that are both in the install
427 state cannot conflict so we don't check.. */
428 Version **VList = D.AllTargets();
429 for (Version **I = VList; *I != 0; I++)
430 {
431 VerIterator Ver(Cache,*I);
432 PkgIterator Pkg = Ver.ParentPkg();
433
434 // See if the current version is conflicting
435 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
436 {
437 if (EarlyRemove(Pkg) == false)
438 return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
439 }
440 }
441 delete [] VList;
442 }
443 }
444
445 // Check for reverse conflicts.
446 CheckRConflicts(Pkg,Pkg.RevDependsList(),
447 Cache[Pkg].InstVerIter(Cache).VerStr());
448 for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
449 P.end() == false; P++)
450 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
451
452 if (Install(Pkg,FileNames[Pkg->ID]) == false)
453 return false;
454
455 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
456
457 // Perform immedate configuration of the package.
458 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
459 if (SmartConfigure(Pkg) == false)
460 return _error->Error("Internal Error, Could not perform immediate configuraton");
461
462 return true;
463 }
464 /*}}}*/
465 // PM::OrderInstall - Installation ordering routine /*{{{*/
466 // ---------------------------------------------------------------------
467 /* */
468 bool pkgPackageManager::OrderInstall()
469 {
470 delete List;
471 List = new pkgOrderList(Cache);
472
473 // Generate the list of affected packages and sort it
474 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
475 {
476 // Consider all depends
477 if ((I->Flags & pkgFLAG_Essential) == pkgFLAG_Essential)
478 {
479 List->Flag(I,pkgOrderList::Immediate);
480 if (Cache[I].InstallVer != 0)
481 for (DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
482 D.end() == false; D++)
483 if (D->Type == pkgDEP_Depends || D->Type == pkgDEP_PreDepends)
484 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
485 if (I->CurrentVer != 0)
486 for (DepIterator D = I.CurrentVer().DependsList();
487 D.end() == false; D++)
488 if (D->Type == pkgDEP_Depends || D->Type == pkgDEP_PreDepends)
489 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
490 }
491
492 // Not interesting
493 if ((Cache[I].Keep() == true ||
494 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
495 I.State() == pkgCache::PkgIterator::NeedsNothing)
496 continue;
497
498 // Append it to the list
499 List->push_back(I);
500
501 if ((I->Flags & pkgFLAG_ImmediateConf) == pkgFLAG_ImmediateConf)
502 List->Flag(I,pkgOrderList::Immediate);
503 }
504
505 if (List->OrderUnpack() == false)
506 return _error->Error("Internal ordering error");
507
508 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
509 {
510 PkgIterator Pkg(Cache,*I);
511
512 // Sanity check
513 if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
514 return _error->Error("Internal Error, trying to manipulate a kept package");
515
516 // Perform a delete or an install
517 if (Cache[Pkg].Delete() == true)
518 {
519 if (SmartRemove(Pkg) == false)
520 return false;
521 }
522 else
523 if (SmartUnPack(Pkg) == false)
524 return false;
525 }
526
527 // Final run through the configure phase
528 if (ConfigureAll() == false)
529 return false;
530
531 // Sanity check
532 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
533 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
534 return _error->Error("Internal error, packages left unconfigured. %s",
535 PkgIterator(Cache,*I).Name());
536
537 return true;
538 }
539 /*}}}*/
540 // PM::DoInstall - Does the installation /*{{{*/
541 // ---------------------------------------------------------------------
542 /* This uses the filenames in FileNames and the information in the
543 DepCache to perform the installation of packages.*/
544 bool pkgPackageManager::DoInstall()
545 {
546 return OrderInstall() && Go();
547 }
548 /*}}}*/