]>
Commit | Line | Data |
---|---|---|
453b82a3 DK |
1 | #include <config.h> |
2 | ||
e6ad8031 MV |
3 | #include <apt-pkg/configuration.h> |
4 | #include <apt-pkg/fileutl.h> | |
31f97d7b | 5 | #include <apt-pkg/strutl.h> |
af36becc | 6 | #include <apt-pkg/install-progress.h> |
c7ea1eba | 7 | |
453b82a3 DK |
8 | #include <signal.h> |
9 | #include <unistd.h> | |
10 | #include <iostream> | |
11 | #include <string> | |
12 | #include <vector> | |
31f97d7b | 13 | #include <sys/ioctl.h> |
e6ad8031 | 14 | #include <sstream> |
5e9458e2 | 15 | #include <fcntl.h> |
5ed88785 | 16 | #include <algorithm> |
c23e6cd5 | 17 | #include <stdio.h> |
e96e4e9c | 18 | |
453b82a3 DK |
19 | #include <apti18n.h> |
20 | ||
31f97d7b MV |
21 | namespace APT { |
22 | namespace Progress { | |
23 | ||
61f954bf MV |
24 | |
25 | /* Return a APT::Progress::PackageManager based on the global | |
26 | * apt configuration (i.e. APT::Status-Fd and APT::Status-deb822-Fd) | |
27 | */ | |
bd5f39b3 MV |
28 | PackageManager* PackageManagerProgressFactory() |
29 | { | |
30 | // select the right progress | |
31 | int status_fd = _config->FindI("APT::Status-Fd", -1); | |
32 | int status_deb822_fd = _config->FindI("APT::Status-deb822-Fd", -1); | |
33 | ||
34 | APT::Progress::PackageManager *progress = NULL; | |
35 | if (status_deb822_fd > 0) | |
36 | progress = new APT::Progress::PackageManagerProgressDeb822Fd( | |
37 | status_deb822_fd); | |
38 | else if (status_fd > 0) | |
39 | progress = new APT::Progress::PackageManagerProgressFd(status_fd); | |
40 | else if(_config->FindB("Dpkg::Progress-Fancy", false) == true) | |
41 | progress = new APT::Progress::PackageManagerFancy(); | |
42 | else if (_config->FindB("Dpkg::Progress", | |
43 | _config->FindB("DpkgPM::Progress", false)) == true) | |
44 | progress = new APT::Progress::PackageManagerText(); | |
45 | else | |
46 | progress = new APT::Progress::PackageManager(); | |
47 | return progress; | |
48 | } | |
49 | ||
65512241 | 50 | bool PackageManager::StatusChanged(std::string /*PackageName*/, |
e6ad8031 MV |
51 | unsigned int StepsDone, |
52 | unsigned int TotalSteps, | |
65512241 | 53 | std::string /*HumanReadableAction*/) |
6c5ae8ed MV |
54 | { |
55 | int reporting_steps = _config->FindI("DpkgPM::Reporting-Steps", 1); | |
56 | percentage = StepsDone/(float)TotalSteps * 100.0; | |
57 | strprintf(progress_str, _("Progress: [%3i%%]"), (int)percentage); | |
58 | ||
59 | if(percentage < (last_reported_progress + reporting_steps)) | |
60 | return false; | |
61 | ||
62 | return true; | |
63 | } | |
64 | ||
e6ad8031 | 65 | PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd) |
65dbd5a1 | 66 | : StepsDone(0), StepsTotal(1) |
e6ad8031 MV |
67 | { |
68 | OutStatusFd = progress_fd; | |
69 | } | |
70 | ||
f9935b1c MV |
71 | void PackageManagerProgressFd::WriteToStatusFd(std::string s) |
72 | { | |
73 | if(OutStatusFd <= 0) | |
74 | return; | |
75 | FileFd::Write(OutStatusFd, s.c_str(), s.size()); | |
76 | } | |
77 | ||
e45c4617 | 78 | void PackageManagerProgressFd::StartDpkg() |
e6ad8031 | 79 | { |
5e9458e2 MV |
80 | if(OutStatusFd <= 0) |
81 | return; | |
82 | ||
83 | // FIXME: use SetCloseExec here once it taught about throwing | |
84 | // exceptions instead of doing _exit(100) on failure | |
85 | fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC); | |
e6ad8031 MV |
86 | |
87 | // send status information that we are about to fork dpkg | |
f9935b1c MV |
88 | std::ostringstream status; |
89 | status << "pmstatus:dpkg-exec:" | |
90 | << (StepsDone/float(StepsTotal)*100.0) | |
91 | << ":" << _("Running dpkg") | |
92 | << std::endl; | |
93 | WriteToStatusFd(status.str()); | |
e6ad8031 MV |
94 | } |
95 | ||
a02db58f | 96 | APT_CONST void PackageManagerProgressFd::Stop() |
e6ad8031 | 97 | { |
e6ad8031 MV |
98 | } |
99 | ||
100 | void PackageManagerProgressFd::Error(std::string PackageName, | |
101 | unsigned int StepsDone, | |
102 | unsigned int TotalSteps, | |
103 | std::string ErrorMessage) | |
104 | { | |
105 | std::ostringstream status; | |
106 | status << "pmerror:" << PackageName | |
107 | << ":" << (StepsDone/float(TotalSteps)*100.0) | |
108 | << ":" << ErrorMessage | |
109 | << std::endl; | |
f9935b1c | 110 | WriteToStatusFd(status.str()); |
e6ad8031 MV |
111 | } |
112 | ||
113 | void PackageManagerProgressFd::ConffilePrompt(std::string PackageName, | |
114 | unsigned int StepsDone, | |
115 | unsigned int TotalSteps, | |
116 | std::string ConfMessage) | |
117 | { | |
118 | std::ostringstream status; | |
119 | status << "pmconffile:" << PackageName | |
120 | << ":" << (StepsDone/float(TotalSteps)*100.0) | |
121 | << ":" << ConfMessage | |
122 | << std::endl; | |
f9935b1c | 123 | WriteToStatusFd(status.str()); |
e6ad8031 MV |
124 | } |
125 | ||
126 | ||
127 | bool PackageManagerProgressFd::StatusChanged(std::string PackageName, | |
128 | unsigned int xStepsDone, | |
129 | unsigned int xTotalSteps, | |
130 | std::string pkg_action) | |
131 | { | |
132 | StepsDone = xStepsDone; | |
133 | StepsTotal = xTotalSteps; | |
134 | ||
135 | // build the status str | |
136 | std::ostringstream status; | |
dd640f3c | 137 | status << "pmstatus:" << StringSplit(PackageName, ":")[0] |
e6ad8031 MV |
138 | << ":" << (StepsDone/float(StepsTotal)*100.0) |
139 | << ":" << pkg_action | |
140 | << std::endl; | |
f9935b1c | 141 | WriteToStatusFd(status.str()); |
65dbd5a1 MV |
142 | |
143 | if(_config->FindB("Debug::APT::Progress::PackageManagerFd", false) == true) | |
144 | std::cerr << "progress: " << PackageName << " " << xStepsDone | |
145 | << " " << xTotalSteps << " " << pkg_action | |
146 | << std::endl; | |
147 | ||
148 | ||
e6ad8031 MV |
149 | return true; |
150 | } | |
151 | ||
c7ea1eba MV |
152 | |
153 | PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd) | |
154 | : StepsDone(0), StepsTotal(1) | |
155 | { | |
156 | OutStatusFd = progress_fd; | |
157 | } | |
158 | ||
159 | void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s) | |
160 | { | |
161 | FileFd::Write(OutStatusFd, s.c_str(), s.size()); | |
162 | } | |
163 | ||
790d41f6 | 164 | void PackageManagerProgressDeb822Fd::StartDpkg() |
c7ea1eba MV |
165 | { |
166 | // FIXME: use SetCloseExec here once it taught about throwing | |
167 | // exceptions instead of doing _exit(100) on failure | |
168 | fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC); | |
169 | ||
170 | // send status information that we are about to fork dpkg | |
171 | std::ostringstream status; | |
172 | status << "Status: " << "progress" << std::endl | |
173 | << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl | |
174 | << "Message: " << _("Running dpkg") << std::endl | |
175 | << std::endl; | |
176 | WriteToStatusFd(status.str()); | |
177 | } | |
178 | ||
a02db58f | 179 | APT_CONST void PackageManagerProgressDeb822Fd::Stop() |
c7ea1eba | 180 | { |
c7ea1eba MV |
181 | } |
182 | ||
183 | void PackageManagerProgressDeb822Fd::Error(std::string PackageName, | |
184 | unsigned int StepsDone, | |
185 | unsigned int TotalSteps, | |
186 | std::string ErrorMessage) | |
187 | { | |
188 | std::ostringstream status; | |
189 | status << "Status: " << "Error" << std::endl | |
190 | << "Package:" << PackageName << std::endl | |
191 | << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl | |
192 | << "Message: " << ErrorMessage << std::endl | |
193 | << std::endl; | |
194 | WriteToStatusFd(status.str()); | |
195 | } | |
196 | ||
197 | void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName, | |
198 | unsigned int StepsDone, | |
199 | unsigned int TotalSteps, | |
200 | std::string ConfMessage) | |
201 | { | |
202 | std::ostringstream status; | |
203 | status << "Status: " << "ConfFile" << std::endl | |
204 | << "Package:" << PackageName << std::endl | |
205 | << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl | |
206 | << "Message: " << ConfMessage << std::endl | |
207 | << std::endl; | |
208 | WriteToStatusFd(status.str()); | |
209 | } | |
210 | ||
211 | ||
212 | bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName, | |
213 | unsigned int xStepsDone, | |
214 | unsigned int xTotalSteps, | |
215 | std::string message) | |
216 | { | |
217 | StepsDone = xStepsDone; | |
218 | StepsTotal = xTotalSteps; | |
219 | ||
220 | // build the status str | |
221 | std::ostringstream status; | |
222 | status << "Status: " << "progress" << std::endl | |
223 | << "Package: " << PackageName << std::endl | |
224 | << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl | |
225 | << "Message: " << message << std::endl | |
226 | << std::endl; | |
227 | WriteToStatusFd(status.str()); | |
228 | ||
229 | return true; | |
230 | } | |
231 | ||
5ed88785 MV |
232 | |
233 | PackageManagerFancy::PackageManagerFancy() | |
234 | : child_pty(-1) | |
235 | { | |
236 | // setup terminal size | |
237 | old_SIGWINCH = signal(SIGWINCH, PackageManagerFancy::staticSIGWINCH); | |
238 | instances.push_back(this); | |
239 | } | |
240 | std::vector<PackageManagerFancy*> PackageManagerFancy::instances; | |
241 | ||
242 | PackageManagerFancy::~PackageManagerFancy() | |
243 | { | |
244 | instances.erase(find(instances.begin(), instances.end(), this)); | |
245 | signal(SIGWINCH, old_SIGWINCH); | |
246 | } | |
247 | ||
248 | void PackageManagerFancy::staticSIGWINCH(int signum) | |
249 | { | |
250 | std::vector<PackageManagerFancy *>::const_iterator I; | |
9ce3cfc9 | 251 | for(I = instances.begin(); I != instances.end(); ++I) |
5ed88785 MV |
252 | (*I)->HandleSIGWINCH(signum); |
253 | } | |
254 | ||
fa211e2d MV |
255 | PackageManagerFancy::TermSize |
256 | PackageManagerFancy::GetTerminalSize() | |
e96e4e9c MV |
257 | { |
258 | struct winsize win; | |
76bd63e2 | 259 | PackageManagerFancy::TermSize s = { 0, 0 }; |
fa211e2d | 260 | |
5ed88785 | 261 | // FIXME: get from "child_pty" instead? |
e96e4e9c | 262 | if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) != 0) |
fa211e2d | 263 | return s; |
5ed88785 MV |
264 | |
265 | if(_config->FindB("Debug::InstallProgress::Fancy", false) == true) | |
76bd63e2 | 266 | std::cerr << "GetTerminalSize: " << win.ws_row << " x " << win.ws_col << std::endl; |
fa211e2d MV |
267 | |
268 | s.rows = win.ws_row; | |
269 | s.columns = win.ws_col; | |
270 | return s; | |
e96e4e9c | 271 | } |
c7ea1eba | 272 | |
db78c60c | 273 | void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows) |
31f97d7b | 274 | { |
5ed88785 MV |
275 | if(_config->FindB("Debug::InstallProgress::Fancy", false) == true) |
276 | std::cerr << "SetupTerminalScrollArea: " << nr_rows << std::endl; | |
277 | ||
76bd63e2 DK |
278 | if (unlikely(nr_rows <= 1)) |
279 | return; | |
280 | ||
31f97d7b MV |
281 | // scroll down a bit to avoid visual glitch when the screen |
282 | // area shrinks by one row | |
283 | std::cout << "\n"; | |
284 | ||
285 | // save cursor | |
286 | std::cout << "\033[s"; | |
287 | ||
288 | // set scroll region (this will place the cursor in the top left) | |
5ed88785 | 289 | std::cout << "\033[0;" << nr_rows - 1 << "r"; |
31f97d7b MV |
290 | |
291 | // restore cursor but ensure its inside the scrolling area | |
292 | std::cout << "\033[u"; | |
293 | static const char *move_cursor_up = "\033[1A"; | |
294 | std::cout << move_cursor_up; | |
db78c60c | 295 | |
5ed88785 | 296 | // ensure its flushed |
31f97d7b | 297 | std::flush(std::cout); |
31f97d7b | 298 | |
5ed88785 MV |
299 | // setup tty size to ensure xterm/linux console are working properly too |
300 | // see bug #731738 | |
301 | struct winsize win; | |
76bd63e2 DK |
302 | if (ioctl(child_pty, TIOCGWINSZ, (char *)&win) != -1) |
303 | { | |
304 | win.ws_row = nr_rows - 1; | |
305 | ioctl(child_pty, TIOCSWINSZ, (char *)&win); | |
306 | } | |
e96e4e9c MV |
307 | } |
308 | ||
309 | void PackageManagerFancy::HandleSIGWINCH(int) | |
310 | { | |
76bd63e2 | 311 | int const nr_terminal_rows = GetTerminalSize().rows; |
e96e4e9c | 312 | SetupTerminalScrollArea(nr_terminal_rows); |
76bd63e2 | 313 | DrawStatusLine(); |
31f97d7b MV |
314 | } |
315 | ||
5ed88785 | 316 | void PackageManagerFancy::Start(int a_child_pty) |
31f97d7b | 317 | { |
5ed88785 | 318 | child_pty = a_child_pty; |
76bd63e2 DK |
319 | int const nr_terminal_rows = GetTerminalSize().rows; |
320 | SetupTerminalScrollArea(nr_terminal_rows); | |
31f97d7b MV |
321 | } |
322 | ||
a22fdebf | 323 | void PackageManagerFancy::Stop() |
31f97d7b | 324 | { |
76bd63e2 | 325 | int const nr_terminal_rows = GetTerminalSize().rows; |
db78c60c MV |
326 | if (nr_terminal_rows > 0) |
327 | { | |
328 | SetupTerminalScrollArea(nr_terminal_rows + 1); | |
31f97d7b | 329 | |
db78c60c MV |
330 | // override the progress line (sledgehammer) |
331 | static const char* clear_screen_below_cursor = "\033[J"; | |
332 | std::cout << clear_screen_below_cursor; | |
333 | } | |
5ed88785 | 334 | child_pty = -1; |
31f97d7b MV |
335 | } |
336 | ||
fa211e2d MV |
337 | std::string |
338 | PackageManagerFancy::GetTextProgressStr(float Percent, int OutputSize) | |
339 | { | |
340 | std::string output; | |
341 | int i; | |
342 | ||
343 | // should we raise a exception here instead? | |
344 | if (Percent < 0.0 || Percent > 1.0 || OutputSize < 3) | |
345 | return output; | |
346 | ||
347 | int BarSize = OutputSize - 2; // bar without the leading "[" and trailing "]" | |
348 | output += "["; | |
349 | for(i=0; i < BarSize*Percent; i++) | |
350 | output += "#"; | |
351 | for (/*nothing*/; i < BarSize; i++) | |
352 | output += "."; | |
353 | output += "]"; | |
354 | return output; | |
355 | } | |
356 | ||
6c5ae8ed | 357 | bool PackageManagerFancy::StatusChanged(std::string PackageName, |
31f97d7b | 358 | unsigned int StepsDone, |
e6ad8031 MV |
359 | unsigned int TotalSteps, |
360 | std::string HumanReadableAction) | |
31f97d7b | 361 | { |
e6ad8031 MV |
362 | if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, |
363 | HumanReadableAction)) | |
6c5ae8ed | 364 | return false; |
31f97d7b | 365 | |
76bd63e2 DK |
366 | return DrawStatusLine(); |
367 | } | |
368 | bool PackageManagerFancy::DrawStatusLine() | |
369 | { | |
370 | PackageManagerFancy::TermSize const size = GetTerminalSize(); | |
371 | if (unlikely(size.rows < 1 || size.columns < 1)) | |
372 | return false; | |
31f97d7b | 373 | |
453b82a3 DK |
374 | static std::string save_cursor = "\033[s"; |
375 | static std::string restore_cursor = "\033[u"; | |
376 | ||
c34d1202 MV |
377 | // green |
378 | static std::string set_bg_color = DeQuoteString( | |
379 | _config->Find("Dpkg::Progress-Fancy::Progress-fg", "%1b[42m")); | |
380 | // black | |
381 | static std::string set_fg_color = DeQuoteString( | |
382 | _config->Find("Dpkg::Progress-Fancy::Progress-bg", "%1b[30m")); | |
453b82a3 DK |
383 | |
384 | static std::string restore_bg = "\033[49m"; | |
385 | static std::string restore_fg = "\033[39m"; | |
386 | ||
31f97d7b MV |
387 | std::cout << save_cursor |
388 | // move cursor position to last row | |
fa211e2d | 389 | << "\033[" << size.rows << ";0f" |
31f97d7b MV |
390 | << set_bg_color |
391 | << set_fg_color | |
392 | << progress_str | |
31f97d7b MV |
393 | << restore_bg |
394 | << restore_fg; | |
395 | std::flush(std::cout); | |
fa211e2d MV |
396 | |
397 | // draw text progress bar | |
398 | if (_config->FindB("Dpkg::Progress-Fancy::Progress-Bar", true)) | |
399 | { | |
400 | int padding = 4; | |
401 | float progressbar_size = size.columns - padding - progress_str.size(); | |
76bd63e2 | 402 | float current_percent = percentage / 100.0; |
fa211e2d MV |
403 | std::cout << " " |
404 | << GetTextProgressStr(current_percent, progressbar_size) | |
405 | << " "; | |
406 | std::flush(std::cout); | |
407 | } | |
408 | ||
409 | // restore | |
410 | std::cout << restore_cursor; | |
411 | std::flush(std::cout); | |
412 | ||
31f97d7b | 413 | last_reported_progress = percentage; |
6c5ae8ed MV |
414 | |
415 | return true; | |
31f97d7b MV |
416 | } |
417 | ||
6c5ae8ed | 418 | bool PackageManagerText::StatusChanged(std::string PackageName, |
31f97d7b | 419 | unsigned int StepsDone, |
e6ad8031 MV |
420 | unsigned int TotalSteps, |
421 | std::string HumanReadableAction) | |
31f97d7b | 422 | { |
e6ad8031 | 423 | if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction)) |
6c5ae8ed | 424 | return false; |
31f97d7b MV |
425 | |
426 | std::cout << progress_str << "\r\n"; | |
427 | std::flush(std::cout); | |
428 | ||
429 | last_reported_progress = percentage; | |
6c5ae8ed MV |
430 | |
431 | return true; | |
31f97d7b MV |
432 | } |
433 | ||
434 | ||
c7ea1eba | 435 | |
d3e8fbb3 DK |
436 | } // namespace progress |
437 | } // namespace apt |