You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

440 lines
13 KiB

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