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