]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: apt-get.cc,v 1.72 1999/08/04 05:37:18 jgg Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | apt-get - Cover for dpkg | |
7 | ||
8 | This is an allout cover for dpkg implementing a safer front end. It is | |
9 | based largely on libapt-pkg. | |
10 | ||
11 | The syntax is different, | |
12 | apt-get [opt] command [things] | |
13 | Where command is: | |
14 | update - Resyncronize the package files from their sources | |
15 | upgrade - Smart-Download the newest versions of all packages | |
16 | dselect-upgrade - Follows dselect's changes to the Status: field | |
17 | and installes new and removes old packages | |
18 | dist-upgrade - Powerfull upgrader designed to handle the issues with | |
19 | a new distribution. | |
20 | install - Download and install a given package (by name, not by .deb) | |
21 | check - Update the package cache and check for broken packages | |
22 | clean - Erase the .debs downloaded to /var/cache/apt/archives and | |
23 | the partial dir too | |
24 | ||
25 | ##################################################################### */ | |
26 | /*}}}*/ | |
27 | // Include Files /*{{{*/ | |
28 | #include <apt-pkg/error.h> | |
29 | #include <apt-pkg/cmndline.h> | |
30 | #include <apt-pkg/init.h> | |
31 | #include <apt-pkg/depcache.h> | |
32 | #include <apt-pkg/sourcelist.h> | |
33 | #include <apt-pkg/algorithms.h> | |
34 | #include <apt-pkg/acquire-item.h> | |
35 | #include <apt-pkg/dpkgpm.h> | |
36 | #include <apt-pkg/strutl.h> | |
37 | #include <apt-pkg/clean.h> | |
38 | #include <apt-pkg/srcrecords.h> | |
39 | #include <apt-pkg/version.h> | |
40 | #include <apt-pkg/cachefile.h> | |
41 | ||
42 | #include <config.h> | |
43 | ||
44 | #include "acqprogress.h" | |
45 | ||
46 | #include <fstream.h> | |
47 | #include <termios.h> | |
48 | #include <sys/ioctl.h> | |
49 | #include <sys/stat.h> | |
50 | #include <sys/vfs.h> | |
51 | #include <signal.h> | |
52 | #include <unistd.h> | |
53 | #include <stdio.h> | |
54 | #include <errno.h> | |
55 | #include <sys/wait.h> | |
56 | /*}}}*/ | |
57 | ||
58 | ostream c0out; | |
59 | ostream c1out; | |
60 | ostream c2out; | |
61 | ofstream devnull("/dev/null"); | |
62 | unsigned int ScreenWidth = 80; | |
63 | ||
64 | // class CacheFile - Cover class for some dependency cache functions /*{{{*/ | |
65 | // --------------------------------------------------------------------- | |
66 | /* */ | |
67 | class CacheFile : public pkgCacheFile | |
68 | { | |
69 | static pkgCache *SortCache; | |
70 | static int NameComp(const void *a,const void *b); | |
71 | ||
72 | public: | |
73 | pkgCache::Package **List; | |
74 | ||
75 | void Sort(); | |
76 | bool CheckDeps(bool AllowBroken = false); | |
77 | bool Open(bool WithLock = true) | |
78 | { | |
79 | OpTextProgress Prog(*_config); | |
80 | if (pkgCacheFile::Open(Prog,WithLock) == false) | |
81 | return false; | |
82 | Sort(); | |
83 | return true; | |
84 | }; | |
85 | CacheFile() : List(0) {}; | |
86 | }; | |
87 | /*}}}*/ | |
88 | ||
89 | // YnPrompt - Yes No Prompt. /*{{{*/ | |
90 | // --------------------------------------------------------------------- | |
91 | /* Returns true on a Yes.*/ | |
92 | bool YnPrompt() | |
93 | { | |
94 | if (_config->FindB("APT::Get::Assume-Yes",false) == true) | |
95 | { | |
96 | c1out << 'Y' << endl; | |
97 | return true; | |
98 | } | |
99 | ||
100 | char C = 0; | |
101 | char Jnk = 0; | |
102 | read(STDIN_FILENO,&C,1); | |
103 | while (C != '\n' && Jnk != '\n') read(STDIN_FILENO,&Jnk,1); | |
104 | ||
105 | if (!(C == 'Y' || C == 'y' || C == '\n' || C == '\r')) | |
106 | return false; | |
107 | return true; | |
108 | } | |
109 | /*}}}*/ | |
110 | // AnalPrompt - Annoying Yes No Prompt. /*{{{*/ | |
111 | // --------------------------------------------------------------------- | |
112 | /* Returns true on a Yes.*/ | |
113 | bool AnalPrompt(const char *Text) | |
114 | { | |
115 | char Buf[1024]; | |
116 | cin.getline(Buf,sizeof(Buf)); | |
117 | if (strcmp(Buf,Text) == 0) | |
118 | return true; | |
119 | return false; | |
120 | } | |
121 | /*}}}*/ | |
122 | // ShowList - Show a list /*{{{*/ | |
123 | // --------------------------------------------------------------------- | |
124 | /* This prints out a string of space seperated words with a title and | |
125 | a two space indent line wraped to the current screen width. */ | |
126 | bool ShowList(ostream &out,string Title,string List) | |
127 | { | |
128 | if (List.empty() == true) | |
129 | return true; | |
130 | ||
131 | // Acount for the leading space | |
132 | int ScreenWidth = ::ScreenWidth - 3; | |
133 | ||
134 | out << Title << endl; | |
135 | string::size_type Start = 0; | |
136 | while (Start < List.size()) | |
137 | { | |
138 | string::size_type End; | |
139 | if (Start + ScreenWidth >= List.size()) | |
140 | End = List.size(); | |
141 | else | |
142 | End = List.rfind(' ',Start+ScreenWidth); | |
143 | ||
144 | if (End == string::npos || End < Start) | |
145 | End = Start + ScreenWidth; | |
146 | out << " " << string(List,Start,End - Start) << endl; | |
147 | Start = End + 1; | |
148 | } | |
149 | return false; | |
150 | } | |
151 | /*}}}*/ | |
152 | // ShowBroken - Debugging aide /*{{{*/ | |
153 | // --------------------------------------------------------------------- | |
154 | /* This prints out the names of all the packages that are broken along | |
155 | with the name of each each broken dependency and a quite version | |
156 | description. */ | |
157 | void ShowBroken(ostream &out,CacheFile &Cache) | |
158 | { | |
159 | out << "Sorry, but the following packages have unmet dependencies:" << endl; | |
160 | for (unsigned J = 0; J < Cache->Head().PackageCount; J++) | |
161 | { | |
162 | pkgCache::PkgIterator I(Cache,Cache.List[J]); | |
163 | ||
164 | if (Cache[I].InstBroken() == false) | |
165 | continue; | |
166 | ||
167 | // Print out each package and the failed dependencies | |
168 | out <<" " << I.Name() << ":"; | |
169 | int Indent = strlen(I.Name()) + 3; | |
170 | bool First = true; | |
171 | if (Cache[I].InstVerIter(Cache).end() == true) | |
172 | { | |
173 | cout << endl; | |
174 | continue; | |
175 | } | |
176 | ||
177 | for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;) | |
178 | { | |
179 | // Compute a single dependency element (glob or) | |
180 | pkgCache::DepIterator Start; | |
181 | pkgCache::DepIterator End; | |
182 | D.GlobOr(Start,End); | |
183 | ||
184 | if (Cache->IsImportantDep(End) == false || | |
185 | (Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall) | |
186 | continue; | |
187 | ||
188 | if (First == false) | |
189 | for (int J = 0; J != Indent; J++) | |
190 | out << ' '; | |
191 | First = false; | |
192 | ||
193 | out << ' ' << End.DepType() << ": " << End.TargetPkg().Name(); | |
194 | ||
195 | // Show a quick summary of the version requirements | |
196 | if (End.TargetVer() != 0) | |
197 | out << " (" << End.CompType() << " " << End.TargetVer() << | |
198 | ")"; | |
199 | ||
200 | /* Show a summary of the target package if possible. In the case | |
201 | of virtual packages we show nothing */ | |
202 | ||
203 | pkgCache::PkgIterator Targ = End.TargetPkg(); | |
204 | if (Targ->ProvidesList == 0) | |
205 | { | |
206 | out << " but "; | |
207 | pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache); | |
208 | if (Ver.end() == false) | |
209 | out << Ver.VerStr() << " is installed"; | |
210 | else | |
211 | { | |
212 | if (Cache[Targ].CandidateVerIter(Cache).end() == true) | |
213 | { | |
214 | if (Targ->ProvidesList == 0) | |
215 | out << "it is not installable"; | |
216 | else | |
217 | out << "it is a virtual package"; | |
218 | } | |
219 | else | |
220 | out << "it is not installed"; | |
221 | } | |
222 | } | |
223 | ||
224 | out << endl; | |
225 | } | |
226 | } | |
227 | } | |
228 | /*}}}*/ | |
229 | // ShowNew - Show packages to newly install /*{{{*/ | |
230 | // --------------------------------------------------------------------- | |
231 | /* */ | |
232 | void ShowNew(ostream &out,CacheFile &Cache) | |
233 | { | |
234 | /* Print out a list of packages that are going to be removed extra | |
235 | to what the user asked */ | |
236 | string List; | |
237 | for (unsigned J = 0; J < Cache->Head().PackageCount; J++) | |
238 | { | |
239 | pkgCache::PkgIterator I(Cache,Cache.List[J]); | |
240 | if (Cache[I].NewInstall() == true) | |
241 | List += string(I.Name()) + " "; | |
242 | } | |
243 | ||
244 | ShowList(out,"The following NEW packages will be installed:",List); | |
245 | } | |
246 | /*}}}*/ | |
247 | // ShowDel - Show packages to delete /*{{{*/ | |
248 | // --------------------------------------------------------------------- | |
249 | /* */ | |
250 | void ShowDel(ostream &out,CacheFile &Cache) | |
251 | { | |
252 | /* Print out a list of packages that are going to be removed extra | |
253 | to what the user asked */ | |
254 | string List; | |
255 | for (unsigned J = 0; J < Cache->Head().PackageCount; J++) | |
256 | { | |
257 | pkgCache::PkgIterator I(Cache,Cache.List[J]); | |
258 | if (Cache[I].Delete() == true) | |
259 | { | |
260 | if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge) | |
261 | List += string(I.Name()) + "* "; | |
262 | else | |
263 | List += string(I.Name()) + " "; | |
264 | } | |
265 | } | |
266 | ||
267 | ShowList(out,"The following packages will be REMOVED:",List); | |
268 | } | |
269 | /*}}}*/ | |
270 | // ShowKept - Show kept packages /*{{{*/ | |
271 | // --------------------------------------------------------------------- | |
272 | /* */ | |
273 | void ShowKept(ostream &out,pkgDepCache &Dep) | |
274 | { | |
275 | pkgCache::PkgIterator I = Dep.PkgBegin(); | |
276 | string List; | |
277 | for (;I.end() != true; I++) | |
278 | { | |
279 | // Not interesting | |
280 | if (Dep[I].Upgrade() == true || Dep[I].Upgradable() == false || | |
281 | I->CurrentVer == 0 || Dep[I].Delete() == true) | |
282 | continue; | |
283 | ||
284 | List += string(I.Name()) + " "; | |
285 | } | |
286 | ShowList(out,"The following packages have been kept back",List); | |
287 | } | |
288 | /*}}}*/ | |
289 | // ShowUpgraded - Show upgraded packages /*{{{*/ | |
290 | // --------------------------------------------------------------------- | |
291 | /* */ | |
292 | void ShowUpgraded(ostream &out,CacheFile &Cache) | |
293 | { | |
294 | string List; | |
295 | for (unsigned J = 0; J < Cache->Head().PackageCount; J++) | |
296 | { | |
297 | pkgCache::PkgIterator I(Cache,Cache.List[J]); | |
298 | ||
299 | // Not interesting | |
300 | if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true) | |
301 | continue; | |
302 | ||
303 | List += string(I.Name()) + " "; | |
304 | } | |
305 | ShowList(out,"The following packages will be upgraded",List); | |
306 | } | |
307 | /*}}}*/ | |
308 | // ShowHold - Show held but changed packages /*{{{*/ | |
309 | // --------------------------------------------------------------------- | |
310 | /* */ | |
311 | bool ShowHold(ostream &out,CacheFile &Cache) | |
312 | { | |
313 | string List; | |
314 | for (unsigned J = 0; J < Cache->Head().PackageCount; J++) | |
315 | { | |
316 | pkgCache::PkgIterator I(Cache,Cache.List[J]); | |
317 | if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() && | |
318 | I->SelectedState == pkgCache::State::Hold) | |
319 | List += string(I.Name()) + " "; | |
320 | } | |
321 | ||
322 | return ShowList(out,"The following held packages will be changed:",List); | |
323 | } | |
324 | /*}}}*/ | |
325 | // ShowEssential - Show an essential package warning /*{{{*/ | |
326 | // --------------------------------------------------------------------- | |
327 | /* This prints out a warning message that is not to be ignored. It shows | |
328 | all essential packages and their dependents that are to be removed. | |
329 | It is insanely risky to remove the dependents of an essential package! */ | |
330 | bool ShowEssential(ostream &out,CacheFile &Cache) | |
331 | { | |
332 | string List; | |
333 | bool *Added = new bool[Cache->HeaderP->PackageCount]; | |
334 | for (unsigned int I = 0; I != Cache->HeaderP->PackageCount; I++) | |
335 | Added[I] = false; | |
336 | ||
337 | for (unsigned J = 0; J < Cache->Head().PackageCount; J++) | |
338 | { | |
339 | pkgCache::PkgIterator I(Cache,Cache.List[J]); | |
340 | if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential) | |
341 | continue; | |
342 | ||
343 | // The essential package is being removed | |
344 | if (Cache[I].Delete() == true) | |
345 | { | |
346 | if (Added[I->ID] == false) | |
347 | { | |
348 | Added[I->ID] = true; | |
349 | List += string(I.Name()) + " "; | |
350 | } | |
351 | } | |
352 | ||
353 | if (I->CurrentVer == 0) | |
354 | continue; | |
355 | ||
356 | // Print out any essential package depenendents that are to be removed | |
357 | for (pkgDepCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++) | |
358 | { | |
359 | // Skip everything but depends | |
360 | if (D->Type != pkgCache::Dep::PreDepends && | |
361 | D->Type != pkgCache::Dep::Depends) | |
362 | continue; | |
363 | ||
364 | pkgCache::PkgIterator P = D.SmartTargetPkg(); | |
365 | if (Cache[P].Delete() == true) | |
366 | { | |
367 | if (Added[P->ID] == true) | |
368 | continue; | |
369 | Added[P->ID] = true; | |
370 | ||
371 | char S[300]; | |
372 | sprintf(S,"%s (due to %s) ",P.Name(),I.Name()); | |
373 | List += S; | |
374 | } | |
375 | } | |
376 | } | |
377 | ||
378 | delete [] Added; | |
379 | if (List.empty() == false) | |
380 | out << "WARNING: The following essential packages will be removed" << endl; | |
381 | return ShowList(out,"This should NOT be done unless you know exactly what you are doing!",List); | |
382 | } | |
383 | /*}}}*/ | |
384 | // Stats - Show some statistics /*{{{*/ | |
385 | // --------------------------------------------------------------------- | |
386 | /* */ | |
387 | void Stats(ostream &out,pkgDepCache &Dep) | |
388 | { | |
389 | unsigned long Upgrade = 0; | |
390 | unsigned long Install = 0; | |
391 | for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++) | |
392 | { | |
393 | if (Dep[I].NewInstall() == true) | |
394 | Install++; | |
395 | else | |
396 | if (Dep[I].Upgrade() == true) | |
397 | Upgrade++; | |
398 | } | |
399 | ||
400 | out << Upgrade << " packages upgraded, " << | |
401 | Install << " newly installed, " << | |
402 | Dep.DelCount() << " to remove and " << | |
403 | Dep.KeepCount() << " not upgraded." << endl; | |
404 | ||
405 | if (Dep.BadCount() != 0) | |
406 | out << Dep.BadCount() << " packages not fully installed or removed." << endl; | |
407 | } | |
408 | /*}}}*/ | |
409 | ||
410 | // CacheFile::NameComp - QSort compare by name /*{{{*/ | |
411 | // --------------------------------------------------------------------- | |
412 | /* */ | |
413 | pkgCache *CacheFile::SortCache = 0; | |
414 | int CacheFile::NameComp(const void *a,const void *b) | |
415 | { | |
416 | if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0) | |
417 | return *(pkgCache::Package **)a - *(pkgCache::Package **)b; | |
418 | ||
419 | const pkgCache::Package &A = **(pkgCache::Package **)a; | |
420 | const pkgCache::Package &B = **(pkgCache::Package **)b; | |
421 | ||
422 | return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name); | |
423 | } | |
424 | /*}}}*/ | |
425 | // CacheFile::Sort - Sort by name /*{{{*/ | |
426 | // --------------------------------------------------------------------- | |
427 | /* */ | |
428 | void CacheFile::Sort() | |
429 | { | |
430 | delete [] List; | |
431 | List = new pkgCache::Package *[Cache->Head().PackageCount]; | |
432 | memset(List,0,sizeof(*List)*Cache->Head().PackageCount); | |
433 | pkgCache::PkgIterator I = Cache->PkgBegin(); | |
434 | for (;I.end() != true; I++) | |
435 | List[I->ID] = I; | |
436 | ||
437 | SortCache = *this; | |
438 | qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp); | |
439 | } | |
440 | /*}}}*/ | |
441 | // CacheFile::Open - Open the cache file /*{{{*/ | |
442 | // --------------------------------------------------------------------- | |
443 | /* This routine generates the caches and then opens the dependency cache | |
444 | and verifies that the system is OK. */ | |
445 | bool CacheFile::CheckDeps(bool AllowBroken) | |
446 | { | |
447 | if (_error->PendingError() == true) | |
448 | return false; | |
449 | ||
450 | // Check that the system is OK | |
451 | if (Cache->DelCount() != 0 || Cache->InstCount() != 0) | |
452 | return _error->Error("Internal Error, non-zero counts"); | |
453 | ||
454 | // Apply corrections for half-installed packages | |
455 | if (pkgApplyStatus(*Cache) == false) | |
456 | return false; | |
457 | ||
458 | // Nothing is broken | |
459 | if (Cache->BrokenCount() == 0 || AllowBroken == true) | |
460 | return true; | |
461 | ||
462 | // Attempt to fix broken things | |
463 | if (_config->FindB("APT::Get::Fix-Broken",false) == true) | |
464 | { | |
465 | c1out << "Correcting dependencies..." << flush; | |
466 | if (pkgFixBroken(*Cache) == false || Cache->BrokenCount() != 0) | |
467 | { | |
468 | c1out << " failed." << endl; | |
469 | ShowBroken(c1out,*this); | |
470 | ||
471 | return _error->Error("Unable to correct dependencies"); | |
472 | } | |
473 | if (pkgMinimizeUpgrade(*Cache) == false) | |
474 | return _error->Error("Unable to minimize the upgrade set"); | |
475 | ||
476 | c1out << " Done" << endl; | |
477 | } | |
478 | else | |
479 | { | |
480 | c1out << "You might want to run `apt-get -f install' to correct these." << endl; | |
481 | ShowBroken(c1out,*this); | |
482 | ||
483 | return _error->Error("Unmet dependencies. Try using -f."); | |
484 | } | |
485 | ||
486 | return true; | |
487 | } | |
488 | /*}}}*/ | |
489 | ||
490 | // InstallPackages - Actually download and install the packages /*{{{*/ | |
491 | // --------------------------------------------------------------------- | |
492 | /* This displays the informative messages describing what is going to | |
493 | happen and then calls the download routines */ | |
494 | bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,bool Saftey = true) | |
495 | { | |
496 | if (_config->FindB("APT::Get::Purge",false) == true) | |
497 | { | |
498 | pkgCache::PkgIterator I = Cache->PkgBegin(); | |
499 | for (; I.end() == false; I++) | |
500 | { | |
501 | if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete) | |
502 | Cache->MarkDelete(I,true); | |
503 | } | |
504 | } | |
505 | ||
506 | bool Fail = false; | |
507 | bool Essential = false; | |
508 | ||
509 | // Show all the various warning indicators | |
510 | ShowDel(c1out,Cache); | |
511 | ShowNew(c1out,Cache); | |
512 | if (ShwKept == true) | |
513 | ShowKept(c1out,Cache); | |
514 | Fail |= !ShowHold(c1out,Cache); | |
515 | if (_config->FindB("APT::Get::Show-Upgraded",false) == true) | |
516 | ShowUpgraded(c1out,Cache); | |
517 | Essential = !ShowEssential(c1out,Cache); | |
518 | Fail |= Essential; | |
519 | Stats(c1out,Cache); | |
520 | ||
521 | // Sanity check | |
522 | if (Cache->BrokenCount() != 0) | |
523 | { | |
524 | ShowBroken(c1out,Cache); | |
525 | return _error->Error("Internal Error, InstallPackages was called with broken packages!"); | |
526 | } | |
527 | ||
528 | if (Cache->DelCount() == 0 && Cache->InstCount() == 0 && | |
529 | Cache->BadCount() == 0) | |
530 | return true; | |
531 | ||
532 | // Run the simulator .. | |
533 | if (_config->FindB("APT::Get::Simulate") == true) | |
534 | { | |
535 | pkgSimulate PM(Cache); | |
536 | pkgPackageManager::OrderResult Res = PM.DoInstall(); | |
537 | if (Res == pkgPackageManager::Failed) | |
538 | return false; | |
539 | if (Res != pkgPackageManager::Completed) | |
540 | return _error->Error("Internal Error, Ordering didn't finish"); | |
541 | return true; | |
542 | } | |
543 | ||
544 | // Create the text record parser | |
545 | pkgRecords Recs(Cache); | |
546 | if (_error->PendingError() == true) | |
547 | return false; | |
548 | ||
549 | // Lock the archive directory | |
550 | FileFd Lock; | |
551 | if (_config->FindB("Debug::NoLocking",false) == false) | |
552 | { | |
553 | Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
554 | if (_error->PendingError() == true) | |
555 | return _error->Error("Unable to lock the download directory"); | |
556 | } | |
557 | ||
558 | // Create the download object | |
559 | AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0)); | |
560 | pkgAcquire Fetcher(&Stat); | |
561 | ||
562 | // Read the source list | |
563 | pkgSourceList List; | |
564 | if (List.ReadMainList() == false) | |
565 | return _error->Error("The list of sources could not be read."); | |
566 | ||
567 | // Create the package manager and prepare to download | |
568 | pkgDPkgPM PM(Cache); | |
569 | if (PM.GetArchives(&Fetcher,&List,&Recs) == false || | |
570 | _error->PendingError() == true) | |
571 | return false; | |
572 | ||
573 | // Display statistics | |
574 | unsigned long FetchBytes = Fetcher.FetchNeeded(); | |
575 | unsigned long FetchPBytes = Fetcher.PartialPresent(); | |
576 | unsigned long DebBytes = Fetcher.TotalNeeded(); | |
577 | if (DebBytes != Cache->DebSize()) | |
578 | { | |
579 | c0out << DebBytes << ',' << Cache->DebSize() << endl; | |
580 | c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl; | |
581 | } | |
582 | ||
583 | // Check for enough free space | |
584 | struct statfs Buf; | |
585 | string OutputDir = _config->FindDir("Dir::Cache::Archives"); | |
586 | if (statfs(OutputDir.c_str(),&Buf) != 0) | |
587 | return _error->Errno("statfs","Couldn't determine free space in %s", | |
588 | OutputDir.c_str()); | |
589 | if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize) | |
590 | return _error->Error("Sorry, you don't have enough free space in %s", | |
591 | OutputDir.c_str()); | |
592 | ||
593 | // Number of bytes | |
594 | c1out << "Need to get "; | |
595 | if (DebBytes != FetchBytes) | |
596 | c1out << SizeToStr(FetchBytes) << "B/" << SizeToStr(DebBytes) << 'B'; | |
597 | else | |
598 | c1out << SizeToStr(DebBytes) << 'B'; | |
599 | ||
600 | c1out << " of archives. After unpacking "; | |
601 | ||
602 | // Size delta | |
603 | if (Cache->UsrSize() >= 0) | |
604 | c1out << SizeToStr(Cache->UsrSize()) << "B will be used." << endl; | |
605 | else | |
606 | c1out << SizeToStr(-1*Cache->UsrSize()) << "B will be freed." << endl; | |
607 | ||
608 | if (_error->PendingError() == true) | |
609 | return false; | |
610 | ||
611 | // Fail safe check | |
612 | if (_config->FindI("quiet",0) >= 2 || | |
613 | _config->FindB("APT::Get::Assume-Yes",false) == true) | |
614 | { | |
615 | if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false) | |
616 | return _error->Error("There are problems and -y was used without --force-yes"); | |
617 | } | |
618 | ||
619 | if (Essential == true && Saftey == true) | |
620 | { | |
621 | c2out << "You are about to do something potentially harmful" << endl; | |
622 | c2out << "To continue type in the phrase 'Yes, I understand this may be bad'" << endl; | |
623 | c2out << " ?] " << flush; | |
624 | if (AnalPrompt("Yes, I understand this may be bad") == false) | |
625 | { | |
626 | c2out << "Abort." << endl; | |
627 | exit(1); | |
628 | } | |
629 | } | |
630 | else | |
631 | { | |
632 | // Prompt to continue | |
633 | if (Ask == true || Fail == true) | |
634 | { | |
635 | if (_config->FindI("quiet",0) < 2 && | |
636 | _config->FindB("APT::Get::Assume-Yes",false) == false) | |
637 | { | |
638 | c2out << "Do you want to continue? [Y/n] " << flush; | |
639 | ||
640 | if (YnPrompt() == false) | |
641 | { | |
642 | c2out << "Abort." << endl; | |
643 | exit(1); | |
644 | } | |
645 | } | |
646 | } | |
647 | } | |
648 | ||
649 | // Just print out the uris an exit if the --print-uris flag was used | |
650 | if (_config->FindB("APT::Get::Print-URIs") == true) | |
651 | { | |
652 | pkgAcquire::UriIterator I = Fetcher.UriBegin(); | |
653 | for (; I != Fetcher.UriEnd(); I++) | |
654 | cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' << | |
655 | I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl; | |
656 | return true; | |
657 | } | |
658 | ||
659 | // Run it | |
660 | while (1) | |
661 | { | |
662 | if (_config->FindB("APT::Get::No-Download",false) == false) | |
663 | if( Fetcher.Run() == pkgAcquire::Failed) | |
664 | return false; | |
665 | ||
666 | // Print out errors | |
667 | bool Failed = false; | |
668 | bool Transient = false; | |
669 | for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++) | |
670 | { | |
671 | if ((*I)->Status == pkgAcquire::Item::StatDone && | |
672 | (*I)->Complete == true) | |
673 | continue; | |
674 | ||
675 | (*I)->Finished(); | |
676 | ||
677 | if ((*I)->Status == pkgAcquire::Item::StatIdle) | |
678 | { | |
679 | Transient = true; | |
680 | // Failed = true; | |
681 | continue; | |
682 | } | |
683 | ||
684 | cerr << "Failed to fetch " << (*I)->DescURI() << endl; | |
685 | cerr << " " << (*I)->ErrorText << endl; | |
686 | Failed = true; | |
687 | } | |
688 | ||
689 | if (_config->FindB("APT::Get::Download-Only",false) == true) | |
690 | { | |
691 | if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false) | |
692 | return _error->Error("Some files failed to download"); | |
693 | return true; | |
694 | } | |
695 | ||
696 | if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false) | |
697 | { | |
698 | /*if (Transient == true) | |
699 | { | |
700 | c2out << "Upgrading with disk swapping is not supported in this version." << endl; | |
701 | c2out << "Try running multiple times with --fix-missing" << endl; | |
702 | }*/ | |
703 | ||
704 | return _error->Error("Unable to fetch some archives, maybe try with --fix-missing?"); | |
705 | } | |
706 | ||
707 | if (Transient == true && Failed == true) | |
708 | return _error->Error("--fix-missing and media swapping is not currently supported"); | |
709 | ||
710 | // Try to deal with missing package files | |
711 | if (Failed == true && PM.FixMissing() == false) | |
712 | { | |
713 | cerr << "Unable to correct missing packages." << endl; | |
714 | return _error->Error("Aborting Install."); | |
715 | } | |
716 | ||
717 | Cache.ReleaseLock(); | |
718 | pkgPackageManager::OrderResult Res = PM.DoInstall(); | |
719 | if (Res == pkgPackageManager::Failed || _error->PendingError() == true) | |
720 | return false; | |
721 | if (Res == pkgPackageManager::Completed) | |
722 | return true; | |
723 | ||
724 | // Reload the fetcher object and loop again for media swapping | |
725 | Fetcher.Shutdown(); | |
726 | if (PM.GetArchives(&Fetcher,&List,&Recs) == false) | |
727 | return false; | |
728 | } | |
729 | } | |
730 | /*}}}*/ | |
731 | ||
732 | // DoUpdate - Update the package lists /*{{{*/ | |
733 | // --------------------------------------------------------------------- | |
734 | /* */ | |
735 | bool DoUpdate(CommandLine &) | |
736 | { | |
737 | // Get the source list | |
738 | pkgSourceList List; | |
739 | if (List.ReadMainList() == false) | |
740 | return false; | |
741 | ||
742 | // Lock the list directory | |
743 | FileFd Lock; | |
744 | if (_config->FindB("Debug::NoLocking",false) == false) | |
745 | { | |
746 | Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); | |
747 | if (_error->PendingError() == true) | |
748 | return _error->Error("Unable to lock the list directory"); | |
749 | } | |
750 | ||
751 | // Create the download object | |
752 | AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0)); | |
753 | pkgAcquire Fetcher(&Stat); | |
754 | ||
755 | // Populate it with the source selection | |
756 | pkgSourceList::const_iterator I; | |
757 | for (I = List.begin(); I != List.end(); I++) | |
758 | { | |
759 | new pkgAcqIndex(&Fetcher,I); | |
760 | if (_error->PendingError() == true) | |
761 | return false; | |
762 | } | |
763 | ||
764 | // Run it | |
765 | if (Fetcher.Run() == pkgAcquire::Failed) | |
766 | return false; | |
767 | ||
768 | bool Failed = false; | |
769 | for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++) | |
770 | { | |
771 | if ((*I)->Status == pkgAcquire::Item::StatDone) | |
772 | continue; | |
773 | ||
774 | (*I)->Finished(); | |
775 | ||
776 | Failed = true; | |
777 | } | |
778 | ||
779 | // Clean out any old list files | |
780 | if (_config->FindB("APT::Get::List-Cleanup",false) == false) | |
781 | { | |
782 | if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false || | |
783 | Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false) | |
784 | return false; | |
785 | } | |
786 | ||
787 | // Prepare the cache. | |
788 | CacheFile Cache; | |
789 | if (Cache.Open() == false) | |
790 | return false; | |
791 | ||
792 | if (Failed == true) | |
793 | return _error->Error("Some index files failed to download, they have been ignored, or old ones used instead."); | |
794 | return true; | |
795 | } | |
796 | /*}}}*/ | |
797 | // DoUpgrade - Upgrade all packages /*{{{*/ | |
798 | // --------------------------------------------------------------------- | |
799 | /* Upgrade all packages without installing new packages or erasing old | |
800 | packages */ | |
801 | bool DoUpgrade(CommandLine &CmdL) | |
802 | { | |
803 | CacheFile Cache; | |
804 | if (Cache.Open() == false || Cache.CheckDeps() == false) | |
805 | return false; | |
806 | ||
807 | // Do the upgrade | |
808 | if (pkgAllUpgrade(Cache) == false) | |
809 | { | |
810 | ShowBroken(c1out,Cache); | |
811 | return _error->Error("Internal Error, AllUpgrade broke stuff"); | |
812 | } | |
813 | ||
814 | return InstallPackages(Cache,true); | |
815 | } | |
816 | /*}}}*/ | |
817 | // DoInstall - Install packages from the command line /*{{{*/ | |
818 | // --------------------------------------------------------------------- | |
819 | /* Install named packages */ | |
820 | bool DoInstall(CommandLine &CmdL) | |
821 | { | |
822 | CacheFile Cache; | |
823 | if (Cache.Open() == false || Cache.CheckDeps(CmdL.FileSize() != 1) == false) | |
824 | return false; | |
825 | ||
826 | // Enter the special broken fixing mode if the user specified arguments | |
827 | bool BrokenFix = false; | |
828 | if (Cache->BrokenCount() != 0) | |
829 | BrokenFix = true; | |
830 | ||
831 | unsigned int ExpectedInst = 0; | |
832 | unsigned int Packages = 0; | |
833 | pkgProblemResolver Fix(Cache); | |
834 | ||
835 | bool DefRemove = false; | |
836 | if (strcasecmp(CmdL.FileList[0],"remove") == 0) | |
837 | DefRemove = true; | |
838 | ||
839 | for (const char **I = CmdL.FileList + 1; *I != 0; I++) | |
840 | { | |
841 | // Duplicate the string | |
842 | unsigned int Length = strlen(*I); | |
843 | char S[300]; | |
844 | if (Length >= sizeof(S)) | |
845 | continue; | |
846 | strcpy(S,*I); | |
847 | ||
848 | // See if we are removing the package | |
849 | bool Remove = DefRemove; | |
850 | while (Cache->FindPkg(S).end() == true) | |
851 | { | |
852 | // Handle an optional end tag indicating what to do | |
853 | if (S[Length - 1] == '-') | |
854 | { | |
855 | Remove = true; | |
856 | S[--Length] = 0; | |
857 | continue; | |
858 | } | |
859 | ||
860 | if (S[Length - 1] == '+') | |
861 | { | |
862 | Remove = false; | |
863 | S[--Length] = 0; | |
864 | continue; | |
865 | } | |
866 | break; | |
867 | } | |
868 | ||
869 | // Locate the package | |
870 | pkgCache::PkgIterator Pkg = Cache->FindPkg(S); | |
871 | Packages++; | |
872 | if (Pkg.end() == true) | |
873 | return _error->Error("Couldn't find package %s",S); | |
874 | ||
875 | // Handle the no-upgrade case | |
876 | if (_config->FindB("APT::Get::no-upgrade",false) == true && | |
877 | Pkg->CurrentVer != 0) | |
878 | { | |
879 | c1out << "Skipping " << Pkg.Name() << ", it is already installed and no-upgrade is set." << endl; | |
880 | continue; | |
881 | } | |
882 | ||
883 | // Check if there is something new to install | |
884 | pkgDepCache::StateCache &State = (*Cache)[Pkg]; | |
885 | if (State.CandidateVer == 0) | |
886 | { | |
887 | if (Pkg->ProvidesList != 0) | |
888 | { | |
889 | c1out << "Package " << S << " is a virtual package provided by:" << endl; | |
890 | ||
891 | pkgCache::PrvIterator I = Pkg.ProvidesList(); | |
892 | for (; I.end() == false; I++) | |
893 | { | |
894 | pkgCache::PkgIterator Pkg = I.OwnerPkg(); | |
895 | ||
896 | if ((*Cache)[Pkg].CandidateVerIter(*Cache) == I.OwnerVer()) | |
897 | { | |
898 | if ((*Cache)[Pkg].Install() == true && (*Cache)[Pkg].NewInstall() == false) | |
899 | c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << | |
900 | " [Installed]"<< endl; | |
901 | else | |
902 | c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl; | |
903 | } | |
904 | } | |
905 | c1out << "You should explicly select one to install." << endl; | |
906 | } | |
907 | else | |
908 | { | |
909 | c1out << "Package " << S << " has no available version, but exists in the database." << endl; | |
910 | c1out << "This typically means that the package was mentioned in a dependency and " << endl; | |
911 | c1out << "never uploaded, or that it is an obsolete package." << endl; | |
912 | ||
913 | string List; | |
914 | pkgCache::DepIterator Dep = Pkg.RevDependsList(); | |
915 | for (; Dep.end() == false; Dep++) | |
916 | { | |
917 | if (Dep->Type != pkgCache::Dep::Replaces) | |
918 | continue; | |
919 | List += string(Dep.ParentPkg().Name()) + " "; | |
920 | } | |
921 | ShowList(c1out,"However the following packages replace it:",List); | |
922 | } | |
923 | ||
924 | return _error->Error("Package %s has no installation candidate",S); | |
925 | } | |
926 | ||
927 | Fix.Protect(Pkg); | |
928 | if (Remove == true) | |
929 | { | |
930 | Fix.Remove(Pkg); | |
931 | Cache->MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false)); | |
932 | continue; | |
933 | } | |
934 | ||
935 | // Install it | |
936 | Cache->MarkInstall(Pkg,false); | |
937 | if (State.Install() == false) | |
938 | c1out << "Sorry, " << S << " is already the newest version" << endl; | |
939 | else | |
940 | ExpectedInst++; | |
941 | ||
942 | // Install it with autoinstalling enabled. | |
943 | if (State.InstBroken() == true && BrokenFix == false) | |
944 | Cache->MarkInstall(Pkg,true); | |
945 | } | |
946 | ||
947 | /* If we are in the Broken fixing mode we do not attempt to fix the | |
948 | problems. This is if the user invoked install without -f and gave | |
949 | packages */ | |
950 | if (BrokenFix == true && Cache->BrokenCount() != 0) | |
951 | { | |
952 | c1out << "You might want to run `apt-get -f install' to correct these:" << endl; | |
953 | ShowBroken(c1out,Cache); | |
954 | ||
955 | return _error->Error("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."); | |
956 | } | |
957 | ||
958 | // Call the scored problem resolver | |
959 | Fix.InstallProtect(); | |
960 | if (Fix.Resolve(true) == false) | |
961 | _error->Discard(); | |
962 | ||
963 | // Now we check the state of the packages, | |
964 | if (Cache->BrokenCount() != 0) | |
965 | { | |
966 | c1out << "Some packages could not be installed. This may mean that you have" << endl; | |
967 | c1out << "requested an impossible situation or if you are using the unstable" << endl; | |
968 | c1out << "distribution that some required packages have not yet been created" << endl; | |
969 | c1out << "or been moved out of Incoming." << endl; | |
970 | if (Packages == 1) | |
971 | { | |
972 | c1out << endl; | |
973 | c1out << "Since you only requested a single operation it is extremely likely that" << endl; | |
974 | c1out << "the package is simply not installable and a bug report against" << endl; | |
975 | c1out << "that package should be filed." << endl; | |
976 | } | |
977 | ||
978 | c1out << "The following information may help to resolve the situation:" << endl; | |
979 | c1out << endl; | |
980 | ShowBroken(c1out,Cache); | |
981 | return _error->Error("Sorry, broken packages"); | |
982 | } | |
983 | ||
984 | /* Print out a list of packages that are going to be installed extra | |
985 | to what the user asked */ | |
986 | if (Cache->InstCount() != ExpectedInst) | |
987 | { | |
988 | string List; | |
989 | for (unsigned J = 0; J < Cache->Head().PackageCount; J++) | |
990 | { | |
991 | pkgCache::PkgIterator I(Cache,Cache.List[J]); | |
992 | if ((*Cache)[I].Install() == false) | |
993 | continue; | |
994 | ||
995 | const char **J; | |
996 | for (J = CmdL.FileList + 1; *J != 0; J++) | |
997 | if (strcmp(*J,I.Name()) == 0) | |
998 | break; | |
999 | ||
1000 | if (*J == 0) | |
1001 | List += string(I.Name()) + " "; | |
1002 | } | |
1003 | ||
1004 | ShowList(c1out,"The following extra packages will be installed:",List); | |
1005 | } | |
1006 | ||
1007 | // See if we need to prompt | |
1008 | if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0) | |
1009 | return InstallPackages(Cache,false,false); | |
1010 | ||
1011 | return InstallPackages(Cache,false); | |
1012 | } | |
1013 | /*}}}*/ | |
1014 | // DoDistUpgrade - Automatic smart upgrader /*{{{*/ | |
1015 | // --------------------------------------------------------------------- | |
1016 | /* Intelligent upgrader that will install and remove packages at will */ | |
1017 | bool DoDistUpgrade(CommandLine &CmdL) | |
1018 | { | |
1019 | CacheFile Cache; | |
1020 | if (Cache.Open() == false || Cache.CheckDeps() == false) | |
1021 | return false; | |
1022 | ||
1023 | c0out << "Calculating Upgrade... " << flush; | |
1024 | if (pkgDistUpgrade(*Cache) == false) | |
1025 | { | |
1026 | c0out << "Failed" << endl; | |
1027 | ShowBroken(c1out,Cache); | |
1028 | return false; | |
1029 | } | |
1030 | ||
1031 | c0out << "Done" << endl; | |
1032 | ||
1033 | return InstallPackages(Cache,true); | |
1034 | } | |
1035 | /*}}}*/ | |
1036 | // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/ | |
1037 | // --------------------------------------------------------------------- | |
1038 | /* Follows dselect's selections */ | |
1039 | bool DoDSelectUpgrade(CommandLine &CmdL) | |
1040 | { | |
1041 | CacheFile Cache; | |
1042 | if (Cache.Open() == false || Cache.CheckDeps() == false) | |
1043 | return false; | |
1044 | ||
1045 | // Install everything with the install flag set | |
1046 | pkgCache::PkgIterator I = Cache->PkgBegin(); | |
1047 | for (;I.end() != true; I++) | |
1048 | { | |
1049 | /* Install the package only if it is a new install, the autoupgrader | |
1050 | will deal with the rest */ | |
1051 | if (I->SelectedState == pkgCache::State::Install) | |
1052 | Cache->MarkInstall(I,false); | |
1053 | } | |
1054 | ||
1055 | /* Now install their deps too, if we do this above then order of | |
1056 | the status file is significant for | groups */ | |
1057 | for (I = Cache->PkgBegin();I.end() != true; I++) | |
1058 | { | |
1059 | /* Install the package only if it is a new install, the autoupgrader | |
1060 | will deal with the rest */ | |
1061 | if (I->SelectedState == pkgCache::State::Install) | |
1062 | Cache->MarkInstall(I,true); | |
1063 | } | |
1064 | ||
1065 | // Apply erasures now, they override everything else. | |
1066 | for (I = Cache->PkgBegin();I.end() != true; I++) | |
1067 | { | |
1068 | // Remove packages | |
1069 | if (I->SelectedState == pkgCache::State::DeInstall || | |
1070 | I->SelectedState == pkgCache::State::Purge) | |
1071 | Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge); | |
1072 | } | |
1073 | ||
1074 | /* Resolve any problems that dselect created, allupgrade cannot handle | |
1075 | such things. We do so quite agressively too.. */ | |
1076 | if (Cache->BrokenCount() != 0) | |
1077 | { | |
1078 | pkgProblemResolver Fix(Cache); | |
1079 | ||
1080 | // Hold back held packages. | |
1081 | if (_config->FindB("APT::Ingore-Hold",false) == false) | |
1082 | { | |
1083 | for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++) | |
1084 | { | |
1085 | if (I->SelectedState == pkgCache::State::Hold) | |
1086 | { | |
1087 | Fix.Protect(I); | |
1088 | Cache->MarkKeep(I); | |
1089 | } | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | if (Fix.Resolve() == false) | |
1094 | { | |
1095 | ShowBroken(c1out,Cache); | |
1096 | return _error->Error("Internal Error, problem resolver broke stuff"); | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | // Now upgrade everything | |
1101 | if (pkgAllUpgrade(Cache) == false) | |
1102 | { | |
1103 | ShowBroken(c1out,Cache); | |
1104 | return _error->Error("Internal Error, problem resolver broke stuff"); | |
1105 | } | |
1106 | ||
1107 | return InstallPackages(Cache,false); | |
1108 | } | |
1109 | /*}}}*/ | |
1110 | // DoClean - Remove download archives /*{{{*/ | |
1111 | // --------------------------------------------------------------------- | |
1112 | /* */ | |
1113 | bool DoClean(CommandLine &CmdL) | |
1114 | { | |
1115 | pkgAcquire Fetcher; | |
1116 | Fetcher.Clean(_config->FindDir("Dir::Cache::archives")); | |
1117 | Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/"); | |
1118 | return true; | |
1119 | } | |
1120 | /*}}}*/ | |
1121 | // DoAutoClean - Smartly remove downloaded archives /*{{{*/ | |
1122 | // --------------------------------------------------------------------- | |
1123 | /* This is similar to clean but it only purges things that cannot be | |
1124 | downloaded, that is old versions of cached packages. */ | |
1125 | class LogCleaner : public pkgArchiveCleaner | |
1126 | { | |
1127 | protected: | |
1128 | virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St) | |
1129 | { | |
1130 | cout << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl; | |
1131 | ||
1132 | if (_config->FindB("APT::Get::Simulate") == false) | |
1133 | unlink(File); | |
1134 | }; | |
1135 | }; | |
1136 | ||
1137 | bool DoAutoClean(CommandLine &CmdL) | |
1138 | { | |
1139 | CacheFile Cache; | |
1140 | if (Cache.Open() == false) | |
1141 | return false; | |
1142 | ||
1143 | LogCleaner Cleaner; | |
1144 | ||
1145 | return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) && | |
1146 | Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache); | |
1147 | } | |
1148 | /*}}}*/ | |
1149 | // DoCheck - Perform the check operation /*{{{*/ | |
1150 | // --------------------------------------------------------------------- | |
1151 | /* Opening automatically checks the system, this command is mostly used | |
1152 | for debugging */ | |
1153 | bool DoCheck(CommandLine &CmdL) | |
1154 | { | |
1155 | CacheFile Cache; | |
1156 | Cache.Open(); | |
1157 | Cache.CheckDeps(); | |
1158 | ||
1159 | return true; | |
1160 | } | |
1161 | /*}}}*/ | |
1162 | // DoSource - Fetch a source archive /*{{{*/ | |
1163 | // --------------------------------------------------------------------- | |
1164 | /* Fetch souce packages */ | |
1165 | struct DscFile | |
1166 | { | |
1167 | string Package; | |
1168 | string Version; | |
1169 | string Dsc; | |
1170 | }; | |
1171 | ||
1172 | bool DoSource(CommandLine &CmdL) | |
1173 | { | |
1174 | CacheFile Cache; | |
1175 | if (Cache.Open(false) == false) | |
1176 | return false; | |
1177 | ||
1178 | if (CmdL.FileSize() <= 1) | |
1179 | return _error->Error("Must specify at least one package to fetch source for"); | |
1180 | ||
1181 | // Read the source list | |
1182 | pkgSourceList List; | |
1183 | if (List.ReadMainList() == false) | |
1184 | return _error->Error("The list of sources could not be read."); | |
1185 | ||
1186 | // Create the text record parsers | |
1187 | pkgRecords Recs(Cache); | |
1188 | pkgSrcRecords SrcRecs(List); | |
1189 | if (_error->PendingError() == true) | |
1190 | return false; | |
1191 | ||
1192 | // Create the download object | |
1193 | AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0)); | |
1194 | pkgAcquire Fetcher(&Stat); | |
1195 | ||
1196 | DscFile *Dsc = new DscFile[CmdL.FileSize()]; | |
1197 | ||
1198 | // Load the requestd sources into the fetcher | |
1199 | unsigned J = 0; | |
1200 | for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++) | |
1201 | { | |
1202 | string Src; | |
1203 | ||
1204 | /* Lookup the version of the package we would install if we were to | |
1205 | install a version and determine the source package name, then look | |
1206 | in the archive for a source package of the same name. In theory | |
1207 | we could stash the version string as well and match that too but | |
1208 | today there aren't multi source versions in the archive. */ | |
1209 | pkgCache::PkgIterator Pkg = Cache->FindPkg(*I); | |
1210 | if (Pkg.end() == false) | |
1211 | { | |
1212 | pkgCache::VerIterator Ver = Cache->GetCandidateVer(Pkg); | |
1213 | if (Ver.end() == false) | |
1214 | { | |
1215 | pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList()); | |
1216 | Src = Parse.SourcePkg(); | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | // No source package name.. | |
1221 | if (Src.empty() == true) | |
1222 | Src = *I; | |
1223 | ||
1224 | // The best hit | |
1225 | pkgSrcRecords::Parser *Last = 0; | |
1226 | unsigned long Offset = 0; | |
1227 | string Version; | |
1228 | bool IsMatch = false; | |
1229 | ||
1230 | // Iterate over all of the hits | |
1231 | pkgSrcRecords::Parser *Parse; | |
1232 | SrcRecs.Restart(); | |
1233 | while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0) | |
1234 | { | |
1235 | string Ver = Parse->Version(); | |
1236 | ||
1237 | // Skip name mismatches | |
1238 | if (IsMatch == true && Parse->Package() != Src) | |
1239 | continue; | |
1240 | ||
1241 | // Newer version or an exact match | |
1242 | if (Last == 0 || pkgVersionCompare(Version,Ver) < 0 || | |
1243 | (Parse->Package() == Src && IsMatch == false)) | |
1244 | { | |
1245 | IsMatch = Parse->Package() == Src; | |
1246 | Last = Parse; | |
1247 | Offset = Parse->Offset(); | |
1248 | Version = Ver; | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | if (Last == 0) | |
1253 | return _error->Error("Unable to find a source package for %s",Src.c_str()); | |
1254 | ||
1255 | // Back track | |
1256 | vector<pkgSrcRecords::File> Lst; | |
1257 | if (Last->Jump(Offset) == false || Last->Files(Lst) == false) | |
1258 | return false; | |
1259 | ||
1260 | // Load them into the fetcher | |
1261 | for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin(); | |
1262 | I != Lst.end(); I++) | |
1263 | { | |
1264 | // Try to guess what sort of file it is we are getting. | |
1265 | string Comp; | |
1266 | if (I->Path.find(".dsc") != string::npos) | |
1267 | { | |
1268 | Comp = "dsc"; | |
1269 | Dsc[J].Package = Last->Package(); | |
1270 | Dsc[J].Version = Last->Version(); | |
1271 | Dsc[J].Dsc = flNotDir(I->Path); | |
1272 | } | |
1273 | ||
1274 | if (I->Path.find(".tar.gz") != string::npos) | |
1275 | Comp = "tar"; | |
1276 | if (I->Path.find(".diff.gz") != string::npos) | |
1277 | Comp = "diff"; | |
1278 | ||
1279 | new pkgAcqFile(&Fetcher,Last->Source()->ArchiveURI(I->Path), | |
1280 | I->MD5Hash,I->Size,Last->Source()->SourceInfo(Src, | |
1281 | Last->Version(),Comp),Src); | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | // Display statistics | |
1286 | unsigned long FetchBytes = Fetcher.FetchNeeded(); | |
1287 | unsigned long FetchPBytes = Fetcher.PartialPresent(); | |
1288 | unsigned long DebBytes = Fetcher.TotalNeeded(); | |
1289 | ||
1290 | // Check for enough free space | |
1291 | struct statfs Buf; | |
1292 | string OutputDir = "."; | |
1293 | if (statfs(OutputDir.c_str(),&Buf) != 0) | |
1294 | return _error->Errno("statfs","Couldn't determine free space in %s", | |
1295 | OutputDir.c_str()); | |
1296 | if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize) | |
1297 | return _error->Error("Sorry, you don't have enough free space in %s", | |
1298 | OutputDir.c_str()); | |
1299 | ||
1300 | // Number of bytes | |
1301 | c1out << "Need to get "; | |
1302 | if (DebBytes != FetchBytes) | |
1303 | c1out << SizeToStr(FetchBytes) << "B/" << SizeToStr(DebBytes) << 'B'; | |
1304 | else | |
1305 | c1out << SizeToStr(DebBytes) << 'B'; | |
1306 | c1out << " of source archives." << endl; | |
1307 | ||
1308 | if (_config->FindB("APT::Get::Simulate",false) == true) | |
1309 | { | |
1310 | for (unsigned I = 0; I != J; I++) | |
1311 | cout << "Fetch Source " << Dsc[I].Package << endl; | |
1312 | return true; | |
1313 | } | |
1314 | ||
1315 | // Just print out the uris an exit if the --print-uris flag was used | |
1316 | if (_config->FindB("APT::Get::Print-URIs") == true) | |
1317 | { | |
1318 | pkgAcquire::UriIterator I = Fetcher.UriBegin(); | |
1319 | for (; I != Fetcher.UriEnd(); I++) | |
1320 | cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' << | |
1321 | I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl; | |
1322 | return true; | |
1323 | } | |
1324 | ||
1325 | // Run it | |
1326 | if (Fetcher.Run() == pkgAcquire::Failed) | |
1327 | return false; | |
1328 | ||
1329 | // Print error messages | |
1330 | bool Failed = false; | |
1331 | for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++) | |
1332 | { | |
1333 | if ((*I)->Status == pkgAcquire::Item::StatDone && | |
1334 | (*I)->Complete == true) | |
1335 | continue; | |
1336 | ||
1337 | cerr << "Failed to fetch " << (*I)->DescURI() << endl; | |
1338 | cerr << " " << (*I)->ErrorText << endl; | |
1339 | Failed = true; | |
1340 | } | |
1341 | if (Failed == true) | |
1342 | return _error->Error("Failed to fetch some archives."); | |
1343 | ||
1344 | if (_config->FindB("APT::Get::Download-only",false) == true) | |
1345 | return true; | |
1346 | ||
1347 | // Unpack the sources | |
1348 | pid_t Process = ExecFork(); | |
1349 | ||
1350 | if (Process == 0) | |
1351 | { | |
1352 | for (unsigned I = 0; I != J; I++) | |
1353 | { | |
1354 | string Dir = Dsc[I].Package + '-' + pkgBaseVersion(Dsc[I].Version.c_str()); | |
1355 | ||
1356 | // See if the package is already unpacked | |
1357 | struct stat Stat; | |
1358 | if (stat(Dir.c_str(),&Stat) == 0 && | |
1359 | S_ISDIR(Stat.st_mode) != 0) | |
1360 | { | |
1361 | c0out << "Skipping unpack of already unpacked source in " << Dir << endl; | |
1362 | } | |
1363 | else | |
1364 | { | |
1365 | // Call dpkg-source | |
1366 | char S[500]; | |
1367 | snprintf(S,sizeof(S),"%s -x %s", | |
1368 | _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(), | |
1369 | Dsc[I].Dsc.c_str()); | |
1370 | if (system(S) != 0) | |
1371 | { | |
1372 | cerr << "Unpack command '" << S << "' failed." << endl; | |
1373 | _exit(1); | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | // Try to compile it with dpkg-buildpackage | |
1378 | if (_config->FindB("APT::Get::Compile",false) == true) | |
1379 | { | |
1380 | // Call dpkg-buildpackage | |
1381 | char S[500]; | |
1382 | snprintf(S,sizeof(S),"cd %s && %s %s", | |
1383 | Dir.c_str(), | |
1384 | _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(), | |
1385 | _config->Find("DPkg::Build-Options","-b -uc").c_str()); | |
1386 | ||
1387 | if (system(S) != 0) | |
1388 | { | |
1389 | cerr << "Build command '" << S << "' failed." << endl; | |
1390 | _exit(1); | |
1391 | } | |
1392 | } | |
1393 | } | |
1394 | ||
1395 | _exit(0); | |
1396 | } | |
1397 | ||
1398 | // Wait for the subprocess | |
1399 | int Status = 0; | |
1400 | while (waitpid(Process,&Status,0) != Process) | |
1401 | { | |
1402 | if (errno == EINTR) | |
1403 | continue; | |
1404 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
1405 | } | |
1406 | ||
1407 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
1408 | return _error->Error("Child process failed"); | |
1409 | ||
1410 | return true; | |
1411 | } | |
1412 | /*}}}*/ | |
1413 | ||
1414 | // ShowHelp - Show a help screen /*{{{*/ | |
1415 | // --------------------------------------------------------------------- | |
1416 | /* */ | |
1417 | bool ShowHelp(CommandLine &CmdL) | |
1418 | { | |
1419 | cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE << | |
1420 | " compiled on " << __DATE__ << " " << __TIME__ << endl; | |
1421 | if (_config->FindB("version") == true) | |
1422 | return 100; | |
1423 | ||
1424 | cout << "Usage: apt-get [options] command" << endl; | |
1425 | cout << " apt-get [options] install pkg1 [pkg2 ...]" << endl; | |
1426 | cout << endl; | |
1427 | cout << "apt-get is a simple command line interface for downloading and" << endl; | |
1428 | cout << "installing packages. The most frequently used commands are update" << endl; | |
1429 | cout << "and install." << endl; | |
1430 | cout << endl; | |
1431 | cout << "Commands:" << endl; | |
1432 | cout << " update - Retrieve new lists of packages" << endl; | |
1433 | cout << " upgrade - Perform an upgrade" << endl; | |
1434 | cout << " install - Install new packages (pkg is libc6 not libc6.deb)" << endl; | |
1435 | cout << " remove - Remove packages" << endl; | |
1436 | cout << " source - Download source archives" << endl; | |
1437 | cout << " dist-upgrade - Distribution upgrade, see apt-get(8)" << endl; | |
1438 | cout << " dselect-upgrade - Follow dselect selections" << endl; | |
1439 | cout << " clean - Erase downloaded archive files" << endl; | |
1440 | cout << " autoclean - Erase old downloaded archive files" << endl; | |
1441 | cout << " check - Verify that there are no broken dependencies" << endl; | |
1442 | cout << endl; | |
1443 | cout << "Options:" << endl; | |
1444 | cout << " -h This help text." << endl; | |
1445 | cout << " -q Loggable output - no progress indicator" << endl; | |
1446 | cout << " -qq No output except for errors" << endl; | |
1447 | cout << " -d Download only - do NOT install or unpack archives" << endl; | |
1448 | cout << " -s No-act. Perform ordering simulation" << endl; | |
1449 | cout << " -y Assume Yes to all queries and do not prompt" << endl; | |
1450 | cout << " -f Attempt to continue if the integrity check fails" << endl; | |
1451 | cout << " -m Attempt to continue if archives are unlocatable" << endl; | |
1452 | cout << " -u Show a list of upgraded packages as well" << endl; | |
1453 | cout << " -b Build the source package after fetching it" << endl; | |
1454 | cout << " -c=? Read this configuration file" << endl; | |
1455 | cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl; | |
1456 | cout << "See the apt-get(8), sources.list(5) and apt.conf(5) manual" << endl; | |
1457 | cout << "pages for more information and options." << endl; | |
1458 | return 100; | |
1459 | } | |
1460 | /*}}}*/ | |
1461 | // GetInitialize - Initialize things for apt-get /*{{{*/ | |
1462 | // --------------------------------------------------------------------- | |
1463 | /* */ | |
1464 | void GetInitialize() | |
1465 | { | |
1466 | _config->Set("quiet",0); | |
1467 | _config->Set("help",false); | |
1468 | _config->Set("APT::Get::Download-Only",false); | |
1469 | _config->Set("APT::Get::Simulate",false); | |
1470 | _config->Set("APT::Get::Assume-Yes",false); | |
1471 | _config->Set("APT::Get::Fix-Broken",false); | |
1472 | _config->Set("APT::Get::Force-Yes",false); | |
1473 | _config->Set("APT::Get::APT::Get::No-List-Cleanup",true); | |
1474 | } | |
1475 | /*}}}*/ | |
1476 | // SigWinch - Window size change signal handler /*{{{*/ | |
1477 | // --------------------------------------------------------------------- | |
1478 | /* */ | |
1479 | void SigWinch(int) | |
1480 | { | |
1481 | // Riped from GNU ls | |
1482 | #ifdef TIOCGWINSZ | |
1483 | struct winsize ws; | |
1484 | ||
1485 | if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5) | |
1486 | ScreenWidth = ws.ws_col - 1; | |
1487 | #endif | |
1488 | } | |
1489 | /*}}}*/ | |
1490 | ||
1491 | int main(int argc,const char *argv[]) | |
1492 | { | |
1493 | CommandLine::Args Args[] = { | |
1494 | {'h',"help","help",0}, | |
1495 | {'v',"version","version",0}, | |
1496 | {'q',"quiet","quiet",CommandLine::IntLevel}, | |
1497 | {'q',"silent","quiet",CommandLine::IntLevel}, | |
1498 | {'d',"download-only","APT::Get::Download-Only",0}, | |
1499 | {'b',"compile","APT::Get::Compile",0}, | |
1500 | {'b',"build","APT::Get::Compile",0}, | |
1501 | {'s',"simulate","APT::Get::Simulate",0}, | |
1502 | {'s',"just-print","APT::Get::Simulate",0}, | |
1503 | {'s',"recon","APT::Get::Simulate",0}, | |
1504 | {'s',"no-act","APT::Get::Simulate",0}, | |
1505 | {'y',"yes","APT::Get::Assume-Yes",0}, | |
1506 | {'y',"assume-yes","APT::Get::Assume-Yes",0}, | |
1507 | {'f',"fix-broken","APT::Get::Fix-Broken",0}, | |
1508 | {'u',"show-upgraded","APT::Get::Show-Upgraded",0}, | |
1509 | {'m',"ignore-missing","APT::Get::Fix-Missing",0}, | |
1510 | {0,"no-download","APT::Get::No-Download",0}, | |
1511 | {0,"fix-missing","APT::Get::Fix-Missing",0}, | |
1512 | {0,"ignore-hold","APT::Ingore-Hold",0}, | |
1513 | {0,"no-upgrade","APT::Get::no-upgrade",0}, | |
1514 | {0,"force-yes","APT::Get::force-yes",0}, | |
1515 | {0,"print-uris","APT::Get::Print-URIs",0}, | |
1516 | {0,"purge","APT::Get::Purge",0}, | |
1517 | {0,"list-cleanup","APT::Get::List-Cleanup",0}, | |
1518 | {'c',"config-file",0,CommandLine::ConfigFile}, | |
1519 | {'o',"option",0,CommandLine::ArbItem}, | |
1520 | {0,0,0,0}}; | |
1521 | CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate}, | |
1522 | {"upgrade",&DoUpgrade}, | |
1523 | {"install",&DoInstall}, | |
1524 | {"remove",&DoInstall}, | |
1525 | {"dist-upgrade",&DoDistUpgrade}, | |
1526 | {"dselect-upgrade",&DoDSelectUpgrade}, | |
1527 | {"clean",&DoClean}, | |
1528 | {"autoclean",&DoAutoClean}, | |
1529 | {"check",&DoCheck}, | |
1530 | {"source",&DoSource}, | |
1531 | {"help",&ShowHelp}, | |
1532 | {0,0}}; | |
1533 | ||
1534 | // Parse the command line and initialize the package library | |
1535 | CommandLine CmdL(Args,_config); | |
1536 | if (pkgInitialize(*_config) == false || | |
1537 | CmdL.Parse(argc,argv) == false) | |
1538 | { | |
1539 | _error->DumpErrors(); | |
1540 | return 100; | |
1541 | } | |
1542 | ||
1543 | // See if the help should be shown | |
1544 | if (_config->FindB("help") == true || | |
1545 | _config->FindB("version") == true || | |
1546 | CmdL.FileSize() == 0) | |
1547 | return ShowHelp(CmdL); | |
1548 | ||
1549 | // Deal with stdout not being a tty | |
1550 | if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1) | |
1551 | _config->Set("quiet","1"); | |
1552 | ||
1553 | // Setup the output streams | |
1554 | c0out.rdbuf(cout.rdbuf()); | |
1555 | c1out.rdbuf(cout.rdbuf()); | |
1556 | c2out.rdbuf(cout.rdbuf()); | |
1557 | if (_config->FindI("quiet",0) > 0) | |
1558 | c0out.rdbuf(devnull.rdbuf()); | |
1559 | if (_config->FindI("quiet",0) > 1) | |
1560 | c1out.rdbuf(devnull.rdbuf()); | |
1561 | ||
1562 | // Setup the signals | |
1563 | signal(SIGPIPE,SIG_IGN); | |
1564 | signal(SIGWINCH,SigWinch); | |
1565 | SigWinch(0); | |
1566 | ||
1567 | // Match the operation | |
1568 | CmdL.DispatchArg(Cmds); | |
1569 | ||
1570 | // Print any errors or warnings found during parsing | |
1571 | if (_error->empty() == false) | |
1572 | { | |
1573 | bool Errors = _error->PendingError(); | |
1574 | _error->DumpErrors(); | |
1575 | return Errors == true?100:0; | |
1576 | } | |
1577 | ||
1578 | return 0; | |
1579 | } |