]> git.saurik.com Git - apt.git/blob - apt-pkg/install-progress.cc
start adding apt.8.xml
[apt.git] / apt-pkg / install-progress.cc
1 #include <apt-pkg/configuration.h>
2 #include <apt-pkg/fileutl.h>
3 #include <apt-pkg/strutl.h>
4 #include <apt-pkg/install-progress.h>
5
6 #include <apti18n.h>
7
8 #include <termios.h>
9 #include <sys/ioctl.h>
10 #include <sstream>
11 #include <fcntl.h>
12
13 namespace APT {
14 namespace Progress {
15
16
17 /* Return a APT::Progress::PackageManager based on the global
18 * apt configuration (i.e. APT::Status-Fd and APT::Status-deb822-Fd)
19 */
20 PackageManager* PackageManagerProgressFactory()
21 {
22 // select the right progress
23 int status_fd = _config->FindI("APT::Status-Fd", -1);
24 int status_deb822_fd = _config->FindI("APT::Status-deb822-Fd", -1);
25
26 APT::Progress::PackageManager *progress = NULL;
27 if (status_deb822_fd > 0)
28 progress = new APT::Progress::PackageManagerProgressDeb822Fd(
29 status_deb822_fd);
30 else if (status_fd > 0)
31 progress = new APT::Progress::PackageManagerProgressFd(status_fd);
32 else if(_config->FindB("Dpkg::Progress-Fancy", false) == true)
33 progress = new APT::Progress::PackageManagerFancy();
34 else if (_config->FindB("Dpkg::Progress",
35 _config->FindB("DpkgPM::Progress", false)) == true)
36 progress = new APT::Progress::PackageManagerText();
37 else
38 progress = new APT::Progress::PackageManager();
39 return progress;
40 }
41
42 bool PackageManager::StatusChanged(std::string PackageName,
43 unsigned int StepsDone,
44 unsigned int TotalSteps,
45 std::string HumanReadableAction)
46 {
47 int reporting_steps = _config->FindI("DpkgPM::Reporting-Steps", 1);
48 percentage = StepsDone/(float)TotalSteps * 100.0;
49 strprintf(progress_str, _("Progress: [%3i%%]"), (int)percentage);
50
51 if(percentage < (last_reported_progress + reporting_steps))
52 return false;
53
54 return true;
55 }
56
57 PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd)
58 : StepsDone(0), StepsTotal(1)
59 {
60 OutStatusFd = progress_fd;
61 }
62
63 void PackageManagerProgressFd::WriteToStatusFd(std::string s)
64 {
65 if(OutStatusFd <= 0)
66 return;
67 FileFd::Write(OutStatusFd, s.c_str(), s.size());
68 }
69
70 void PackageManagerProgressFd::StartDpkg()
71 {
72 if(OutStatusFd <= 0)
73 return;
74
75 // FIXME: use SetCloseExec here once it taught about throwing
76 // exceptions instead of doing _exit(100) on failure
77 fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC);
78
79 // send status information that we are about to fork dpkg
80 std::ostringstream status;
81 status << "pmstatus:dpkg-exec:"
82 << (StepsDone/float(StepsTotal)*100.0)
83 << ":" << _("Running dpkg")
84 << std::endl;
85 WriteToStatusFd(status.str());
86 }
87
88 void PackageManagerProgressFd::Stop()
89 {
90 }
91
92 void PackageManagerProgressFd::Error(std::string PackageName,
93 unsigned int StepsDone,
94 unsigned int TotalSteps,
95 std::string ErrorMessage)
96 {
97 std::ostringstream status;
98 status << "pmerror:" << PackageName
99 << ":" << (StepsDone/float(TotalSteps)*100.0)
100 << ":" << ErrorMessage
101 << std::endl;
102 WriteToStatusFd(status.str());
103 }
104
105 void PackageManagerProgressFd::ConffilePrompt(std::string PackageName,
106 unsigned int StepsDone,
107 unsigned int TotalSteps,
108 std::string ConfMessage)
109 {
110 std::ostringstream status;
111 status << "pmconffile:" << PackageName
112 << ":" << (StepsDone/float(TotalSteps)*100.0)
113 << ":" << ConfMessage
114 << std::endl;
115 WriteToStatusFd(status.str());
116 }
117
118
119 bool PackageManagerProgressFd::StatusChanged(std::string PackageName,
120 unsigned int xStepsDone,
121 unsigned int xTotalSteps,
122 std::string pkg_action)
123 {
124 StepsDone = xStepsDone;
125 StepsTotal = xTotalSteps;
126
127 // build the status str
128 std::ostringstream status;
129 status << "pmstatus:" << StringSplit(PackageName, ":")[0]
130 << ":" << (StepsDone/float(StepsTotal)*100.0)
131 << ":" << pkg_action
132 << std::endl;
133 WriteToStatusFd(status.str());
134
135 if(_config->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
136 std::cerr << "progress: " << PackageName << " " << xStepsDone
137 << " " << xTotalSteps << " " << pkg_action
138 << std::endl;
139
140
141 return true;
142 }
143
144
145 PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd)
146 : StepsDone(0), StepsTotal(1)
147 {
148 OutStatusFd = progress_fd;
149 }
150
151 void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s)
152 {
153 FileFd::Write(OutStatusFd, s.c_str(), s.size());
154 }
155
156 void PackageManagerProgressDeb822Fd::StartDpkg()
157 {
158 // FIXME: use SetCloseExec here once it taught about throwing
159 // exceptions instead of doing _exit(100) on failure
160 fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC);
161
162 // send status information that we are about to fork dpkg
163 std::ostringstream status;
164 status << "Status: " << "progress" << std::endl
165 << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl
166 << "Message: " << _("Running dpkg") << std::endl
167 << std::endl;
168 WriteToStatusFd(status.str());
169 }
170
171 void PackageManagerProgressDeb822Fd::Stop()
172 {
173 }
174
175 void PackageManagerProgressDeb822Fd::Error(std::string PackageName,
176 unsigned int StepsDone,
177 unsigned int TotalSteps,
178 std::string ErrorMessage)
179 {
180 std::ostringstream status;
181 status << "Status: " << "Error" << std::endl
182 << "Package:" << PackageName << std::endl
183 << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl
184 << "Message: " << ErrorMessage << std::endl
185 << std::endl;
186 WriteToStatusFd(status.str());
187 }
188
189 void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName,
190 unsigned int StepsDone,
191 unsigned int TotalSteps,
192 std::string ConfMessage)
193 {
194 std::ostringstream status;
195 status << "Status: " << "ConfFile" << std::endl
196 << "Package:" << PackageName << std::endl
197 << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl
198 << "Message: " << ConfMessage << std::endl
199 << std::endl;
200 WriteToStatusFd(status.str());
201 }
202
203
204 bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName,
205 unsigned int xStepsDone,
206 unsigned int xTotalSteps,
207 std::string message)
208 {
209 StepsDone = xStepsDone;
210 StepsTotal = xTotalSteps;
211
212 // build the status str
213 std::ostringstream status;
214 status << "Status: " << "progress" << std::endl
215 << "Package: " << PackageName << std::endl
216 << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl
217 << "Message: " << message << std::endl
218 << std::endl;
219 WriteToStatusFd(status.str());
220
221 return true;
222 }
223
224
225 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
226 {
227 // scroll down a bit to avoid visual glitch when the screen
228 // area shrinks by one row
229 std::cout << "\n";
230
231 // save cursor
232 std::cout << "\033[s";
233
234 // set scroll region (this will place the cursor in the top left)
235 std::cout << "\033[1;" << nr_rows - 1 << "r";
236
237 // restore cursor but ensure its inside the scrolling area
238 std::cout << "\033[u";
239 static const char *move_cursor_up = "\033[1A";
240 std::cout << move_cursor_up;
241
242 // setup env for (hopefully!) ncurses
243 string s;
244 strprintf(s, "%i", nr_rows);
245 setenv("LINES", s.c_str(), 1);
246
247 std::flush(std::cout);
248 }
249
250 PackageManagerFancy::PackageManagerFancy()
251 : nr_terminal_rows(-1)
252 {
253 struct winsize win;
254 if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) == 0)
255 {
256 nr_terminal_rows = win.ws_row;
257 }
258 }
259
260 void PackageManagerFancy::Start()
261 {
262 if (nr_terminal_rows > 0)
263 SetupTerminalScrollArea(nr_terminal_rows);
264 }
265
266 void PackageManagerFancy::Stop()
267 {
268 if (nr_terminal_rows > 0)
269 {
270 SetupTerminalScrollArea(nr_terminal_rows + 1);
271
272 // override the progress line (sledgehammer)
273 static const char* clear_screen_below_cursor = "\033[J";
274 std::cout << clear_screen_below_cursor;
275 }
276 }
277
278 bool PackageManagerFancy::StatusChanged(std::string PackageName,
279 unsigned int StepsDone,
280 unsigned int TotalSteps,
281 std::string HumanReadableAction)
282 {
283 if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps,
284 HumanReadableAction))
285 return false;
286
287 int row = nr_terminal_rows;
288
289 static string save_cursor = "\033[s";
290 static string restore_cursor = "\033[u";
291
292 static string set_bg_color = "\033[42m"; // green
293 static string set_fg_color = "\033[30m"; // black
294
295 static string restore_bg = "\033[49m";
296 static string restore_fg = "\033[39m";
297
298 std::cout << save_cursor
299 // move cursor position to last row
300 << "\033[" << row << ";0f"
301 << set_bg_color
302 << set_fg_color
303 << progress_str
304 << restore_cursor
305 << restore_bg
306 << restore_fg;
307 std::flush(std::cout);
308 last_reported_progress = percentage;
309
310 return true;
311 }
312
313 bool PackageManagerText::StatusChanged(std::string PackageName,
314 unsigned int StepsDone,
315 unsigned int TotalSteps,
316 std::string HumanReadableAction)
317 {
318 if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction))
319 return false;
320
321 std::cout << progress_str << "\r\n";
322 std::flush(std::cout);
323
324 last_reported_progress = percentage;
325
326 return true;
327 }
328
329
330
331 }; // namespace progress
332 }; // namespace apt