install-progress.cc 10.0 KB

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