You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

425 lines
13 KiB

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