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