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