]> git.saurik.com Git - apt.git/blob - apt-private/acqprogress.cc
add pkgAcquire::TransactionHasError()
[apt.git] / apt-private / acqprogress.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acqprogress.cc,v 1.24 2003/04/27 01:56:48 doogie Exp $
4 /* ######################################################################
5
6 Acquire Progress - Command line progress meter
7
8 ##################################################################### */
9 /*}}}*/
10 // Include files /*{{{*/
11 #include<config.h>
12
13 #include <apt-pkg/acquire.h>
14 #include <apt-pkg/acquire-item.h>
15 #include <apt-pkg/acquire-worker.h>
16 #include <apt-pkg/configuration.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/error.h>
19
20 #include <apt-private/acqprogress.h>
21
22 #include <string.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <iostream>
26 #include <unistd.h>
27
28 #include <apti18n.h>
29 /*}}}*/
30
31 using namespace std;
32
33 // AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
35 /* */
36 AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int const Quiet) :
37 pkgAcquireStatus(), ScreenWidth(ScreenWidth), ID(0), Quiet(Quiet)
38 {
39 BlankLine[0] = 0;
40 // testcases use it to disable pulses without disabling other user messages
41 if (Quiet == 0 && _config->FindB("quiet::NoUpdate", false) == true)
42 this->Quiet = 1;
43 }
44 /*}}}*/
45 // AcqTextStatus::Start - Downloading has started /*{{{*/
46 // ---------------------------------------------------------------------
47 /* */
48 void AcqTextStatus::Start()
49 {
50 pkgAcquireStatus::Start();
51 BlankLine[0] = 0;
52 ID = 1;
53 }
54 /*}}}*/
55 // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
56 // ---------------------------------------------------------------------
57 /* */
58 void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
59 {
60 if (Quiet > 1)
61 return;
62
63 if (Quiet <= 0)
64 cout << '\r' << BlankLine << '\r';
65
66 cout << _("Hit ") << Itm.Description;
67 cout << endl;
68 Update = true;
69 }
70 /*}}}*/
71 // AcqTextStatus::Fetch - An item has started to download /*{{{*/
72 // ---------------------------------------------------------------------
73 /* This prints out the short description and the expected size */
74 void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
75 {
76 Update = true;
77 if (Itm.Owner->Complete == true)
78 return;
79
80 Itm.Owner->ID = ID++;
81
82 if (Quiet > 1)
83 return;
84
85 if (Quiet <= 0)
86 cout << '\r' << BlankLine << '\r';
87
88 cout << _("Get:") << Itm.Owner->ID << ' ' << Itm.Description;
89 if (Itm.Owner->FileSize != 0)
90 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
91 cout << endl;
92 }
93 /*}}}*/
94 // AcqTextStatus::Done - Completed a download /*{{{*/
95 // ---------------------------------------------------------------------
96 /* We don't display anything... */
97 void AcqTextStatus::Done(pkgAcquire::ItemDesc &/*Itm*/)
98 {
99 Update = true;
100 }
101 /*}}}*/
102 // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
103 // ---------------------------------------------------------------------
104 /* We print out the error text */
105 void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
106 {
107 if (Quiet > 1)
108 return;
109
110 // Ignore certain kinds of transient failures (bad code)
111 if (Itm.Owner->Status == pkgAcquire::Item::StatIdle)
112 return;
113
114 if (Quiet <= 0)
115 cout << '\r' << BlankLine << '\r';
116
117 if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
118 {
119 cout << _("Ign ") << Itm.Description << endl;
120 }
121 else
122 {
123 cout << _("Err ") << Itm.Description << endl;
124 cout << " " << Itm.Owner->ErrorText << endl;
125 }
126
127 Update = true;
128 }
129 /*}}}*/
130 // AcqTextStatus::Stop - Finished downloading /*{{{*/
131 // ---------------------------------------------------------------------
132 /* This prints out the bytes downloaded and the overall average line
133 speed */
134 void AcqTextStatus::Stop()
135 {
136 pkgAcquireStatus::Stop();
137 if (Quiet > 1)
138 return;
139
140 if (Quiet <= 0)
141 cout << '\r' << BlankLine << '\r' << flush;
142
143 if (_config->FindB("quiet::NoStatistic", false) == true)
144 return;
145
146 if (FetchedBytes != 0 && _error->PendingError() == false)
147 ioprintf(cout,_("Fetched %sB in %s (%sB/s)\n"),
148 SizeToStr(FetchedBytes).c_str(),
149 TimeToStr(ElapsedTime).c_str(),
150 SizeToStr(CurrentCPS).c_str());
151 }
152 /*}}}*/
153 // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
154 // ---------------------------------------------------------------------
155 /* This draws the current progress. Each line has an overall percent
156 meter and a per active item status meter along with an overall
157 bandwidth and ETA indicator. */
158 bool AcqTextStatus::Pulse(pkgAcquire *Owner)
159 {
160 pkgAcquireStatus::Pulse(Owner);
161
162 if (Quiet > 0)
163 return true;
164
165 enum {Long = 0,Medium,Short} Mode = Medium;
166
167 char Buffer[sizeof(BlankLine)];
168 char *End = Buffer + sizeof(Buffer);
169 char *S = Buffer;
170 if (ScreenWidth >= sizeof(Buffer))
171 ScreenWidth = sizeof(Buffer)-1;
172
173 // Put in the percent done
174 sprintf(S,"%.0f%%", Percent);
175
176 bool Shown = false;
177 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
178 I = Owner->WorkerStep(I))
179 {
180 S += strlen(S);
181
182 // There is no item running
183 if (I->CurrentItem == 0)
184 {
185 if (I->Status.empty() == false)
186 {
187 snprintf(S,End-S," [%s]",I->Status.c_str());
188 Shown = true;
189 }
190
191 continue;
192 }
193
194 Shown = true;
195
196 // Add in the short description
197 if (I->CurrentItem->Owner->ID != 0)
198 snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
199 I->CurrentItem->ShortDesc.c_str());
200 else
201 snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
202 S += strlen(S);
203
204 // Show the short mode string
205 if (I->CurrentItem->Owner->Mode != 0)
206 {
207 snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
208 S += strlen(S);
209 }
210
211 // Add the current progress
212 if (Mode == Long)
213 snprintf(S,End-S," %llu",I->CurrentSize);
214 else
215 {
216 if (Mode == Medium || I->TotalSize == 0)
217 snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
218 }
219 S += strlen(S);
220
221 // Add the total size and percent
222 if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
223 {
224 if (Mode == Short)
225 snprintf(S,End-S," %.0f%%",
226 (I->CurrentSize*100.0)/I->TotalSize);
227 else
228 snprintf(S,End-S,"/%sB %.0f%%",SizeToStr(I->TotalSize).c_str(),
229 (I->CurrentSize*100.0)/I->TotalSize);
230 }
231 S += strlen(S);
232 snprintf(S,End-S,"]");
233 }
234
235 // Show something..
236 if (Shown == false)
237 snprintf(S,End-S,_(" [Working]"));
238
239 /* Put in the ETA and cps meter, block off signals to prevent strangeness
240 during resizing */
241 sigset_t Sigs,OldSigs;
242 sigemptyset(&Sigs);
243 sigaddset(&Sigs,SIGWINCH);
244 sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
245
246 if (CurrentCPS != 0)
247 {
248 char Tmp[300];
249 unsigned long long ETA = (TotalBytes - CurrentBytes)/CurrentCPS;
250 sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
251 unsigned int Len = strlen(Buffer);
252 unsigned int LenT = strlen(Tmp);
253 if (Len + LenT < ScreenWidth)
254 {
255 memset(Buffer + Len,' ',ScreenWidth - Len);
256 strcpy(Buffer + ScreenWidth - LenT,Tmp);
257 }
258 }
259 Buffer[ScreenWidth] = 0;
260 BlankLine[ScreenWidth] = 0;
261 sigprocmask(SIG_SETMASK,&OldSigs,0);
262
263 // Draw the current status
264 if (_config->FindB("Apt::Color", false) == true)
265 cout << _config->Find("APT::Color::Yellow");
266 if (strlen(Buffer) == strlen(BlankLine))
267 cout << '\r' << Buffer << flush;
268 else
269 cout << '\r' << BlankLine << '\r' << Buffer << flush;
270 if (_config->FindB("Apt::Color", false) == true)
271 cout << _config->Find("APT::Color::Neutral") << flush;
272
273 memset(BlankLine,' ',strlen(Buffer));
274 BlankLine[strlen(Buffer)] = 0;
275
276 Update = false;
277
278 return true;
279 }
280 /*}}}*/
281 // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
282 // ---------------------------------------------------------------------
283 /* Prompt for a media swap */
284 bool AcqTextStatus::MediaChange(string Media,string Drive)
285 {
286 // If we do not output on a terminal and one of the options to avoid user
287 // interaction is given, we assume that no user is present who could react
288 // on your media change request
289 if (isatty(STDOUT_FILENO) != 1 && Quiet >= 2 &&
290 (_config->FindB("APT::Get::Assume-Yes",false) == true ||
291 _config->FindB("APT::Get::Force-Yes",false) == true ||
292 _config->FindB("APT::Get::Trivial-Only",false) == true))
293
294 return false;
295
296 if (Quiet <= 0)
297 cout << '\r' << BlankLine << '\r';
298 ioprintf(cout,_("Media change: please insert the disc labeled\n"
299 " '%s'\n"
300 "in the drive '%s' and press enter\n"),
301 Media.c_str(),Drive.c_str());
302
303 char C = 0;
304 bool bStatus = true;
305 while (C != '\n' && C != '\r')
306 {
307 int len = read(STDIN_FILENO,&C,1);
308 if(C == 'c' || len <= 0)
309 bStatus = false;
310 }
311
312 if(bStatus)
313 Update = true;
314 return bStatus;
315 }
316 /*}}}*/