install-progress.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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::Start()
  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. // clear the Keep-Fd again
  73. _config->Clear("APT::Keep-Fds", OutStatusFd);
  74. }
  75. void PackageManagerProgressFd::Error(std::string PackageName,
  76. unsigned int StepsDone,
  77. unsigned int TotalSteps,
  78. std::string ErrorMessage)
  79. {
  80. std::ostringstream status;
  81. status << "pmerror:" << PackageName
  82. << ":" << (StepsDone/float(TotalSteps)*100.0)
  83. << ":" << ErrorMessage
  84. << std::endl;
  85. WriteToStatusFd(status.str());
  86. }
  87. void PackageManagerProgressFd::ConffilePrompt(std::string PackageName,
  88. unsigned int StepsDone,
  89. unsigned int TotalSteps,
  90. std::string ConfMessage)
  91. {
  92. std::ostringstream status;
  93. status << "pmconffile:" << PackageName
  94. << ":" << (StepsDone/float(TotalSteps)*100.0)
  95. << ":" << ConfMessage
  96. << std::endl;
  97. WriteToStatusFd(status.str());
  98. }
  99. bool PackageManagerProgressFd::StatusChanged(std::string PackageName,
  100. unsigned int xStepsDone,
  101. unsigned int xTotalSteps,
  102. std::string pkg_action)
  103. {
  104. StepsDone = xStepsDone;
  105. StepsTotal = xTotalSteps;
  106. // build the status str
  107. std::ostringstream status;
  108. status << "pmstatus:" << StringSplit(PackageName, ":")[0]
  109. << ":" << (StepsDone/float(StepsTotal)*100.0)
  110. << ":" << pkg_action
  111. << std::endl;
  112. WriteToStatusFd(status.str());
  113. if(_config->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
  114. std::cerr << "progress: " << PackageName << " " << xStepsDone
  115. << " " << xTotalSteps << " " << pkg_action
  116. << std::endl;
  117. return true;
  118. }
  119. PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd)
  120. : StepsDone(0), StepsTotal(1)
  121. {
  122. OutStatusFd = progress_fd;
  123. }
  124. void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s)
  125. {
  126. FileFd::Write(OutStatusFd, s.c_str(), s.size());
  127. }
  128. void PackageManagerProgressDeb822Fd::Start()
  129. {
  130. // FIXME: use SetCloseExec here once it taught about throwing
  131. // exceptions instead of doing _exit(100) on failure
  132. fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC);
  133. // send status information that we are about to fork dpkg
  134. std::ostringstream status;
  135. status << "Status: " << "progress" << std::endl
  136. << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl
  137. << "Message: " << _("Running dpkg") << std::endl
  138. << std::endl;
  139. WriteToStatusFd(status.str());
  140. }
  141. void PackageManagerProgressDeb822Fd::Stop()
  142. {
  143. // clear the Keep-Fd again
  144. _config->Clear("APT::Keep-Fds", OutStatusFd);
  145. }
  146. void PackageManagerProgressDeb822Fd::Error(std::string PackageName,
  147. unsigned int StepsDone,
  148. unsigned int TotalSteps,
  149. std::string ErrorMessage)
  150. {
  151. std::ostringstream status;
  152. status << "Status: " << "Error" << std::endl
  153. << "Package:" << PackageName << std::endl
  154. << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl
  155. << "Message: " << ErrorMessage << std::endl
  156. << std::endl;
  157. WriteToStatusFd(status.str());
  158. }
  159. void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName,
  160. unsigned int StepsDone,
  161. unsigned int TotalSteps,
  162. std::string ConfMessage)
  163. {
  164. std::ostringstream status;
  165. status << "Status: " << "ConfFile" << std::endl
  166. << "Package:" << PackageName << std::endl
  167. << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl
  168. << "Message: " << ConfMessage << std::endl
  169. << std::endl;
  170. WriteToStatusFd(status.str());
  171. }
  172. bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName,
  173. unsigned int xStepsDone,
  174. unsigned int xTotalSteps,
  175. std::string message)
  176. {
  177. StepsDone = xStepsDone;
  178. StepsTotal = xTotalSteps;
  179. // build the status str
  180. std::ostringstream status;
  181. status << "Status: " << "progress" << std::endl
  182. << "Package: " << PackageName << std::endl
  183. << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl
  184. << "Message: " << message << std::endl
  185. << std::endl;
  186. WriteToStatusFd(status.str());
  187. return true;
  188. }
  189. void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
  190. {
  191. // scroll down a bit to avoid visual glitch when the screen
  192. // area shrinks by one row
  193. std::cout << "\n";
  194. // save cursor
  195. std::cout << "\033[s";
  196. // set scroll region (this will place the cursor in the top left)
  197. std::cout << "\033[1;" << nr_rows - 1 << "r";
  198. // restore cursor but ensure its inside the scrolling area
  199. std::cout << "\033[u";
  200. static const char *move_cursor_up = "\033[1A";
  201. std::cout << move_cursor_up;
  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