install-progress.cc 10 KB

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