]> git.saurik.com Git - apt.git/blame - cmdline/acqprogress.cc
Small segv bug fix
[apt.git] / cmdline / acqprogress.cc
CommitLineData
0919e3f9
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
281daf46 3// $Id: acqprogress.cc,v 1.16 1999/07/03 03:10:35 jgg 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>
cdcc6d34 14#include <apt-pkg/strutl.h>
b0db36b1 15
0919e3f9 16#include <stdio.h>
b0db36b1 17#include <signal.h>
0919e3f9
AL
18 /*}}}*/
19
20// AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
21// ---------------------------------------------------------------------
22/* */
d7827aca 23AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int Quiet) :
0919e3f9
AL
24 ScreenWidth(ScreenWidth), Quiet(Quiet)
25{
26}
27 /*}}}*/
28// AcqTextStatus::Start - Downloading has started /*{{{*/
29// ---------------------------------------------------------------------
30/* */
31void AcqTextStatus::Start()
32{
33 pkgAcquireStatus::Start();
34 BlankLine[0] = 0;
35 ID = 1;
36};
37 /*}}}*/
38// AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
39// ---------------------------------------------------------------------
40/* */
41void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
42{
43 if (Quiet > 1)
44 return;
45
46 if (Quiet <= 0)
47 cout << '\r' << BlankLine << '\r';
48
49 cout << "Hit " << Itm.Description;
50 if (Itm.Owner->FileSize != 0)
314037ba 51 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
0919e3f9
AL
52 cout << endl;
53 Update = true;
54};
55 /*}}}*/
56// AcqTextStatus::Fetch - An item has started to download /*{{{*/
57// ---------------------------------------------------------------------
58/* This prints out the short description and the expected size */
59void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
60{
61 Update = true;
62 if (Itm.Owner->Complete == true)
63 return;
64
65 Itm.Owner->ID = ID++;
66
67 if (Quiet > 1)
68 return;
69
70 if (Quiet <= 0)
71 cout << '\r' << BlankLine << '\r';
72
eec898ad 73 cout << "Get:" << Itm.Owner->ID << ' ' << Itm.Description;
0919e3f9 74 if (Itm.Owner->FileSize != 0)
314037ba 75 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
0919e3f9
AL
76 cout << endl;
77};
78 /*}}}*/
79// AcqTextStatus::Done - Completed a download /*{{{*/
80// ---------------------------------------------------------------------
81/* We don't display anything... */
82void AcqTextStatus::Done(pkgAcquire::ItemDesc &Itm)
83{
84 Update = true;
85};
86 /*}}}*/
87// AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
88// ---------------------------------------------------------------------
89/* We print out the error text */
90void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
91{
92 if (Quiet > 1)
93 return;
94
281daf46
AL
95 if (Itm.Owner->Status == pkgAcquire::Item::StatIdle)
96 return;
97
0919e3f9
AL
98 if (Quiet <= 0)
99 cout << '\r' << BlankLine << '\r';
100
2b154e53 101 if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
681d76d0
AL
102 {
103 cout << "Ign " << Itm.Description << endl;
104 }
105 else
106 {
107 cout << "Err " << Itm.Description << endl;
108 cout << " " << Itm.Owner->ErrorText << endl;
109 }
110
0919e3f9
AL
111 Update = true;
112};
113 /*}}}*/
114// AcqTextStatus::Stop - Finished downloading /*{{{*/
115// ---------------------------------------------------------------------
116/* This prints out the bytes downloaded and the overall average line
117 speed */
118void AcqTextStatus::Stop()
119{
120 pkgAcquireStatus::Stop();
121 if (Quiet > 1)
122 return;
123
124 if (Quiet <= 0)
125 cout << '\r' << BlankLine << '\r';
126
127 if (FetchedBytes != 0)
314037ba 128 cout << "Fetched " << SizeToStr(FetchedBytes) << "B in " <<
0919e3f9 129 TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) <<
314037ba 130 "B/s)" << endl;
0919e3f9
AL
131}
132 /*}}}*/
133// AcqTextStatus::Pulse - Regular event pulse /*{{{*/
134// ---------------------------------------------------------------------
135/* This draws the current progress. Each line has an overall percent
136 meter and a per active item status meter along with an overall
137 bandwidth and ETA indicator. */
024d1123 138bool AcqTextStatus::Pulse(pkgAcquire *Owner)
0919e3f9
AL
139{
140 if (Quiet > 0)
024d1123 141 return true;
0919e3f9
AL
142
143 pkgAcquireStatus::Pulse(Owner);
144
145 enum {Long = 0,Medium,Short} Mode = Long;
146
147 char Buffer[300];
148 char *End = Buffer + sizeof(Buffer);
149 char *S = Buffer;
150
151 // Put in the percent done
d568ed2d
AL
152 sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems)));
153
154 bool Shown = false;
0919e3f9
AL
155 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
156 I = Owner->WorkerStep(I))
157 {
158 S += strlen(S);
159
160 // There is no item running
161 if (I->CurrentItem == 0)
162 {
163 if (I->Status.empty() == false)
afce5764 164 {
0919e3f9 165 snprintf(S,End-S," [%s]",I->Status.c_str());
afce5764
AL
166 Shown = true;
167 }
168
0919e3f9
AL
169 continue;
170 }
171
d568ed2d
AL
172 Shown = true;
173
0919e3f9
AL
174 // Add in the short description
175 if (I->CurrentItem->Owner->ID != 0)
eec898ad 176 snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
0919e3f9
AL
177 I->CurrentItem->ShortDesc.c_str());
178 else
179 snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
180 S += strlen(S);
181
182 // Show the short mode string
183 if (I->CurrentItem->Owner->Mode != 0)
184 {
185 snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
186 S += strlen(S);
187 }
188
189 // Add the current progress
190 if (Mode == Long)
f40e3a64 191 snprintf(S,End-S," %lu",I->CurrentSize);
0919e3f9
AL
192 else
193 {
194 if (Mode == Medium || I->TotalSize == 0)
314037ba 195 snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
0919e3f9
AL
196 }
197 S += strlen(S);
198
199 // Add the total size and percent
200 if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
201 {
202 if (Mode == Short)
f40e3a64 203 snprintf(S,End-S," %lu%%",
0919e3f9
AL
204 long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
205 else
314037ba 206 snprintf(S,End-S,"/%sB %lu%%",SizeToStr(I->TotalSize).c_str(),
0919e3f9
AL
207 long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
208 }
209 S += strlen(S);
210 snprintf(S,End-S,"]");
211 }
212
d568ed2d
AL
213 // Show something..
214 if (Shown == false)
215 snprintf(S,End-S," [Working]");
216
b0db36b1
AL
217 /* Put in the ETA and cps meter, block off signals to prevent strangeness
218 during resizing */
219 sigset_t Sigs,OldSigs;
220 sigemptyset(&Sigs);
221 sigaddset(&Sigs,SIGWINCH);
222 sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
223
0919e3f9
AL
224 if (CurrentCPS != 0)
225 {
226 char Tmp[300];
227 unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
314037ba 228 sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
0919e3f9
AL
229 unsigned int Len = strlen(Buffer);
230 unsigned int LenT = strlen(Tmp);
231 if (Len + LenT < ScreenWidth)
232 {
233 memset(Buffer + Len,' ',ScreenWidth - Len);
234 strcpy(Buffer + ScreenWidth - LenT,Tmp);
235 }
236 }
237 Buffer[ScreenWidth] = 0;
b0db36b1
AL
238 BlankLine[ScreenWidth] = 0;
239 sigprocmask(SIG_UNBLOCK,&OldSigs,0);
240
0919e3f9
AL
241 // Draw the current status
242 if (strlen(Buffer) == strlen(BlankLine))
243 cout << '\r' << Buffer << flush;
244 else
245 cout << '\r' << BlankLine << '\r' << Buffer << flush;
246 memset(BlankLine,' ',strlen(Buffer));
247 BlankLine[strlen(Buffer)] = 0;
248
249 Update = false;
024d1123
AL
250
251 return true;
0919e3f9
AL
252}
253 /*}}}*/
542ec555
AL
254// AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
255// ---------------------------------------------------------------------
256/* Prompt for a media swap */
257bool AcqTextStatus::MediaChange(string Media,string Drive)
258{
259 if (Quiet <= 0)
260 cout << '\r' << BlankLine << '\r';
ac49a1e5
AL
261 cout << "Media Change: Please insert the disc labeled '" << Media << "' in "\
262 "the drive '" << Drive << "' and press enter" << endl;
542ec555 263
ac49a1e5
AL
264 char C = 0;
265 while (C != '\n' && C != '\r')
266 read(STDIN_FILENO,&C,1);
542ec555
AL
267
268 Update = true;
269 return true;
270}
271 /*}}}*/