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