From 392acdf963eeeb532ed2a2de07bbb6d4d7525ece Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 17 Feb 2015 16:54:54 -0500 Subject: [PATCH] Move console code to libglnx The main goal is to be able to share this with OSTree. --- Makefile-rpm-ostree.am | 6 +- libglnx | 2 +- src/rpmostree-compose-builtin-tree.c | 19 ++- src/rpmostree-console-progress.c | 220 --------------------------- src/rpmostree-console-progress.h | 42 ----- 5 files changed, 11 insertions(+), 278 deletions(-) delete mode 100644 src/rpmostree-console-progress.c delete mode 100644 src/rpmostree-console-progress.h diff --git a/Makefile-rpm-ostree.am b/Makefile-rpm-ostree.am index 8716fdc6..48070b1d 100644 --- a/Makefile-rpm-ostree.am +++ b/Makefile-rpm-ostree.am @@ -20,8 +20,6 @@ bin_PROGRAMS += rpm-ostree noinst_LTLIBRARIES += librpmostree.la librpmostree_la_SOURCES = \ - src/rpmostree-console-progress.c \ - src/rpmostree-console-progress.h \ src/rpmostree-postprocess.c \ src/rpmostree-postprocess.h \ src/rpmostree-json-parsing.c \ @@ -32,8 +30,6 @@ librpmostree_la_SOURCES = \ src/rpmostree-passwd-util.h \ src/rpmostree-libcontainer.c \ src/rpmostree-libcontainer.h \ - src/rpmostree-console-progress.c \ - src/rpmostree-console-progress.h \ src/rpmostree-cleanup.h \ src/rpmostree-treepkgdiff.c \ src/rpmostree-treepkgdiff.h \ @@ -71,7 +67,7 @@ rpm_ostree_SOURCES += \ endif rpm_ostree_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/src -I$(libglnx_srcpath) -DPKGLIBDIR=\"$(pkglibdir)\" $(PKGDEP_RPMOSTREE_CFLAGS) -rpm_ostree_LDADD = $(AM_LDFLAGS) $(PKGDEP_RPMOSTREE_LIBS) librpmostree.la +rpm_ostree_LDADD = $(AM_LDFLAGS) $(PKGDEP_RPMOSTREE_LIBS) libglnx.la librpmostree.la privdatadir=$(pkglibdir) privdata_DATA = src/tmpfiles-ostree-integration.conf diff --git a/libglnx b/libglnx index ba67dd39..64936b67 160000 --- a/libglnx +++ b/libglnx @@ -1 +1 @@ -Subproject commit ba67dd39a7d5cc779f83bb22512740cdb4c61b33 +Subproject commit 64936b67001410974f1bd92803a46cdb77f89db5 diff --git a/src/rpmostree-compose-builtin-tree.c b/src/rpmostree-compose-builtin-tree.c index ab73d017..a2e9cc3a 100644 --- a/src/rpmostree-compose-builtin-tree.c +++ b/src/rpmostree-compose-builtin-tree.c @@ -36,7 +36,6 @@ #include "rpmostree-cleanup.h" #include "rpmostree-treepkgdiff.h" #include "rpmostree-libcontainer.h" -#include "rpmostree-console-progress.h" #include "rpmostree-postprocess.h" #include "rpmostree-passwd-util.h" @@ -163,7 +162,7 @@ on_hifstate_percentage_changed (HifState *hifstate, gpointer user_data) { const char *text = user_data; - rpmostree_console_progress_text_percent (text, percentage); + glnx_console_progress_text_percent (text, percentage); } static gboolean @@ -297,14 +296,14 @@ install_packages_in_root (RpmOstreeTreeComposeContext *self, } /* --- Downloading metadata --- */ - { _cleanup_rpmostree_console_progress_ G_GNUC_UNUSED gpointer dummy; + { g_auto(GLnxConsoleRef) console = { 0, }; gs_unref_object HifState *hifstate = hif_state_new (); progress_sigid = g_signal_connect (hifstate, "percentage-changed", G_CALLBACK (on_hifstate_percentage_changed), "Downloading metadata:"); - rpmostree_console_progress_start (); + glnx_console_lock (&console); if (!hif_context_setup_sack (hifctx, hifstate, error)) goto out; @@ -319,14 +318,14 @@ install_packages_in_root (RpmOstreeTreeComposeContext *self, } /* --- Resolving dependencies --- */ - { _cleanup_rpmostree_console_progress_ G_GNUC_UNUSED gpointer dummy; + { g_auto(GLnxConsoleRef) console = { 0, }; gs_unref_object HifState *hifstate = hif_state_new (); progress_sigid = g_signal_connect (hifstate, "percentage-changed", G_CALLBACK (on_hifstate_percentage_changed), "Resolving dependencies:"); - rpmostree_console_progress_start (); + glnx_console_lock (&console); if (!hif_transaction_depsolve (hif_context_get_transaction (hifctx), hif_context_get_goal (hifctx), @@ -367,14 +366,14 @@ install_packages_in_root (RpmOstreeTreeComposeContext *self, } /* --- Downloading packages --- */ - { _cleanup_rpmostree_console_progress_ G_GNUC_UNUSED gpointer dummy; + { g_auto(GLnxConsoleRef) console = { 0, }; gs_unref_object HifState *hifstate = hif_state_new (); progress_sigid = g_signal_connect (hifstate, "percentage-changed", G_CALLBACK (on_hifstate_percentage_changed), "Downloading packages:"); - rpmostree_console_progress_start (); + glnx_console_lock (&console); if (!hif_transaction_download (hif_context_get_transaction (hifctx), hifstate, error)) goto out; @@ -382,14 +381,14 @@ install_packages_in_root (RpmOstreeTreeComposeContext *self, g_signal_handler_disconnect (hifstate, progress_sigid); } - { _cleanup_rpmostree_console_progress_ G_GNUC_UNUSED gpointer dummy; + { g_auto(GLnxConsoleRef) console = { 0, }; gs_unref_object HifState *hifstate = hif_state_new (); progress_sigid = g_signal_connect (hifstate, "percentage-changed", G_CALLBACK (on_hifstate_percentage_changed), "Installing packages:"); - rpmostree_console_progress_start (); + glnx_console_lock (&console); if (!hif_transaction_commit (hif_context_get_transaction (hifctx), hif_context_get_goal (hifctx), diff --git a/src/rpmostree-console-progress.c b/src/rpmostree-console-progress.c deleted file mode 100644 index 91977ad4..00000000 --- a/src/rpmostree-console-progress.c +++ /dev/null @@ -1,220 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- - * - * Copyright (C) 2013,2014 Colin Walters - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation; either version 2 of the licence or (at - * your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General - * Public License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#include "config.h" - -#include "rpmostree-console-progress.h" - -#include -#include -#include -#include -#include -#include - -static char *current_text = NULL; -static gint current_percent = -1; - -static volatile guint cached_columns = 0; -static volatile guint cached_lines = 0; - -static int -fd_columns (int fd) -{ - struct winsize ws = {}; - - if (ioctl (fd, TIOCGWINSZ, &ws) < 0) - return -errno; - - if (ws.ws_col <= 0) - return -EIO; - - return ws.ws_col; -} - -static guint -columns (void) -{ - if (G_UNLIKELY (cached_columns == 0)) - { - int c; - - c = fd_columns (STDOUT_FILENO); - - if (c <= 0) - c = 80; - - if (c > 256) - c = 256; - - cached_columns = c; - } - - return cached_columns; -} - -#if 0 -static int -fd_lines (int fd) -{ - struct winsize ws = {}; - - if (ioctl (fd, TIOCGWINSZ, &ws) < 0) - return -errno; - - if (ws.ws_row <= 0) - return -EIO; - - return ws.ws_row; -} - -static guint -lines (void) -{ - if (G_UNLIKELY (cached_lines == 0)) - { - int l; - - l = fd_lines (STDOUT_FILENO); - - if (l <= 0) - l = 24; - - cached_lines = l; - } - - return cached_lines; -} -#endif - -static void -on_sigwinch (int signum) -{ - cached_columns = 0; - cached_lines = 0; -} - -void -rpmostree_console_progress_start (void) -{ - GSConsole *console = gs_console_get (); - static gsize sigwinch_initialized = 0; - - if (!console) - return; - - current_percent = 0; - - if (g_once_init_enter (&sigwinch_initialized)) - { - signal (SIGWINCH, on_sigwinch); - g_once_init_leave (&sigwinch_initialized, 1); - } - - { static const char initbuf[] = { '\n', 0x1B, 0x37 }; - (void) fwrite (initbuf, 1, sizeof (initbuf), stdout); - } -} - -static void -printpad (const char *padbuf, - guint padbuf_len, - guint n) -{ - const guint d = n / padbuf_len; - const guint r = n % padbuf_len; - guint i; - - for (i = 0; i < d; i++) - fwrite (padbuf, 1, padbuf_len, stdout); - fwrite (padbuf, 1, r, stdout); -} - -void -rpmostree_console_progress_text_percent (const char *text, - guint percentage) -{ - static const char equals[] = "===================="; - const guint n_equals = sizeof (equals) - 1; - static const char spaces[] = " "; - const guint n_spaces = sizeof (spaces) - 1; - GSConsole *console = gs_console_get (); - const guint ncolumns = columns (); - const guint bar_min = 10; - const guint input_textlen = text ? strlen (text) : 0; - guint textlen; - guint barlen; - - if (!console) - return; - - g_return_if_fail (percentage <= 100); - if (text && !*text) - text = NULL; - - if (percentage == current_percent - && g_strcmp0 (text, current_text) == 0) - return; - - if (ncolumns < bar_min) - return; /* TODO: spinner */ - - /* Restore cursor */ - { const char beginbuf[2] = { 0x1B, 0x38 }; - (void) fwrite (beginbuf, 1, sizeof (beginbuf), stdout); - } - - textlen = MIN (input_textlen, ncolumns - bar_min); - barlen = ncolumns - textlen; - - if (textlen > 0) - { - fwrite (text, 1, textlen - 1, stdout); - fputc (' ', stdout); - } - - { - const guint nbraces = 2; - const guint textpercent_len = 5; - const guint bar_internal_len = barlen - nbraces - textpercent_len; - const guint eqlen = bar_internal_len * (percentage / 100.0); - const guint spacelen = bar_internal_len - eqlen; - - fputc ('[', stdout); - printpad (equals, n_equals, eqlen); - printpad (spaces, n_spaces, spacelen); - fputc (']', stdout); - fprintf (stdout, " %3d%%", percentage); - } - - { const guint spacelen = ncolumns - textlen - barlen; - printpad (spaces, n_spaces, spacelen); - } - - fflush (stdout); -} - -void -rpmostree_console_progress_end (void) -{ - current_percent = -1; - g_clear_pointer (¤t_text, g_free); - fputc ('\n', stdout); -} diff --git a/src/rpmostree-console-progress.h b/src/rpmostree-console-progress.h deleted file mode 100644 index 7ff384f9..00000000 --- a/src/rpmostree-console-progress.h +++ /dev/null @@ -1,42 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- - * - * Copyright (C) 2013,2014 Colin Walters - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation; either version 2 of the licence or (at - * your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General - * Public License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#pragma once - -#include - -G_BEGIN_DECLS - -void rpmostree_console_progress_start (void); - -void rpmostree_console_progress_text_percent (const char *text, - guint percentage); - -void rpmostree_console_progress_end (void); - -static inline void -rpmostree_console_progress_cleanup (void **p) -{ - rpmostree_console_progress_end (); -} - -#define _cleanup_rpmostree_console_progress_ __attribute__((cleanup(rpmostree_console_progress_cleanup))) - -G_END_DECLS