install-progress.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. namespace APT {
  11. namespace Progress {
  12. bool PackageManager::StatusChanged(std::string PackageName,
  13. unsigned int StepsDone,
  14. unsigned int TotalSteps,
  15. std::string HumanReadableAction)
  16. {
  17. int reporting_steps = _config->FindI("DpkgPM::Reporting-Steps", 1);
  18. percentage = StepsDone/(float)TotalSteps * 100.0;
  19. strprintf(progress_str, _("Progress: [%3i%%]"), (int)percentage);
  20. if(percentage < (last_reported_progress + reporting_steps))
  21. return false;
  22. return true;
  23. }
  24. PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd)
  25. : StepsDone(0), StepsTotal(1)
  26. {
  27. OutStatusFd = progress_fd;
  28. }
  29. void PackageManagerProgressFd::WriteToStatusFd(std::string s)
  30. {
  31. if(OutStatusFd <= 0)
  32. return;
  33. FileFd::Write(OutStatusFd, s.c_str(), s.size());
  34. }
  35. void PackageManagerProgressFd::Start()
  36. {
  37. if(OutStatusFd <= 0)
  38. return;
  39. // FIXME: use SetCloseExec here once it taught about throwing
  40. // exceptions instead of doing _exit(100) on failure
  41. fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC);
  42. // send status information that we are about to fork dpkg
  43. std::ostringstream status;
  44. status << "pmstatus:dpkg-exec:"
  45. << (StepsDone/float(StepsTotal)*100.0)
  46. << ":" << _("Running dpkg")
  47. << std::endl;
  48. WriteToStatusFd(status.str());
  49. }
  50. void PackageManagerProgressFd::Stop()
  51. {
  52. // clear the Keep-Fd again
  53. _config->Clear("APT::Keep-Fds", OutStatusFd);
  54. }
  55. void PackageManagerProgressFd::Error(std::string PackageName,
  56. unsigned int StepsDone,
  57. unsigned int TotalSteps,
  58. std::string ErrorMessage)
  59. {
  60. std::ostringstream status;
  61. status << "pmerror:" << PackageName
  62. << ":" << (StepsDone/float(TotalSteps)*100.0)
  63. << ":" << ErrorMessage
  64. << std::endl;
  65. WriteToStatusFd(status.str());
  66. }
  67. void PackageManagerProgressFd::ConffilePrompt(std::string PackageName,
  68. unsigned int StepsDone,
  69. unsigned int TotalSteps,
  70. std::string ConfMessage)
  71. {
  72. std::ostringstream status;
  73. status << "pmconffile:" << PackageName
  74. << ":" << (StepsDone/float(TotalSteps)*100.0)
  75. << ":" << ConfMessage
  76. << std::endl;
  77. WriteToStatusFd(status.str());
  78. }
  79. bool PackageManagerProgressFd::StatusChanged(std::string PackageName,
  80. unsigned int xStepsDone,
  81. unsigned int xTotalSteps,
  82. std::string pkg_action)
  83. {
  84. StepsDone = xStepsDone;
  85. StepsTotal = xTotalSteps;
  86. // build the status str
  87. std::ostringstream status;
  88. status << "pmstatus:" << StringSplit(PackageName, ":")[0]
  89. << ":" << (StepsDone/float(StepsTotal)*100.0)
  90. << ":" << pkg_action
  91. << std::endl;
  92. WriteToStatusFd(status.str());
  93. if(_config->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
  94. std::cerr << "progress: " << PackageName << " " << xStepsDone
  95. << " " << xTotalSteps << " " << pkg_action
  96. << std::endl;
  97. return true;
  98. }
  99. PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd)
  100. : StepsDone(0), StepsTotal(1)
  101. {
  102. OutStatusFd = progress_fd;
  103. }
  104. void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s)
  105. {
  106. FileFd::Write(OutStatusFd, s.c_str(), s.size());
  107. }
  108. void PackageManagerProgressDeb822Fd::Start()
  109. {
  110. // FIXME: use SetCloseExec here once it taught about throwing
  111. // exceptions instead of doing _exit(100) on failure
  112. fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC);
  113. // send status information that we are about to fork dpkg
  114. std::ostringstream status;
  115. status << "Status: " << "progress" << std::endl
  116. << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl
  117. << "Message: " << _("Running dpkg") << std::endl
  118. << std::endl;
  119. WriteToStatusFd(status.str());
  120. }
  121. void PackageManagerProgressDeb822Fd::Stop()
  122. {
  123. // clear the Keep-Fd again
  124. _config->Clear("APT::Keep-Fds", OutStatusFd);
  125. }
  126. void PackageManagerProgressDeb822Fd::Error(std::string PackageName,
  127. unsigned int StepsDone,
  128. unsigned int TotalSteps,
  129. std::string ErrorMessage)
  130. {
  131. std::ostringstream status;
  132. status << "Status: " << "Error" << std::endl
  133. << "Package:" << PackageName << std::endl
  134. << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl
  135. << "Message: " << ErrorMessage << std::endl
  136. << std::endl;
  137. WriteToStatusFd(status.str());
  138. }
  139. void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName,
  140. unsigned int StepsDone,
  141. unsigned int TotalSteps,
  142. std::string ConfMessage)
  143. {
  144. std::ostringstream status;
  145. status << "Status: " << "ConfFile" << std::endl
  146. << "Package:" << PackageName << std::endl
  147. << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl
  148. << "Message: " << ConfMessage << std::endl
  149. << std::endl;
  150. WriteToStatusFd(status.str());
  151. }
  152. bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName,
  153. unsigned int xStepsDone,
  154. unsigned int xTotalSteps,
  155. std::string message)
  156. {
  157. StepsDone = xStepsDone;
  158. StepsTotal = xTotalSteps;
  159. // build the status str
  160. std::ostringstream status;
  161. status << "Status: " << "progress" << std::endl
  162. << "Package: " << PackageName << std::endl
  163. << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl
  164. << "Message: " << message << std::endl
  165. << std::endl;
  166. WriteToStatusFd(status.str());
  167. return true;
  168. }
  169. void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
  170. {
  171. // scroll down a bit to avoid visual glitch when the screen
  172. // area shrinks by one row
  173. std::cout << "\n";
  174. // save cursor
  175. std::cout << "\033[s";
  176. // set scroll region (this will place the cursor in the top left)
  177. std::cout << "\033[1;" << nr_rows - 1 << "r";
  178. // restore cursor but ensure its inside the scrolling area
  179. std::cout << "\033[u";
  180. static const char *move_cursor_up = "\033[1A";
  181. std::cout << move_cursor_up;
  182. std::flush(std::cout);
  183. }
  184. PackageManagerFancy::PackageManagerFancy()
  185. : nr_terminal_rows(-1)
  186. {
  187. struct winsize win;
  188. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) == 0)
  189. {
  190. nr_terminal_rows = win.ws_row;
  191. }
  192. }
  193. void PackageManagerFancy::Start()
  194. {
  195. if (nr_terminal_rows > 0)
  196. SetupTerminalScrollArea(nr_terminal_rows);
  197. }
  198. void PackageManagerFancy::Stop()
  199. {
  200. if (nr_terminal_rows > 0)
  201. {
  202. SetupTerminalScrollArea(nr_terminal_rows + 1);
  203. // override the progress line (sledgehammer)
  204. static const char* clear_screen_below_cursor = "\033[J";
  205. std::cout << clear_screen_below_cursor;
  206. }
  207. }
  208. bool PackageManagerFancy::StatusChanged(std::string PackageName,
  209. unsigned int StepsDone,
  210. unsigned int TotalSteps,
  211. std::string HumanReadableAction)
  212. {
  213. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps,
  214. HumanReadableAction))
  215. return false;
  216. int row = nr_terminal_rows;
  217. static string save_cursor = "\033[s";
  218. static string restore_cursor = "\033[u";
  219. static string set_bg_color = "\033[42m"; // green
  220. static string set_fg_color = "\033[30m"; // black
  221. static string restore_bg = "\033[49m";
  222. static string restore_fg = "\033[39m";
  223. std::cout << save_cursor
  224. // move cursor position to last row
  225. << "\033[" << row << ";0f"
  226. << set_bg_color
  227. << set_fg_color
  228. << progress_str
  229. << restore_cursor
  230. << restore_bg
  231. << restore_fg;
  232. std::flush(std::cout);
  233. last_reported_progress = percentage;
  234. return true;
  235. }
  236. bool PackageManagerText::StatusChanged(std::string PackageName,
  237. unsigned int StepsDone,
  238. unsigned int TotalSteps,
  239. std::string HumanReadableAction)
  240. {
  241. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction))
  242. return false;
  243. std::cout << progress_str << "\r\n";
  244. std::flush(std::cout);
  245. last_reported_progress = percentage;
  246. return true;
  247. }
  248. }; // namespace progress
  249. }; // namespace apt