install-progress.cc 12 KB

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